Merge pull request #11694 from Microsoft/vladima/reload-tmp

allow reload from temp files
This commit is contained in:
Vladimir Matveev 2016-10-17 23:15:22 -07:00
parent 68c485d9f9
commit 121f51e552
4 changed files with 66 additions and 7 deletions

View File

@ -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 {
@ -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();
}
@ -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");
});
});
}

View File

@ -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;

View File

@ -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();
}
}

View File

@ -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);
}
}