Skip to content

Commit cca3c1f

Browse files
authored
Merge pull request #11694 from Microsoft/vladima/reload-tmp
allow reload from temp files
2 parents ea3cbfb + 2458a1c commit cca3c1f

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,18 @@ namespace ts.projectSystem {
169169
return host;
170170
}
171171

172+
class TestSession extends server.Session {
173+
getProjectService() {
174+
return this.projectService;
175+
}
176+
};
177+
172178
export function createSession(host: server.ServerHost, typingsInstaller?: server.ITypingsInstaller, projectServiceEventHandler?: server.ProjectServiceEventHandler) {
173179
if (typingsInstaller === undefined) {
174180
typingsInstaller = new TestTypingsInstaller("/a/data/", /*throttleLimit*/5, host);
175181
}
176-
return new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ projectServiceEventHandler !== undefined, projectServiceEventHandler);
182+
183+
return new TestSession(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ projectServiceEventHandler !== undefined, projectServiceEventHandler);
177184
}
178185

179186
export interface CreateProjectServiceParameters {
@@ -533,12 +540,13 @@ namespace ts.projectSystem {
533540
this.reloadFS(filesOrFolders);
534541
}
535542

543+
write(s: string) {
544+
}
545+
536546
readonly readFile = (s: string) => (<File>this.fs.get(this.toPath(s))).content;
537547
readonly resolvePath = (s: string) => s;
538548
readonly getExecutingFilePath = () => this.executingFilePath;
539549
readonly getCurrentDirectory = () => this.currentDirectory;
540-
readonly writeCompressedData = () => notImplemented();
541-
readonly write = (s: string) => notImplemented();
542550
readonly exit = () => notImplemented();
543551
}
544552

@@ -2191,4 +2199,53 @@ namespace ts.projectSystem {
21912199
assert.isTrue(inferredProject.containsFile(<server.NormalizedPath>file1.path));
21922200
});
21932201
});
2202+
2203+
describe("reload", () => {
2204+
it("should work with temp file", () => {
2205+
const f1 = {
2206+
path: "/a/b/app.ts",
2207+
content: "let x = 1"
2208+
};
2209+
const tmp = {
2210+
path: "/a/b/app.tmp",
2211+
content: "const y = 42"
2212+
};
2213+
const host = createServerHost([f1, tmp]);
2214+
const session = createSession(host);
2215+
2216+
// send open request
2217+
session.executeCommand(<server.protocol.OpenRequest>{
2218+
type: "request",
2219+
command: "open",
2220+
seq: 1,
2221+
arguments: { file: f1.path }
2222+
});
2223+
2224+
// reload from tmp file
2225+
session.executeCommand(<server.protocol.ReloadRequest>{
2226+
type: "request",
2227+
command: "reload",
2228+
seq: 2,
2229+
arguments: { file: f1.path, tmpfile: tmp.path }
2230+
});
2231+
2232+
// verify content
2233+
const projectServiice = session.getProjectService();
2234+
const snap1 = projectServiice.getScriptInfo(f1.path).snap();
2235+
assert.equal(snap1.getText(0, snap1.getLength()), tmp.content, "content should be equal to the content of temp file");
2236+
2237+
// reload from original file file
2238+
session.executeCommand(<server.protocol.ReloadRequest>{
2239+
type: "request",
2240+
command: "reload",
2241+
seq: 2,
2242+
arguments: { file: f1.path }
2243+
});
2244+
2245+
// verify content
2246+
const snap2 = projectServiice.getScriptInfo(f1.path).snap();
2247+
assert.equal(snap2.getText(0, snap2.getLength()), f1.content, "content should be equal to the content of original file");
2248+
2249+
});
2250+
});
21942251
}

src/server/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,11 @@ namespace ts.server {
400400
}
401401
}
402402

403-
reloadScript(filename: NormalizedPath): boolean {
403+
reloadScript(filename: NormalizedPath, tempFileName?: NormalizedPath): boolean {
404404
const script = this.projectService.getScriptInfoForNormalizedPath(filename);
405405
if (script) {
406406
Debug.assert(script.isAttached(this));
407-
script.reloadFromFile();
407+
script.reloadFromFile(tempFileName);
408408
return true;
409409
}
410410
return false;

src/server/scriptInfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ namespace ts.server {
126126
this.host.writeFile(fileName, snap.getText(0, snap.getLength()));
127127
}
128128

129-
reloadFromFile() {
129+
reloadFromFile(tempFileName?: NormalizedPath) {
130130
if (this.hasMixedContent) {
131131
this.reload("");
132132
}
133133
else {
134-
this.svc.reloadFromFile(this.fileName);
134+
this.svc.reloadFromFile(tempFileName || this.fileName);
135135
this.markContainingProjectsAsDirty();
136136
}
137137
}

src/server/session.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,11 +1041,12 @@ namespace ts.server {
10411041

10421042
private reload(args: protocol.ReloadRequestArgs, reqSeq: number) {
10431043
const file = toNormalizedPath(args.file);
1044+
const tempFileName = args.tmpfile && toNormalizedPath(args.tmpfile);
10441045
const project = this.projectService.getDefaultProjectForFile(file, /*refreshInferredProjects*/ true);
10451046
if (project) {
10461047
this.changeSeq++;
10471048
// make sure no changes happen before this one is finished
1048-
if (project.reloadScript(file)) {
1049+
if (project.reloadScript(file, tempFileName)) {
10491050
this.output(undefined, CommandNames.Reload, reqSeq);
10501051
}
10511052
}

0 commit comments

Comments
 (0)