mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
fix(lsp/format): language formatter used should be based on language id (#17148)
Closes #11897
This commit is contained in:
parent
fcca54d3ba
commit
423474caa8
6 changed files with 57 additions and 10 deletions
|
@ -526,7 +526,7 @@ mod tests {
|
||||||
documents.open(
|
documents.open(
|
||||||
specifier.clone(),
|
specifier.clone(),
|
||||||
*version,
|
*version,
|
||||||
language_id.clone(),
|
*language_id,
|
||||||
(*source).into(),
|
(*source).into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1010,7 +1010,7 @@ mod tests {
|
||||||
documents.open(
|
documents.open(
|
||||||
specifier.clone(),
|
specifier.clone(),
|
||||||
*version,
|
*version,
|
||||||
language_id.clone(),
|
*language_id,
|
||||||
(*source).into(),
|
(*source).into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ static TSX_HEADERS: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
||||||
.collect()
|
.collect()
|
||||||
});
|
});
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum LanguageId {
|
pub enum LanguageId {
|
||||||
JavaScript,
|
JavaScript,
|
||||||
Jsx,
|
Jsx,
|
||||||
|
@ -93,6 +93,31 @@ pub enum LanguageId {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LanguageId {
|
impl LanguageId {
|
||||||
|
pub fn as_media_type(&self) -> MediaType {
|
||||||
|
match self {
|
||||||
|
LanguageId::JavaScript => MediaType::JavaScript,
|
||||||
|
LanguageId::Jsx => MediaType::Jsx,
|
||||||
|
LanguageId::TypeScript => MediaType::TypeScript,
|
||||||
|
LanguageId::Tsx => MediaType::Tsx,
|
||||||
|
LanguageId::Json => MediaType::Json,
|
||||||
|
LanguageId::JsonC => MediaType::Json,
|
||||||
|
LanguageId::Markdown | LanguageId::Unknown => MediaType::Unknown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_extension(&self) -> Option<&'static str> {
|
||||||
|
match self {
|
||||||
|
LanguageId::JavaScript => Some("js"),
|
||||||
|
LanguageId::Jsx => Some("jsx"),
|
||||||
|
LanguageId::TypeScript => Some("ts"),
|
||||||
|
LanguageId::Tsx => Some("tsx"),
|
||||||
|
LanguageId::Json => Some("json"),
|
||||||
|
LanguageId::JsonC => Some("jsonc"),
|
||||||
|
LanguageId::Markdown => Some("md"),
|
||||||
|
LanguageId::Unknown => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn as_headers(&self) -> Option<&HashMap<String, String>> {
|
fn as_headers(&self) -> Option<&HashMap<String, String>> {
|
||||||
match self {
|
match self {
|
||||||
Self::JavaScript => Some(&JS_HEADERS),
|
Self::JavaScript => Some(&JS_HEADERS),
|
||||||
|
@ -394,7 +419,7 @@ impl Document {
|
||||||
Ok(Document(Arc::new(DocumentInner {
|
Ok(Document(Arc::new(DocumentInner {
|
||||||
specifier: self.0.specifier.clone(),
|
specifier: self.0.specifier.clone(),
|
||||||
fs_version: self.0.fs_version.clone(),
|
fs_version: self.0.fs_version.clone(),
|
||||||
maybe_language_id: self.0.maybe_language_id.clone(),
|
maybe_language_id: self.0.maybe_language_id,
|
||||||
dependencies,
|
dependencies,
|
||||||
text_info,
|
text_info,
|
||||||
line_index,
|
line_index,
|
||||||
|
@ -464,10 +489,22 @@ impl Document {
|
||||||
|
|
||||||
pub fn media_type(&self) -> MediaType {
|
pub fn media_type(&self) -> MediaType {
|
||||||
if let Some(Ok(module)) = &self.0.maybe_module {
|
if let Some(Ok(module)) = &self.0.maybe_module {
|
||||||
module.media_type
|
return module.media_type;
|
||||||
} else {
|
|
||||||
MediaType::from(&self.0.specifier)
|
|
||||||
}
|
}
|
||||||
|
let specifier_media_type = MediaType::from(&self.0.specifier);
|
||||||
|
if specifier_media_type != MediaType::Unknown {
|
||||||
|
return specifier_media_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
self
|
||||||
|
.0
|
||||||
|
.maybe_language_id
|
||||||
|
.map(|id| id.as_media_type())
|
||||||
|
.unwrap_or(MediaType::Unknown)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn maybe_language_id(&self) -> Option<LanguageId> {
|
||||||
|
self.0.maybe_language_id
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the current language server client version if any.
|
/// Returns the current language server client version if any.
|
||||||
|
|
|
@ -1212,6 +1212,14 @@ impl Inner {
|
||||||
}
|
}
|
||||||
Some(Err(err)) => Err(anyhow!("{}", err)),
|
Some(Err(err)) => Err(anyhow!("{}", err)),
|
||||||
None => {
|
None => {
|
||||||
|
// the file path is only used to determine what formatter should
|
||||||
|
// be used to format the file, so give the filepath an extension
|
||||||
|
// that matches what the user selected as the language
|
||||||
|
let file_path = document
|
||||||
|
.maybe_language_id()
|
||||||
|
.and_then(|id| id.as_extension())
|
||||||
|
.map(|ext| file_path.with_extension(ext))
|
||||||
|
.unwrap_or(file_path);
|
||||||
// it's not a js/ts file, so attempt to format its contents
|
// it's not a js/ts file, so attempt to format its contents
|
||||||
format_file(&file_path, &document.content(), &fmt_options)
|
format_file(&file_path, &document.content(), &fmt_options)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3466,7 +3466,7 @@ mod tests {
|
||||||
documents.open(
|
documents.open(
|
||||||
specifier.clone(),
|
specifier.clone(),
|
||||||
*version,
|
*version,
|
||||||
language_id.clone(),
|
*language_id,
|
||||||
(*source).into(),
|
(*source).into(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5339,7 +5339,9 @@ mod lsp {
|
||||||
"textDocument/didOpen",
|
"textDocument/didOpen",
|
||||||
json!({
|
json!({
|
||||||
"textDocument": {
|
"textDocument": {
|
||||||
"uri": "file:///a/file.json",
|
// Also test out using a non-json file extension here.
|
||||||
|
// What should matter is the language identifier.
|
||||||
|
"uri": "file:///a/file.lock",
|
||||||
"languageId": "json",
|
"languageId": "json",
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"text": "{\"key\":\"value\"}"
|
"text": "{\"key\":\"value\"}"
|
||||||
|
@ -5353,7 +5355,7 @@ mod lsp {
|
||||||
"textDocument/formatting",
|
"textDocument/formatting",
|
||||||
json!({
|
json!({
|
||||||
"textDocument": {
|
"textDocument": {
|
||||||
"uri": "file:///a/file.json"
|
"uri": "file:///a/file.lock"
|
||||||
},
|
},
|
||||||
"options": {
|
"options": {
|
||||||
"tabSize": 2,
|
"tabSize": 2,
|
||||||
|
|
Loading…
Reference in a new issue