pasteEdits returns no edit when there are no imports needed (#59189)

This commit is contained in:
navya9singh
2024-07-09 10:27:41 -07:00
committed by GitHub
parent a6fb4dc103
commit ded36b21ac
11 changed files with 60 additions and 87 deletions

View File

@@ -1029,7 +1029,7 @@ export class SessionClient implements LanguageService {
};
const request = this.processRequest<protocol.GetPasteEditsRequest>(protocol.CommandTypes.GetPasteEdits, args);
const response = this.processResponse<protocol.GetPasteEditsResponse>(request);
if (!response.body) {
if (response.body.edits.length === 0) {
return { edits: [] };
}
const edits: FileTextChanges[] = this.convertCodeEditsToTextChanges(response.body.edits);

View File

@@ -71,8 +71,9 @@ function pasteEdits(
newText = actualPastedText ? newText.slice(0, pos) + actualPastedText[0] + newText.slice(end) : newText.slice(0, pos) + pastedText[i] + newText.slice(end);
}
let importAdder: codefix.ImportAdder;
Debug.checkDefined(host.runWithTemporaryFileUpdate).call(host, targetFile.fileName, newText, (updatedProgram: Program, originalProgram: Program | undefined, updatedFile: SourceFile) => {
const importAdder = codefix.createImportAdder(updatedFile, updatedProgram, preferences, host);
importAdder = codefix.createImportAdder(updatedFile, updatedProgram, preferences, host);
if (copiedFrom?.range) {
Debug.assert(copiedFrom.range.length === pastedText.length);
copiedFrom.range.forEach(copy => {
@@ -115,6 +116,13 @@ function pasteEdits(
}
importAdder.writeFixes(changes, getQuotePreference(copiedFrom ? copiedFrom.file : targetFile, preferences));
});
/**
* If there are no import fixes, getPasteEdits should return without making any changes to the file.
*/
if (!importAdder!.hasFixes()) {
return;
}
pasteLocations.forEach((paste, i) => {
changes.replaceRangeWithText(
targetFile,

View File

@@ -18,6 +18,11 @@ describe("unittests:: tsserver:: pasteEdits", () => {
content: `const a = 1;
const b = 2;
const c = 3;`,
};
const file1: File = {
path: "/project/a/file1.ts",
content: `export const r = 1;
export const s = 2;`,
};
const tsconfig: File = {
path: "/project/tsconfig.json",
@@ -27,7 +32,7 @@ const c = 3;`,
function e();
const f = r + s;`;
const host = createServerHost([target, tsconfig, libFile]);
const host = createServerHost([target, file1, tsconfig, libFile]);
const session = new TestSession(host);
openFilesForSession([target], session);