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.
imgbot
Michael Davis 2 years ago committed by GitHub
parent ae81fbdbf6
commit 5f043dde56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -863,10 +863,7 @@ pub fn signature_help_impl(cx: &mut Context, invoked: SignatureHelpInvoked) {
} }
}; };
let doc = doc!(editor); let doc = doc!(editor);
let language = doc let language = doc.language_name().unwrap_or("");
.language()
.and_then(|scope| scope.strip_prefix("source."))
.unwrap_or("");
let signature = match response let signature = match response
.signatures .signatures

@ -324,10 +324,7 @@ impl Component for Completion {
// option.documentation // option.documentation
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let language = doc let language = doc.language_name().unwrap_or("");
.language()
.and_then(|scope| scope.strip_prefix("source."))
.unwrap_or("");
let text = doc.text().slice(..); let text = doc.text().slice(..);
let cursor_pos = doc.selection(view.id).primary().cursor(text); let cursor_pos = doc.selection(view.id).primary().cursor(text);
let coords = helix_core::visual_coords_at_pos(text, cursor_pos, doc.tab_width()); let coords = helix_core::visual_coords_at_pos(text, cursor_pos, doc.tab_width());

@ -329,7 +329,7 @@ fn render_file_type<F>(context: &mut RenderContext, write: F)
where where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy, F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{ {
let file_type = context.doc.language_id().unwrap_or("text"); let file_type = context.doc.language_name().unwrap_or("text");
write(context, format!(" {} ", file_type), None); write(context, format!(" {} ", file_type), None);
} }

@ -926,22 +926,32 @@ impl Document {
} }
/// Corresponding language scope name. Usually `source.<lang>`. /// Corresponding language scope name. Usually `source.<lang>`.
pub fn language(&self) -> Option<&str> { pub fn language_scope(&self) -> Option<&str> {
self.language self.language
.as_ref() .as_ref()
.map(|language| language.scope.as_str()) .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 ID for the document. Either the `language-id` from the
/// `language-server` configuration, or the document language if no /// `language-server` configuration, or the document language if no
/// `language-id` has been specified. /// `language-id` has been specified.
pub fn language_id(&self) -> Option<&str> { pub fn language_id(&self) -> Option<&str> {
self.language_config()? let language_config = self.language.as_deref()?;
language_config
.language_server .language_server
.as_ref()? .as_ref()?
.language_id .language_id
.as_deref() .as_deref()
.or_else(|| Some(self.language()?.rsplit_once('.')?.1)) .or(Some(language_config.language_id.as_str()))
} }
/// Corresponding [`LanguageConfiguration`]. /// Corresponding [`LanguageConfiguration`].

Loading…
Cancel
Save