- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.1k
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
Changes from 7 commits
10be994
              0d5380b
              7b1d203
              b56a4af
              4544ad3
              44626c4
              e12d2cf
              f292acd
              30e5e2d
              8248ff8
              c0b1356
              b660771
              c0f61de
              9acc51a
              aebaae1
              5044795
              76897ba
              d7083b8
              a29d5a8
              f80483c
              65490bf
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| import { findIndex } from "../compiler/core.js"; | ||
| import { | ||
| CancellationToken, | ||
| Program, | ||
|  | @@ -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"; | ||
|  | @@ -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]; | ||
|  | @@ -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. | ||
| */ | ||
|         
                  navya9singh marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| const updatedRanges: TextRange[] = []; | ||
| let offset = 0; | ||
| pasteLocations.forEach((location, i) => { | ||
| const deletionNeeded = location.pos === location.end ? 0 : location.end - location.pos + 1; | ||
|         
                  DanielRosenwasser marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| const textToBePasted = actualPastedText ? actualPastedText[0] : pastedText[i]; | ||
|          | ||
| const startPos = location.pos - offset; | ||
| const endPos = startPos + textToBePasted.length - 1; | ||
| updatedRanges.push({ pos: startPos, end: endPos }); | ||
| offset += deletionNeeded - textToBePasted.length; | ||
|         
                  DanielRosenwasser marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| }); | ||
|  | ||
| updatedRanges.forEach(range => { | ||
| const enclosingNode = findAncestor(getTokenAtPosition(context.sourceFile, range.pos), cb => rangeContainsPosition({ pos: cb.pos, end: cb.end }, range.end)); | ||
|         
                  DanielRosenwasser marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| if (!enclosingNode) return; | ||
| forEachChild(enclosingNode, function cb(node) { | ||
|         
                  DanielRosenwasser marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| if (isIdentifier(node) && rangeContainsPosition(range, node.getStart()) && !originalProgram?.getTypeChecker().resolveName(node.text, node, SymbolFlags.All, /*excludeGlobals*/ false)) { | ||
|         
                  DanielRosenwasser marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| importAdder.addImportForUnresolvedIdentifier(context, node, /*useAutoImportProvider*/ true); | ||
| } | ||
| node.forEachChild(cb); | ||
| }); | ||
| }); | ||
| } | ||
| importAdder.writeFixes(changes, getQuotePreference(copiedFrom ? copiedFrom.file : targetFile, preferences)); | ||
|  | ||
Uh oh!
There was an error while loading. Please reload this page.