on master help sign in
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.

commit5f043dde56c20e694078ceb46aac5f053327196c authorMichael Davis <mcarsondavis@gmail.com> dateMon 29 Aug 2022, 20:59 (4 years ago) parentae81fbd
4 files changed+16−12
helix-term/src/commands/lsp.rs+1−4 helix-term/src/ui/completion.rs+1−4 helix-term/src/ui/statusline.rs+1−1 helix-view/src/document.rs+13−3
helix-term/src/commands/lsp.rs+1−4
@@ -863,10 +863,7 @@ pub fn signature_help_impl(cx: &mut Context, invoked: SignatureHelpInvoked) {
863863 }
864864 };
865865 let doc = doc!(editor);
866− let language = doc
867− .language()
868− .and_then(|scope| scope.strip_prefix("source."))
869− .unwrap_or("");
866+ let language = doc.language_name().unwrap_or("");
870867
871868 let signature = match response
872869 .signatures
helix-term/src/ui/completion.rs+1−4
@@ -324,10 +324,7 @@ impl Component for Completion {
324324 // option.documentation
325325
326326 let (view, doc) = current!(cx.editor);
327− let language = doc
328− .language()
329− .and_then(|scope| scope.strip_prefix("source."))
330− .unwrap_or("");
327+ let language = doc.language_name().unwrap_or("");
331328 let text = doc.text().slice(..);
332329 let cursor_pos = doc.selection(view.id).primary().cursor(text);
333330 let coords = helix_core::visual_coords_at_pos(text, cursor_pos, doc.tab_width());
helix-term/src/ui/statusline.rs+1−1
@@ -329,7 +329,7 @@ fn render_file_type<F>(context: &mut RenderContext, write: F)
329329where
330330 F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
331331{
332− let file_type = context.doc.language_id().unwrap_or("text");
332+ let file_type = context.doc.language_name().unwrap_or("text");
333333
334334 write(context, format!(" {} ", file_type), None);
335335}
helix-view/src/document.rs+13−3
@@ -926,22 +926,32 @@ impl Document {
926926 }
927927
928928 /// Corresponding language scope name. Usually `source.<lang>`.
929− pub fn language(&self) -> Option<&str> {
929+ pub fn language_scope(&self) -> Option<&str> {
930930 self.language
931931 .as_ref()
932932 .map(|language| language.scope.as_str())
933933 }
934934
935+ /// Language name for the document. Corresponds to the `name` key in
936+ /// `languages.toml` configuration.
937+ pub fn language_name(&self) -> Option<&str> {
938+ self.language
939+ .as_ref()
940+ .map(|language| language.language_id.as_str())
941+ }
942+
935943 /// Language ID for the document. Either the `language-id` from the
936944 /// `language-server` configuration, or the document language if no
937945 /// `language-id` has been specified.
938946 pub fn language_id(&self) -> Option<&str> {
939− self.language_config()?
947+ let language_config = self.language.as_deref()?;
948+
949+ language_config
940950 .language_server
941951 .as_ref()?
942952 .language_id
943953 .as_deref()
944− .or_else(|| Some(self.language()?.rsplit_once('.')?.1))
954+ .or(Some(language_config.language_id.as_str()))
945955 }
946956
947957 /// Corresponding [`LanguageConfiguration`].