Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,17 @@ 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) {
let fileToOpen: FourSlashFile = this.findFile(indexOrName);
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;
let fileName = fileToOpen.fileName.replace(Harness.IO.directoryName(fileToOpen.fileName), "").substr(1);
const fileName = fileToOpen.fileName.replace(Harness.IO.directoryName(fileToOpen.fileName), "").substr(1);
this.scenarioActions.push(`<OpenFile FileName="" SrcFileId="${fileName}" FileId="${fileName}" />`);

// 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) {
Expand Down
8 changes: 4 additions & 4 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
17 changes: 11 additions & 6 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,14 +992,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 than the one on disk
*/
openFile(fileName: string, openedByClient: boolean) {
openFile(fileName: string, openedByClient: boolean, fileContent?: string) {
fileName = ts.normalizePath(fileName);
var info = ts.lookUp(this.filenameToScriptInfo, fileName);
let info = ts.lookUp(this.filenameToScriptInfo, fileName);
if (!info) {
var content: string;
let content: string;
if (this.host.fileExists(fileName)) {
content = this.host.readFile(fileName);
content = fileContent || this.host.readFile(fileName);
}
if (!content) {
if (openedByClient) {
Expand All @@ -1017,6 +1018,9 @@ namespace ts.server {
}
}
if (info) {
if (fileContent) {
info.svc.reload(fileContent);
}
if (openedByClient) {
info.isOpen = true;
}
Expand Down Expand Up @@ -1047,10 +1051,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 than the one on disk
*/
openClientFile(fileName: string) {
openClientFile(fileName: string, fileContent?: string) {
this.openOrUpdateConfiguredProjectForFile(fileName);
var info = this.openFile(fileName, true);
const info = this.openFile(fileName, true, fileContent);
this.addOpenFile(info);
this.printProjects();
return info;
Expand Down
5 changes: 5 additions & 0 deletions src/server/protocol.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ 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;
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/// <reference path="editorServices.ts" />

namespace ts.server {
var spaceCache:string[] = [];
const spaceCache: string[] = [];

interface StackTraceError extends Error {
stack?: string;
Expand Down Expand Up @@ -531,9 +531,13 @@ namespace ts.server {
};
}

private openClientFile(fileName: string) {
var file = ts.normalizePath(fileName);
this.projectService.openClientFile(file);
/**
* @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 than the one on disk
*/
private openClientFile(fileName: string, fileContent?: string) {
const file = ts.normalizePath(fileName);
this.projectService.openClientFile(file, fileContent);
}

private getQuickInfo(line: number, offset: number, fileName: string): protocol.QuickInfoResponseBody {
Expand Down Expand Up @@ -966,7 +970,7 @@ namespace ts.server {
},
[CommandNames.Open]: (request: protocol.Request) => {
var openArgs = <protocol.OpenRequestArgs>request.arguments;
this.openClientFile(openArgs.file);
this.openClientFile(openArgs.file, openArgs.fileContent);
return {responseRequired: false}
},
[CommandNames.Quickinfo]: (request: protocol.Request) => {
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/server/openFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="../fourslash.ts"/>

// @Filename: test1.ts
////t.

// @Filename: test.ts
////var t = '10';

// @Filename: tsconfig.json
////{ "files": ["test.ts", "test1.ts"] }

var overridingContent = "var t = 10; t.";
goTo.file("test.ts", overridingContent);
goTo.file("test1.ts");
goTo.eof();
verify.completionListContains("toExponential");