Skip to content

Commit 1659a9d

Browse files
authored
Handling statements from a known source file (#58679)
1 parent 0cc62c3 commit 1659a9d

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/services/pasteEdits.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addRange } from "../compiler/core.js";
1+
import { findIndex } from "../compiler/core.js";
22
import {
33
CancellationToken,
44
Program,
@@ -8,7 +8,6 @@ import {
88
TextRange,
99
UserPreferences,
1010
} from "../compiler/types.js";
11-
import { getLineOfLocalPosition } from "../compiler/utilities.js";
1211
import {
1312
codefix,
1413
Debug,
@@ -77,7 +76,19 @@ function pasteEdits(
7776
if (copiedFrom?.range) {
7877
Debug.assert(copiedFrom.range.length === pastedText.length);
7978
copiedFrom.range.forEach(copy => {
80-
addRange(statements, copiedFrom.file.statements, getLineOfLocalPosition(copiedFrom.file, copy.pos), getLineOfLocalPosition(copiedFrom.file, copy.end) + 1);
79+
const statementsInSourceFile = copiedFrom.file.statements;
80+
const startNodeIndex = findIndex(statementsInSourceFile, s => s.end > copy.pos);
81+
if (startNodeIndex === -1) return undefined;
82+
let endNodeIndex = findIndex(statementsInSourceFile, s => s.end >= copy.end, startNodeIndex);
83+
/**
84+
* [|console.log(a);
85+
* |]
86+
* console.log(b);
87+
*/
88+
if (endNodeIndex !== -1 && copy.end <= statementsInSourceFile[endNodeIndex].getStart()) {
89+
endNodeIndex--;
90+
}
91+
statements.push(...statementsInSourceFile.slice(startNodeIndex, endNodeIndex === -1 ? statementsInSourceFile.length : endNodeIndex + 1));
8192
});
8293
const usage = getUsageInfo(copiedFrom.file, statements, originalProgram!.getTypeChecker(), getExistingLocals(updatedFile, statements, originalProgram!.getTypeChecker()));
8394
Debug.assertIsDefined(originalProgram);

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ export const abc = 10;
55

66
//// [/b.ts]
77
import { abc } from "./a";
8-
console.log(abc);
8+
9+
console.log(abc);
10+
11+
12+
console.log("abc");
913

1014
//// [/c.ts]
1115

@@ -69,7 +73,7 @@ Info seq [hh:mm:ss:mss] Files (6)
6973
/lib.decorators.legacy.d.ts Text-1 lib.decorators.legacy.d.ts-Text
7074
/c.ts SVC-1-0 ""
7175
/a.ts Text-1 "export const abc = 10;"
72-
/b.ts Text-1 "import { abc } from \"./a\";\nconsole.log(abc);"
76+
/b.ts Text-1 "import { abc } from \"./a\";\n\nconsole.log(abc);\n\n\nconsole.log(\"abc\");"
7377

7478

7579
lib.d.ts
@@ -221,7 +225,22 @@ Info seq [hh:mm:ss:mss] request:
221225
"offset": 1
222226
}
223227
}
224-
]
228+
],
229+
"copiedFrom": {
230+
"file": "b.ts",
231+
"spans": [
232+
{
233+
"start": {
234+
"line": 3,
235+
"offset": 1
236+
},
237+
"end": {
238+
"line": 5,
239+
"offset": 1
240+
}
241+
}
242+
]
243+
}
225244
},
226245
"command": "getPasteEdits"
227246
}
@@ -234,9 +253,11 @@ Info seq [hh:mm:ss:mss] Files (6)
234253
/lib.decorators.legacy.d.ts Text-1 lib.decorators.legacy.d.ts-Text
235254
/c.ts SVC-1-1 "console.log(abc);"
236255
/a.ts Text-1 "export const abc = 10;"
237-
/b.ts Text-1 "import { abc } from \"./a\";\nconsole.log(abc);"
256+
/b.ts Text-1 "import { abc } from \"./a\";\n\nconsole.log(abc);\n\n\nconsole.log(\"abc\");"
238257

239258
Info seq [hh:mm:ss:mss] -----------------------------------------------
259+
Info seq [hh:mm:ss:mss] getExportInfoMap: cache miss or empty; calculating new results
260+
Info seq [hh:mm:ss:mss] getExportInfoMap: done in * ms
240261
Info seq [hh:mm:ss:mss] response:
241262
{
242263
"seq": 0,

tests/cases/fourslash/server/pasteEdits_blankTargetFile.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
// @Filename: /b.ts
1010
//// import { abc } from "./a";
11-
//// console.log(abc);
11+
////
12+
//// [|console.log(abc);
13+
////
14+
//// |]
15+
//// console.log("abc");
1216

1317
// @Filename: /tsconfig.json
1418
////{ "files": ["c.ts", "a.ts", "b.ts"] }
@@ -18,6 +22,7 @@ verify.pasteEdits({
1822
args: {
1923
pastedText: [`console.log(abc);`],
2024
pasteLocations: [range[0]],
25+
copiedFrom: { file: "b.ts", range: [range[1]] },
2126
},
2227
newFileContents: {
2328
"/c.ts":

0 commit comments

Comments
 (0)