GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Fixing delay caused in vscode due to pasteEdits by navya9singh · Pull Request #59542 · microsoft/TypeScript Conversation
|
| const updatedRanges: TextRange[] = []; | ||
| let offset = 0; | ||
| pasteLocations.forEach((location, i) => { | ||
| const deletionNeeded = location.pos === location.end ? 0 : location.end - location.pos; |
There was a problem 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;
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| }); | ||
|
|
||
| updatedRanges.forEach(range => { | ||
| const enclosingNode = findAncestor(getTokenAtPosition(context.sourceFile, range.pos), ancestorNode => rangeContainsPosition(ancestorNode, range.end)); |
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Looking a lot better! I left some sugestions. I'd look over the other tests to see if they need similar feedback, but there's a cat on my right arm.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| pastedText: [ `const t = 1 + juice + p;`,`function avacado() { return sauce; }`, | ||
| `fig + kiwi`, | ||
| `function k() { | ||
| const cherry = 3 + tomato + cucumber; | ||
| }` | ||
| ], |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| pastedText: [ `const t = 1 + juice + p;`,`function avacado() { return sauce; }`, | |
| `fig + kiwi`, | |
| `function k() { | |
| const cherry = 3 + tomato + cucumber; | |
| }` | |
| ], | |
| pastedText: [ | |
| `const t = 1 + juice + p;`,`function avacado() { return sauce; }`, | |
| `fig + kiwi`, | |
| `function k() { | |
| const cherry = 3 + tomato + cucumber; | |
| }` | |
| ], |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| pasteLocations.forEach((location, i) => { | ||
| const oldTextLength = location.end - location.pos; | ||
| const textToBePasted = actualPastedText ? actualPastedText[0] : pastedText[i]; | ||
| const startPos = location.pos + offset; | ||
| const endPos = startPos + textToBePasted.length; | ||
| updatedRanges.push({ pos: startPos, end: endPos }); | ||
| offset += textToBePasted.length - oldTextLength; | ||
| }); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Why is it that this need to be separate pass and not inline where we are iterating over nodes in the the updated range?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In the updatedFile, we don't know the exact range of where the pasted text exists, so doing this pass helps us get the right location of the pasted text. Then, it becomes easy to just go over the updated ranges and check for unresolved nodes. Is that what you were asking about?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Isnt it same as :
let offset = 0; pasteLocations.forEach((location, i) => { const oldTextLength = location.end - location.pos; const textToBePasted = actualPastedText ? actualPastedText[0] : pastedText[i]; const startPos = location.pos + offset; const endPos = startPos + textToBePasted.length; const range = { pos: startPos, end: endPos } satisfies TextRange; offset += textToBePasted.length - oldTextLength; const enclosingNode = findAncestor( getTokenAtPosition(context.sourceFile, range.pos), ancestorNode => rangeContainsRange(ancestorNode, range), ); if (!enclosingNode) return; forEachChild(enclosingNode, function importUnresolvedIdentifiers(node) { if ( isIdentifier(node) && rangeContainsPosition( range, node.getStart(updatedFile), ) && !originalProgram?.getTypeChecker().resolveName( node.text, node, SymbolFlags.All, /*excludeGlobals*/ false, ) ) { return importAdder.addImportForUnresolvedIdentifier( context, node, /*useAutoImportProvider*/ true, ); } node.forEachChild(importUnresolvedIdentifiers); }); });
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| const textToBePasted = actualPastedText ? actualPastedText[0] : pastedText[i]; | ||
| const startPos = location.pos + offset; | ||
| const endPos = startPos + textToBePasted.length; | ||
| const range = { pos: startPos, end: endPos } satisfies TextRange; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| const range = { pos: startPos, end: endPos } satisfies TextRange; | |
| const range: TextRange = { pos: startPos, end: endPos }; |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| if ( | ||
| isIdentifier(node) && rangeContainsPosition( | ||
| range, | ||
| node.getStart(updatedFile), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Do you need to run node.getStart()? Can you just use node.pos?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yes, node.pos gives the position with the whitespaces. So, for example in pasteEdits_multiplePastesConsistentlyLargerInSize.ts for the 3rd paste location (const test = [|1 + 2|] + 3; ) , node.pos will give the position of the whitespace before 1, but the updated range of the pasted text starts from 1. As node.pos will not lie in this range, it will skip checking for the pasted text here. node.Start() avoids this by giving the exact position of 1.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| `function k() { | ||
| const cherry = 3 + tomato + cucumber; | ||
| }` | ||
| ], |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
| ], | |
| ], |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| let offset = 0; | ||
| pasteLocations.forEach((location, i) => { | ||
| const oldTextLength = location.end - location.pos; | ||
| const textToBePasted = actualPastedText ? actualPastedText[0] : pastedText[i]; |
There was a problem 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?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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].
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Apart from daniel's code formatting comments, looks good.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| forEachChild(enclosingNode, function importUnresolvedIdentifiers(node) { | ||
| const isImportCandidate = isIdentifier(node) && | ||
| rangeContainsPosition(range, node.getStart(updatedFile)) && | ||
| !originalProgram?.getTypeChecker().resolveName( |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Shouldn’t this use the updated program? For example, if I have a single paste of
const b = 0; console.log(b);
won’t this try to resolve b against the original text which didn’t have a declaration for b, so we’ll try to auto-import it unnecessarily?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I didnt see this before, but originalProgram's typechecker usage is sketchy as well.. The reuse of sourceFile and parent pointer changing makes it non-viable to use.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test that fails when using originalProgram?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| pastedText: [ | ||
| `const b = 1; | ||
| console.log(b);`], | ||
| pasteLocations: [range[0]], |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Two things
test.ranges() like everywhere else.ranges instead of range).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
TypeScript Bot (@typescript-bot) cherry-pick this to release-5.6 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Starting jobs; this comment will be updated as builds start and complete.
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Hey, Daniel Rosenwasser (@DanielRosenwasser)! I've created #59695 for you. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…e-5.6 (#59695) Co-authored-by: navya9singh <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.