1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-05 05:49:20 -05:00

fix(lsp): better handling of languageId (#11755)

Fixes #11521
Fixes #11742
This commit is contained in:
Kitson Kelly 2021-08-19 13:19:12 +10:00 committed by GitHub
parent bf0bacbc0e
commit 0d83afd939
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 11 deletions

View file

@ -22,6 +22,7 @@ There are several settings that the language server supports for a workspace:
- `deno.cache` - `deno.cache`
- `deno.config` - `deno.config`
- `deno.importMap` - `deno.importMap`
- `deno.internalDebug`
- `deno.codeLens.implementations` - `deno.codeLens.implementations`
- `deno.codeLens.references` - `deno.codeLens.references`
- `deno.codeLens.referencesAllFunctions` - `deno.codeLens.referencesAllFunctions`
@ -159,3 +160,21 @@ client:
suggestions: boolean; suggestions: boolean;
} }
``` ```
## Language IDs
The language server supports diagnostics and formatting for the following
[text document language IDs](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentItem):
- `"javascript"`
- `"javascriptreact"`
- `"jsx"` _non standard, same as `javascriptreact`_
- `"typescript"`
- `"typescriptreact"`
- `"tsx"` _non standard, same as `typescriptreact`_
The language server supports only formatting for the following language IDs:
- `"json"`
- `"jsonc"`
- `"markdown"`

View file

@ -6,7 +6,6 @@ use super::tsc;
use crate::media_type::MediaType; use crate::media_type::MediaType;
use deno_core::error::anyhow;
use deno_core::error::custom_error; use deno_core::error::custom_error;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::error::Context; use deno_core::error::Context;
@ -28,21 +27,24 @@ pub enum LanguageId {
Json, Json,
JsonC, JsonC,
Markdown, Markdown,
Unknown,
} }
impl FromStr for LanguageId { impl FromStr for LanguageId {
type Err = AnyError; type Err = AnyError;
fn from_str(s: &str) -> Result<Self, AnyError> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
"javascript" => Ok(Self::JavaScript), "javascript" => Ok(Self::JavaScript),
"javascriptreact" => Ok(Self::Jsx), "javascriptreact" => Ok(Self::Jsx),
"jsx" => Ok(Self::Jsx),
"typescript" => Ok(Self::TypeScript), "typescript" => Ok(Self::TypeScript),
"typescriptreact" => Ok(Self::Tsx), "typescriptreact" => Ok(Self::Tsx),
"tsx" => Ok(Self::Tsx),
"json" => Ok(Self::Json), "json" => Ok(Self::Json),
"jsonc" => Ok(Self::JsonC), "jsonc" => Ok(Self::JsonC),
"markdown" => Ok(Self::Markdown), "markdown" => Ok(Self::Markdown),
_ => Err(anyhow!("Unsupported language id: {}", s)), _ => Ok(Self::Unknown),
} }
} }
} }
@ -57,6 +59,7 @@ impl<'a> From<&'a LanguageId> for MediaType {
LanguageId::Markdown => MediaType::Unknown, LanguageId::Markdown => MediaType::Unknown,
LanguageId::Tsx => MediaType::Tsx, LanguageId::Tsx => MediaType::Tsx,
LanguageId::TypeScript => MediaType::TypeScript, LanguageId::TypeScript => MediaType::TypeScript,
LanguageId::Unknown => MediaType::Unknown,
} }
} }
} }
@ -448,6 +451,35 @@ mod tests {
use deno_core::resolve_url; use deno_core::resolve_url;
use lspower::lsp; use lspower::lsp;
#[test]
fn test_language_id() {
assert_eq!(
"javascript".parse::<LanguageId>().unwrap(),
LanguageId::JavaScript
);
assert_eq!(
"javascriptreact".parse::<LanguageId>().unwrap(),
LanguageId::Jsx
);
assert_eq!("jsx".parse::<LanguageId>().unwrap(), LanguageId::Jsx);
assert_eq!(
"typescript".parse::<LanguageId>().unwrap(),
LanguageId::TypeScript
);
assert_eq!(
"typescriptreact".parse::<LanguageId>().unwrap(),
LanguageId::Tsx
);
assert_eq!("tsx".parse::<LanguageId>().unwrap(), LanguageId::Tsx);
assert_eq!("json".parse::<LanguageId>().unwrap(), LanguageId::Json);
assert_eq!("jsonc".parse::<LanguageId>().unwrap(), LanguageId::JsonC);
assert_eq!(
"markdown".parse::<LanguageId>().unwrap(),
LanguageId::Markdown
);
assert_eq!("rust".parse::<LanguageId>().unwrap(), LanguageId::Unknown);
}
#[test] #[test]
fn test_document_cache_contains() { fn test_document_cache_contains() {
let mut document_cache = DocumentCache::default(); let mut document_cache = DocumentCache::default();
@ -543,10 +575,18 @@ mod tests {
document_cache.open( document_cache.open(
specifier.clone(), specifier.clone(),
1, 1,
LanguageId::TypeScript, "typescript".parse().unwrap(),
"console.log(\"hello world\");\n", "console.log(\"hello world\");\n",
); );
assert!(document_cache.is_diagnosable(&specifier)); assert!(document_cache.is_diagnosable(&specifier));
let specifier = resolve_url("file:///a/file.rs").unwrap();
document_cache.open(
specifier.clone(),
1,
"rust".parse().unwrap(),
"pub mod a;",
);
assert!(!document_cache.is_diagnosable(&specifier));
let specifier = let specifier =
resolve_url("asset:///lib.es2015.symbol.wellknown.d.ts").unwrap(); resolve_url("asset:///lib.es2015.symbol.wellknown.d.ts").unwrap();
assert!(document_cache.is_diagnosable(&specifier)); assert!(document_cache.is_diagnosable(&specifier));

View file

@ -772,13 +772,21 @@ impl Inner {
// already managed by the language service // already managed by the language service
return; return;
} }
let language_id = match params.text_document.language_id.parse() { let language_id =
Ok(language_id) => language_id, params
Err(err) => { .text_document
.language_id
.parse()
.unwrap_or_else(|err| {
error!("{}", err); error!("{}", err);
LanguageId::TypeScript LanguageId::Unknown
});
if language_id == LanguageId::Unknown {
warn!(
"Unsupported language id \"{}\" received for document \"{}\".",
params.text_document.language_id, params.text_document.uri
);
} }
};
let media_type = MediaType::from(&language_id); let media_type = MediaType::from(&language_id);
self.documents.open( self.documents.open(
specifier.clone(), specifier.clone(),