Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/edit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ harness = false
debug-latency = []

[dependencies]
lsh.workspace = true
stdext.workspace = true

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[build-dependencies]
stdext.workspace = true
lsh.workspace = true
# The default toml crate bundles its dependencies with bad compile times. Thanks.
# Thankfully toml-span exists. FWIW the alternative is yaml-rust (without the 2 suffix).
toml-span = { version = "0.6", default-features = false }
Expand Down
21 changes: 21 additions & 0 deletions crates/edit/build/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#![allow(irrefutable_let_patterns)]

use stdext::arena::scratch_arena;

use crate::helpers::env_opt;

mod helpers;
Expand All @@ -24,12 +26,31 @@ fn main() {
_ => TargetOs::Unix,
};

compile_lsh();
compile_i18n();
configure_icu(target_os);
#[cfg(windows)]
configure_windows_binary(target_os);
}

fn compile_lsh() {
let scratch = scratch_arena(None);

let lsh_path = lsh::compiler::builtin_definitions_path();
let out_dir = env_opt("OUT_DIR");
let out_path = format!("{out_dir}/lsh_definitions.rs");

let mut generator = lsh::compiler::Generator::new(&scratch);
match generator.read_directory(lsh_path).and_then(|_| generator.generate_rust()) {
Ok(c) => std::fs::write(out_path, c).unwrap(),
Err(err) => {
panic!("failed to compile lsh definitions: {err}");
}
};

println!("cargo::rerun-if-changed={}", lsh_path.display());
}

fn compile_i18n() {
let i18n_path = "../../i18n/edit.toml";

Expand Down
38 changes: 34 additions & 4 deletions crates/edit/src/bin/edit/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{fs, io};

use edit::buffer::{RcTextBuffer, TextBuffer};
use edit::helpers::{CoordType, Point};
use edit::lsh::{FILE_ASSOCIATIONS, Language, process_file_associations};
use edit::{path, sys};

use crate::apperr;
Expand All @@ -20,6 +21,7 @@ pub struct Document {
pub filename: String,
pub file_id: Option<sys::FileId>,
pub new_file_counter: usize,
pub language_override: Option<Option<&'static Language>>,
}

impl Document {
Expand Down Expand Up @@ -62,15 +64,41 @@ impl Document {
fn set_path(&mut self, path: PathBuf) {
let filename = path.file_name().unwrap_or_default().to_string_lossy().into_owned();
let dir = path.parent().map(ToOwned::to_owned).unwrap_or_default();

self.filename = filename;
self.dir = Some(DisplayablePathBuf::from_path(dir));
self.path = Some(path);
self.update_file_mode();

self.buffer.borrow_mut().set_ruler(if self.filename == "COMMIT_EDITMSG" { 72 } else { 0 });
self.update_language();
}

pub fn auto_detect_language(&mut self) {
self.language_override = None;
self.update_language();
}

fn update_file_mode(&mut self) {
let mut tb = self.buffer.borrow_mut();
tb.set_ruler(if self.filename == "COMMIT_EDITMSG" { 72 } else { 0 });
pub fn override_language(&mut self, lang: Option<&'static Language>) {
self.language_override = Some(lang);
self.update_language();
}

fn update_language(&mut self) {
self.buffer.borrow_mut().set_language(self.get_language());
}

fn get_language(&self) -> Option<&'static Language> {
if let Some(lang) = self.language_override {
return lang;
}

if let Some(path) = &self.path
&& let Some(lang) = process_file_associations(FILE_ASSOCIATIONS, path)
{
return Some(lang);
}

None
}
}

Expand Down Expand Up @@ -140,6 +168,7 @@ impl DocumentManager {
filename: Default::default(),
file_id: None,
new_file_counter: 0,
language_override: None,
};
self.gen_untitled_name(&mut doc);

Expand Down Expand Up @@ -201,6 +230,7 @@ impl DocumentManager {
filename: Default::default(),
file_id,
new_file_counter: 0,
language_override: None,
};
doc.set_path(path);

Expand Down
64 changes: 60 additions & 4 deletions crates/edit/src/bin/edit/draw_statusbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use edit::fuzzy::score_fuzzy;
use edit::helpers::*;
use edit::icu;
use edit::input::vk;
use edit::lsh::LANGUAGES;
use edit::tui::*;
use stdext::arena::scratch_arena;
use stdext::arena_format;
Expand All @@ -28,15 +29,21 @@ pub fn draw_statusbar(ctx: &mut Context, state: &mut State) {

ctx.table_next_row();

if ctx.button("newline", if tb.is_crlf() { "CRLF" } else { "LF" }, ButtonStyle::default()) {
let is_crlf = tb.is_crlf();
tb.normalize_newlines(!is_crlf);
}
state.wants_language_picker |= ctx.button(
"language",
tb.language().map_or("Plain Text", |l| l.name),
ButtonStyle::default(),
);
if state.wants_statusbar_focus {
state.wants_statusbar_focus = false;
ctx.steal_focus();
}

if ctx.button("newline", if tb.is_crlf() { "CRLF" } else { "LF" }, ButtonStyle::default()) {
let is_crlf = tb.is_crlf();
tb.normalize_newlines(!is_crlf);
}

state.wants_encoding_picker |=
ctx.button("encoding", tb.encoding(), ButtonStyle::default());
if state.wants_encoding_picker {
Expand Down Expand Up @@ -201,6 +208,55 @@ pub fn draw_statusbar(ctx: &mut Context, state: &mut State) {
ctx.table_end();
}

pub fn draw_dialog_language_change(ctx: &mut Context, state: &mut State) {
let doc = state.documents.active_mut();
let mut done = doc.is_none();

ctx.modal_begin("language", loc(LocId::LanguageSelectMode));
if let Some(doc) = doc {
let width = (ctx.size().width - 20).max(10);
let height = (ctx.size().height - 10).max(10);

ctx.scrollarea_begin("scrollarea", Size { width, height });
ctx.attr_background_rgba(ctx.indexed_alpha(IndexedColor::Black, 1, 4));
ctx.inherit_focus();
{
ctx.list_begin("languages");
ctx.inherit_focus();

let auto_detect = doc.language_override.is_none();
let selected = if auto_detect { None } else { doc.buffer.borrow().language() };

if ctx.list_item(auto_detect, loc(LocId::LanguageAutoDetect))
== ListSelection::Activated
{
doc.auto_detect_language();
done = true;
}

if ctx.list_item(selected.is_none(), "Plain Text") == ListSelection::Activated {
doc.override_language(None);
done = true;
}

for lang in LANGUAGES {
if ctx.list_item(Some(lang) == selected, lang.name) == ListSelection::Activated {
doc.override_language(Some(lang));
done = true;
}
}
ctx.list_end();
}
ctx.scrollarea_end();
}
done |= ctx.modal_end();

if done {
state.wants_language_picker = false;
ctx.needs_rerender();
}
}

pub fn draw_dialog_encoding_change(ctx: &mut Context, state: &mut State) {
let encoding = state.documents.active_mut().map_or("", |doc| doc.buffer.borrow().encoding());
let reopen = state.wants_encoding_change == StateEncodingChange::Reopen;
Expand Down
3 changes: 3 additions & 0 deletions crates/edit/src/bin/edit/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ fn draw(ctx: &mut Context, state: &mut State) {
if state.wants_save {
draw_handle_save(ctx, state);
}
if state.wants_language_picker {
draw_dialog_language_change(ctx, state);
}
if state.wants_encoding_change != StateEncodingChange::None {
draw_dialog_encoding_change(ctx, state);
}
Expand Down
4 changes: 4 additions & 0 deletions crates/edit/src/bin/edit/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ pub struct State {
pub search_options: buffer::SearchOptions,
pub search_success: bool,

pub wants_language_picker: bool,

pub wants_encoding_picker: bool,
pub wants_encoding_change: StateEncodingChange,
pub encoding_picker_needle: String,
Expand Down Expand Up @@ -200,6 +202,8 @@ impl State {
search_options: Default::default(),
search_success: true,

wants_language_picker: false,

wants_encoding_picker: false,
encoding_picker_needle: Default::default(),
encoding_picker_results: Default::default(),
Expand Down
116 changes: 0 additions & 116 deletions crates/edit/src/buffer/line_cache.rs

This file was deleted.

Loading
Loading