Fixing exception on unsaved file (#60362)

This commit is contained in:
navya9singh
2024-11-02 22:55:36 -07:00
committed by GitHub
parent 11b2930fa2
commit 0ec4d30a6e
3 changed files with 288 additions and 0 deletions
+2
View File
@@ -156,6 +156,7 @@ import {
indent,
isConfigFile,
isConfiguredProject,
isDynamicFileName,
isExternalProject,
isInferredProject,
ITypingsInstaller,
@@ -2981,6 +2982,7 @@ export class Session<TMessage = string> implements EventSender {
}
private getPasteEdits(args: protocol.GetPasteEditsRequestArgs): protocol.PasteEditsAction | undefined {
const { file, project } = this.getFileAndProject(args);
if (isDynamicFileName(file)) return undefined;
const copiedFrom = args.copiedFrom
? { file: args.copiedFrom.file, range: args.copiedFrom.spans.map(copies => this.getRange({ file: args.copiedFrom!.file, startLine: copies.start.line, startOffset: copies.start.offset, endLine: copies.end.line, endOffset: copies.end.offset }, project.getScriptInfoForNormalizedPath(toNormalizedPath(args.copiedFrom!.file))!)) }
: undefined;
@@ -46,4 +46,42 @@ const f = r + s;`;
verifyGetErrRequest({ session, files: [target.path] });
baselineTsserverLogs("pasteEdits", "adds paste edits", session);
});
it("should not error", () => {
const file1: File = {
path: "/home/src/projects/project/file1.ts",
content: `export const r = 1;
console.log(r);`,
};
const tsconfig: File = {
path: "/home/src/projects/project/tsconfig.json",
content: "{}",
};
const host = TestServerHost.createServerHost([file1, tsconfig]);
const session = new TestSession(host);
session.executeCommandSeq<ts.server.protocol.UpdateOpenRequest>({
command: ts.server.protocol.CommandTypes.UpdateOpen,
arguments: {
changedFiles: [],
closedFiles: [],
openFiles: [
{
file: "^/untitled/ts-nul-authority/Untitled-1",
fileContent: "function foo(){}\r\n \r\n",
scriptKindName: "TS",
},
],
},
});
session.executeCommandSeq<ts.server.protocol.GetPasteEditsRequest>({
command: ts.server.protocol.CommandTypes.GetPasteEdits,
arguments: {
file: "^/untitled/ts-nul-authority/Untitled-1",
pastedText: ["console.log(r);"],
pasteLocations: [{ start: { line: 1, offset: 0 }, end: { line: 1, offset: 0 } }],
copiedFrom: { file: file1.path, spans: [{ start: { line: 2, offset: 0 }, end: { line: 2, offset: 13 } }] },
},
});
verifyGetErrRequest({ session, files: ["^/untitled/ts-nul-authority/Untitled-1"] });
baselineTsserverLogs("pasteEdits", "should not error", session);
});
});