From e8add6f46d0f208c96407151276a985dd34fc93f Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Mon, 8 Aug 2022 22:04:41 +0300 Subject: [PATCH] Add error handling to set language command If you type a nonexistant language an appropriate message will show, and the language won't be changed. --- helix-term/src/commands/typed.rs | 11 ++++++++++- helix-view/src/document.rs | 6 +++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index f005d9bf6..127eea0dc 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1253,7 +1253,16 @@ fn language( } let doc = doc_mut!(cx.editor); - doc.set_language_by_language_id(&args[0], cx.editor.syn_loader.clone()); + + let loader = cx.editor.syn_loader.clone(); + if args[0] == "text" { + doc.set_language(None, Some(loader)) + } else { + let ok = doc.set_language_by_language_id(&args[0], loader); + if !ok { + anyhow::bail!("invalid language: {}", args[0]); + } + } doc.detect_indent_and_line_ending(); let id = doc.id(); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index a0d504407..84c92320c 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -683,9 +683,13 @@ pub fn set_language_by_language_id( &mut self, language_id: &str, config_loader: Arc, - ) { + ) -> bool { let language_config = config_loader.language_config_for_language_id(language_id); + if language_config.is_none() { + return false; + } self.set_language(language_config, Some(config_loader)); + true } /// Set the LSP.