Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the correct language ID for JavaScript & TypeScript #1466

Merged
merged 3 commits into from
Jan 15, 2022

Conversation

hovsater
Copy link
Contributor

@hovsater hovsater commented Jan 8, 2022

The TypeScript language server expects the languageId to be javascript for .js, javascriptreact for .jsx, typescript for .ts, and typescriptreact for .tsx. In practice, only javascript, typescript and typescriptreact are relevant.

Since the goal is to keep language scopes inline with what other editors are doing, I didn't want to change the scope for tsx to source.typescriptreact.

Instead, I changed the implementation so that the language name is used as the languageId for LSP. This works, since we can set tree-sitter-library explicitly if the language name doesn't match the name of the tree sitter library.

While this doesn't seem to break any of the existing languages, I'm not sure if this is fine, or if we should introduce a new configuration option for languages called language-id and only fallback to using the language name if it's not set.

Any feedback is much appreciated. 🙂

@hovsater hovsater force-pushed the lsp-typescript branch 2 times, most recently from 901cc63 to fe630fe Compare January 8, 2022 21:16
@hovsater hovsater changed the title Ensure we use the correct language ID for JavaScript/TypeScript Ensure we use the correct language ID TypeScript (tsx) Jan 8, 2022
@hovsater hovsater changed the title Ensure we use the correct language ID TypeScript (tsx) Ensure we use the correct language ID TypeScript Jan 8, 2022
@hovsater hovsater changed the title Ensure we use the correct language ID TypeScript Use the correct language ID for JavaScript & TypeScript Jan 8, 2022
@hovsater hovsater force-pushed the lsp-typescript branch 2 times, most recently from 6962b63 to 01782ee Compare January 8, 2022 21:33
languages.toml Outdated
@@ -144,7 +144,8 @@ language-server = { command = "typescript-language-server", args = ["--stdio"] }
indent = { tab-width = 2, unit = " " }

[[language]]
name = "tsx"
name = "typescriptreact"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite like this since now our language names are tied to what the LSP expects (which might also be different per language server). Let's instead add a language-id key to https://github.com/helix-editor/helix/blob/01782ee16adcffd56af44890ad772477dbefe610/helix-core/src/syntax.rs#L93-98

pub language_id: Option<String>

Then the lsp code can use config.language_server.language_id.unwrap_or(config.name)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought so as well (I briefly mentioned it in the pull request description). I'll update the PR. 🙂

Copy link
Contributor Author

@hovsater hovsater Jan 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, should it really be part of the LSP settings and not the language itself? It's not really a configuration of the LSP, but more specifically an identifier of the language itself. I think it would make more sense to put language-id under the language section. Then language-id could default to the language name if language-id wasn't present.

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a lsp-specific language identifier. There's no standard conventions here, so I'd prefer to keep it grouped under the language server. It's quite likely that there could be another Javascript language server that expects a different identifier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c42377c.

Comment on lines 851 to 857
let fallback = self.language().and_then(|s| s.split('.').last());

self.language
.as_ref()
.and_then(|config| config.language_server.as_ref())
.and_then(|lsp_config| lsp_config.language_id.as_ref())
.map_or(fallback, |id| Some(id.as_str()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use map.or_else(|| ...) to avoid computing the fallback unless it's needed. Also rsplit_once is a nice optimization over split(..).last():

Suggested change
let fallback = self.language().and_then(|s| s.split('.').last());
self.language
.as_ref()
.and_then(|config| config.language_server.as_ref())
.and_then(|lsp_config| lsp_config.language_id.as_ref())
.map_or(fallback, |id| Some(id.as_str()))
self.language
.as_ref()
.and_then(|config| config.language_server.as_ref())
.and_then(|lsp_config| lsp_config.language_id.as_ref())
.map_or_else(
|| self.language().and_then(|s| s.rsplit_once('.').map(|(_, last)| last)),
|id| Some(id.as_str())
)

I'm also wondering why not

Suggested change
let fallback = self.language().and_then(|s| s.split('.').last());
self.language
.as_ref()
.and_then(|config| config.language_server.as_ref())
.and_then(|lsp_config| lsp_config.language_id.as_ref())
.map_or(fallback, |id| Some(id.as_str()))
self.language
.as_ref()
.and_then(|config| config.language_server.as_ref())
.map(|lsp_config| lsp_config.language_id.as_str())
.unwrap_or_else(|| self.language().and_then(|s| s.rsplit_once('.').map(|(_, last)| last)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cc80d3e.

I'm also wondering why not

I wasn't allowed to call as_str() on std::option::Option. On a more general note, I'm still very new to Rust, so my code is probably less than ideal most of the time. Happy to learn from feedback though. 🙂

Comment on lines 847 to 849
/// 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.
Copy link
Contributor

@pickfire pickfire Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing / on second line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2e0d66c.

@archseer
Copy link
Member

Looks good now, thanks :) 🎉

@archseer archseer merged commit 3a34036 into helix-editor:master Jan 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants