@@ -179399,13 +179399,13 @@ function pasteEditsProvider(targetFile, pastedText, pasteLocations, copiedFrom,
179399
179399
function pasteEdits(targetFile, pastedText, pasteLocations, copiedFrom, host, preferences, formatContext, cancellationToken, changes) {
179400
179400
let actualPastedText;
179401
179401
if (pastedText.length !== pasteLocations.length) {
179402
- actualPastedText = pastedText.length === 1 ? pastedText : [ pastedText.join("\n")] ;
179402
+ actualPastedText = pastedText.length === 1 ? pastedText[0] : pastedText.join(getNewLineOrDefaultFromHost(formatContext.host, formatContext.options)) ;
179403
179403
}
179404
179404
const statements = [];
179405
179405
let newText = targetFile.text;
179406
179406
for (let i = pasteLocations.length - 1; i >= 0; i--) {
179407
179407
const { pos, end } = pasteLocations[i];
179408
- newText = actualPastedText ? newText.slice(0, pos) + actualPastedText[0] + newText.slice(end) : newText.slice(0, pos) + pastedText[i] + newText.slice(end);
179408
+ newText = actualPastedText ? newText.slice(0, pos) + actualPastedText + newText.slice(end) : newText.slice(0, pos) + pastedText[i] + newText.slice(end);
179409
179409
}
179410
179410
let importAdder;
179411
179411
Debug.checkDefined(host.runWithTemporaryFileUpdate).call(host, targetFile.fileName, newText, (updatedProgram, originalProgram, updatedFile) => {
@@ -179436,22 +179436,37 @@ function pasteEdits(targetFile, pastedText, pasteLocations, copiedFrom, host, pr
179436
179436
preferences,
179437
179437
formatContext
179438
179438
};
179439
- forEachChild(updatedFile, function cb(node) {
179440
- if (isIdentifier(node) && !(originalProgram == null ? void 0 : originalProgram.getTypeChecker().resolveName(
179441
- node.text,
179442
- node,
179443
- -1 /* All */,
179444
- /*excludeGlobals*/
179445
- false
179446
- ))) {
179447
- importAdder.addImportForUnresolvedIdentifier(
179448
- context,
179439
+ let offset = 0;
179440
+ pasteLocations.forEach((location, i) => {
179441
+ const oldTextLength = location.end - location.pos;
179442
+ const textToBePasted = actualPastedText ?? pastedText[i];
179443
+ const startPos = location.pos + offset;
179444
+ const endPos = startPos + textToBePasted.length;
179445
+ const range = { pos: startPos, end: endPos };
179446
+ offset += textToBePasted.length - oldTextLength;
179447
+ const enclosingNode = findAncestor(
179448
+ getTokenAtPosition(context.sourceFile, range.pos),
179449
+ (ancestorNode) => rangeContainsRange(ancestorNode, range)
179450
+ );
179451
+ if (!enclosingNode) return;
179452
+ forEachChild(enclosingNode, function importUnresolvedIdentifiers(node) {
179453
+ const isImportCandidate = isIdentifier(node) && rangeContainsPosition(range, node.getStart(updatedFile)) && !(updatedProgram == null ? void 0 : updatedProgram.getTypeChecker().resolveName(
179454
+ node.text,
179449
179455
node,
179450
- /*useAutoImportProvider*/
179451
- true
179452
- );
179453
- }
179454
- node.forEachChild(cb);
179456
+ -1 /* All */,
179457
+ /*excludeGlobals*/
179458
+ false
179459
+ ));
179460
+ if (isImportCandidate) {
179461
+ return importAdder.addImportForUnresolvedIdentifier(
179462
+ context,
179463
+ node,
179464
+ /*useAutoImportProvider*/
179465
+ true
179466
+ );
179467
+ }
179468
+ node.forEachChild(importUnresolvedIdentifiers);
179469
+ });
179455
179470
});
179456
179471
}
179457
179472
importAdder.writeFixes(changes, getQuotePreference(copiedFrom ? copiedFrom.file : targetFile, preferences));
@@ -179463,7 +179478,7 @@ function pasteEdits(targetFile, pastedText, pasteLocations, copiedFrom, host, pr
179463
179478
changes.replaceRangeWithText(
179464
179479
targetFile,
179465
179480
{ pos: paste.pos, end: paste.end },
179466
- actualPastedText ? actualPastedText[0] : pastedText[i]
179481
+ actualPastedText ?? pastedText[i]
179467
179482
);
179468
179483
});
179469
179484
}
0 commit comments