aboutsummaryrefslogtreecommitdiff
path: root/helix-term
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term')
-rw-r--r--helix-term/src/commands/typed.rs26
-rw-r--r--helix-term/src/ui/mod.rs21
2 files changed, 47 insertions, 0 deletions
diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 4c044793..8f74adb6 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -217,6 +217,7 @@ fn write_impl(cx: &mut compositor::Context, path: Option<&Cow<str>>) -> anyhow::
if path.is_some() {
let id = doc.id();
+ doc.detect_language(cx.editor.syn_loader.clone());
let _ = cx.editor.refresh_language_server(id);
}
Ok(())
@@ -931,6 +932,24 @@ fn setting(
Ok(())
}
+/// Change the language of the current buffer at runtime.
+fn language(
+ cx: &mut compositor::Context,
+ args: &[Cow<str>],
+ _event: PromptEvent,
+) -> anyhow::Result<()> {
+ if args.len() != 1 {
+ anyhow::bail!("Bad arguments. Usage: `:set-language language`");
+ }
+
+ let doc = doc_mut!(cx.editor);
+ doc.set_language_by_language_id(&args[0], cx.editor.syn_loader.clone());
+
+ let id = doc.id();
+ cx.editor.refresh_language_server(id);
+ Ok(())
+}
+
fn sort(
cx: &mut compositor::Context,
args: &[Cow<str>],
@@ -1409,6 +1428,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
completer: None,
},
TypableCommand {
+ name: "set-language",
+ aliases: &["lang"],
+ doc: "Set the language of current buffer.",
+ fun: language,
+ completer: Some(completers::language),
+ },
+ TypableCommand {
name: "set-option",
aliases: &["set"],
doc: "Set a config option at runtime",
diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs
index a90debdb..2dca870b 100644
--- a/helix-term/src/ui/mod.rs
+++ b/helix-term/src/ui/mod.rs
@@ -298,6 +298,27 @@ pub mod completers {
})
}
+ pub fn language(editor: &Editor, input: &str) -> Vec<Completion> {
+ let matcher = Matcher::default();
+
+ let mut matches: Vec<_> = editor
+ .syn_loader
+ .language_configs()
+ .filter_map(|config| {
+ matcher
+ .fuzzy_match(&config.language_id, input)
+ .map(|score| (&config.language_id, score))
+ })
+ .collect();
+
+ matches.sort_unstable_by_key(|(_language, score)| Reverse(*score));
+
+ matches
+ .into_iter()
+ .map(|(language, _score)| ((0..), language.clone().into()))
+ .collect()
+ }
+
pub fn directory(_editor: &Editor, input: &str) -> Vec<Completion> {
filename_impl(input, |entry| {
let is_dir = entry.file_type().map_or(false, |entry| entry.is_dir());