From 8550c7de4e6ea0afe139ed1574c6e4f3bfdbb070 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Date: Tue, 4 Aug 2015 15:46:38 -0700 Subject: [PATCH] Address feedback. --- src/harness/fourslash.ts | 22 ++++++++-------- src/server/client.ts | 41 ++++++++++++++++-------------- src/server/protocol.d.ts | 4 +-- src/server/session.ts | 31 +++++++++++----------- tests/cases/fourslash/fourslash.ts | 2 +- 5 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 2cd06e93d41..f7564ef52f0 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2166,15 +2166,17 @@ module FourSlash { this.raiseError('verifyDocumentHighlightsAtPositionListContains failed - found 0 highlights, expected at least one.'); } - for (let i = 0; i < documentHighlights.length; i++) if (documentHighlights[i].fileName === fileName) { - let { highlightSpans } = documentHighlights[i]; + for (let documentHighlight of documentHighlights) { + if (documentHighlight.fileName === fileName) { + let { highlightSpans } = documentHighlight; - for (let highlight of highlightSpans) { - if (highlight && highlight.textSpan.start === start && ts.textSpanEnd(highlight.textSpan) === end) { - if (typeof kind !== "undefined" && highlight.kind !== kind) { - this.raiseError('verifyDocumentHighlightsAtPositionListContains failed - item "kind" value does not match, actual: ' + highlight.kind + ', expected: ' + kind + '.'); + for (let highlight of highlightSpans) { + if (highlight && highlight.textSpan.start === start && ts.textSpanEnd(highlight.textSpan) === end) { + if (typeof kind !== "undefined" && highlight.kind !== kind) { + this.raiseError('verifyDocumentHighlightsAtPositionListContains failed - item "kind" value does not match, actual: ' + highlight.kind + ', expected: ' + kind + '.'); + } + return; } - return; } } } @@ -2187,9 +2189,9 @@ module FourSlash { this.taoInvalidReason = 'verifyDocumentHighlightsAtPositionListCount NYI'; let documentHighlights = this.getDocumentHighlightsAtCurrentPosition(fileNamesToSearch); - let actualCount = documentHighlights - ? documentHighlights.reduce((currentCount, currentDocumentHighlights) => { - return currentCount + currentDocumentHighlights.highlightSpans.length}, 0) + let actualCount = documentHighlights + ? documentHighlights.reduce((currentCount, { fileName, highlightSpans }) => { + return currentCount + highlightSpans.length}, 0) : 0; if (expectedCount !== actualCount) { diff --git a/src/server/client.ts b/src/server/client.ts index f02961f02ad..d11a3c75466 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -528,29 +528,32 @@ namespace ts.server { } getDocumentHighlights(fileName: string, position: number): DocumentHighlights[] { - var lineOffset = this.positionToOneBasedLineOffset(fileName, position); - var args: protocol.FileLocationRequestArgs = { - file: fileName, - line: lineOffset.line, - offset: lineOffset.offset, - }; + let { line, offset } = this.positionToOneBasedLineOffset(fileName, position); + let args: protocol.FileLocationRequestArgs = { file: fileName, line, offset }; - var request = this.processRequest(CommandNames.DocumentHighlights, args); - var response = this.processResponse(request); + let request = this.processRequest(CommandNames.DocumentHighlights, args); + let response = this.processResponse(request); + + let _self = this; + return response.body.map(convertToDocumentHighlights); + + function convertToDocumentHighlights(item: ts.server.protocol.DocumentHighlightsItem): ts.DocumentHighlights { + let { file, highlightSpans } = item; - return response.body.map(entry => { // convert ts.server.protocol.DocumentHighlightsItem to ts.DocumentHighlights return { - fileName: entry.file, - highlightSpans: entry.highlightSpans.map(span => { // convert ts.server.protocol.HighlightSpan to ts.HighlighSpan - var start = this.lineOffsetToPosition(entry.file, span.start); - var end = this.lineOffsetToPosition(entry.file, span.end); - return { - textSpan: ts.createTextSpanFromBounds(start, end), - kind: span.kind - }; - }) + fileName: file, + highlightSpans: highlightSpans.map(convertHighlightSpan2) }; - }); + + function convertHighlightSpan2(span: ts.server.protocol.HighlightSpan): ts.HighlightSpan { + let start = _self.lineOffsetToPosition(file, span.start); + let end = _self.lineOffsetToPosition(file, span.end); + return { + textSpan: ts.createTextSpanFromBounds(start, end), + kind: span.kind + }; + } + } } getOutliningSpans(fileName: string): OutliningSpan[] { diff --git a/src/server/protocol.d.ts b/src/server/protocol.d.ts index 0822f537365..bdabf89a982 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.d.ts @@ -246,13 +246,13 @@ declare namespace ts.server.protocol { export interface DocumentHighlightsRequest extends FileLocationRequest { } - export interface HighLightSpan extends TextSpan { + export interface HighlightSpan extends TextSpan { kind: string } export interface DocumentHighlightsItem { file: string, - highlightSpans: HighLightSpan[]; + highlightSpans: HighlightSpan[]; } export interface DocumentHighlightsResponse extends Response { diff --git a/src/server/session.ts b/src/server/session.ts index c91cf385c0f..8a9c15b2449 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -354,7 +354,7 @@ namespace ts.server { let { compilerService } = project; let position = compilerService.host.lineOffsetToPosition(fileName, line, offset); - let filesToSearch = [fileName]; // only search for highlights inside the current file + let filesToSearch = [ fileName ]; // only search for highlights inside the current file let documentHighlights = compilerService.languageService.getDocumentHighlights(fileName, position, filesToSearch); @@ -362,22 +362,23 @@ namespace ts.server { return undefined; } - return documentHighlights.map(documentHighlight => { // convert ts.DocumentHighlights to ts.server.protocol.DocumentHighlightsItem - var file = documentHighlight.fileName; + return documentHighlights.map(convertToDocumentHighlightsItem); + + function convertToDocumentHighlightsItem(documentHighlights: ts.DocumentHighlights): ts.server.protocol.DocumentHighlightsItem { + let { fileName, highlightSpans } = documentHighlights; + return { - file: file, - highlightSpans: documentHighlight.highlightSpans.map(highlightSpan => { // convert to ts.HighlightSpan to ts.server.protocol.HighlightSpan - let { textSpan, kind } = highlightSpan; - let start = compilerService.host.positionToLineOffset(file, textSpan.start); - let end = compilerService.host.positionToLineOffset(file, ts.textSpanEnd(textSpan)); - return { - start: start, - end: end, - kind: kind - } - }) + file: fileName, + highlightSpans: highlightSpans.map(convertHighlightSpan1) + }; + + function convertHighlightSpan1(highlightSpan: ts.HighlightSpan): ts.server.protocol.HighlightSpan { + let { textSpan, kind } = highlightSpan; + let start = compilerService.host.positionToLineOffset(fileName, textSpan.start); + let end = compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(textSpan)); + return { start, end, kind }; } - }); + } } private getProjectInfo(fileName: string, needFileNameList: boolean): protocol.ProjectInfo { diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index e809834e568..44ce4525754 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -425,7 +425,7 @@ module FourSlashInterface { FourSlash.currentTestState.verifyDocumentHighlightsAtPositionListContains(range.fileName, range.start, range.end, fileNamesToSearch, kind); } - public documentHighlightsAtPositionCount(expectedCount: number, fileNamesToSearch: string[], kind?: string) { + public documentHighlightsAtPositionCount(expectedCount: number, fileNamesToSearch: string[]) { FourSlash.currentTestState.verifyDocumentHighlightsAtPositionListCount(expectedCount, fileNamesToSearch); }