diff options
-rw-r--r-- | cli/lsp/analysis.rs | 46 | ||||
-rw-r--r-- | cli/tests/lsp_tests.rs | 33 | ||||
-rw-r--r-- | cli/tests/testdata/lsp/code_action_params_imports.json | 30 | ||||
-rw-r--r-- | cli/tests/testdata/lsp/code_action_resolve_params_imports.json | 28 | ||||
-rw-r--r-- | cli/tests/testdata/lsp/code_action_resolve_response_imports.json | 32 | ||||
-rw-r--r-- | cli/tests/testdata/lsp/code_action_response_imports.json | 44 |
6 files changed, 138 insertions, 75 deletions
diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index c4b9f0a86..bce89cc35 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -184,28 +184,30 @@ pub fn fix_ts_import_changes( for change in changes { let mut text_changes = Vec::new(); for text_change in &change.text_changes { - if let Some(captures) = - IMPORT_SPECIFIER_RE.captures(&text_change.new_text) - { - let specifier = captures - .get(1) - .ok_or_else(|| anyhow!("Missing capture."))? - .as_str(); - if let Some(new_specifier) = - check_specifier(specifier, referrer, documents) - { - let new_text = - text_change.new_text.replace(specifier, &new_specifier); - text_changes.push(tsc::TextChange { - span: text_change.span.clone(), - new_text, - }); - } else { - text_changes.push(text_change.clone()); - } - } else { - text_changes.push(text_change.clone()); - } + let lines = text_change.new_text.split('\n'); + + let new_lines: Vec<String> = lines + .map(|line| { + // This assumes that there's only one import per line. + if let Some(captures) = IMPORT_SPECIFIER_RE.captures(line) { + let specifier = captures.get(1).unwrap().as_str(); + if let Some(new_specifier) = + check_specifier(specifier, referrer, documents) + { + line.replace(specifier, &new_specifier) + } else { + line.to_string() + } + } else { + line.to_string() + } + }) + .collect(); + + text_changes.push(tsc::TextChange { + span: text_change.span.clone(), + new_text: new_lines.join("\n").to_string(), + }); } r.push(tsc::FileTextChanges { file_name: change.file_name.clone(), diff --git a/cli/tests/lsp_tests.rs b/cli/tests/lsp_tests.rs index 8ed6a67d7..1236b6c45 100644 --- a/cli/tests/lsp_tests.rs +++ b/cli/tests/lsp_tests.rs @@ -3390,7 +3390,16 @@ mod lsp { "uri": "file:///a/file00.ts", "languageId": "typescript", "version": 1, - "text": "export const abc = \"abc\";\nexport const def = \"def\";\n" + "text": r#"export interface MallardDuckConfigOptions extends DuckConfigOptions { + kind: "mallard"; +} + +export class MallardDuckConfig extends DuckConfig { + constructor(options: MallardDuckConfigOptions) { + super(options); + } +} +"# } })); session.did_open(json!({ @@ -3398,7 +3407,27 @@ mod lsp { "uri": "file:///a/file01.ts", "languageId": "typescript", "version": 1, - "text": "\nconsole.log(abc);\nconsole.log(def)\n" + "text": r#"import { DuckConfigOptions } from "./file02.ts"; + +export class DuckConfig { + readonly kind; + constructor(options: DuckConfigOptions) { + this.kind = options.kind; + } +} +"# + } + })); + session.did_open(json!({ + "textDocument": { + "uri": "file:///a/file02.ts", + "languageId": "typescript", + "version": 1, + "text": r#"export interface DuckConfigOptions { + kind: string; + quacks: boolean; +} +"# } })); diff --git a/cli/tests/testdata/lsp/code_action_params_imports.json b/cli/tests/testdata/lsp/code_action_params_imports.json index 7a5824923..891257ca0 100644 --- a/cli/tests/testdata/lsp/code_action_params_imports.json +++ b/cli/tests/testdata/lsp/code_action_params_imports.json @@ -1,15 +1,15 @@ { "textDocument": { - "uri": "file:///a/file01.ts" + "uri": "file:///a/file00.ts" }, "range": { "start": { - "line": 1, - "character": 12 + "line": 0, + "character": 0 }, "end": { - "line": 1, - "character": 15 + "line": 6, + "character": 0 } }, "context": { @@ -17,34 +17,34 @@ { "range": { "start": { - "line": 1, - "character": 12 + "line": 0, + "character": 50 }, "end": { - "line": 1, - "character": 15 + "line": 0, + "character": 67 } }, "severity": 1, "code": 2304, "source": "deno-ts", - "message": "Cannot find name 'abc'." + "message": "Cannot find name 'DuckConfigOptions'." }, { "range": { "start": { - "line": 2, - "character": 12 + "line": 4, + "character": 39 }, "end": { - "line": 2, - "character": 15 + "line": 4, + "character": 49 } }, "severity": 1, "code": 2304, "source": "deno-ts", - "message": "Cannot find name 'def'." + "message": "Cannot find name 'DuckConfig'." } ], "only": [ diff --git a/cli/tests/testdata/lsp/code_action_resolve_params_imports.json b/cli/tests/testdata/lsp/code_action_resolve_params_imports.json index 60178bbfe..5e1beb87b 100644 --- a/cli/tests/testdata/lsp/code_action_resolve_params_imports.json +++ b/cli/tests/testdata/lsp/code_action_resolve_params_imports.json @@ -5,22 +5,38 @@ { "range": { "start": { - "line": 1, - "character": 12 + "line": 0, + "character": 50 }, "end": { - "line": 1, - "character": 15 + "line": 0, + "character": 67 } }, "severity": 1, "code": 2304, "source": "deno-ts", - "message": "Cannot find name 'abc'." + "message": "Cannot find name 'DuckConfigOptions'." + }, + { + "range": { + "start": { + "line": 4, + "character": 39 + }, + "end": { + "line": 4, + "character": 49 + } + }, + "severity": 1, + "code": 2304, + "source": "deno-ts", + "message": "Cannot find name 'DuckConfig'." } ], "data": { - "specifier": "file:///a/file01.ts", + "specifier": "file:///a/file00.ts", "fixId": "fixMissingImport" } } diff --git a/cli/tests/testdata/lsp/code_action_resolve_response_imports.json b/cli/tests/testdata/lsp/code_action_resolve_response_imports.json index 6621c501f..6ffc3192e 100644 --- a/cli/tests/testdata/lsp/code_action_resolve_response_imports.json +++ b/cli/tests/testdata/lsp/code_action_resolve_response_imports.json @@ -5,25 +5,41 @@ { "range": { "start": { - "line": 1, - "character": 12 + "line": 0, + "character": 50 }, "end": { - "line": 1, - "character": 15 + "line": 0, + "character": 67 } }, "severity": 1, "code": 2304, "source": "deno-ts", - "message": "Cannot find name 'abc'." + "message": "Cannot find name 'DuckConfigOptions'." + }, + { + "range": { + "start": { + "line": 4, + "character": 39 + }, + "end": { + "line": 4, + "character": 49 + } + }, + "severity": 1, + "code": 2304, + "source": "deno-ts", + "message": "Cannot find name 'DuckConfig'." } ], "edit": { "documentChanges": [ { "textDocument": { - "uri": "file:///a/file01.ts", + "uri": "file:///a/file00.ts", "version": 1 }, "edits": [ @@ -38,14 +54,14 @@ "character": 0 } }, - "newText": "import { abc,def } from \"./file00.ts\";\n" + "newText": "import { DuckConfig } from \"./file01.ts\";\nimport { DuckConfigOptions } from \"./file02.ts\";\n\n" } ] } ] }, "data": { - "specifier": "file:///a/file01.ts", + "specifier": "file:///a/file00.ts", "fixId": "fixMissingImport" } } diff --git a/cli/tests/testdata/lsp/code_action_response_imports.json b/cli/tests/testdata/lsp/code_action_response_imports.json index 4bbbb9c75..23eb6645d 100644 --- a/cli/tests/testdata/lsp/code_action_response_imports.json +++ b/cli/tests/testdata/lsp/code_action_response_imports.json @@ -1,30 +1,30 @@ [ { - "title": "Add import from \"./file00.ts\"", + "title": "Add import from \"./file02.ts\"", "kind": "quickfix", "diagnostics": [ { "range": { "start": { - "line": 1, - "character": 12 + "line": 0, + "character": 50 }, "end": { - "line": 1, - "character": 15 + "line": 0, + "character": 67 } }, "severity": 1, "code": 2304, "source": "deno-ts", - "message": "Cannot find name 'abc'." + "message": "Cannot find name 'DuckConfigOptions'." } ], "edit": { "documentChanges": [ { "textDocument": { - "uri": "file:///a/file01.ts", + "uri": "file:///a/file00.ts", "version": 1 }, "edits": [ @@ -39,7 +39,7 @@ "character": 0 } }, - "newText": "import { abc } from \"./file00.ts\";\n" + "newText": "import { DuckConfigOptions } from \"./file02.ts\";\n\n" } ] } @@ -53,51 +53,51 @@ { "range": { "start": { - "line": 1, - "character": 12 + "line": 0, + "character": 50 }, "end": { - "line": 1, - "character": 15 + "line": 0, + "character": 67 } }, "severity": 1, "code": 2304, "source": "deno-ts", - "message": "Cannot find name 'abc'." + "message": "Cannot find name 'DuckConfigOptions'." } ], "data": { - "specifier": "file:///a/file01.ts", + "specifier": "file:///a/file00.ts", "fixId": "fixMissingImport" } }, { - "title": "Add import from \"./file00.ts\"", + "title": "Add import from \"./file01.ts\"", "kind": "quickfix", "diagnostics": [ { "range": { "start": { - "line": 2, - "character": 12 + "line": 4, + "character": 39 }, "end": { - "line": 2, - "character": 15 + "line": 4, + "character": 49 } }, "severity": 1, "code": 2304, "source": "deno-ts", - "message": "Cannot find name 'def'." + "message": "Cannot find name 'DuckConfig'." } ], "edit": { "documentChanges": [ { "textDocument": { - "uri": "file:///a/file01.ts", + "uri": "file:///a/file00.ts", "version": 1 }, "edits": [ @@ -112,7 +112,7 @@ "character": 0 } }, - "newText": "import { def } from \"./file00.ts\";\n" + "newText": "import { DuckConfig } from \"./file01.ts\";\n\n" } ] } |