From 8944df18d44da30b689ec53861d7214e534d8ee0 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Sun, 15 Feb 2015 17:37:26 -0800 Subject: [PATCH] use EncodedFile everywhere in responses --- src/server/client.ts | 85 +++++++++++++++++++++++----------------- src/server/protocol.ts | 64 +++++++++++++++--------------- src/server/protodef.d.ts | 8 +++- 3 files changed, 85 insertions(+), 72 deletions(-) diff --git a/src/server/client.ts b/src/server/client.ts index d23bc5c7359..24666c8f4c7 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -6,11 +6,20 @@ module ts.server { writeMessage(message: string): void; } + interface RenameEntry extends RenameInfo { + fileName: string; + position: number; + locations: RenameLocation[]; + findInStrings: boolean; + findInComments: boolean; + } + export class SessionClient implements LanguageService { private sequence: number = 0; private fileMapping: ts.Map = {}; private lineMaps: ts.Map = {}; private messages: string[] = []; + private lastRenameEntry: RenameEntry; constructor(private host: SessionClientHost) { } @@ -54,7 +63,7 @@ module ts.server { }; } - private getFileNameFromEncodedFile(fileId: ServerProtocol.EncodedFile): string { + private decodeEncodedFileId(fileId: ServerProtocol.EncodedFile): string { var fileName: string; if (typeof fileId === "object") { fileName = (fileId).file; @@ -203,7 +212,7 @@ module ts.server { var response = this.processResponse(request); return response.body.map(entry => { - var fileName = this.getFileNameFromEncodedFile(entry.file); + var fileName = this.decodeEncodedFileId(entry.file); var start = this.lineColToPosition(fileName, entry.start); var end = this.lineColToPosition(fileName, entry.end); @@ -270,12 +279,13 @@ module ts.server { var response = this.processResponse(request); return response.body.map(entry => { + var fileName = this.decodeEncodedFileId(entry.file); var start = this.lineColToPosition(fileName, entry.start); var end = this.lineColToPosition(fileName, entry.end); return { containerKind: "", containerName: "", - fileName: entry.file, + fileName: fileName, textSpan: ts.createTextSpanFromBounds(start, end), kind: "", name: "" @@ -295,10 +305,11 @@ module ts.server { var response = this.processResponse(request); return response.body.refs.map(entry => { + var fileName = this.decodeEncodedFileId(entry.file); var start = this.lineColToPosition(fileName, entry.start); var end = this.lineColToPosition(fileName, entry.end); return { - fileName: entry.file, + fileName: fileName, textSpan: ts.createTextSpanFromBounds(start, end), isWriteAccess: entry.isWriteAccess, }; @@ -321,29 +332,7 @@ module ts.server { throw new Error("Not Implemented Yet."); } - getRenameInfo(fileName: string, position: number): RenameInfo { - var lineCol = this.positionToOneBasedLineCol(fileName, position); - var args: ServerProtocol.RenameRequestArgs = { - file: fileName, - line: lineCol.line, - col: lineCol.col - }; - - var request = this.processRequest(CommandNames.Rename, args); - var response = this.processResponse(request); - - return { - canRename: response.body.info.canRename, - displayName: response.body.info.displayName, - fullDisplayName: response.body.info.fullDisplayName, - kind: response.body.info.kind, - kindModifiers: response.body.info.kindModifiers, - localizedErrorMessage: response.body.info.localizedErrorMessage, - triggerSpan: ts.createTextSpanFromBounds(position, position) - }; - } - - findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] { + getRenameInfo(fileName: string, position: number, findInStrings?: boolean, findInComments?: boolean): RenameInfo { var lineCol = this.positionToOneBasedLineCol(fileName, position); var args: ServerProtocol.RenameRequestArgs = { file: fileName, @@ -356,18 +345,40 @@ module ts.server { var request = this.processRequest(CommandNames.Rename, args); var response = this.processResponse(request); - if (!response.body.info.canRename) { - return []; + return this.lastRenameEntry = { + canRename: response.body.info.canRename, + displayName: response.body.info.displayName, + fullDisplayName: response.body.info.fullDisplayName, + kind: response.body.info.kind, + kindModifiers: response.body.info.kindModifiers, + localizedErrorMessage: response.body.info.localizedErrorMessage, + triggerSpan: ts.createTextSpanFromBounds(position, position), + fileName: fileName, + position: position, + findInStrings: findInStrings, + findInComments: findInComments, + locations: response.body.locs.map((entry) => { + var fileName = this.decodeEncodedFileId(entry.file); + var start = this.lineColToPosition(fileName, entry.start); + var end = this.lineColToPosition(fileName, entry.end); + return { + textSpan: ts.createTextSpanFromBounds(start, end), + fileName: fileName + }; + }) + }; + } + + findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] { + if (!this.lastRenameEntry || + this.lastRenameEntry.fileName !== fileName || + this.lastRenameEntry.position !== position || + this.lastRenameEntry.findInStrings != findInStrings || + this.lastRenameEntry.findInComments != findInComments) { + this.getRenameInfo(fileName, position, findInStrings, findInComments); } - return response.body.locs.map((entry) => { - var start = this.lineColToPosition(entry.file, entry.start); - var end = this.lineColToPosition(entry.file, entry.end); - return { - textSpan: ts.createTextSpanFromBounds(start, end), - fileName: entry.file - }; - }); + return this.lastRenameEntry.locations; } decodeNavigationBarItems(items: ServerProtocol.NavigationBarItem[], fileName: string): NavigationBarItem[] { diff --git a/src/server/protocol.ts b/src/server/protocol.ts index 04b3ce065b0..386a477cf0f 100644 --- a/src/server/protocol.ts +++ b/src/server/protocol.ts @@ -27,7 +27,7 @@ module ts.server { } interface FileStart { - file: string; + file: ServerProtocol.EncodedFile; start: ILineInfo; } @@ -92,9 +92,9 @@ module ts.server { locs: FileRange[]; } - function formatDiag(file: string, project: Project, diag: ts.Diagnostic) { + function formatDiag(fileName: string, project: Project, diag: ts.Diagnostic) { return { - start: project.compilerService.host.positionToLineCol(file, diag.start), + start: project.compilerService.host.positionToLineCol(fileName, diag.start), len: diag.length, text: diag.messageText, }; @@ -204,7 +204,7 @@ module ts.server { this.send(res); } - encodeFilename(fileName: string): ServerProtocol.EncodedFile { + encodeFileName(fileName: string): ServerProtocol.EncodedFile { var id = ts.lookUp(this.fileHash, fileName); if (!id) { id = this.nextFileId++; @@ -216,15 +216,15 @@ module ts.server { } } - output(info: any, cmdName: string, reqSeq = 0, errorMsg?: string) { - this.response(info, cmdName, reqSeq, errorMsg); + output(body: any, commandName: string, requestSequence = 0, errorMessage?: string) { + this.response(body, commandName, requestSequence, errorMessage); } semanticCheck(file: string, project: Project) { var diags = project.compilerService.languageService.getSemanticDiagnostics(file); if (diags) { var bakedDiags = diags.map((diag) => formatDiag(file, project, diag)); - this.event({ file: file, diagnostics: bakedDiags }, "semanticDiag"); + this.event({ file: this.encodeFileName(file), diagnostics: bakedDiags }, "semanticDiag"); } } @@ -232,7 +232,7 @@ module ts.server { var diags = project.compilerService.languageService.getSyntacticDiagnostics(file); if (diags) { var bakedDiags = diags.map((diag) => formatDiag(file, project, diag)); - this.event({ file: file, diagnostics: bakedDiags }, "syntaxDiag"); + this.event({ file: this.encodeFileName(file), diagnostics: bakedDiags }, "syntaxDiag"); } } @@ -293,11 +293,9 @@ module ts.server { } return definitions.map(def => ({ - file: def && def.fileName, - start: def && - compilerService.host.positionToLineCol(def.fileName, def.textSpan.start), - end: def && - compilerService.host.positionToLineCol(def.fileName, ts.textSpanEnd(def.textSpan)) + file: this.encodeFileName(def.fileName), + start: compilerService.host.positionToLineCol(def.fileName, def.textSpan.start), + end: compilerService.host.positionToLineCol(def.fileName, ts.textSpanEnd(def.textSpan)) })); } @@ -322,15 +320,15 @@ module ts.server { }; } - var renameLocs = compilerService.languageService.findRenameLocations(file, position, findInStrings, findInComments); - if (!renameLocs) { + var renameLocations = compilerService.languageService.findRenameLocations(file, position, findInStrings, findInComments); + if (!renameLocations) { throw Errors.NoContent; } - var bakedRenameLocs = renameLocs.map(loc => ({ - file: loc.fileName, - start: compilerService.host.positionToLineCol(loc.fileName, loc.textSpan.start), - end: compilerService.host.positionToLineCol(loc.fileName, ts.textSpanEnd(loc.textSpan)), + var bakedRenameLocs = renameLocations.map(location => ({ + file: this.encodeFileName(location.fileName), + start: compilerService.host.positionToLineCol(location.fileName, location.textSpan.start), + end: compilerService.host.positionToLineCol(location.fileName, ts.textSpanEnd(location.textSpan)), })); return { info: renameInfo, locs: bakedRenameLocs }; @@ -368,7 +366,7 @@ module ts.server { var snap = compilerService.host.getScriptSnapshot(ref.fileName); var lineText = snap.getText(refLineSpan.start, ts.textSpanEnd(refLineSpan)).replace(/\r|\n/g, ""); return { - file: ref.fileName, + file: this.encodeFileName(ref.fileName), start: start, lineText: lineText, end: compilerService.host.positionToLineCol(ref.fileName, ts.textSpanEnd(ref.textSpan)), @@ -383,8 +381,8 @@ module ts.server { }; } - openClientFile(rawfile: string) { - var file = ts.normalizePath(rawfile); + openClientFile(fileName: string) { + var file = ts.normalizePath(fileName); this.projectService.openClientFile(file); } @@ -536,8 +534,8 @@ module ts.server { } } - change(line: number, col: number, deleteLen: number, insertString: string, rawfile: string) { - var file = ts.normalizePath(rawfile); + change(line: number, col: number, deleteLen: number, insertString: string, fileName: string) { + var file = ts.normalizePath(fileName); var project = this.projectService.getProjectForFile(file); if (project) { var compilerService = project.compilerService; @@ -553,9 +551,9 @@ module ts.server { } } - reload(rawfile: string, rawtmpfile: string, reqSeq = 0) { - var file = ts.normalizePath(rawfile); - var tmpfile = ts.normalizePath(rawtmpfile); + reload(fileName: string, tempFileName: string, reqSeq = 0) { + var file = ts.normalizePath(fileName); + var tmpfile = ts.normalizePath(tempFileName); var project = this.projectService.getProjectForFile(file); if (project) { this.changeSeq++; @@ -566,9 +564,9 @@ module ts.server { } } - saveToTmp(rawfile: string, rawtmpfile: string) { - var file = ts.normalizePath(rawfile); - var tmpfile = ts.normalizePath(rawtmpfile); + saveToTmp(fileName: string, tempFileName: string) { + var file = ts.normalizePath(fileName); + var tmpfile = ts.normalizePath(tempFileName); var project = this.projectService.getProjectForFile(file); if (project) { @@ -576,8 +574,8 @@ module ts.server { } } - closeClientFile(rawfile: string) { - var file = ts.normalizePath(rawfile); + closeClientFile(fileName: string) { + var file = ts.normalizePath(fileName); this.projectService.closeClientFile(file); } @@ -635,7 +633,7 @@ module ts.server { var bakedItem: ServerProtocol.NavtoItem = { name: navItem.name, kind: navItem.kind, - file: this.encodeFilename(navItem.fileName), + file: this.encodeFileName(navItem.fileName), start: start, end: end, }; diff --git a/src/server/protodef.d.ts b/src/server/protodef.d.ts index 12fd7163c1a..d2dcf4260ef 100644 --- a/src/server/protodef.d.ts +++ b/src/server/protodef.d.ts @@ -101,8 +101,12 @@ declare module ServerProtocol { a specific source file. */ export interface CodeSpan extends TextSpan { - /** File containing the definition */ - file: string; + /** + File containing the definition; the value of this + field will always be a string, number of a mapping between + a string and a number. + */ + file: EncodedFile; } /**