Skip to content

Commit

Permalink
Improved toggle-syntax command
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazurel committed Aug 15, 2022
1 parent 9a137dd commit ab75f96
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
5 changes: 3 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use helix_core::{
pos_at_coords, syntax, Selection,
};
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_view::document::DocumentEvent;
use helix_view::{align_view, editor::ConfigEvent, document::DocumentEvent, editor::ConfigEvent, theme, Align, Editor};
use helix_view::{
align_view, document::DocumentEvent, editor::ConfigEvent, theme, tree::Layout, Align, Editor,
};
use serde_json::json;

use crate::{
Expand Down
8 changes: 0 additions & 8 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ impl MappableCommand {
record_macro, "Record macro",
replay_macro, "Replay macro",
command_palette, "Open command pallete",
toggle_syntax, "Toggle (on/off) syntax for current document",
);
}

Expand Down Expand Up @@ -4856,10 +4855,3 @@ fn replay_macro(cx: &mut Context) {
cx.editor.macro_replaying.pop();
}));
}

fn toggle_syntax(cx: &mut Context) {
let (_, doc) = current!(cx.editor);
doc.enable_syntax = !doc.enable_syntax;
// Force reload syntax
doc.detect_language(cx.editor.syn_loader.clone());
}
32 changes: 32 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,31 @@ fn run_shell_command(
Ok(())
}

fn toggle_syntax(
cx: &mut compositor::Context,
args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
if args.len() != 1 {
return Ok(());
}

let (_, doc) = current!(cx.editor);
let arg = args.get(0).unwrap().as_ref();
match arg {
"on" => {
doc.enable_syntax = true;
doc.detect_language(cx.editor.syn_loader.clone());
}
"off" => {
doc.enable_syntax = false;
doc.detect_language(cx.editor.syn_loader.clone());
}
_ => {}
};
Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -1995,6 +2020,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: run_shell_command,
completer: Some(completers::directory),
},
TypableCommand {
name: "toggle-syntax",
aliases: &[],
doc: "Enable/Disable syntax for current document",
fun: toggle_syntax,
completer: Some(completers::onoff)
}
];

pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
Expand Down
4 changes: 4 additions & 0 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ pub mod completers {
})
}

pub fn onoff(_editor: &Editor, _input: &str) -> Vec<Completion> {
vec![(0.., Cow::from("on")), (0.., Cow::from("off"))]
}

pub fn yesno(_editor: &Editor, _input: &str) -> Vec<Completion> {
vec![(0.., Cow::from("yes")), (0.., Cow::from("no"))]
}
Expand Down
10 changes: 5 additions & 5 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,11 @@ impl Document {

if self.text.len_bytes() > BIG_FILE_THRESHOLD {
self.enable_syntax = false;
// Notify that syntax has been disabled
self.document_events
.0
.send(DocumentEvent::DisabledSyntax)
.unwrap();
}

Ok(())
Expand All @@ -677,11 +682,6 @@ impl Document {
} else {
// If there is some syntax and it should be disabled, disable it.
self.syntax = None;
// Notify that syntax has been disabled
self.document_events
.0
.send(DocumentEvent::DisabledSyntax)
.unwrap();
}

self.language = Some(language_config);
Expand Down

0 comments on commit ab75f96

Please sign in to comment.