Skip to content

Commit 4eb8c73

Browse files
Merge pull request #2666 from Microsoft/occurrencesOnServer
Support getOccurrences on the TS Server
2 parents 21a5611 + 180f17d commit 4eb8c73

File tree

17 files changed

+170
-81
lines changed

17 files changed

+170
-81
lines changed

src/server/client.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ module ts.server {
104104
var response: T = JSON.parse(responseBody);
105105
}
106106
catch (e) {
107-
throw new Error("Malformed response: Failed to parse server response: " + lastMessage + ". \r\n Error detailes: " + e.message);
107+
throw new Error("Malformed response: Failed to parse server response: " + lastMessage + ". \r\n Error details: " + e.message);
108108
}
109109

110110
// verify the sequence numbers
@@ -446,6 +446,7 @@ module ts.server {
446446
if (!response.body) {
447447
return undefined;
448448
}
449+
449450
var helpItems: protocol.SignatureHelpItems = response.body;
450451
var span = helpItems.applicableSpan;
451452
var start = this.lineOffsetToPosition(fileName, span.start);
@@ -465,7 +466,26 @@ module ts.server {
465466
}
466467

467468
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[] {
468-
throw new Error("Not Implemented Yet.");
469+
var lineOffset = this.positionToOneBasedLineOffset(fileName, position);
470+
var args: protocol.FileLocationRequestArgs = {
471+
file: fileName,
472+
line: lineOffset.line,
473+
offset: lineOffset.offset,
474+
};
475+
476+
var request = this.processRequest<protocol.OccurrencesRequest>(CommandNames.Occurrences, args);
477+
var response = this.processResponse<protocol.OccurrencesResponse>(request);
478+
479+
return response.body.map(entry => {
480+
var fileName = entry.file;
481+
var start = this.lineOffsetToPosition(fileName, entry.start);
482+
var end = this.lineOffsetToPosition(fileName, entry.end);
483+
return {
484+
fileName,
485+
textSpan: ts.createTextSpanFromBounds(start, end),
486+
isWriteAccess: entry.isWriteAccess,
487+
};
488+
});
469489
}
470490

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

src/server/protocol.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,25 @@ declare module ts.server.protocol {
165165
body?: FileSpan[];
166166
}
167167

168+
/**
169+
* Get occurrences request; value of command field is
170+
* "occurrences". Return response giving spans that are relevant
171+
* in the file at a given line and column.
172+
*/
173+
export interface OccurrencesRequest extends FileLocationRequest {
174+
}
175+
176+
export interface OccurrencesResponseItem extends FileSpan {
177+
/**
178+
* True if the occurrence is a write location, false otherwise.
179+
*/
180+
isWriteAccess: boolean;
181+
}
182+
183+
export interface OccurrencesResponse extends Response {
184+
body?: OccurrencesResponseItem[];
185+
}
186+
168187
/**
169188
* Find references request; value of command field is
170189
* "references". Return response giving the file locations that

src/server/session.ts

Lines changed: 79 additions & 67 deletions
Large diffs are not rendered by default.

tests/cases/fourslash/server/brace.ts renamed to tests/cases/fourslash/server/brace01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="fourslash.ts"/>
1+
/// <reference path="../fourslash.ts"/>
22

33
//////curly braces
44
////module Foo [|{

tests/cases/fourslash/server/completions.ts renamed to tests/cases/fourslash/server/completions01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path='fourslash.ts'/>
1+
/// <reference path="../fourslash.ts"/>
22

33
////var x: string[] = [];
44
////x.forEach(function (y) { y/*1*/

tests/cases/fourslash/server/completions2.ts renamed to tests/cases/fourslash/server/completions02.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="fourslash.ts" />
1+
/// <reference path="../fourslash.ts"/>
22

33
////class Foo {
44
////}

tests/cases/fourslash/server/definition.ts renamed to tests/cases/fourslash/server/definition01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path='fourslash.ts'/>
1+
/// <reference path="../fourslash.ts"/>
22

33
// @Filename: b.ts
44
////import n = require('a/*1*/');

tests/cases/fourslash/server/format.ts renamed to tests/cases/fourslash/server/format01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path='fourslash.ts' />
1+
/// <reference path="../fourslash.ts"/>
22

33
/////**/module Default{var x= ( { } ) ;}
44

tests/cases/fourslash/server/formatonkey.ts renamed to tests/cases/fourslash/server/formatonkey01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path='fourslash.ts' />
1+
/// <reference path="../fourslash.ts"/>
22

33
////switch (1) {
44
//// case 1:

tests/cases/fourslash/server/navbar.ts renamed to tests/cases/fourslash/server/navbar01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="fourslash.ts"/>
1+
/// <reference path="../fourslash.ts"/>
22

33
////// Interface
44
////{| "itemName": "IPoint", "kind": "interface", "parentName": "" |}interface IPoint {

0 commit comments

Comments
 (0)