Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/services/pasteEdits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addRange } from "../compiler/core.js";
import { findIndex } from "../compiler/core.js";
import {
CancellationToken,
Program,
Expand All @@ -8,7 +8,6 @@ import {
TextRange,
UserPreferences,
} from "../compiler/types.js";
import { getLineOfLocalPosition } from "../compiler/utilities.js";
import {
codefix,
Debug,
Expand Down Expand Up @@ -77,7 +76,19 @@ function pasteEdits(
if (copiedFrom?.range) {
Debug.assert(copiedFrom.range.length === pastedText.length);
copiedFrom.range.forEach(copy => {
addRange(statements, copiedFrom.file.statements, getLineOfLocalPosition(copiedFrom.file, copy.pos), getLineOfLocalPosition(copiedFrom.file, copy.end) + 1);
const statementsInSourceFile = copiedFrom.file.statements;
const startNodeIndex = findIndex(statementsInSourceFile, s => s.end > copy.pos);
if (startNodeIndex === -1) return undefined;
let endNodeIndex = findIndex(statementsInSourceFile, s => s.end >= copy.end, startNodeIndex);
/**
* [|console.log(a);
* |]
* console.log(b);
*/
if (endNodeIndex !== -1 && copy.end <= statementsInSourceFile[endNodeIndex].getStart()) {
endNodeIndex--;
}
Comment on lines +88 to +90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be baked into the findIndex predicate, but this is alright.

statements.push(...statementsInSourceFile.slice(startNodeIndex, endNodeIndex === -1 ? statementsInSourceFile.length : endNodeIndex + 1));
});
const usage = getUsageInfo(copiedFrom.file, statements, originalProgram!.getTypeChecker(), getExistingLocals(updatedFile, statements, originalProgram!.getTypeChecker()));
Debug.assertIsDefined(originalProgram);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export const abc = 10;

//// [/b.ts]
import { abc } from "./a";
console.log(abc);

console.log(abc);


console.log("abc");

//// [/c.ts]

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


lib.d.ts
Expand Down Expand Up @@ -221,7 +225,22 @@ Info seq [hh:mm:ss:mss] request:
"offset": 1
}
}
]
],
"copiedFrom": {
"file": "b.ts",
"spans": [
{
"start": {
"line": 3,
"offset": 1
},
"end": {
"line": 5,
"offset": 1
}
}
]
}
},
"command": "getPasteEdits"
}
Expand All @@ -234,9 +253,11 @@ Info seq [hh:mm:ss:mss] Files (6)
/lib.decorators.legacy.d.ts Text-1 lib.decorators.legacy.d.ts-Text
/c.ts SVC-1-1 "console.log(abc);"
/a.ts Text-1 "export const abc = 10;"
/b.ts Text-1 "import { abc } from \"./a\";\nconsole.log(abc);"
/b.ts Text-1 "import { abc } from \"./a\";\n\nconsole.log(abc);\n\n\nconsole.log(\"abc\");"

Info seq [hh:mm:ss:mss] -----------------------------------------------
Info seq [hh:mm:ss:mss] getExportInfoMap: cache miss or empty; calculating new results
Info seq [hh:mm:ss:mss] getExportInfoMap: done in * ms
Info seq [hh:mm:ss:mss] response:
{
"seq": 0,
Expand Down
7 changes: 6 additions & 1 deletion tests/cases/fourslash/server/pasteEdits_blankTargetFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

// @Filename: /b.ts
//// import { abc } from "./a";
//// console.log(abc);
////
//// [|console.log(abc);
////
//// |]
//// console.log("abc");

// @Filename: /tsconfig.json
////{ "files": ["c.ts", "a.ts", "b.ts"] }
Expand All @@ -18,6 +22,7 @@ verify.pasteEdits({
args: {
pastedText: [`console.log(abc);`],
pasteLocations: [range[0]],
copiedFrom: { file: "b.ts", range: [range[1]] },
},
newFileContents: {
"/c.ts":
Expand Down