Skip to content

Fixing delay caused in vscode due to pasteEdits #59542

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

Merged
merged 21 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
40 changes: 32 additions & 8 deletions src/services/pasteEdits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { findIndex } from "../compiler/core.js";
import {
CancellationToken,
Program,
Expand All @@ -12,10 +11,14 @@ import {
codefix,
Debug,
fileShouldUseJavaScriptRequire,
findAncestor,
findIndex,
forEachChild,
formatting,
getQuotePreference,
getTokenAtPosition,
isIdentifier,
rangeContainsPosition,
textChanges,
} from "./_namespaces/ts.js";
import { addTargetFileImports } from "./refactors/helpers.js";
Expand Down Expand Up @@ -64,7 +67,6 @@ function pasteEdits(
}

const statements: Statement[] = [];

let newText = targetFile.text;
for (let i = pasteLocations.length - 1; i >= 0; i--) {
const { pos, end } = pasteLocations[i];
Expand Down Expand Up @@ -106,12 +108,34 @@ function pasteEdits(
preferences,
formatContext,
};
forEachChild(updatedFile, function cb(node) {
if (isIdentifier(node) && !originalProgram?.getTypeChecker().resolveName(node.text, node, SymbolFlags.All, /*excludeGlobals*/ false)) {
// generate imports
importAdder.addImportForUnresolvedIdentifier(context, node, /*useAutoImportProvider*/ true);
}
node.forEachChild(cb);

/**
* `updatedRanges` represent the new ranges that account for the offset changes caused by pasting new text and
* `offset` represents by how much the starting position of `pasteLocations` needs to be changed.
*
* We iterate over each updated range to get the node that wholly encloses the updated range. For each child of that node, it is checked if the
* identifier lies within the updated range and if it is not resolved, we try resolving it.
*/
const updatedRanges: TextRange[] = [];
let offset = 0;
pasteLocations.forEach((location, i) => {
const deletionNeeded = location.pos === location.end ? 0 : location.end - location.pos + 1;
const textToBePasted = actualPastedText ? actualPastedText[0] : pastedText[i];
Copy link
Member

Choose a reason for hiding this comment

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

Is it ever possible that the count of paste locations exceeds the number of items in pastedText?

Copy link
Member

Choose a reason for hiding this comment

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

This is done at top of the function to make sure its always right

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, in that case all pastedText is joined together with a newline character and that is pasted at all locations. Line 64 does this.

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha - while you're here, can you fix that so it uses the appropriate newline character instead of \n?

Copy link
Member

Choose a reason for hiding this comment

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

Also, actualPastedText doesn't have to be an array if you only ever use it for actualPastedText[0]. Just use actualPastedText ?? pastedText[i].

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean using getNewLineOrDefaultFromHost() for the appropriate newline character?

const startPos = location.pos - offset;
const endPos = startPos + textToBePasted.length - 1;
updatedRanges.push({ pos: startPos, end: endPos });
offset += deletionNeeded - textToBePasted.length;
});

updatedRanges.forEach(range => {
const enclosingNode = findAncestor(getTokenAtPosition(context.sourceFile, range.pos), cb => rangeContainsPosition({ pos: cb.pos, end: cb.end }, range.end));
if (!enclosingNode) return;
forEachChild(enclosingNode, function cb(node) {
if (isIdentifier(node) && rangeContainsPosition(range, node.getStart()) && !originalProgram?.getTypeChecker().resolveName(node.text, node, SymbolFlags.All, /*excludeGlobals*/ false)) {
importAdder.addImportForUnresolvedIdentifier(context, node, /*useAutoImportProvider*/ true);
}
node.forEachChild(cb);
});
});
}
importAdder.writeFixes(changes, getQuotePreference(copiedFrom ? copiedFrom.file : targetFile, preferences));
Expand Down
Loading