mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-12 20:01:02 -05:00
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.
This commit is contained in:
@@ -348,7 +348,18 @@ module ts.server {
|
||||
|
||||
var request = this.processRequest<ts.server.protocol.RenameRequest>(CommandNames.Rename, args);
|
||||
var response = this.processResponse<ts.server.protocol.RenameResponse>(request);
|
||||
|
||||
var locations: RenameLocation[] = [];
|
||||
response.body.locs.map((entry: ts.server.protocol.SpanGroup) => {
|
||||
var fileName = entry.file;
|
||||
entry.locs.map((loc: ts.server.protocol.TextSpan) => {
|
||||
var start = this.lineColToPosition(fileName, loc.start);
|
||||
var end = this.lineColToPosition(fileName, loc.end);
|
||||
locations.push({
|
||||
textSpan: ts.createTextSpanFromBounds(start, end),
|
||||
fileName: fileName
|
||||
});
|
||||
});
|
||||
});
|
||||
return this.lastRenameEntry = {
|
||||
canRename: response.body.info.canRename,
|
||||
displayName: response.body.info.displayName,
|
||||
@@ -361,15 +372,7 @@ module ts.server {
|
||||
position: position,
|
||||
findInStrings: findInStrings,
|
||||
findInComments: findInComments,
|
||||
locations: response.body.locs.map((entry) => {
|
||||
var fileName = entry.file;
|
||||
var start = this.lineColToPosition(fileName, entry.start);
|
||||
var end = this.lineColToPosition(fileName, entry.end);
|
||||
return {
|
||||
textSpan: ts.createTextSpanFromBounds(start, end),
|
||||
fileName: fileName
|
||||
};
|
||||
})
|
||||
locations: locations
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
36
src/server/protocol.d.ts
vendored
36
src/server/protocol.d.ts
vendored
@@ -247,7 +247,7 @@ declare module ts.server.protocol {
|
||||
/**
|
||||
* Error message if item can not be renamed.
|
||||
*/
|
||||
localizedErrorMessage: string;
|
||||
localizedErrorMessage?: string;
|
||||
|
||||
/**
|
||||
* Display name of the item to be renamed.
|
||||
@@ -270,21 +270,33 @@ declare module ts.server.protocol {
|
||||
kindModifiers: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A group of text spans, all in 'file'.
|
||||
*/
|
||||
export interface SpanGroup {
|
||||
/** The file to which the spans apply */
|
||||
file: string;
|
||||
/** The text spans in this group */
|
||||
locs: TextSpan[];
|
||||
}
|
||||
|
||||
export interface RenameResponseBody {
|
||||
/**
|
||||
* Information about the item to be renamed.
|
||||
*/
|
||||
info: RenameInfo;
|
||||
|
||||
/**
|
||||
* An array of span groups (one per file) that refer to the item to be renamed.
|
||||
*/
|
||||
locs: SpanGroup[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Rename response message.
|
||||
*/
|
||||
export interface RenameResponse extends Response {
|
||||
body?: {
|
||||
/**
|
||||
* Information about the item to be renamed.
|
||||
*/
|
||||
info: RenameInfo;
|
||||
|
||||
/**
|
||||
* An array of code locations that refer to the item to be renamed.
|
||||
*/
|
||||
locs: CodeSpan[];
|
||||
}
|
||||
body?: RenameResponseBody;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,7 +18,7 @@ module ts.server {
|
||||
strBuilder += " ";
|
||||
}
|
||||
spaceCache[n] = strBuilder;
|
||||
}
|
||||
}
|
||||
return spaceCache[n];
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ module ts.server {
|
||||
}
|
||||
|
||||
function sortNavItems(items: ts.NavigateToItem[]) {
|
||||
|
||||
return items.sort((a, b) => {
|
||||
if (a.matchKind < b.matchKind) {
|
||||
return -1;
|
||||
@@ -77,17 +78,6 @@ module ts.server {
|
||||
})
|
||||
}
|
||||
|
||||
interface FileRange {
|
||||
file?: string;
|
||||
start: ILineInfo;
|
||||
end: ILineInfo;
|
||||
}
|
||||
|
||||
interface FileRanges {
|
||||
file: string;
|
||||
locs: FileRange[];
|
||||
}
|
||||
|
||||
function formatDiag(fileName: string, project: Project, diag: ts.Diagnostic) {
|
||||
return {
|
||||
start: project.compilerService.host.positionToLineCol(fileName, diag.start),
|
||||
@@ -283,7 +273,7 @@ module ts.server {
|
||||
}));
|
||||
}
|
||||
|
||||
getRenameLocations(line: number, col: number, fileName: string, findInComments: boolean, findInStrings: boolean): { info: RenameInfo; locs: ts.server.protocol.CodeSpan[] } {
|
||||
getRenameLocations(line: number, col: number, fileName: string,findInComments: boolean, findInStrings: boolean): ts.server.protocol.RenameResponseBody {
|
||||
var file = ts.normalizePath(fileName);
|
||||
var project = this.projectService.getProjectForFile(file);
|
||||
if (!project) {
|
||||
@@ -313,7 +303,40 @@ module ts.server {
|
||||
file: location.fileName,
|
||||
start: compilerService.host.positionToLineCol(location.fileName, location.textSpan.start),
|
||||
end: compilerService.host.positionToLineCol(location.fileName, ts.textSpanEnd(location.textSpan)),
|
||||
}));
|
||||
})).sort((a, b) => {
|
||||
if (a.file < b.file) {
|
||||
return -1;
|
||||
}
|
||||
else if (a.file > b.file) {
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
// reverse sort assuming no overlap
|
||||
if (a.start.line < b.start.line) {
|
||||
return 1;
|
||||
}
|
||||
else if (a.start.line > b.start.line) {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
return b.start.col - a.start.col;
|
||||
}
|
||||
}
|
||||
}).reduce<ts.server.protocol.SpanGroup[]>((accum: ts.server.protocol.SpanGroup[], cur: ts.server.protocol.CodeSpan) => {
|
||||
var curFileAccum: ts.server.protocol.SpanGroup;
|
||||
if (accum.length > 0) {
|
||||
curFileAccum = accum[accum.length - 1];
|
||||
if (curFileAccum.file != cur.file) {
|
||||
curFileAccum = undefined;
|
||||
}
|
||||
}
|
||||
if (!curFileAccum) {
|
||||
curFileAccum = { file: cur.file, locs: [] };
|
||||
accum.push(curFileAccum);
|
||||
}
|
||||
curFileAccum.locs.push({ start: cur.start, end: cur.end });
|
||||
return accum;
|
||||
}, []);
|
||||
|
||||
return { info: renameInfo, locs: bakedRenameLocs };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user