Skip to content

Commit cadd57c

Browse files
author
steveluc
committed
Change rename response to return the rename info + nested location
information: one array of location information per file. Add interface definition for rename response body.
1 parent d2712dd commit cadd57c

3 files changed

Lines changed: 74 additions & 36 deletions

File tree

src/server/client.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,18 @@ module ts.server {
348348

349349
var request = this.processRequest<ts.server.protocol.RenameRequest>(CommandNames.Rename, args);
350350
var response = this.processResponse<ts.server.protocol.RenameResponse>(request);
351-
351+
var locations: RenameLocation[] = [];
352+
response.body.locs.map((entry: ts.server.protocol.SpanGroup) => {
353+
var fileName = entry.file;
354+
entry.locs.map((loc: ts.server.protocol.TextSpan) => {
355+
var start = this.lineColToPosition(fileName, loc.start);
356+
var end = this.lineColToPosition(fileName, loc.end);
357+
locations.push({
358+
textSpan: ts.createTextSpanFromBounds(start, end),
359+
fileName: fileName
360+
});
361+
});
362+
});
352363
return this.lastRenameEntry = {
353364
canRename: response.body.info.canRename,
354365
displayName: response.body.info.displayName,
@@ -361,15 +372,7 @@ module ts.server {
361372
position: position,
362373
findInStrings: findInStrings,
363374
findInComments: findInComments,
364-
locations: response.body.locs.map((entry) => {
365-
var fileName = entry.file;
366-
var start = this.lineColToPosition(fileName, entry.start);
367-
var end = this.lineColToPosition(fileName, entry.end);
368-
return {
369-
textSpan: ts.createTextSpanFromBounds(start, end),
370-
fileName: fileName
371-
};
372-
})
375+
locations: locations
373376
};
374377
}
375378

src/server/protocol.d.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ declare module ts.server.protocol {
247247
/**
248248
* Error message if item can not be renamed.
249249
*/
250-
localizedErrorMessage: string;
250+
localizedErrorMessage?: string;
251251

252252
/**
253253
* Display name of the item to be renamed.
@@ -270,21 +270,33 @@ declare module ts.server.protocol {
270270
kindModifiers: string;
271271
}
272272

273+
/**
274+
* A group of text spans, all in 'file'.
275+
*/
276+
export interface SpanGroup {
277+
/** The file to which the spans apply */
278+
file: string;
279+
/** The text spans in this group */
280+
locs: TextSpan[];
281+
}
282+
283+
export interface RenameResponseBody {
284+
/**
285+
* Information about the item to be renamed.
286+
*/
287+
info: RenameInfo;
288+
289+
/**
290+
* An array of span groups (one per file) that refer to the item to be renamed.
291+
*/
292+
locs: SpanGroup[];
293+
}
294+
273295
/**
274296
* Rename response message.
275297
*/
276298
export interface RenameResponse extends Response {
277-
body?: {
278-
/**
279-
* Information about the item to be renamed.
280-
*/
281-
info: RenameInfo;
282-
283-
/**
284-
* An array of code locations that refer to the item to be renamed.
285-
*/
286-
locs: CodeSpan[];
287-
}
299+
body?: RenameResponseBody;
288300
}
289301

290302
/**

src/server/session.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module ts.server {
1818
strBuilder += " ";
1919
}
2020
spaceCache[n] = strBuilder;
21-
}
21+
}
2222
return spaceCache[n];
2323
}
2424

@@ -54,6 +54,7 @@ module ts.server {
5454
}
5555

5656
function sortNavItems(items: ts.NavigateToItem[]) {
57+
5758
return items.sort((a, b) => {
5859
if (a.matchKind < b.matchKind) {
5960
return -1;
@@ -77,17 +78,6 @@ module ts.server {
7778
})
7879
}
7980

80-
interface FileRange {
81-
file?: string;
82-
start: ILineInfo;
83-
end: ILineInfo;
84-
}
85-
86-
interface FileRanges {
87-
file: string;
88-
locs: FileRange[];
89-
}
90-
9181
function formatDiag(fileName: string, project: Project, diag: ts.Diagnostic) {
9282
return {
9383
start: project.compilerService.host.positionToLineCol(fileName, diag.start),
@@ -283,7 +273,7 @@ module ts.server {
283273
}));
284274
}
285275

286-
getRenameLocations(line: number, col: number, fileName: string, findInComments: boolean, findInStrings: boolean): { info: RenameInfo; locs: ts.server.protocol.CodeSpan[] } {
276+
getRenameLocations(line: number, col: number, fileName: string,findInComments: boolean, findInStrings: boolean): ts.server.protocol.RenameResponseBody {
287277
var file = ts.normalizePath(fileName);
288278
var project = this.projectService.getProjectForFile(file);
289279
if (!project) {
@@ -313,7 +303,40 @@ module ts.server {
313303
file: location.fileName,
314304
start: compilerService.host.positionToLineCol(location.fileName, location.textSpan.start),
315305
end: compilerService.host.positionToLineCol(location.fileName, ts.textSpanEnd(location.textSpan)),
316-
}));
306+
})).sort((a, b) => {
307+
if (a.file < b.file) {
308+
return -1;
309+
}
310+
else if (a.file > b.file) {
311+
return 1;
312+
}
313+
else {
314+
// reverse sort assuming no overlap
315+
if (a.start.line < b.start.line) {
316+
return 1;
317+
}
318+
else if (a.start.line > b.start.line) {
319+
return -1;
320+
}
321+
else {
322+
return b.start.col - a.start.col;
323+
}
324+
}
325+
}).reduce<ts.server.protocol.SpanGroup[]>((accum: ts.server.protocol.SpanGroup[], cur: ts.server.protocol.CodeSpan) => {
326+
var curFileAccum: ts.server.protocol.SpanGroup;
327+
if (accum.length > 0) {
328+
curFileAccum = accum[accum.length - 1];
329+
if (curFileAccum.file != cur.file) {
330+
curFileAccum = undefined;
331+
}
332+
}
333+
if (!curFileAccum) {
334+
curFileAccum = { file: cur.file, locs: [] };
335+
accum.push(curFileAccum);
336+
}
337+
curFileAccum.locs.push({ start: cur.start, end: cur.end });
338+
return accum;
339+
}, []);
317340

318341
return { info: renameInfo, locs: bakedRenameLocs };
319342
}

0 commit comments

Comments
 (0)