Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,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 {
Expand Down Expand Up @@ -515,11 +522,13 @@ namespace ts.projectSystem {
this.reloadFS(filesOrFolders);
}

write(s: string) {
}

readonly readFile = (s: string) => (<File>this.fs.get(this.toPath(s))).content;
readonly resolvePath = (s: string) => s;
readonly getExecutingFilePath = () => this.executingFilePath;
readonly getCurrentDirectory = () => this.currentDirectory;
readonly write = (s: string) => notImplemented();
readonly exit = () => notImplemented();
readonly getEnvironmentVariable = (v: string) => notImplemented();
}
Expand Down Expand Up @@ -2420,4 +2429,53 @@ namespace ts.projectSystem {
assert.isTrue(inferredProject.containsFile(<server.NormalizedPath>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(<server.protocol.OpenRequest>{
type: "request",
command: "open",
seq: 1,
arguments: { file: f1.path }
});

// reload from tmp file
session.executeCommand(<server.protocol.ReloadRequest>{
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(<server.protocol.ReloadRequest>{
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");

});
});
}
4 changes: 2 additions & 2 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,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;
Expand Down
4 changes: 2 additions & 2 deletions src/server/scriptInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,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();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1076,11 +1076,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);
}
}
Expand Down