mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-12-15 00:06:16 -06:00
Merge pull request #11694 from Microsoft/vladima/reload-tmp
allow reload from temp files
This commit is contained in:
parent
68c485d9f9
commit
121f51e552
@ -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");
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user