diff --git a/lib/typescript.js b/lib/typescript.js index dab9aea778086..4dae4289c3c6d 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -179399,13 +179399,13 @@ function pasteEditsProvider(targetFile, pastedText, pasteLocations, copiedFrom, function pasteEdits(targetFile, pastedText, pasteLocations, copiedFrom, host, preferences, formatContext, cancellationToken, changes) { let actualPastedText; if (pastedText.length !== pasteLocations.length) { - actualPastedText = pastedText.length === 1 ? pastedText : [pastedText.join("\n")]; + actualPastedText = pastedText.length === 1 ? pastedText[0] : pastedText.join(getNewLineOrDefaultFromHost(formatContext.host, formatContext.options)); } const statements = []; let newText = targetFile.text; for (let i = pasteLocations.length - 1; i >= 0; i--) { const { pos, end } = pasteLocations[i]; - newText = actualPastedText ? newText.slice(0, pos) + actualPastedText[0] + newText.slice(end) : newText.slice(0, pos) + pastedText[i] + newText.slice(end); + newText = actualPastedText ? newText.slice(0, pos) + actualPastedText + newText.slice(end) : newText.slice(0, pos) + pastedText[i] + newText.slice(end); } let importAdder; Debug.checkDefined(host.runWithTemporaryFileUpdate).call(host, targetFile.fileName, newText, (updatedProgram, originalProgram, updatedFile) => { @@ -179436,22 +179436,37 @@ function pasteEdits(targetFile, pastedText, pasteLocations, copiedFrom, host, pr preferences, formatContext }; - forEachChild(updatedFile, function cb(node) { - if (isIdentifier(node) && !(originalProgram == null ? void 0 : originalProgram.getTypeChecker().resolveName( - node.text, - node, - -1 /* All */, - /*excludeGlobals*/ - false - ))) { - importAdder.addImportForUnresolvedIdentifier( - context, + let offset = 0; + pasteLocations.forEach((location, i) => { + const oldTextLength = location.end - location.pos; + const textToBePasted = actualPastedText ?? pastedText[i]; + const startPos = location.pos + offset; + const endPos = startPos + textToBePasted.length; + const range = { pos: startPos, end: endPos }; + offset += textToBePasted.length - oldTextLength; + const enclosingNode = findAncestor( + getTokenAtPosition(context.sourceFile, range.pos), + (ancestorNode) => rangeContainsRange(ancestorNode, range) + ); + if (!enclosingNode) return; + forEachChild(enclosingNode, function importUnresolvedIdentifiers(node) { + const isImportCandidate = isIdentifier(node) && rangeContainsPosition(range, node.getStart(updatedFile)) && !(updatedProgram == null ? void 0 : updatedProgram.getTypeChecker().resolveName( + node.text, node, - /*useAutoImportProvider*/ - true - ); - } - node.forEachChild(cb); + -1 /* All */, + /*excludeGlobals*/ + false + )); + if (isImportCandidate) { + return importAdder.addImportForUnresolvedIdentifier( + context, + node, + /*useAutoImportProvider*/ + true + ); + } + node.forEachChild(importUnresolvedIdentifiers); + }); }); } importAdder.writeFixes(changes, getQuotePreference(copiedFrom ? copiedFrom.file : targetFile, preferences)); @@ -179463,7 +179478,7 @@ function pasteEdits(targetFile, pastedText, pasteLocations, copiedFrom, host, pr changes.replaceRangeWithText( targetFile, { pos: paste.pos, end: paste.end }, - actualPastedText ? actualPastedText[0] : pastedText[i] + actualPastedText ?? pastedText[i] ); }); }