diff options
author | sigmaSd <bedisnbiba@gmail.com> | 2023-09-05 16:36:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-05 16:36:35 +0100 |
commit | be1fc754a14683bf640b7bf0ecf6e286d02ee118 (patch) | |
tree | e7861eba1de4c0d2bb3afe1cc8c407f8e3b4f668 /cli | |
parent | 4a561f12dbae5a49203eb2c08fed71d9d0dfeb99 (diff) |
feat(lsp): provide the deno.cache command server-side (#20111)
Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
Diffstat (limited to 'cli')
-rw-r--r-- | cli/lsp/capabilities.rs | 10 | ||||
-rw-r--r-- | cli/lsp/completions.rs | 63 | ||||
-rw-r--r-- | cli/lsp/diagnostics.rs | 2 | ||||
-rw-r--r-- | cli/lsp/language_server.rs | 47 | ||||
-rw-r--r-- | cli/lsp/registries.rs | 10 | ||||
-rw-r--r-- | cli/tests/integration/lsp_tests.rs | 4 |
6 files changed, 110 insertions, 26 deletions
diff --git a/cli/lsp/capabilities.rs b/cli/lsp/capabilities.rs index e814daf0b..f342b41b0 100644 --- a/cli/lsp/capabilities.rs +++ b/cli/lsp/capabilities.rs @@ -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( diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs index ce83fdeed..d459daee0 100644 --- a/cli/lsp/completions.rs +++ b/cli/lsp/completions.rs @@ -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() diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index f2f45a928..0bee08c1d 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -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() } diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index d4e5b3da9..a893308ae 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -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, diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index 71501d0c2..d2044ae66 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -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 diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index 19b968602..c86d30456 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -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"] } }]) ); |