Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling statements from a known source file #58679

Merged
merged 4 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 6 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,11 @@ 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;
const endNodeIndex = findIndex(statementsInSourceFile, s => s.end >= copy.end, startNodeIndex);
Copy link
Member

Choose a reason for hiding this comment

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

This includes the next statement if the end of the selection includes any leading trivia of the next statement:

// both statements get added to `statements`:
[|console.log(a);
|]
console.log(b);

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,9 @@ export const abc = 10;

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

console.log(abc);


//// [/c.ts]

Expand Down Expand Up @@ -69,7 +71,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"


lib.d.ts
Expand Down Expand Up @@ -221,7 +223,22 @@ Info seq [hh:mm:ss:mss] request:
"offset": 1
}
}
]
],
"copiedFrom": {
"file": "b.ts",
"spans": [
{
"start": {
"line": 3,
"offset": 1
},
"end": {
"line": 3,
"offset": 18
}
}
]
}
},
"command": "getPasteEdits"
}
Expand All @@ -234,9 +251,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"

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
5 changes: 4 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,9 @@

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

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