mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 08:33:43 -05:00
feat(lsp): provide the deno.cache command server-side (#20111)
Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
This commit is contained in:
parent
4a561f12db
commit
be1fc754a1
6 changed files with 110 additions and 26 deletions
|
@ -39,6 +39,7 @@ fn code_action_capabilities(
|
|||
|
||||
pub fn server_capabilities(
|
||||
client_capabilities: &ClientCapabilities,
|
||||
enable_builtin_commands: bool,
|
||||
) -> ServerCapabilities {
|
||||
let code_action_provider = code_action_capabilities(client_capabilities);
|
||||
ServerCapabilities {
|
||||
|
@ -118,7 +119,14 @@ pub fn server_capabilities(
|
|||
rename_provider: Some(OneOf::Left(true)),
|
||||
document_link_provider: None,
|
||||
color_provider: None,
|
||||
execute_command_provider: None,
|
||||
execute_command_provider: Some(ExecuteCommandOptions {
|
||||
commands: if enable_builtin_commands {
|
||||
vec!["deno.cache".into()]
|
||||
} else {
|
||||
vec![]
|
||||
},
|
||||
..Default::default()
|
||||
}),
|
||||
call_hierarchy_provider: Some(CallHierarchyServerCapability::Simple(true)),
|
||||
semantic_tokens_provider: Some(
|
||||
SemanticTokensServerCapabilities::SemanticTokensOptions(
|
||||
|
|
|
@ -169,7 +169,8 @@ pub async fn get_import_completions(
|
|||
} else if text.starts_with("npm:") {
|
||||
Some(lsp::CompletionResponse::List(lsp::CompletionList {
|
||||
is_incomplete: false,
|
||||
items: get_npm_completions(&text, &range, npm_search_api).await?,
|
||||
items: get_npm_completions(specifier, &text, &range, npm_search_api)
|
||||
.await?,
|
||||
}))
|
||||
} else if !text.is_empty() {
|
||||
// completion of modules from a module registry or cache
|
||||
|
@ -464,6 +465,7 @@ fn get_relative_specifiers(
|
|||
|
||||
/// Get completions for `npm:` specifiers.
|
||||
async fn get_npm_completions(
|
||||
referrer: &ModuleSpecifier,
|
||||
specifier: &str,
|
||||
range: &lsp::Range,
|
||||
npm_search_api: &impl NpmSearchApi,
|
||||
|
@ -513,7 +515,7 @@ async fn get_npm_completions(
|
|||
let command = Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!([&specifier])]),
|
||||
arguments: Some(vec![json!([&specifier]), json!(referrer)]),
|
||||
});
|
||||
let text_edit = Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
|
||||
range: *range,
|
||||
|
@ -546,7 +548,7 @@ async fn get_npm_completions(
|
|||
let command = Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!([&specifier])]),
|
||||
arguments: Some(vec![json!([&specifier]), json!(referrer)]),
|
||||
});
|
||||
let text_edit = Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
|
||||
range: *range,
|
||||
|
@ -854,9 +856,11 @@ mod tests {
|
|||
character: 32,
|
||||
},
|
||||
};
|
||||
let actual = get_npm_completions("npm:puppe", &range, &npm_search_api)
|
||||
.await
|
||||
.unwrap();
|
||||
let referrer = ModuleSpecifier::parse("file:///referrer.ts").unwrap();
|
||||
let actual =
|
||||
get_npm_completions(&referrer, "npm:puppe", &range, &npm_search_api)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
actual,
|
||||
vec![
|
||||
|
@ -872,7 +876,7 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!(["npm:puppeteer"])])
|
||||
arguments: Some(vec![json!(["npm:puppeteer"]), json!(&referrer)])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
@ -891,7 +895,10 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!(["npm:puppeteer-core"])])
|
||||
arguments: Some(vec![
|
||||
json!(["npm:puppeteer-core"]),
|
||||
json!(&referrer)
|
||||
])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
@ -910,9 +917,10 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!([
|
||||
"npm:puppeteer-extra-plugin-stealth"
|
||||
])])
|
||||
arguments: Some(vec![
|
||||
json!(["npm:puppeteer-extra-plugin-stealth"]),
|
||||
json!(&referrer)
|
||||
])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
@ -931,7 +939,10 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!(["npm:puppeteer-extra-plugin"])])
|
||||
arguments: Some(vec![
|
||||
json!(["npm:puppeteer-extra-plugin"]),
|
||||
json!(&referrer)
|
||||
])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
@ -967,9 +978,11 @@ mod tests {
|
|||
character: 37,
|
||||
},
|
||||
};
|
||||
let actual = get_npm_completions("npm:puppeteer@", &range, &npm_search_api)
|
||||
.await
|
||||
.unwrap();
|
||||
let referrer = ModuleSpecifier::parse("file:///referrer.ts").unwrap();
|
||||
let actual =
|
||||
get_npm_completions(&referrer, "npm:puppeteer@", &range, &npm_search_api)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
actual,
|
||||
vec![
|
||||
|
@ -985,7 +998,10 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!(["npm:puppeteer@21.0.2"])])
|
||||
arguments: Some(vec![
|
||||
json!(["npm:puppeteer@21.0.2"]),
|
||||
json!(&referrer)
|
||||
])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
@ -1004,7 +1020,10 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!(["npm:puppeteer@21.0.1"])])
|
||||
arguments: Some(vec![
|
||||
json!(["npm:puppeteer@21.0.1"]),
|
||||
json!(&referrer)
|
||||
])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
@ -1023,7 +1042,10 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!(["npm:puppeteer@21.0.0"])])
|
||||
arguments: Some(vec![
|
||||
json!(["npm:puppeteer@21.0.0"]),
|
||||
json!(&referrer)
|
||||
])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
@ -1042,7 +1064,10 @@ mod tests {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!(["npm:puppeteer@20.9.0"])])
|
||||
arguments: Some(vec![
|
||||
json!(["npm:puppeteer@20.9.0"]),
|
||||
json!(&referrer)
|
||||
])
|
||||
}),
|
||||
commit_characters: Some(
|
||||
IMPORT_COMMIT_CHARS.iter().map(|&c| c.into()).collect()
|
||||
|
|
|
@ -996,7 +996,7 @@ impl DenoDiagnostic {
|
|||
command: Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!([data.specifier])]),
|
||||
arguments: Some(vec![json!([data.specifier]), json!(&specifier)]),
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
|
|
|
@ -1238,7 +1238,24 @@ impl Inner {
|
|||
parent_process_checker::start(parent_pid)
|
||||
}
|
||||
|
||||
let capabilities = capabilities::server_capabilities(¶ms.capabilities);
|
||||
// TODO(nayeemrmn): This flag exists to avoid breaking the extension for the
|
||||
// 1.37.0 release. Eventually make this always true.
|
||||
// See https://github.com/denoland/deno/pull/20111#issuecomment-1705776794.
|
||||
let mut enable_builtin_commands = false;
|
||||
if let Some(value) = ¶ms.initialization_options {
|
||||
if let Some(object) = value.as_object() {
|
||||
if let Some(value) = object.get("enableBuiltinCommands") {
|
||||
if value.as_bool() == Some(true) {
|
||||
enable_builtin_commands = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let capabilities = capabilities::server_capabilities(
|
||||
¶ms.capabilities,
|
||||
enable_builtin_commands,
|
||||
);
|
||||
|
||||
let version = format!(
|
||||
"{} ({}, {})",
|
||||
|
@ -3031,6 +3048,34 @@ impl Inner {
|
|||
|
||||
#[tower_lsp::async_trait]
|
||||
impl tower_lsp::LanguageServer for LanguageServer {
|
||||
async fn execute_command(
|
||||
&self,
|
||||
params: ExecuteCommandParams,
|
||||
) -> LspResult<Option<Value>> {
|
||||
if params.command == "deno.cache" {
|
||||
let mut arguments = params.arguments.into_iter();
|
||||
let uris = serde_json::to_value(arguments.next()).unwrap();
|
||||
let uris: Vec<Url> = serde_json::from_value(uris)
|
||||
.map_err(|err| LspError::invalid_params(err.to_string()))?;
|
||||
let referrer = serde_json::to_value(arguments.next()).unwrap();
|
||||
let referrer: Url = serde_json::from_value(referrer)
|
||||
.map_err(|err| LspError::invalid_params(err.to_string()))?;
|
||||
return self
|
||||
.cache_request(Some(
|
||||
serde_json::to_value(lsp_custom::CacheParams {
|
||||
referrer: TextDocumentIdentifier { uri: referrer },
|
||||
uris: uris
|
||||
.into_iter()
|
||||
.map(|uri| TextDocumentIdentifier { uri })
|
||||
.collect(),
|
||||
})
|
||||
.expect("well formed json"),
|
||||
))
|
||||
.await;
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn initialize(
|
||||
&self,
|
||||
params: InitializeParams,
|
||||
|
|
|
@ -753,7 +753,10 @@ impl ModuleRegistry {
|
|||
Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!([item_specifier])]),
|
||||
arguments: Some(vec![
|
||||
json!([item_specifier]),
|
||||
json!(&specifier),
|
||||
]),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
|
@ -887,7 +890,10 @@ impl ModuleRegistry {
|
|||
Some(lsp::Command {
|
||||
title: "".to_string(),
|
||||
command: "deno.cache".to_string(),
|
||||
arguments: Some(vec![json!([item_specifier])]),
|
||||
arguments: Some(vec![
|
||||
json!([item_specifier]),
|
||||
json!(&specifier),
|
||||
]),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
|
|
|
@ -4257,7 +4257,7 @@ fn lsp_code_actions_deno_cache() {
|
|||
"command": {
|
||||
"title": "",
|
||||
"command": "deno.cache",
|
||||
"arguments": [["https://deno.land/x/a/mod.ts"]]
|
||||
"arguments": [["https://deno.land/x/a/mod.ts"], "file:///a/file.ts"]
|
||||
}
|
||||
}])
|
||||
);
|
||||
|
@ -4342,7 +4342,7 @@ fn lsp_code_actions_deno_cache_npm() {
|
|||
"command": {
|
||||
"title": "",
|
||||
"command": "deno.cache",
|
||||
"arguments": [["npm:chalk"]]
|
||||
"arguments": [["npm:chalk"], "file:///a/file.ts"]
|
||||
}
|
||||
}])
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue