Skip to content

Commit ded36b2

Browse files
authored
pasteEdits returns no edit when there are no imports needed (microsoft#59189)
1 parent a6fb4dc commit ded36b2

File tree

11 files changed

+60
-87
lines changed

11 files changed

+60
-87
lines changed

src/harness/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ export class SessionClient implements LanguageService {
10291029
};
10301030
const request = this.processRequest<protocol.GetPasteEditsRequest>(protocol.CommandTypes.GetPasteEdits, args);
10311031
const response = this.processResponse<protocol.GetPasteEditsResponse>(request);
1032-
if (!response.body) {
1032+
if (response.body.edits.length === 0) {
10331033
return { edits: [] };
10341034
}
10351035
const edits: FileTextChanges[] = this.convertCodeEditsToTextChanges(response.body.edits);

src/services/pasteEdits.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ function pasteEdits(
7171
newText = actualPastedText ? newText.slice(0, pos) + actualPastedText[0] + newText.slice(end) : newText.slice(0, pos) + pastedText[i] + newText.slice(end);
7272
}
7373

74+
let importAdder: codefix.ImportAdder;
7475
Debug.checkDefined(host.runWithTemporaryFileUpdate).call(host, targetFile.fileName, newText, (updatedProgram: Program, originalProgram: Program | undefined, updatedFile: SourceFile) => {
75-
const importAdder = codefix.createImportAdder(updatedFile, updatedProgram, preferences, host);
76+
importAdder = codefix.createImportAdder(updatedFile, updatedProgram, preferences, host);
7677
if (copiedFrom?.range) {
7778
Debug.assert(copiedFrom.range.length === pastedText.length);
7879
copiedFrom.range.forEach(copy => {
@@ -115,6 +116,13 @@ function pasteEdits(
115116
}
116117
importAdder.writeFixes(changes, getQuotePreference(copiedFrom ? copiedFrom.file : targetFile, preferences));
117118
});
119+
120+
/**
121+
* If there are no import fixes, getPasteEdits should return without making any changes to the file.
122+
*/
123+
if (!importAdder!.hasFixes()) {
124+
return;
125+
}
118126
pasteLocations.forEach((paste, i) => {
119127
changes.replaceRangeWithText(
120128
targetFile,

src/testRunner/unittests/tsserver/pasteEdits.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ describe("unittests:: tsserver:: pasteEdits", () => {
1818
content: `const a = 1;
1919
const b = 2;
2020
const c = 3;`,
21+
};
22+
const file1: File = {
23+
path: "/project/a/file1.ts",
24+
content: `export const r = 1;
25+
export const s = 2;`,
2126
};
2227
const tsconfig: File = {
2328
path: "/project/tsconfig.json",
@@ -27,7 +32,7 @@ const c = 3;`,
2732
function e();
2833
const f = r + s;`;
2934

30-
const host = createServerHost([target, tsconfig, libFile]);
35+
const host = createServerHost([target, file1, tsconfig, libFile]);
3136
const session = new TestSession(host);
3237
openFilesForSession([target], session);
3338

tests/baselines/reference/tsserver/fourslashServer/pasteEdits_noImportNeeded.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -259,24 +259,7 @@ Info seq [hh:mm:ss:mss] response:
259259
"updateGraphDurationMs": *
260260
},
261261
"body": {
262-
"edits": [
263-
{
264-
"fileName": "/b.ts",
265-
"textChanges": [
266-
{
267-
"start": {
268-
"line": 1,
269-
"offset": 1
270-
},
271-
"end": {
272-
"line": 1,
273-
"offset": 1
274-
},
275-
"newText": "export"
276-
}
277-
]
278-
}
279-
],
262+
"edits": [],
280263
"fixId": "providePostPasteEdits"
281264
}
282265
}

tests/baselines/reference/tsserver/fourslashServer/pasteEdits_pasteComments.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -229,24 +229,7 @@ Info seq [hh:mm:ss:mss] response:
229229
"updateGraphDurationMs": *
230230
},
231231
"body": {
232-
"edits": [
233-
{
234-
"fileName": "/target.ts",
235-
"textChanges": [
236-
{
237-
"start": {
238-
"line": 2,
239-
"offset": 14
240-
},
241-
"end": {
242-
"line": 2,
243-
"offset": 14
244-
},
245-
"newText": "/**\n* Testing comment line 1\n* line 2\n* line 3\n* line 4\n*/"
246-
}
247-
]
248-
}
249-
],
232+
"edits": [],
250233
"fixId": "providePostPasteEdits"
251234
}
252235
}

tests/baselines/reference/tsserver/fourslashServer/pasteEdits_pasteIntoSameFile.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,7 @@ Info seq [hh:mm:ss:mss] response:
246246
"updateGraphDurationMs": *
247247
},
248248
"body": {
249-
"edits": [
250-
{
251-
"fileName": "/target.ts",
252-
"textChanges": [
253-
{
254-
"start": {
255-
"line": 4,
256-
"offset": 1
257-
},
258-
"end": {
259-
"line": 4,
260-
"offset": 1
261-
},
262-
"newText": "console.log(k);"
263-
}
264-
]
265-
}
266-
],
249+
"edits": [],
267250
"fixId": "providePostPasteEdits"
268251
}
269252
}

tests/baselines/reference/tsserver/pasteEdits/adds-paste-edits.js

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const a = 1;
66
const b = 2;
77
const c = 3;
88

9+
//// [/project/a/file1.ts]
10+
export const r = 1;
11+
export const s = 2;
12+
913
//// [/project/tsconfig.json]
1014
{}
1115

@@ -47,6 +51,7 @@ Info seq [hh:mm:ss:mss] event:
4751
}
4852
Info seq [hh:mm:ss:mss] Config: /project/tsconfig.json : {
4953
"rootNames": [
54+
"/project/a/file1.ts",
5055
"/project/a/target.ts"
5156
],
5257
"options": {
@@ -55,17 +60,21 @@ Info seq [hh:mm:ss:mss] Config: /project/tsconfig.json : {
5560
}
5661
Info seq [hh:mm:ss:mss] DirectoryWatcher:: Added:: WatchInfo: /project 1 undefined Config: /project/tsconfig.json WatchType: Wild card directory
5762
Info seq [hh:mm:ss:mss] Elapsed:: *ms DirectoryWatcher:: Added:: WatchInfo: /project 1 undefined Config: /project/tsconfig.json WatchType: Wild card directory
63+
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /project/a/file1.ts 500 undefined WatchType: Closed Script info
5864
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /project/tsconfig.json
5965
Info seq [hh:mm:ss:mss] FileWatcher:: Added:: WatchInfo: /a/lib/lib.d.ts 500 undefined WatchType: Closed Script info
6066
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /project/tsconfig.json projectStateVersion: 1 projectProgramVersion: 0 structureChanged: true structureIsReused:: Not Elapsed:: *ms
6167
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
62-
Info seq [hh:mm:ss:mss] Files (2)
68+
Info seq [hh:mm:ss:mss] Files (3)
6369
/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; }"
70+
/project/a/file1.ts Text-1 "export const r = 1;\nexport const s = 2;"
6471
/project/a/target.ts SVC-1-0 "const a = 1;\nconst b = 2;\nconst c = 3;"
6572

6673

6774
../a/lib/lib.d.ts
6875
Default library for target 'es5'
76+
a/file1.ts
77+
Matched by default include pattern '**/*'
6978
a/target.ts
7079
Matched by default include pattern '**/*'
7180

@@ -93,8 +102,8 @@ Info seq [hh:mm:ss:mss] event:
93102
"jsSize": 0,
94103
"jsx": 0,
95104
"jsxSize": 0,
96-
"ts": 1,
97-
"tsSize": 38,
105+
"ts": 2,
106+
"tsSize": 77,
98107
"tsx": 0,
99108
"tsxSize": 0,
100109
"dts": 1,
@@ -132,7 +141,7 @@ Info seq [hh:mm:ss:mss] event:
132141
}
133142
}
134143
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
135-
Info seq [hh:mm:ss:mss] Files (2)
144+
Info seq [hh:mm:ss:mss] Files (3)
136145

137146
Info seq [hh:mm:ss:mss] -----------------------------------------------
138147
Info seq [hh:mm:ss:mss] Open files:
@@ -154,6 +163,8 @@ After request
154163
FsWatches::
155164
/a/lib/lib.d.ts: *new*
156165
{}
166+
/project/a/file1.ts: *new*
167+
{}
157168
/project/tsconfig.json: *new*
158169
{}
159170

@@ -171,6 +182,10 @@ ScriptInfos::
171182
version: Text-1
172183
containingProjects: 1
173184
/project/tsconfig.json
185+
/project/a/file1.ts *new*
186+
version: Text-1
187+
containingProjects: 1
188+
/project/tsconfig.json
174189
/project/a/target.ts (Open) *new*
175190
version: SVC-1-0
176191
containingProjects: 1
@@ -205,8 +220,9 @@ Info seq [hh:mm:ss:mss] request:
205220
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /project/tsconfig.json
206221
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /project/tsconfig.json projectStateVersion: 2 projectProgramVersion: 1 structureChanged: false structureIsReused:: Completely Elapsed:: *ms
207222
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
208-
Info seq [hh:mm:ss:mss] Files (2)
223+
Info seq [hh:mm:ss:mss] Files (3)
209224
/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; }"
225+
/project/a/file1.ts Text-1 "export const r = 1;\nexport const s = 2;"
210226
/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;"
211227

212228
Info seq [hh:mm:ss:mss] -----------------------------------------------
@@ -217,6 +233,17 @@ Info seq [hh:mm:ss:mss] response:
217233
{
218234
"fileName": "/project/a/target.ts",
219235
"textChanges": [
236+
{
237+
"start": {
238+
"line": 1,
239+
"offset": 1
240+
},
241+
"end": {
242+
"line": 1,
243+
"offset": 1
244+
},
245+
"newText": "import { r, s } from \"./file1\";\n\n"
246+
},
220247
{
221248
"start": {
222249
"line": 1,
@@ -251,6 +278,10 @@ ScriptInfos::
251278
version: Text-1
252279
containingProjects: 1
253280
/project/tsconfig.json
281+
/project/a/file1.ts
282+
version: Text-1
283+
containingProjects: 1
284+
/project/tsconfig.json
254285
/project/a/target.ts (Open) *changed*
255286
version: SVC-1-2 *changed*
256287
containingProjects: 1
@@ -281,8 +312,9 @@ Before running Timeout callback:: count: 1
281312
Info seq [hh:mm:ss:mss] Starting updateGraphWorker: Project: /project/tsconfig.json
282313
Info seq [hh:mm:ss:mss] Finishing updateGraphWorker: Project: /project/tsconfig.json projectStateVersion: 3 projectProgramVersion: 1 structureChanged: false structureIsReused:: Completely Elapsed:: *ms
283314
Info seq [hh:mm:ss:mss] Project '/project/tsconfig.json' (Configured)
284-
Info seq [hh:mm:ss:mss] Files (2)
315+
Info seq [hh:mm:ss:mss] Files (3)
285316
/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; }"
317+
/project/a/file1.ts Text-1 "export const r = 1;\nexport const s = 2;"
286318
/project/a/target.ts SVC-1-2 "const a = 1;\nconst b = 2;\nconst c = 3;"
287319

288320
Info seq [hh:mm:ss:mss] -----------------------------------------------

tests/cases/fourslash/server/pasteEdits_noImportNeeded.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,5 @@ verify.pasteEdits({
1818
pasteLocations: [range[0]],
1919
copiedFrom: { file: "a.ts", range: [range[1]] },
2020
},
21-
newFileContents: {
22-
"/b.ts":
23-
`export`
24-
}
21+
newFileContents: {}
2522
});

tests/cases/fourslash/server/pasteEdits_pasteComments.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,5 @@ verify.pasteEdits({
1919
*/`],
2020
pasteLocations: [range[0]],
2121
},
22-
newFileContents: {
23-
"/target.ts":
24-
`const a = 10;
25-
const b = 10;/**
26-
* Testing comment line 1
27-
* line 2
28-
* line 3
29-
* line 4
30-
*/
31-
const c = 10;`
32-
}
22+
newFileContents: {}
3323
});

tests/cases/fourslash/server/pasteEdits_pasteIntoSameFile.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,5 @@ verify.pasteEdits({
1717
pasteLocations: [range[1]],
1818
copiedFrom: { file: "target.ts", range: [range[0]] },
1919
},
20-
newFileContents: {
21-
"/target.ts":
22-
`const k = 1;
23-
console.log(k);
24-
25-
console.log(k);
26-
console.log("test");`
27-
}
20+
newFileContents: {}
2821
});

tests/cases/fourslash/server/pasteEdits_unknownSourceFile.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@ interface Testing {
3838
test4: Test4;
3939
}const c = 10;`
4040
},
41-
fixId: "providePostPasteEdits"
4241
});

0 commit comments

Comments
 (0)