pasteEdits returns no edit when there are no imports needed (#59189)

This commit is contained in:
navya9singh 2024-07-09 10:27:41 -07:00 committed by GitHub
parent a6fb4dc103
commit ded36b21ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 60 additions and 87 deletions

View File

@ -1029,7 +1029,7 @@ export class SessionClient implements LanguageService {
};
const request = this.processRequest<protocol.GetPasteEditsRequest>(protocol.CommandTypes.GetPasteEdits, args);
const response = this.processResponse<protocol.GetPasteEditsResponse>(request);
if (!response.body) {
if (response.body.edits.length === 0) {
return { edits: [] };
}
const edits: FileTextChanges[] = this.convertCodeEditsToTextChanges(response.body.edits);

View File

@ -71,8 +71,9 @@ function pasteEdits(
newText = actualPastedText ? newText.slice(0, pos) + actualPastedText[0] + newText.slice(end) : newText.slice(0, pos) + pastedText[i] + newText.slice(end);
}
let importAdder: codefix.ImportAdder;
Debug.checkDefined(host.runWithTemporaryFileUpdate).call(host, targetFile.fileName, newText, (updatedProgram: Program, originalProgram: Program | undefined, updatedFile: SourceFile) => {
const importAdder = codefix.createImportAdder(updatedFile, updatedProgram, preferences, host);
importAdder = codefix.createImportAdder(updatedFile, updatedProgram, preferences, host);
if (copiedFrom?.range) {
Debug.assert(copiedFrom.range.length === pastedText.length);
copiedFrom.range.forEach(copy => {
@ -115,6 +116,13 @@ function pasteEdits(
}
importAdder.writeFixes(changes, getQuotePreference(copiedFrom ? copiedFrom.file : targetFile, preferences));
});
/**
* If there are no import fixes, getPasteEdits should return without making any changes to the file.
*/
if (!importAdder!.hasFixes()) {
return;
}
pasteLocations.forEach((paste, i) => {
changes.replaceRangeWithText(
targetFile,

View File

@ -18,6 +18,11 @@ describe("unittests:: tsserver:: pasteEdits", () => {
content: `const a = 1;
const b = 2;
const c = 3;`,
};
const file1: File = {
path: "/project/a/file1.ts",
content: `export const r = 1;
export const s = 2;`,
};
const tsconfig: File = {
path: "/project/tsconfig.json",
@ -27,7 +32,7 @@ const c = 3;`,
function e();
const f = r + s;`;
const host = createServerHost([target, tsconfig, libFile]);
const host = createServerHost([target, file1, tsconfig, libFile]);
const session = new TestSession(host);
openFilesForSession([target], session);

View File

@ -259,24 +259,7 @@ Info seq [hh:mm:ss:mss] response:
"updateGraphDurationMs": *
},
"body": {
"edits": [
{
"fileName": "/b.ts",
"textChanges": [
{
"start": {
"line": 1,
"offset": 1
},
"end": {
"line": 1,
"offset": 1
},
"newText": "export"
}
]
}
],
"edits": [],
"fixId": "providePostPasteEdits"
}
}

View File

@ -229,24 +229,7 @@ Info seq [hh:mm:ss:mss] response:
"updateGraphDurationMs": *
},
"body": {
"edits": [
{
"fileName": "/target.ts",
"textChanges": [
{
"start": {
"line": 2,
"offset": 14
},
"end": {
"line": 2,
"offset": 14
},
"newText": "/**\n* Testing comment line 1\n* line 2\n* line 3\n* line 4\n*/"
}
]
}
],
"edits": [],
"fixId": "providePostPasteEdits"
}
}

View File

@ -246,24 +246,7 @@ Info seq [hh:mm:ss:mss] response:
"updateGraphDurationMs": *
},
"body": {
"edits": [
{
"fileName": "/target.ts",
"textChanges": [
{
"start": {
"line": 4,
"offset": 1
},
"end": {
"line": 4,
"offset": 1
},
"newText": "console.log(k);"
}
]
}
],
"edits": [],
"fixId": "providePostPasteEdits"
}
}

View File

@ -6,6 +6,10 @@ const a = 1;
const b = 2;
const c = 3;
//// [/project/a/file1.ts]
export const r = 1;
export const s = 2;
//// [/project/tsconfig.json]
{}
@ -47,6 +51,7 @@ Info seq [hh:mm:ss:mss] event:
}
Info seq [hh:mm:ss:mss] Config: /project/tsconfig.json : {
"rootNames": [
"/project/a/file1.ts",
"/project/a/target.ts"
],
"options": {
@ -55,17 +60,21 @@ Info seq [hh:mm:ss:mss] Config: /project/tsconfig.json : {
}
Info seq [hh:mm:ss:mss] DirectoryWatcher:: Added:: WatchInfo: /project 1 undefined Config: /project/tsconfig.json WatchType: Wild card directory
Info seq [hh:mm:ss:mss] Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: /project 1 undefined Config: /project/tsconfig.json WatchType: Wild card directory
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /project/a/file1.ts 500 undefined WatchType: Closed Script info
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /project/tsconfig.json
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 500 undefined WatchType: Closed Script info
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /project/tsconfig.json projectStateVersion: 1 projectProgramVersion: 0 structureChanged: true structureIsReused:: Not Elapsed:: *ms
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
Info seq [hh:mm:ss:mss] Files (2)
Info seq [hh:mm:ss:mss] Files (3)
/a/lib/lib.d.ts Text-1 "/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }"
/project/a/file1.ts Text-1 "export const r = 1;\nexport const s = 2;"
/project/a/target.ts SVC-1-0 "const a = 1;\nconst b = 2;\nconst c = 3;"
../a/lib/lib.d.ts
Default library for target 'es5'
a/file1.ts
Matched by default include pattern '**/*'
a/target.ts
Matched by default include pattern '**/*'
@ -93,8 +102,8 @@ Info seq [hh:mm:ss:mss] event:
"jsSize": 0,
"jsx": 0,
"jsxSize": 0,
"ts": 1,
"tsSize": 38,
"ts": 2,
"tsSize": 77,
"tsx": 0,
"tsxSize": 0,
"dts": 1,
@ -132,7 +141,7 @@ Info seq [hh:mm:ss:mss] event:
}
}
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
Info seq [hh:mm:ss:mss] Files (2)
Info seq [hh:mm:ss:mss] Files (3)
Info seq [hh:mm:ss:mss] -----------------------------------------------
Info seq [hh:mm:ss:mss] Open files:
@ -154,6 +163,8 @@ After request
FsWatches::
/a/lib/lib.d.ts: *new*
{}
/project/a/file1.ts: *new*
{}
/project/tsconfig.json: *new*
{}
@ -171,6 +182,10 @@ ScriptInfos::
version: Text-1
containingProjects: 1
/project/tsconfig.json
/project/a/file1.ts *new*
version: Text-1
containingProjects: 1
/project/tsconfig.json
/project/a/target.ts (Open) *new*
version: SVC-1-0
containingProjects: 1
@ -205,8 +220,9 @@ Info seq [hh:mm:ss:mss] request:
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /project/tsconfig.json
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /project/tsconfig.json projectStateVersion: 2 projectProgramVersion: 1 structureChanged: false structureIsReused:: Completely Elapsed:: *ms
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
Info seq [hh:mm:ss:mss] Files (2)
Info seq [hh:mm:ss:mss] Files (3)
/a/lib/lib.d.ts Text-1 "/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }"
/project/a/file1.ts Text-1 "export const r = 1;\nexport const s = 2;"
/project/a/target.ts SVC-1-1 "const a = 1;const q = 1;\nfunction e();\nconst f = r + s;\nconst b = 2;\nconst c = 3;"
Info seq [hh:mm:ss:mss] -----------------------------------------------
@ -217,6 +233,17 @@ Info seq [hh:mm:ss:mss] response:
{
"fileName": "/project/a/target.ts",
"textChanges": [
{
"start": {
"line": 1,
"offset": 1
},
"end": {
"line": 1,
"offset": 1
},
"newText": "import { r, s } from \"./file1\";\n\n"
},
{
"start": {
"line": 1,
@ -251,6 +278,10 @@ ScriptInfos::
version: Text-1
containingProjects: 1
/project/tsconfig.json
/project/a/file1.ts
version: Text-1
containingProjects: 1
/project/tsconfig.json
/project/a/target.ts (Open) *changed*
version: SVC-1-2 *changed*
containingProjects: 1
@ -281,8 +312,9 @@ Before running Timeout callback:: count: 1
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /project/tsconfig.json
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /project/tsconfig.json projectStateVersion: 3 projectProgramVersion: 1 structureChanged: false structureIsReused:: Completely Elapsed:: *ms
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
Info seq [hh:mm:ss:mss] Files (2)
Info seq [hh:mm:ss:mss] Files (3)
/a/lib/lib.d.ts Text-1 "/// <reference no-default-lib=\"true\"/>\ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array<T> { length: number; [n: number]: T; }"
/project/a/file1.ts Text-1 "export const r = 1;\nexport const s = 2;"
/project/a/target.ts SVC-1-2 "const a = 1;\nconst b = 2;\nconst c = 3;"
Info seq [hh:mm:ss:mss] -----------------------------------------------

View File

@ -18,8 +18,5 @@ verify.pasteEdits({
pasteLocations: [range[0]],
copiedFrom: { file: "a.ts", range: [range[1]] },
},
newFileContents: {
"/b.ts":
`export`
}
newFileContents: {}
});

View File

@ -19,15 +19,5 @@ verify.pasteEdits({
*/`],
pasteLocations: [range[0]],
},
newFileContents: {
"/target.ts":
`const a = 10;
const b = 10;/**
* Testing comment line 1
* line 2
* line 3
* line 4
*/
const c = 10;`
}
newFileContents: {}
});

View File

@ -17,12 +17,5 @@ verify.pasteEdits({
pasteLocations: [range[1]],
copiedFrom: { file: "target.ts", range: [range[0]] },
},
newFileContents: {
"/target.ts":
`const k = 1;
console.log(k);
console.log(k);
console.log("test");`
}
newFileContents: {}
});

View File

@ -38,5 +38,4 @@ interface Testing {
test4: Test4;
}const c = 10;`
},
fixId: "providePostPasteEdits"
});