diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/lsp/config.rs | 7 | ||||
-rw-r--r-- | cli/lsp/language_server.rs | 40 | ||||
-rw-r--r-- | cli/tests/integration/lsp_tests.rs | 112 |
3 files changed, 142 insertions, 17 deletions
diff --git a/cli/lsp/config.rs b/cli/lsp/config.rs index 298c86a55..03464d1f8 100644 --- a/cli/lsp/config.rs +++ b/cli/lsp/config.rs @@ -773,7 +773,12 @@ fn resolve_lockfile_from_path( lockfile_path: PathBuf, ) -> Option<Arc<Mutex<Lockfile>>> { match Lockfile::new(lockfile_path, false) { - Ok(value) => Some(Arc::new(Mutex::new(value))), + Ok(value) => { + if let Ok(specifier) = ModuleSpecifier::from_file_path(&value.filename) { + lsp_log!(" Resolved lock file: \"{}\"", specifier); + } + Some(Arc::new(Mutex::new(value))) + } Err(err) => { lsp_warn!("Error loading lockfile: {:#}", err); None diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 6dead0fcf..5a4c3aeae 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -1493,22 +1493,30 @@ impl Inner { .collect(); // if the current deno.json has changed, we need to reload it - if let Some(config_file) = self.config.maybe_config_file() { - if changes.contains(&config_file.specifier) - || self - .config - .maybe_lockfile() - .map(|l| has_lockfile_changed(&l.lock(), &changes)) - .unwrap_or(false) - { - if let Err(err) = self.update_config_file().await { - self.client.show_message(MessageType::WARNING, err); - } - if let Err(err) = self.update_tsconfig().await { - self.client.show_message(MessageType::WARNING, err); - } - touched = true; + let has_config_changed = match self.config.maybe_config_file() { + Some(config_file) => changes.contains(&config_file.specifier), + None => { + // check for auto-discovery + changes.iter().any(|url| { + url.path().ends_with("/deno.json") + || url.path().ends_with("/deno.jsonc") + }) } + } || match self.config.maybe_lockfile() { + Some(lockfile) => has_lockfile_changed(&lockfile.lock(), &changes), + None => { + // check for auto-discovery + changes.iter().any(|url| url.path().ends_with("/deno.lock")) + } + }; + if has_config_changed { + if let Err(err) = self.update_config_file().await { + self.client.show_message(MessageType::WARNING, err); + } + if let Err(err) = self.update_tsconfig().await { + self.client.show_message(MessageType::WARNING, err); + } + touched = true; } if let Some(package_json) = &self.maybe_package_json { @@ -2940,7 +2948,7 @@ impl tower_lsp::LanguageServer for LanguageServer { let watch_registration_options = DidChangeWatchedFilesRegistrationOptions { watchers: vec![FileSystemWatcher { - glob_pattern: "**/*.{json,jsonc}".to_string(), + glob_pattern: "**/*.{json,jsonc,lock}".to_string(), kind: Some(WatchKind::Change), }], }; diff --git a/cli/tests/integration/lsp_tests.rs b/cli/tests/integration/lsp_tests.rs index 22463b58a..261c98b52 100644 --- a/cli/tests/integration/lsp_tests.rs +++ b/cli/tests/integration/lsp_tests.rs @@ -433,6 +433,118 @@ fn lsp_import_map_embedded_in_config_file_after_initialize() { } #[test] +fn lsp_import_map_config_file_auto_discovered() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir(); + temp_dir.create_dir_all("lib"); + temp_dir.write("lib/b.ts", r#"export const b = "b";"#); + + let mut client = context.new_lsp_command().capture_stderr().build(); + client.initialize_default(); + + // add the deno.json + temp_dir.write("deno.jsonc", r#"{ "imports": { "/~/": "./lib/" } }"#); + client.did_change_watched_files(json!({ + "changes": [{ + "uri": temp_dir.uri().join("deno.jsonc").unwrap(), + "type": 2 + }] + })); + client.wait_until_stderr_line(|line| { + line.contains("Auto-resolved configuration file:") + }); + + let uri = temp_dir.uri().join("a.ts").unwrap(); + + let diagnostics = client.did_open(json!({ + "textDocument": { + "uri": uri, + "languageId": "typescript", + "version": 1, + "text": "import { b } from \"/~/b.ts\";\n\nconsole.log(b);\n" + } + })); + + assert_eq!(diagnostics.all().len(), 0); + + let res = client.write_request( + "textDocument/hover", + json!({ + "textDocument": { + "uri": uri + }, + "position": { "line": 2, "character": 12 } + }), + ); + assert_eq!( + res, + json!({ + "contents": [ + { + "language": "typescript", + "value":"(alias) const b: \"b\"\nimport b" + }, + "" + ], + "range": { + "start": { "line": 2, "character": 12 }, + "end": { "line": 2, "character": 13 } + } + }) + ); + + // now cause a syntax error + temp_dir.write("deno.jsonc", r#",,#,#,,"#); + client.did_change_watched_files(json!({ + "changes": [{ + "uri": temp_dir.uri().join("deno.jsonc").unwrap(), + "type": 2 + }] + })); + assert_eq!(client.read_diagnostics().all().len(), 1); + + // now fix it, and things should work again + temp_dir.write("deno.jsonc", r#"{ "imports": { "/~/": "./lib/" } }"#); + client.did_change_watched_files(json!({ + "changes": [{ + "uri": temp_dir.uri().join("deno.jsonc").unwrap(), + "type": 2 + }] + })); + client.wait_until_stderr_line(|line| { + line.contains("Auto-resolved configuration file:") + }); + let res = client.write_request( + "textDocument/hover", + json!({ + "textDocument": { + "uri": uri + }, + "position": { "line": 2, "character": 12 } + }), + ); + assert_eq!( + res, + json!({ + "contents": [ + { + "language": "typescript", + "value":"(alias) const b: \"b\"\nimport b" + }, + "" + ], + "range": { + "start": { "line": 2, "character": 12 }, + "end": { "line": 2, "character": 13 } + } + }) + ); + assert_eq!(client.read_diagnostics().all().len(), 0); + + client.shutdown(); +} + +#[test] fn lsp_deno_task() { let context = TestContextBuilder::new().use_temp_cwd().build(); let temp_dir = context.temp_dir(); |