Skip to content

Refactor navigation bar #9220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ namespace ts {
return result;
}

export function filterMutate<T>(array: T[], f: (x: T) => boolean): void {
let outIndex = 0;
for (const item of array) {
if (f(item)) {
array[outIndex] = item;
outIndex++;
}
}
array.length = outIndex;
}

export function map<T, U>(array: T[], f: (x: T) => U): U[] {
let result: U[];
if (array) {
Expand Down
17 changes: 11 additions & 6 deletions src/server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ namespace ts.server {
return lineMap;
}

private lineOffsetToPosition(fileName: string, lineOffset: protocol.Location): number {
return ts.computePositionOfLineAndCharacter(this.getLineMap(fileName), lineOffset.line - 1, lineOffset.offset - 1);
private lineOffsetToPosition(fileName: string, lineOffset: protocol.Location, lineMap?: number[]): number {
lineMap = lineMap || this.getLineMap(fileName);
return ts.computePositionOfLineAndCharacter(lineMap, lineOffset.line - 1, lineOffset.offset - 1);
}

private positionToOneBasedLineOffset(fileName: string, position: number): protocol.Location {
Expand Down Expand Up @@ -449,7 +450,7 @@ namespace ts.server {
return this.lastRenameEntry.locations;
}

decodeNavigationBarItems(items: protocol.NavigationBarItem[], fileName: string): NavigationBarItem[] {
decodeNavigationBarItems(items: protocol.NavigationBarItem[], fileName: string, lineMap: number[]): NavigationBarItem[] {
if (!items) {
return [];
}
Expand All @@ -458,8 +459,11 @@ namespace ts.server {
text: item.text,
kind: item.kind,
kindModifiers: item.kindModifiers || "",
spans: item.spans.map(span => createTextSpanFromBounds(this.lineOffsetToPosition(fileName, span.start), this.lineOffsetToPosition(fileName, span.end))),
childItems: this.decodeNavigationBarItems(item.childItems, fileName),
spans: item.spans.map(span =>
createTextSpanFromBounds(
this.lineOffsetToPosition(fileName, span.start, lineMap),
this.lineOffsetToPosition(fileName, span.end, lineMap))),
childItems: this.decodeNavigationBarItems(item.childItems, fileName, lineMap),
indent: item.indent,
bolded: false,
grayed: false
Expand All @@ -474,7 +478,8 @@ namespace ts.server {
const request = this.processRequest<protocol.NavBarRequest>(CommandNames.NavBar, args);
const response = this.processResponse<protocol.NavBarResponse>(request);

return this.decodeNavigationBarItems(response.body, fileName);
const lineMap = this.getLineMap(fileName);
return this.decodeNavigationBarItems(response.body, fileName, lineMap);
}

getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): TextSpan {
Expand Down
14 changes: 9 additions & 5 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,16 @@ namespace ts.server {
* @param line 1-based index
* @param offset 1-based index
*/
positionToLineOffset(filename: string, position: number): ILineInfo {
positionToLineOffset(filename: string, position: number, lineIndex?: LineIndex): ILineInfo {
lineIndex = lineIndex || this.getLineIndex(filename);
const lineOffset = lineIndex.charOffsetToLineNumberAndPos(position);
return { line: lineOffset.line, offset: lineOffset.offset + 1 };
}

getLineIndex(filename: string): LineIndex {
const path = toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName);
const script: ScriptInfo = this.filenameToScript.get(path);
const index = script.snap().index;
const lineOffset = index.charOffsetToLineNumberAndPos(position);
return { line: lineOffset.line, offset: lineOffset.offset + 1 };
return script.snap().index;
}
}

Expand Down Expand Up @@ -1452,7 +1456,7 @@ namespace ts.server {
}

// if the project is too large, the root files might not have been all loaded if the total
// program size reached the upper limit. In that case project.projectOptions.files should
// program size reached the upper limit. In that case project.projectOptions.files should
// be more precise. However this would only happen for configured project.
const oldFileNames = project.projectOptions ? project.projectOptions.files : project.compilerService.host.roots.map(info => info.fileName);
const newFileNames = ts.filter(projectOptions.files, f => this.host.fileExists(f));
Expand Down
10 changes: 5 additions & 5 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ namespace ts.server {
this.projectService.closeClientFile(file);
}

private decorateNavigationBarItem(project: Project, fileName: string, items: ts.NavigationBarItem[]): protocol.NavigationBarItem[] {
private decorateNavigationBarItem(project: Project, fileName: string, items: ts.NavigationBarItem[], lineIndex: LineIndex): protocol.NavigationBarItem[] {
if (!items) {
return undefined;
}
Expand All @@ -875,10 +875,10 @@ namespace ts.server {
kind: item.kind,
kindModifiers: item.kindModifiers,
spans: item.spans.map(span => ({
start: compilerService.host.positionToLineOffset(fileName, span.start),
end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span))
start: compilerService.host.positionToLineOffset(fileName, span.start, lineIndex),
end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span), lineIndex)
})),
childItems: this.decorateNavigationBarItem(project, fileName, item.childItems),
childItems: this.decorateNavigationBarItem(project, fileName, item.childItems, lineIndex),
indent: item.indent
}));
}
Expand All @@ -896,7 +896,7 @@ namespace ts.server {
return undefined;
}

return this.decorateNavigationBarItem(project, fileName, items);
return this.decorateNavigationBarItem(project, fileName, items, compilerService.host.getLineIndex(fileName));
}

private getNavigateToItems(searchValue: string, fileName: string, maxResultCount?: number): protocol.NavtoItem[] {
Expand Down
Loading