From a5ffb450f5a282c39dee07e4449b4fd57326912e Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Fri, 5 Aug 2022 10:03:40 -0500 Subject: [PATCH] Derive Document language name from `languages.toml` `name` key This changes switches from deriving the language name from the `languages.toml` `scope` key to `name` (`language_id` in the `LanguageConfiguration` type). For the most part it works to derive the language name from scope by chopping off `source.` or `rsplit_once` on `.` but for some languages we have now like html (`text.html.basic`), it doesn't. This also should be a more accurate fallback for the `language_id` method which is used in LSP and currently uses the `rsplit_once` strategy. Here we expose the language's name as `language_name` on `Document` and replace ad-hoc calculations of the language name with the new method. This is most impactful for the `file-type` statusline element which is using `language_id`. --- helix-term/src/commands/lsp.rs | 5 +---- helix-term/src/ui/completion.rs | 5 +---- helix-view/src/document.rs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 38507e4dd1cb..61eed63874eb 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -863,10 +863,7 @@ pub fn signature_help_impl(cx: &mut Context, invoked: SignatureHelpInvoked) { } }; let doc = doc!(editor); - let language = doc - .language() - .and_then(|scope| scope.strip_prefix("source.")) - .unwrap_or(""); + let language = doc.language_name().unwrap_or(""); let signature = match response .signatures diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index c1816db1fda8..a96a9dc30d95 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -321,10 +321,7 @@ impl Component for Completion { // option.documentation let (view, doc) = current!(cx.editor); - let language = doc - .language() - .and_then(|scope| scope.strip_prefix("source.")) - .unwrap_or(""); + let language = doc.language_name().unwrap_or(""); let text = doc.text().slice(..); let cursor_pos = doc.selection(view.id).primary().cursor(text); let coords = helix_core::visual_coords_at_pos(text, cursor_pos, doc.tab_width()); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index ab64f4850ad6..77a119e912d2 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -927,22 +927,32 @@ impl Document { } /// Corresponding language scope name. Usually `source.`. - pub fn language(&self) -> Option<&str> { + pub fn language_scope(&self) -> Option<&str> { self.language .as_ref() .map(|language| language.scope.as_str()) } + /// Language name for the document. Corresponds to the `name` key in + /// `languages.toml` configuration. + pub fn language_name(&self) -> Option<&str> { + self.language + .as_ref() + .map(|language| language.language_id.as_str()) + } + /// Language ID for the document. Either the `language-id` from the /// `language-server` configuration, or the document language if no /// `language-id` has been specified. pub fn language_id(&self) -> Option<&str> { - self.language_config()? + let language_config = self.language.as_deref()?; + + language_config .language_server .as_ref()? .language_id .as_deref() - .or_else(|| Some(self.language()?.rsplit_once('.')?.1)) + .or(Some(language_config.language_id.as_str())) } /// Corresponding [`LanguageConfiguration`].