diff options
author | Michael Davis | 2022-08-30 01:59:45 +0000 |
---|---|---|
committer | GitHub | 2022-08-30 01:59:45 +0000 |
commit | 5f043dde56c20e694078ceb46aac5f053327196c (patch) | |
tree | 37892f2c929a8c3bf6a78fbd9173b78e3b50c356 /helix-view/src | |
parent | ae81fbdbf6e44cc6b2aa35876afab65055e40df5 (diff) |
Derive Document language name from languages.toml name key (#3338)
* 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`.
* Use `Document::language_name` for the `file-type` statusline element
The `file-type` indicator element in the statusline was using
`Document::language_id` which is meant to be used to for telling
Language Servers what language we're using. That works for languages
with `language-server` configurations in `languages.toml` but shows
text otherwise. The new `Document::language_name` method from the
parent commit is a more accurate way to determine the language.
Diffstat (limited to 'helix-view/src')
-rw-r--r-- | helix-view/src/document.rs | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index c96f222d..0668e99d 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -926,22 +926,32 @@ impl Document { } /// Corresponding language scope name. Usually `source.<lang>`. - 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`]. |