From 2458a1c8840e4fe8471f352f45a4f60de07501bd Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Mon, 17 Oct 2016 21:32:37 -0700 Subject: [PATCH] allow reload from temp files --- .../unittests/tsserverProjectSystem.ts | 63 ++++++++++++++++++- src/server/project.ts | 4 +- src/server/scriptInfo.ts | 4 +- src/server/session.ts | 3 +- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 4251430ce4d2d..f389375395a84 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -169,11 +169,18 @@ namespace ts.projectSystem { return host; } + class TestSession extends server.Session { + getProjectService() { + return this.projectService; + } + }; + export function createSession(host: server.ServerHost, typingsInstaller?: server.ITypingsInstaller, projectServiceEventHandler?: server.ProjectServiceEventHandler) { if (typingsInstaller === undefined) { typingsInstaller = new TestTypingsInstaller("/a/data/", /*throttleLimit*/5, host); } - return new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ projectServiceEventHandler !== undefined, projectServiceEventHandler); + + return new TestSession(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ projectServiceEventHandler !== undefined, projectServiceEventHandler); } export interface CreateProjectServiceParameters { @@ -533,12 +540,13 @@ namespace ts.projectSystem { this.reloadFS(filesOrFolders); } + write(s: string) { + } + readonly readFile = (s: string) => (this.fs.get(this.toPath(s))).content; readonly resolvePath = (s: string) => s; readonly getExecutingFilePath = () => this.executingFilePath; readonly getCurrentDirectory = () => this.currentDirectory; - readonly writeCompressedData = () => notImplemented(); - readonly write = (s: string) => notImplemented(); readonly exit = () => notImplemented(); } @@ -2191,4 +2199,53 @@ namespace ts.projectSystem { assert.isTrue(inferredProject.containsFile(file1.path)); }); }); + + describe("reload", () => { + it("should work with temp file", () => { + const f1 = { + path: "/a/b/app.ts", + content: "let x = 1" + }; + const tmp = { + path: "/a/b/app.tmp", + content: "const y = 42" + }; + const host = createServerHost([f1, tmp]); + const session = createSession(host); + + // send open request + session.executeCommand({ + type: "request", + command: "open", + seq: 1, + arguments: { file: f1.path } + }); + + // reload from tmp file + session.executeCommand({ + type: "request", + command: "reload", + seq: 2, + arguments: { file: f1.path, tmpfile: tmp.path } + }); + + // verify content + const projectServiice = session.getProjectService(); + const snap1 = projectServiice.getScriptInfo(f1.path).snap(); + assert.equal(snap1.getText(0, snap1.getLength()), tmp.content, "content should be equal to the content of temp file"); + + // reload from original file file + session.executeCommand({ + type: "request", + command: "reload", + seq: 2, + arguments: { file: f1.path } + }); + + // verify content + const snap2 = projectServiice.getScriptInfo(f1.path).snap(); + assert.equal(snap2.getText(0, snap2.getLength()), f1.content, "content should be equal to the content of original file"); + + }); + }); } \ No newline at end of file diff --git a/src/server/project.ts b/src/server/project.ts index 01858b4cc6815..62f6d578e4381 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -400,11 +400,11 @@ namespace ts.server { } } - reloadScript(filename: NormalizedPath): boolean { + reloadScript(filename: NormalizedPath, tempFileName?: NormalizedPath): boolean { const script = this.projectService.getScriptInfoForNormalizedPath(filename); if (script) { Debug.assert(script.isAttached(this)); - script.reloadFromFile(); + script.reloadFromFile(tempFileName); return true; } return false; diff --git a/src/server/scriptInfo.ts b/src/server/scriptInfo.ts index 68dd59d2d3266..d12b0d0b579f1 100644 --- a/src/server/scriptInfo.ts +++ b/src/server/scriptInfo.ts @@ -126,12 +126,12 @@ namespace ts.server { this.host.writeFile(fileName, snap.getText(0, snap.getLength())); } - reloadFromFile() { + reloadFromFile(tempFileName?: NormalizedPath) { if (this.hasMixedContent) { this.reload(""); } else { - this.svc.reloadFromFile(this.fileName); + this.svc.reloadFromFile(tempFileName || this.fileName); this.markContainingProjectsAsDirty(); } } diff --git a/src/server/session.ts b/src/server/session.ts index 238751c0d6387..6817624e131cd 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1041,11 +1041,12 @@ namespace ts.server { private reload(args: protocol.ReloadRequestArgs, reqSeq: number) { const file = toNormalizedPath(args.file); + const tempFileName = args.tmpfile && toNormalizedPath(args.tmpfile); const project = this.projectService.getDefaultProjectForFile(file, /*refreshInferredProjects*/ true); if (project) { this.changeSeq++; // make sure no changes happen before this one is finished - if (project.reloadScript(file)) { + if (project.reloadScript(file, tempFileName)) { this.output(undefined, CommandNames.Reload, reqSeq); } }