From f92d24188826e4e132f935a5c8571f0ebab53153 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 10 Nov 2015 13:36:19 -0800 Subject: [PATCH 1/5] Add file content as a parameter for the tsserver open command --- src/server/editorServices.ts | 10 ++++++---- src/server/protocol.d.ts | 1 + src/server/session.ts | 10 +++++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 333cea2745d6f..45d987ea53456 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1006,14 +1006,15 @@ namespace ts.server { /** * @param filename is absolute pathname + * @param fileContent is a known version of the file content that is more up to date */ - openFile(fileName: string, openedByClient: boolean) { + openFile(fileName: string, openedByClient: boolean, fileContent?: string) { fileName = ts.normalizePath(fileName); let info = ts.lookUp(this.filenameToScriptInfo, fileName); if (!info) { let content: string; if (this.host.fileExists(fileName)) { - content = this.host.readFile(fileName); + content = fileContent ? fileContent : this.host.readFile(fileName); } if (!content) { if (openedByClient) { @@ -1060,10 +1061,11 @@ namespace ts.server { /** * Open file whose contents is managed by the client * @param filename is absolute pathname + * @param fileContent is a known version of the file content that is more up to date */ - openClientFile(fileName: string) { + openClientFile(fileName: string, fileContent?: string) { this.openOrUpdateConfiguredProjectForFile(fileName); - const info = this.openFile(fileName, true); + const info = this.openFile(fileName, true, fileContent); this.addOpenFile(info); this.printProjects(); return info; diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index ad1fc5e92de2a..6354a3a37aeb0 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -513,6 +513,7 @@ declare namespace ts.server.protocol { * Information found in an "open" request. */ export interface OpenRequestArgs extends FileRequestArgs { + fileContent?: string; } /** diff --git a/src/server/session.ts b/src/server/session.ts index 770256cc9f700..c589c528ec22f 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -532,9 +532,13 @@ namespace ts.server { }; } - private openClientFile(fileName: string) { + /** + * @param fileName is the name of the file to be opened + * @param fileContent is a version of the file content that is known to be more up to date + */ + private openClientFile(fileName: string, fileContent?: string) { const file = ts.normalizePath(fileName); - this.projectService.openClientFile(file); + this.projectService.openClientFile(file, fileContent); } private getQuickInfo(line: number, offset: number, fileName: string): protocol.QuickInfoResponseBody { @@ -968,7 +972,7 @@ namespace ts.server { }, [CommandNames.Open]: (request: protocol.Request) => { const openArgs = request.arguments; - this.openClientFile(openArgs.file); + this.openClientFile(openArgs.file, openArgs.fileContent); return {responseRequired: false}; }, [CommandNames.Quickinfo]: (request: protocol.Request) => { From b9778e6d20b9a96f40ece46d08d23df8cfc4c6bd Mon Sep 17 00:00:00 2001 From: zhengbli Date: Wed, 11 Nov 2015 10:31:09 -0800 Subject: [PATCH 2/5] cr feedback --- src/server/editorServices.ts | 6 +++--- src/server/session.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 45d987ea53456..889c96b6c4ec8 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1006,7 +1006,7 @@ namespace ts.server { /** * @param filename is absolute pathname - * @param fileContent is a known version of the file content that is more up to date + * @param fileContent is a known version of the file content that is more up to date than the one on disk */ openFile(fileName: string, openedByClient: boolean, fileContent?: string) { fileName = ts.normalizePath(fileName); @@ -1014,7 +1014,7 @@ namespace ts.server { if (!info) { let content: string; if (this.host.fileExists(fileName)) { - content = fileContent ? fileContent : this.host.readFile(fileName); + content = fileContent || this.host.readFile(fileName); } if (!content) { if (openedByClient) { @@ -1061,7 +1061,7 @@ namespace ts.server { /** * Open file whose contents is managed by the client * @param filename is absolute pathname - * @param fileContent is a known version of the file content that is more up to date + * @param fileContent is a known version of the file content that is more up to date than the one on disk */ openClientFile(fileName: string, fileContent?: string) { this.openOrUpdateConfiguredProjectForFile(fileName); diff --git a/src/server/session.ts b/src/server/session.ts index c589c528ec22f..f04f73f644cba 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -534,7 +534,7 @@ namespace ts.server { /** * @param fileName is the name of the file to be opened - * @param fileContent is a version of the file content that is known to be more up to date + * @param fileContent is a version of the file content that is known to be more up to date than the one on disk */ private openClientFile(fileName: string, fileContent?: string) { const file = ts.normalizePath(fileName); From 278b35b354913e81fd1ea6886684ecef6c5e99a1 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 12 Nov 2015 11:16:11 -0800 Subject: [PATCH 3/5] Adding test and comments. Override file content even if already opened. --- src/harness/fourslash.ts | 8 ++++---- src/harness/harnessLanguageService.ts | 8 ++++---- src/server/client.ts | 4 ++-- src/server/editorServices.ts | 3 +++ src/server/protocol.d.ts | 4 ++++ tests/cases/fourslash/fourslash.ts | 8 ++++---- tests/cases/fourslash/server/openFile.ts | 17 +++++++++++++++++ 7 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 tests/cases/fourslash/server/openFile.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 1c8593a9e1294..545776ec08427 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -385,9 +385,9 @@ namespace FourSlash { } // Opens a file given its 0-based index or fileName - public openFile(index: number): void; - public openFile(name: string): void; - public openFile(indexOrName: any) { + public openFile(index: number, content?: string): void; + public openFile(name: string, content?: string): void; + public openFile(indexOrName: any, content?: string) { const fileToOpen: FourSlashFile = this.findFile(indexOrName); fileToOpen.fileName = ts.normalizeSlashes(fileToOpen.fileName); this.activeFile = fileToOpen; @@ -395,7 +395,7 @@ namespace FourSlash { this.scenarioActions.push(``); // Let the host know that this file is now open - this.languageServiceAdapterHost.openFile(fileToOpen.fileName); + this.languageServiceAdapterHost.openFile(fileToOpen.fileName, content); } public verifyErrorExistsBetweenMarkers(startMarkerName: string, endMarkerName: string, negative: boolean) { diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index faf0ad28eb526..0c267c38f14a6 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -153,7 +153,7 @@ namespace Harness.LanguageService { throw new Error("No script with name '" + fileName + "'"); } - public openFile(fileName: string): void { + public openFile(fileName: string, content?: string): void { } /** @@ -493,9 +493,9 @@ namespace Harness.LanguageService { this.client = client; } - openFile(fileName: string): void { - super.openFile(fileName); - this.client.openFile(fileName); + openFile(fileName: string, content?: string): void { + super.openFile(fileName, content); + this.client.openFile(fileName, content); } editScript(fileName: string, start: number, end: number, newText: string) { diff --git a/src/server/client.ts b/src/server/client.ts index ae234750d88da..08939b2b44aca 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -120,8 +120,8 @@ namespace ts.server { return response; } - openFile(fileName: string): void { - var args: protocol.FileRequestArgs = { file: fileName }; + openFile(fileName: string, content?: string): void { + var args: protocol.OpenRequestArgs = { file: fileName, fileContent: content }; this.processRequest(CommandNames.Open, args); } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 889c96b6c4ec8..795bd7db7321e 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1031,6 +1031,9 @@ namespace ts.server { } } if (info) { + if (fileContent) { + info.svc.reload(fileContent); + } if (openedByClient) { info.isOpen = true; } diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index 6354a3a37aeb0..101c8207c9d33 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -513,6 +513,10 @@ declare namespace ts.server.protocol { * Information found in an "open" request. */ export interface OpenRequestArgs extends FileRequestArgs { + /** + * Used when a version of the file content is known to be more up to date than the one on disk. + * Then the known content will be used upon opening instead of the disk copy + */ fileContent?: string; } diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 41e8d9005f762..f6451e1f1c4cc 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -169,10 +169,10 @@ module FourSlashInterface { // Opens a file, given either its index as it // appears in the test source, or its filename // as specified in the test metadata - public file(index: number); - public file(name: string); - public file(indexOrName: any) { - FourSlash.currentTestState.openFile(indexOrName); + public file(index: number, content?: string); + public file(name: string, content?: string); + public file(indexOrName: any, content?: string) { + FourSlash.currentTestState.openFile(indexOrName, content); } } diff --git a/tests/cases/fourslash/server/openFile.ts b/tests/cases/fourslash/server/openFile.ts new file mode 100644 index 0000000000000..41d2bbe8e53dd --- /dev/null +++ b/tests/cases/fourslash/server/openFile.ts @@ -0,0 +1,17 @@ +/// + +// @Filename: test1.ts +////t. + +// @Filename: test.ts +////var t = '10'; + +// @Filename: tsconfig.json +////{ "files": ["test.ts", "test1.ts"] } + +var overridingContent = "var t = 10; t."; +debugger; +goTo.file("test.ts", overridingContent); +goTo.file("test1.ts"); +goTo.eof(); +verify.completionListContains("toExponential"); From a0549fa316d05bd79ab1bb97b32744b9748091a4 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 12 Nov 2015 11:33:44 -0800 Subject: [PATCH 4/5] Fix lint compliant --- src/server/protocol.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index 101c8207c9d33..3a66975332382 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -516,7 +516,7 @@ declare namespace ts.server.protocol { /** * Used when a version of the file content is known to be more up to date than the one on disk. * Then the known content will be used upon opening instead of the disk copy - */ + */ fileContent?: string; } From fe9d73eb5bc08e752793f143c31c713c93ef85d0 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Thu, 12 Nov 2015 11:42:21 -0800 Subject: [PATCH 5/5] Remove debugger statement from test --- tests/cases/fourslash/server/openFile.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/cases/fourslash/server/openFile.ts b/tests/cases/fourslash/server/openFile.ts index 41d2bbe8e53dd..320e52c9f5e1a 100644 --- a/tests/cases/fourslash/server/openFile.ts +++ b/tests/cases/fourslash/server/openFile.ts @@ -10,7 +10,6 @@ ////{ "files": ["test.ts", "test1.ts"] } var overridingContent = "var t = 10; t."; -debugger; goTo.file("test.ts", overridingContent); goTo.file("test1.ts"); goTo.eof();