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 10 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
37 changes: 30 additions & 7 deletions src/services/pasteEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import {
codefix,
Debug,
fileShouldUseJavaScriptRequire,
findAncestor,
findIndex,
forEachChild,
formatting,
getQuotePreference,
getTokenAtPosition,
isIdentifier,
Program,
rangeContainsPosition,
SourceFile,
Statement,
SymbolFlags,
Expand Down Expand Up @@ -62,7 +65,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 @@ -104,12 +106,33 @@ 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;
Copy link
Member

Choose a reason for hiding this comment

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

If location.pos === location.end, you'll end up with 0 on the second branch - so this is more about the length of the paste selection, right?

I'd recommend getting rid of the unnecessary branch and rewriting this to something like

const selectedTextLength = location.end - location.pos;

or

const pasteSelectionLength = location.end - location.pos;

or

const oldTextLength = location.end - location.pos;

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;
updatedRanges.push({ pos: startPos, end: endPos });
offset += deletionNeeded - textToBePasted.length;
});

updatedRanges.forEach(range => {
const enclosingNode = findAncestor(getTokenAtPosition(context.sourceFile, range.pos), ancestorNode => rangeContainsPosition(ancestorNode, range.end));
Copy link
Member

Choose a reason for hiding this comment

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

Aside: I almost wanted to suggest rangeContainsRange but I guess this just works because you've found the token at the start position, but you're trying to find the parent that also contains the end.

if (!enclosingNode) return;
forEachChild(enclosingNode, function cb(node) {
if (isIdentifier(node) && rangeContainsPosition(range, node.getStart(updatedFile)) && !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