Skip to content

Commit bd39847

Browse files
committed
Merge pull request #5593 from zhengbli/i5530
Add file content as a parameter for the tsserver open command
2 parents 7743d20 + fe9d73e commit bd39847

File tree

8 files changed

+51
-21
lines changed

8 files changed

+51
-21
lines changed

src/harness/fourslash.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,17 @@ namespace FourSlash {
385385
}
386386

387387
// Opens a file given its 0-based index or fileName
388-
public openFile(index: number): void;
389-
public openFile(name: string): void;
390-
public openFile(indexOrName: any) {
388+
public openFile(index: number, content?: string): void;
389+
public openFile(name: string, content?: string): void;
390+
public openFile(indexOrName: any, content?: string) {
391391
const fileToOpen: FourSlashFile = this.findFile(indexOrName);
392392
fileToOpen.fileName = ts.normalizeSlashes(fileToOpen.fileName);
393393
this.activeFile = fileToOpen;
394394
const fileName = fileToOpen.fileName.replace(Harness.IO.directoryName(fileToOpen.fileName), "").substr(1);
395395
this.scenarioActions.push(`<OpenFile FileName="" SrcFileId="${fileName}" FileId="${fileName}" />`);
396396

397397
// Let the host know that this file is now open
398-
this.languageServiceAdapterHost.openFile(fileToOpen.fileName);
398+
this.languageServiceAdapterHost.openFile(fileToOpen.fileName, content);
399399
}
400400

401401
public verifyErrorExistsBetweenMarkers(startMarkerName: string, endMarkerName: string, negative: boolean) {

src/harness/harnessLanguageService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace Harness.LanguageService {
153153
throw new Error("No script with name '" + fileName + "'");
154154
}
155155

156-
public openFile(fileName: string): void {
156+
public openFile(fileName: string, content?: string): void {
157157
}
158158

159159
/**
@@ -493,9 +493,9 @@ namespace Harness.LanguageService {
493493
this.client = client;
494494
}
495495

496-
openFile(fileName: string): void {
497-
super.openFile(fileName);
498-
this.client.openFile(fileName);
496+
openFile(fileName: string, content?: string): void {
497+
super.openFile(fileName, content);
498+
this.client.openFile(fileName, content);
499499
}
500500

501501
editScript(fileName: string, start: number, end: number, newText: string) {

src/server/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ namespace ts.server {
120120
return response;
121121
}
122122

123-
openFile(fileName: string): void {
124-
var args: protocol.FileRequestArgs = { file: fileName };
123+
openFile(fileName: string, content?: string): void {
124+
var args: protocol.OpenRequestArgs = { file: fileName, fileContent: content };
125125
this.processRequest(CommandNames.Open, args);
126126
}
127127

src/server/editorServices.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,14 +1006,15 @@ namespace ts.server {
10061006

10071007
/**
10081008
* @param filename is absolute pathname
1009+
* @param fileContent is a known version of the file content that is more up to date than the one on disk
10091010
*/
1010-
openFile(fileName: string, openedByClient: boolean) {
1011+
openFile(fileName: string, openedByClient: boolean, fileContent?: string) {
10111012
fileName = ts.normalizePath(fileName);
10121013
let info = ts.lookUp(this.filenameToScriptInfo, fileName);
10131014
if (!info) {
10141015
let content: string;
10151016
if (this.host.fileExists(fileName)) {
1016-
content = this.host.readFile(fileName);
1017+
content = fileContent || this.host.readFile(fileName);
10171018
}
10181019
if (!content) {
10191020
if (openedByClient) {
@@ -1030,6 +1031,9 @@ namespace ts.server {
10301031
}
10311032
}
10321033
if (info) {
1034+
if (fileContent) {
1035+
info.svc.reload(fileContent);
1036+
}
10331037
if (openedByClient) {
10341038
info.isOpen = true;
10351039
}
@@ -1060,10 +1064,11 @@ namespace ts.server {
10601064
/**
10611065
* Open file whose contents is managed by the client
10621066
* @param filename is absolute pathname
1067+
* @param fileContent is a known version of the file content that is more up to date than the one on disk
10631068
*/
1064-
openClientFile(fileName: string) {
1069+
openClientFile(fileName: string, fileContent?: string) {
10651070
this.openOrUpdateConfiguredProjectForFile(fileName);
1066-
const info = this.openFile(fileName, true);
1071+
const info = this.openFile(fileName, true, fileContent);
10671072
this.addOpenFile(info);
10681073
this.printProjects();
10691074
return info;

src/server/protocol.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ declare namespace ts.server.protocol {
513513
* Information found in an "open" request.
514514
*/
515515
export interface OpenRequestArgs extends FileRequestArgs {
516+
/**
517+
* Used when a version of the file content is known to be more up to date than the one on disk.
518+
* Then the known content will be used upon opening instead of the disk copy
519+
*/
520+
fileContent?: string;
516521
}
517522

518523
/**

src/server/session.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,13 @@ namespace ts.server {
532532
};
533533
}
534534

535-
private openClientFile(fileName: string) {
535+
/**
536+
* @param fileName is the name of the file to be opened
537+
* @param fileContent is a version of the file content that is known to be more up to date than the one on disk
538+
*/
539+
private openClientFile(fileName: string, fileContent?: string) {
536540
const file = ts.normalizePath(fileName);
537-
this.projectService.openClientFile(file);
541+
this.projectService.openClientFile(file, fileContent);
538542
}
539543

540544
private getQuickInfo(line: number, offset: number, fileName: string): protocol.QuickInfoResponseBody {
@@ -968,7 +972,7 @@ namespace ts.server {
968972
},
969973
[CommandNames.Open]: (request: protocol.Request) => {
970974
const openArgs = <protocol.OpenRequestArgs>request.arguments;
971-
this.openClientFile(openArgs.file);
975+
this.openClientFile(openArgs.file, openArgs.fileContent);
972976
return {responseRequired: false};
973977
},
974978
[CommandNames.Quickinfo]: (request: protocol.Request) => {

tests/cases/fourslash/fourslash.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ module FourSlashInterface {
169169
// Opens a file, given either its index as it
170170
// appears in the test source, or its filename
171171
// as specified in the test metadata
172-
public file(index: number);
173-
public file(name: string);
174-
public file(indexOrName: any) {
175-
FourSlash.currentTestState.openFile(indexOrName);
172+
public file(index: number, content?: string);
173+
public file(name: string, content?: string);
174+
public file(indexOrName: any, content?: string) {
175+
FourSlash.currentTestState.openFile(indexOrName, content);
176176
}
177177
}
178178

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="../fourslash.ts"/>
2+
3+
// @Filename: test1.ts
4+
////t.
5+
6+
// @Filename: test.ts
7+
////var t = '10';
8+
9+
// @Filename: tsconfig.json
10+
////{ "files": ["test.ts", "test1.ts"] }
11+
12+
var overridingContent = "var t = 10; t.";
13+
goTo.file("test.ts", overridingContent);
14+
goTo.file("test1.ts");
15+
goTo.eof();
16+
verify.completionListContains("toExponential");

0 commit comments

Comments
 (0)