mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
fix(lsp): allow formatting vendor files (#20844)
This commit is contained in:
parent
29026fac21
commit
84c9300aff
2 changed files with 86 additions and 11 deletions
|
@ -1632,23 +1632,30 @@ impl Inner {
|
||||||
&self,
|
&self,
|
||||||
params: DocumentFormattingParams,
|
params: DocumentFormattingParams,
|
||||||
) -> LspResult<Option<Vec<TextEdit>>> {
|
) -> LspResult<Option<Vec<TextEdit>>> {
|
||||||
let specifier = self
|
let mut specifier = self
|
||||||
.url_map
|
.url_map
|
||||||
.normalize_url(¶ms.text_document.uri, LspUrlKind::File);
|
.normalize_url(¶ms.text_document.uri, LspUrlKind::File);
|
||||||
let document = match self.documents.get(&specifier) {
|
|
||||||
Some(doc) if doc.is_open() => doc,
|
|
||||||
_ => return Ok(None),
|
|
||||||
};
|
|
||||||
let mark = self.performance.mark("formatting", Some(¶ms));
|
|
||||||
let file_path = specifier_to_file_path(&specifier).map_err(|err| {
|
|
||||||
error!("{}", err);
|
|
||||||
LspError::invalid_request()
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// skip formatting any files ignored by the config file
|
// skip formatting any files ignored by the config file
|
||||||
if !self.fmt_options.files.matches_specifier(&specifier) {
|
if !self.fmt_options.files.matches_specifier(&specifier) {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
let document = match self.documents.get(&specifier) {
|
||||||
|
Some(doc) if doc.is_open() => doc,
|
||||||
|
_ => return Ok(None),
|
||||||
|
};
|
||||||
|
// Detect vendored paths. Vendor file URLs will normalize to their remote
|
||||||
|
// counterparts, but for formatting we want to favour the file URL.
|
||||||
|
// TODO(nayeemrmn): Implement `Document::file_resource_path()` or similar.
|
||||||
|
if specifier.scheme() != "file"
|
||||||
|
&& params.text_document.uri.scheme() == "file"
|
||||||
|
{
|
||||||
|
specifier = params.text_document.uri.clone();
|
||||||
|
}
|
||||||
|
let file_path = specifier_to_file_path(&specifier).map_err(|err| {
|
||||||
|
error!("{}", err);
|
||||||
|
LspError::invalid_request()
|
||||||
|
})?;
|
||||||
|
let mark = self.performance.mark("formatting", Some(¶ms));
|
||||||
|
|
||||||
// spawn a blocking task to allow doing other work while this is occurring
|
// spawn a blocking task to allow doing other work while this is occurring
|
||||||
let text_edits = deno_core::unsync::spawn_blocking({
|
let text_edits = deno_core::unsync::spawn_blocking({
|
||||||
|
|
|
@ -688,6 +688,74 @@ fn lsp_import_map_node_specifiers() {
|
||||||
client.shutdown();
|
client.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lsp_format_vendor_path() {
|
||||||
|
let context = TestContextBuilder::new()
|
||||||
|
.use_http_server()
|
||||||
|
.use_temp_cwd()
|
||||||
|
.build();
|
||||||
|
let temp_dir = context.temp_dir();
|
||||||
|
temp_dir.write("deno.json", json!({ "vendor": true }).to_string());
|
||||||
|
let mut client = context.new_lsp_command().build();
|
||||||
|
client.initialize_default();
|
||||||
|
client.did_open(json!({
|
||||||
|
"textDocument": {
|
||||||
|
"uri": "file:///a/file.ts",
|
||||||
|
"languageId": "typescript",
|
||||||
|
"version": 1,
|
||||||
|
"text": r#"import "http://localhost:4545/run/002_hello.ts";"#,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
client.write_request(
|
||||||
|
"workspace/executeCommand",
|
||||||
|
json!({
|
||||||
|
"command": "deno.cache",
|
||||||
|
"arguments": [[], "file:///a/file.ts"],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
assert!(temp_dir
|
||||||
|
.path()
|
||||||
|
.join("vendor/http_localhost_4545/run/002_hello.ts")
|
||||||
|
.exists());
|
||||||
|
client.did_open(json!({
|
||||||
|
"textDocument": {
|
||||||
|
"uri": temp_dir.uri().join("vendor/http_localhost_4545/run/002_hello.ts").unwrap(),
|
||||||
|
"languageId": "typescript",
|
||||||
|
"version": 1,
|
||||||
|
"text": r#"console.log("Hello World");"#,
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
let res = client.write_request(
|
||||||
|
"textDocument/formatting",
|
||||||
|
json!({
|
||||||
|
"textDocument": {
|
||||||
|
"uri": temp_dir.uri().join("vendor/http_localhost_4545/run/002_hello.ts").unwrap(),
|
||||||
|
},
|
||||||
|
"options": {
|
||||||
|
"tabSize": 2,
|
||||||
|
"insertSpaces": true,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
res,
|
||||||
|
json!([{
|
||||||
|
"range": {
|
||||||
|
"start": {
|
||||||
|
"line": 0,
|
||||||
|
"character": 27,
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 0,
|
||||||
|
"character": 27,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"newText": "\n",
|
||||||
|
}]),
|
||||||
|
);
|
||||||
|
client.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
// Regression test for https://github.com/denoland/deno/issues/19802.
|
// Regression test for https://github.com/denoland/deno/issues/19802.
|
||||||
// Disable the `workspace/configuration` capability. Ensure the LSP falls back
|
// Disable the `workspace/configuration` capability. Ensure the LSP falls back
|
||||||
// to using `enablePaths` from the `InitializationOptions`.
|
// to using `enablePaths` from the `InitializationOptions`.
|
||||||
|
|
Loading…
Reference in a new issue