Skip to content

Commit 8550c7d

Browse files
author
Tien Nguyen
committed
Address feedback.
1 parent 67d986a commit 8550c7d

File tree

5 files changed

+54
-48
lines changed

5 files changed

+54
-48
lines changed

src/harness/fourslash.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,15 +2166,17 @@ module FourSlash {
21662166
this.raiseError('verifyDocumentHighlightsAtPositionListContains failed - found 0 highlights, expected at least one.');
21672167
}
21682168

2169-
for (let i = 0; i < documentHighlights.length; i++) if (documentHighlights[i].fileName === fileName) {
2170-
let { highlightSpans } = documentHighlights[i];
2171-
2172-
for (let highlight of highlightSpans) {
2173-
if (highlight && highlight.textSpan.start === start && ts.textSpanEnd(highlight.textSpan) === end) {
2174-
if (typeof kind !== "undefined" && highlight.kind !== kind) {
2175-
this.raiseError('verifyDocumentHighlightsAtPositionListContains failed - item "kind" value does not match, actual: ' + highlight.kind + ', expected: ' + kind + '.');
2169+
for (let documentHighlight of documentHighlights) {
2170+
if (documentHighlight.fileName === fileName) {
2171+
let { highlightSpans } = documentHighlight;
2172+
2173+
for (let highlight of highlightSpans) {
2174+
if (highlight && highlight.textSpan.start === start && ts.textSpanEnd(highlight.textSpan) === end) {
2175+
if (typeof kind !== "undefined" && highlight.kind !== kind) {
2176+
this.raiseError('verifyDocumentHighlightsAtPositionListContains failed - item "kind" value does not match, actual: ' + highlight.kind + ', expected: ' + kind + '.');
2177+
}
2178+
return;
21762179
}
2177-
return;
21782180
}
21792181
}
21802182
}
@@ -2187,9 +2189,9 @@ module FourSlash {
21872189
this.taoInvalidReason = 'verifyDocumentHighlightsAtPositionListCount NYI';
21882190

21892191
let documentHighlights = this.getDocumentHighlightsAtCurrentPosition(fileNamesToSearch);
2190-
let actualCount = documentHighlights
2191-
? documentHighlights.reduce((currentCount, currentDocumentHighlights) => {
2192-
return currentCount + currentDocumentHighlights.highlightSpans.length}, 0)
2192+
let actualCount = documentHighlights
2193+
? documentHighlights.reduce((currentCount, { fileName, highlightSpans }) => {
2194+
return currentCount + highlightSpans.length}, 0)
21932195
: 0;
21942196

21952197
if (expectedCount !== actualCount) {

src/server/client.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -528,29 +528,32 @@ namespace ts.server {
528528
}
529529

530530
getDocumentHighlights(fileName: string, position: number): DocumentHighlights[] {
531-
var lineOffset = this.positionToOneBasedLineOffset(fileName, position);
532-
var args: protocol.FileLocationRequestArgs = {
533-
file: fileName,
534-
line: lineOffset.line,
535-
offset: lineOffset.offset,
536-
};
531+
let { line, offset } = this.positionToOneBasedLineOffset(fileName, position);
532+
let args: protocol.FileLocationRequestArgs = { file: fileName, line, offset };
537533

538-
var request = this.processRequest<protocol.DocumentHighlightsRequest>(CommandNames.DocumentHighlights, args);
539-
var response = this.processResponse<protocol.DocumentHighlightsResponse>(request);
534+
let request = this.processRequest<protocol.DocumentHighlightsRequest>(CommandNames.DocumentHighlights, args);
535+
let response = this.processResponse<protocol.DocumentHighlightsResponse>(request);
536+
537+
let _self = this;
538+
return response.body.map(convertToDocumentHighlights);
539+
540+
function convertToDocumentHighlights(item: ts.server.protocol.DocumentHighlightsItem): ts.DocumentHighlights {
541+
let { file, highlightSpans } = item;
540542

541-
return response.body.map(entry => { // convert ts.server.protocol.DocumentHighlightsItem to ts.DocumentHighlights
542543
return {
543-
fileName: entry.file,
544-
highlightSpans: entry.highlightSpans.map(span => { // convert ts.server.protocol.HighlightSpan to ts.HighlighSpan
545-
var start = this.lineOffsetToPosition(entry.file, span.start);
546-
var end = this.lineOffsetToPosition(entry.file, span.end);
547-
return {
548-
textSpan: ts.createTextSpanFromBounds(start, end),
549-
kind: span.kind
550-
};
551-
})
544+
fileName: file,
545+
highlightSpans: highlightSpans.map(convertHighlightSpan2)
552546
};
553-
});
547+
548+
function convertHighlightSpan2(span: ts.server.protocol.HighlightSpan): ts.HighlightSpan {
549+
let start = _self.lineOffsetToPosition(file, span.start);
550+
let end = _self.lineOffsetToPosition(file, span.end);
551+
return {
552+
textSpan: ts.createTextSpanFromBounds(start, end),
553+
kind: span.kind
554+
};
555+
}
556+
}
554557
}
555558

556559
getOutliningSpans(fileName: string): OutliningSpan[] {

src/server/protocol.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,13 @@ declare namespace ts.server.protocol {
246246
export interface DocumentHighlightsRequest extends FileLocationRequest {
247247
}
248248

249-
export interface HighLightSpan extends TextSpan {
249+
export interface HighlightSpan extends TextSpan {
250250
kind: string
251251
}
252252

253253
export interface DocumentHighlightsItem {
254254
file: string,
255-
highlightSpans: HighLightSpan[];
255+
highlightSpans: HighlightSpan[];
256256
}
257257

258258
export interface DocumentHighlightsResponse extends Response {

src/server/session.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -354,30 +354,31 @@ namespace ts.server {
354354

355355
let { compilerService } = project;
356356
let position = compilerService.host.lineOffsetToPosition(fileName, line, offset);
357-
let filesToSearch = [fileName]; // only search for highlights inside the current file
357+
let filesToSearch = [ fileName ]; // only search for highlights inside the current file
358358

359359
let documentHighlights = compilerService.languageService.getDocumentHighlights(fileName, position, filesToSearch);
360360

361361
if (!documentHighlights) {
362362
return undefined;
363363
}
364364

365-
return documentHighlights.map(documentHighlight => { // convert ts.DocumentHighlights to ts.server.protocol.DocumentHighlightsItem
366-
var file = documentHighlight.fileName;
365+
return documentHighlights.map(convertToDocumentHighlightsItem);
366+
367+
function convertToDocumentHighlightsItem(documentHighlights: ts.DocumentHighlights): ts.server.protocol.DocumentHighlightsItem {
368+
let { fileName, highlightSpans } = documentHighlights;
369+
367370
return {
368-
file: file,
369-
highlightSpans: documentHighlight.highlightSpans.map(highlightSpan => { // convert to ts.HighlightSpan to ts.server.protocol.HighlightSpan
370-
let { textSpan, kind } = highlightSpan;
371-
let start = compilerService.host.positionToLineOffset(file, textSpan.start);
372-
let end = compilerService.host.positionToLineOffset(file, ts.textSpanEnd(textSpan));
373-
return {
374-
start: start,
375-
end: end,
376-
kind: kind
377-
}
378-
})
371+
file: fileName,
372+
highlightSpans: highlightSpans.map(convertHighlightSpan1)
373+
};
374+
375+
function convertHighlightSpan1(highlightSpan: ts.HighlightSpan): ts.server.protocol.HighlightSpan {
376+
let { textSpan, kind } = highlightSpan;
377+
let start = compilerService.host.positionToLineOffset(fileName, textSpan.start);
378+
let end = compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(textSpan));
379+
return { start, end, kind };
379380
}
380-
});
381+
}
381382
}
382383

383384
private getProjectInfo(fileName: string, needFileNameList: boolean): protocol.ProjectInfo {

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ module FourSlashInterface {
425425
FourSlash.currentTestState.verifyDocumentHighlightsAtPositionListContains(range.fileName, range.start, range.end, fileNamesToSearch, kind);
426426
}
427427

428-
public documentHighlightsAtPositionCount(expectedCount: number, fileNamesToSearch: string[], kind?: string) {
428+
public documentHighlightsAtPositionCount(expectedCount: number, fileNamesToSearch: string[]) {
429429
FourSlash.currentTestState.verifyDocumentHighlightsAtPositionListCount(expectedCount, fileNamesToSearch);
430430
}
431431

0 commit comments

Comments
 (0)