Skip to content

Commit 6e918d1

Browse files
committed
Merge pull request #5656 from zhengbli/CherryPicking5593To17
Cherrypicking PR 5593
2 parents e3b2fe9 + 04a05b2 commit 6e918d1

File tree

8 files changed

+57
-27
lines changed

8 files changed

+57
-27
lines changed

src/harness/fourslash.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,17 @@ namespace FourSlash {
373373
}
374374

375375
// Opens a file given its 0-based index or fileName
376-
public openFile(index: number): void;
377-
public openFile(name: string): void;
378-
public openFile(indexOrName: any) {
379-
let fileToOpen: FourSlashFile = this.findFile(indexOrName);
376+
public openFile(index: number, content?: string): void;
377+
public openFile(name: string, content?: string): void;
378+
public openFile(indexOrName: any, content?: string) {
379+
const fileToOpen: FourSlashFile = this.findFile(indexOrName);
380380
fileToOpen.fileName = ts.normalizeSlashes(fileToOpen.fileName);
381381
this.activeFile = fileToOpen;
382-
let fileName = fileToOpen.fileName.replace(Harness.IO.directoryName(fileToOpen.fileName), "").substr(1);
382+
const fileName = fileToOpen.fileName.replace(Harness.IO.directoryName(fileToOpen.fileName), "").substr(1);
383383
this.scenarioActions.push(`<OpenFile FileName="" SrcFileId="${fileName}" FileId="${fileName}" />`);
384384

385385
// Let the host know that this file is now open
386-
this.languageServiceAdapterHost.openFile(fileToOpen.fileName);
386+
this.languageServiceAdapterHost.openFile(fileToOpen.fileName, content);
387387
}
388388

389389
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,15 @@ namespace ts.server {
992992

993993
/**
994994
* @param filename is absolute pathname
995+
* @param fileContent is a known version of the file content that is more up to date than the one on disk
995996
*/
996-
openFile(fileName: string, openedByClient: boolean) {
997+
openFile(fileName: string, openedByClient: boolean, fileContent?: string) {
997998
fileName = ts.normalizePath(fileName);
998-
var info = ts.lookUp(this.filenameToScriptInfo, fileName);
999+
let info = ts.lookUp(this.filenameToScriptInfo, fileName);
9991000
if (!info) {
1000-
var content: string;
1001+
let content: string;
10011002
if (this.host.fileExists(fileName)) {
1002-
content = this.host.readFile(fileName);
1003+
content = fileContent || this.host.readFile(fileName);
10031004
}
10041005
if (!content) {
10051006
if (openedByClient) {
@@ -1017,6 +1018,9 @@ namespace ts.server {
10171018
}
10181019
}
10191020
if (info) {
1021+
if (fileContent) {
1022+
info.svc.reload(fileContent);
1023+
}
10201024
if (openedByClient) {
10211025
info.isOpen = true;
10221026
}
@@ -1047,10 +1051,11 @@ namespace ts.server {
10471051
/**
10481052
* Open file whose contents is managed by the client
10491053
* @param filename is absolute pathname
1054+
* @param fileContent is a known version of the file content that is more up to date than the one on disk
10501055
*/
1051-
openClientFile(fileName: string) {
1056+
openClientFile(fileName: string, fileContent?: string) {
10521057
this.openOrUpdateConfiguredProjectForFile(fileName);
1053-
var info = this.openFile(fileName, true);
1058+
const info = this.openFile(fileName, true, fileContent);
10541059
this.addOpenFile(info);
10551060
this.printProjects();
10561061
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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/// <reference path="editorServices.ts" />
55

66
namespace ts.server {
7-
var spaceCache:string[] = [];
7+
const spaceCache: string[] = [];
88

99
interface StackTraceError extends Error {
1010
stack?: string;
@@ -531,9 +531,13 @@ namespace ts.server {
531531
};
532532
}
533533

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

539543
private getQuickInfo(line: number, offset: number, fileName: string): protocol.QuickInfoResponseBody {
@@ -966,7 +970,7 @@ namespace ts.server {
966970
},
967971
[CommandNames.Open]: (request: protocol.Request) => {
968972
var openArgs = <protocol.OpenRequestArgs>request.arguments;
969-
this.openClientFile(openArgs.file);
973+
this.openClientFile(openArgs.file, openArgs.fileContent);
970974
return {responseRequired: false}
971975
},
972976
[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)