1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-31 19:44:10 -05:00

fix(lsp): update import map config when deno.json changes (#19476)

Half of #19468
This commit is contained in:
David Sherret 2023-06-13 09:24:22 -04:00 committed by GitHub
parent 07cbec4a82
commit 39bf1d2fd5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 6 deletions
cli
lsp
tests/integration
test_util/src

View file

@ -1577,6 +1577,7 @@ impl Inner {
touched = true;
}
}
if let Some(config_info) = self.maybe_config_file_info.as_mut() {
if let Some(lockfile) = config_info.maybe_lockfile.as_ref() {
let lockfile_path = lockfile.lock().filename.clone();
@ -1595,6 +1596,7 @@ impl Inner {
}
}
}
if let Some(package_json) = &self.maybe_package_json {
// always update the package json if the deno config changes
if touched || changes.contains(&package_json.specifier()) {
@ -1604,16 +1606,21 @@ impl Inner {
touched = true;
}
}
// if the current import map, or config file has changed, we need to
// reload the import map
if let Some(import_map_uri) = &self.maybe_import_map_uri {
if touched || changes.contains(import_map_uri) {
if let Err(err) = self.update_import_map().await {
self.client.show_message(MessageType::WARNING, err);
}
touched = true;
let import_map_changed = self
.maybe_import_map_uri
.as_ref()
.map(|uri| changes.contains(uri))
.unwrap_or(false);
if touched || import_map_changed {
if let Err(err) = self.update_import_map().await {
self.client.show_message(MessageType::WARNING, err);
}
touched = true;
}
if touched {
self.recreate_npm_services_if_necessary().await;
self.refresh_documents_config();

View file

@ -359,6 +359,79 @@ fn lsp_import_map_embedded_in_config_file() {
client.shutdown();
}
#[test]
fn lsp_import_map_embedded_in_config_file_after_initialize() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
temp_dir.write("deno.embedded_import_map.jsonc", "{}");
temp_dir.create_dir_all("lib");
temp_dir.write("lib/b.ts", r#"export const b = "b";"#);
let mut client = context.new_lsp_command().build();
client.initialize(|builder| {
builder.set_config("./deno.embedded_import_map.jsonc");
});
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(), 1);
// update the import map
temp_dir.write(
"deno.embedded_import_map.jsonc",
r#"{
"imports": {
"/~/": "./lib/"
}
}"#,
);
client.did_change_watched_files(json!({
"changes": [{
"uri": temp_dir.uri().join("deno.embedded_import_map.jsonc").unwrap(),
"type": 2
}]
}));
assert_eq!(client.read_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 }
}
})
);
client.shutdown();
}
#[test]
fn lsp_deno_task() {
let context = TestContextBuilder::new().use_temp_cwd().build();

View file

@ -655,6 +655,10 @@ impl LspClient {
self.write_response(id, result);
}
pub fn did_change_watched_files(&mut self, params: Value) {
self.write_notification("workspace/didChangeWatchedFiles", params);
}
fn get_latest_diagnostic_batch_index(&mut self) -> usize {
let result = self
.write_request("deno/internalLatestDiagnosticBatchIndex", json!(null));