mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 16:42:21 -05:00
fix(lsp): "Add all missing imports" uses correct specifiers (#17216)
This commit fixes "Add all missing imports" quick fix; before it was replacing all occurrences with the same specifier. Now every line returned from TSC is processed individually.
This commit is contained in:
parent
203b5a4822
commit
4a1a6b7985
6 changed files with 138 additions and 75 deletions
|
@ -184,28 +184,30 @@ pub fn fix_ts_import_changes(
|
||||||
for change in changes {
|
for change in changes {
|
||||||
let mut text_changes = Vec::new();
|
let mut text_changes = Vec::new();
|
||||||
for text_change in &change.text_changes {
|
for text_change in &change.text_changes {
|
||||||
if let Some(captures) =
|
let lines = text_change.new_text.split('\n');
|
||||||
IMPORT_SPECIFIER_RE.captures(&text_change.new_text)
|
|
||||||
{
|
let new_lines: Vec<String> = lines
|
||||||
let specifier = captures
|
.map(|line| {
|
||||||
.get(1)
|
// This assumes that there's only one import per line.
|
||||||
.ok_or_else(|| anyhow!("Missing capture."))?
|
if let Some(captures) = IMPORT_SPECIFIER_RE.captures(line) {
|
||||||
.as_str();
|
let specifier = captures.get(1).unwrap().as_str();
|
||||||
if let Some(new_specifier) =
|
if let Some(new_specifier) =
|
||||||
check_specifier(specifier, referrer, documents)
|
check_specifier(specifier, referrer, documents)
|
||||||
{
|
{
|
||||||
let new_text =
|
line.replace(specifier, &new_specifier)
|
||||||
text_change.new_text.replace(specifier, &new_specifier);
|
} else {
|
||||||
text_changes.push(tsc::TextChange {
|
line.to_string()
|
||||||
span: text_change.span.clone(),
|
}
|
||||||
new_text,
|
} else {
|
||||||
});
|
line.to_string()
|
||||||
} else {
|
}
|
||||||
text_changes.push(text_change.clone());
|
})
|
||||||
}
|
.collect();
|
||||||
} else {
|
|
||||||
text_changes.push(text_change.clone());
|
text_changes.push(tsc::TextChange {
|
||||||
}
|
span: text_change.span.clone(),
|
||||||
|
new_text: new_lines.join("\n").to_string(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
r.push(tsc::FileTextChanges {
|
r.push(tsc::FileTextChanges {
|
||||||
file_name: change.file_name.clone(),
|
file_name: change.file_name.clone(),
|
||||||
|
|
|
@ -3390,7 +3390,16 @@ mod lsp {
|
||||||
"uri": "file:///a/file00.ts",
|
"uri": "file:///a/file00.ts",
|
||||||
"languageId": "typescript",
|
"languageId": "typescript",
|
||||||
"version": 1,
|
"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!({
|
session.did_open(json!({
|
||||||
|
@ -3398,7 +3407,27 @@ mod lsp {
|
||||||
"uri": "file:///a/file01.ts",
|
"uri": "file:///a/file01.ts",
|
||||||
"languageId": "typescript",
|
"languageId": "typescript",
|
||||||
"version": 1,
|
"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;
|
||||||
|
}
|
||||||
|
"#
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
{
|
{
|
||||||
"textDocument": {
|
"textDocument": {
|
||||||
"uri": "file:///a/file01.ts"
|
"uri": "file:///a/file00.ts"
|
||||||
},
|
},
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 12
|
"character": 0
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 6,
|
||||||
"character": 15
|
"character": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"context": {
|
"context": {
|
||||||
|
@ -17,34 +17,34 @@
|
||||||
{
|
{
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 12
|
"character": 50
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 15
|
"character": 67
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"severity": 1,
|
"severity": 1,
|
||||||
"code": 2304,
|
"code": 2304,
|
||||||
"source": "deno-ts",
|
"source": "deno-ts",
|
||||||
"message": "Cannot find name 'abc'."
|
"message": "Cannot find name 'DuckConfigOptions'."
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 2,
|
"line": 4,
|
||||||
"character": 12
|
"character": 39
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 2,
|
"line": 4,
|
||||||
"character": 15
|
"character": 49
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"severity": 1,
|
"severity": 1,
|
||||||
"code": 2304,
|
"code": 2304,
|
||||||
"source": "deno-ts",
|
"source": "deno-ts",
|
||||||
"message": "Cannot find name 'def'."
|
"message": "Cannot find name 'DuckConfig'."
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"only": [
|
"only": [
|
||||||
|
|
|
@ -5,22 +5,38 @@
|
||||||
{
|
{
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 12
|
"character": 50
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 15
|
"character": 67
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"severity": 1,
|
"severity": 1,
|
||||||
"code": 2304,
|
"code": 2304,
|
||||||
"source": "deno-ts",
|
"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": {
|
"data": {
|
||||||
"specifier": "file:///a/file01.ts",
|
"specifier": "file:///a/file00.ts",
|
||||||
"fixId": "fixMissingImport"
|
"fixId": "fixMissingImport"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,25 +5,41 @@
|
||||||
{
|
{
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 12
|
"character": 50
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 15
|
"character": 67
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"severity": 1,
|
"severity": 1,
|
||||||
"code": 2304,
|
"code": 2304,
|
||||||
"source": "deno-ts",
|
"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": {
|
"edit": {
|
||||||
"documentChanges": [
|
"documentChanges": [
|
||||||
{
|
{
|
||||||
"textDocument": {
|
"textDocument": {
|
||||||
"uri": "file:///a/file01.ts",
|
"uri": "file:///a/file00.ts",
|
||||||
"version": 1
|
"version": 1
|
||||||
},
|
},
|
||||||
"edits": [
|
"edits": [
|
||||||
|
@ -38,14 +54,14 @@
|
||||||
"character": 0
|
"character": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"newText": "import { abc,def } from \"./file00.ts\";\n"
|
"newText": "import { DuckConfig } from \"./file01.ts\";\nimport { DuckConfigOptions } from \"./file02.ts\";\n\n"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"data": {
|
"data": {
|
||||||
"specifier": "file:///a/file01.ts",
|
"specifier": "file:///a/file00.ts",
|
||||||
"fixId": "fixMissingImport"
|
"fixId": "fixMissingImport"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,30 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"title": "Add import from \"./file00.ts\"",
|
"title": "Add import from \"./file02.ts\"",
|
||||||
"kind": "quickfix",
|
"kind": "quickfix",
|
||||||
"diagnostics": [
|
"diagnostics": [
|
||||||
{
|
{
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 12
|
"character": 50
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 15
|
"character": 67
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"severity": 1,
|
"severity": 1,
|
||||||
"code": 2304,
|
"code": 2304,
|
||||||
"source": "deno-ts",
|
"source": "deno-ts",
|
||||||
"message": "Cannot find name 'abc'."
|
"message": "Cannot find name 'DuckConfigOptions'."
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"edit": {
|
"edit": {
|
||||||
"documentChanges": [
|
"documentChanges": [
|
||||||
{
|
{
|
||||||
"textDocument": {
|
"textDocument": {
|
||||||
"uri": "file:///a/file01.ts",
|
"uri": "file:///a/file00.ts",
|
||||||
"version": 1
|
"version": 1
|
||||||
},
|
},
|
||||||
"edits": [
|
"edits": [
|
||||||
|
@ -39,7 +39,7 @@
|
||||||
"character": 0
|
"character": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"newText": "import { abc } from \"./file00.ts\";\n"
|
"newText": "import { DuckConfigOptions } from \"./file02.ts\";\n\n"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -53,51 +53,51 @@
|
||||||
{
|
{
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 12
|
"character": 50
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 1,
|
"line": 0,
|
||||||
"character": 15
|
"character": 67
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"severity": 1,
|
"severity": 1,
|
||||||
"code": 2304,
|
"code": 2304,
|
||||||
"source": "deno-ts",
|
"source": "deno-ts",
|
||||||
"message": "Cannot find name 'abc'."
|
"message": "Cannot find name 'DuckConfigOptions'."
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"data": {
|
"data": {
|
||||||
"specifier": "file:///a/file01.ts",
|
"specifier": "file:///a/file00.ts",
|
||||||
"fixId": "fixMissingImport"
|
"fixId": "fixMissingImport"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"title": "Add import from \"./file00.ts\"",
|
"title": "Add import from \"./file01.ts\"",
|
||||||
"kind": "quickfix",
|
"kind": "quickfix",
|
||||||
"diagnostics": [
|
"diagnostics": [
|
||||||
{
|
{
|
||||||
"range": {
|
"range": {
|
||||||
"start": {
|
"start": {
|
||||||
"line": 2,
|
"line": 4,
|
||||||
"character": 12
|
"character": 39
|
||||||
},
|
},
|
||||||
"end": {
|
"end": {
|
||||||
"line": 2,
|
"line": 4,
|
||||||
"character": 15
|
"character": 49
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"severity": 1,
|
"severity": 1,
|
||||||
"code": 2304,
|
"code": 2304,
|
||||||
"source": "deno-ts",
|
"source": "deno-ts",
|
||||||
"message": "Cannot find name 'def'."
|
"message": "Cannot find name 'DuckConfig'."
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"edit": {
|
"edit": {
|
||||||
"documentChanges": [
|
"documentChanges": [
|
||||||
{
|
{
|
||||||
"textDocument": {
|
"textDocument": {
|
||||||
"uri": "file:///a/file01.ts",
|
"uri": "file:///a/file00.ts",
|
||||||
"version": 1
|
"version": 1
|
||||||
},
|
},
|
||||||
"edits": [
|
"edits": [
|
||||||
|
@ -112,7 +112,7 @@
|
||||||
"character": 0
|
"character": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"newText": "import { def } from \"./file00.ts\";\n"
|
"newText": "import { DuckConfig } from \"./file01.ts\";\n\n"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue