Skip to content

Commit 166bc49

Browse files
author
Andy Hanson
committed
Refactor navigation bar
1 parent 0e6f8eb commit 166bc49

27 files changed

+1380
-1017
lines changed

src/compiler/core.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ namespace ts {
138138
return result;
139139
}
140140

141+
export function filterMutate<T>(array: T[], f: (x: T) => boolean): void {
142+
let outIndex = 0;
143+
for (const item of array) {
144+
if (f(item)) {
145+
array[outIndex] = item;
146+
outIndex++;
147+
}
148+
}
149+
array.length = outIndex;
150+
}
151+
141152
export function map<T, U>(array: T[], f: (x: T) => U): U[] {
142153
let result: U[];
143154
if (array) {

src/compiler/parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ namespace ts {
1717
}
1818

1919
function visitNode<T>(cbNode: (node: Node) => T, node: Node): T {
20-
if (node) {
20+
if (node !== void 0) {
2121
return cbNode(node);
2222
}
2323
}
2424

2525
function visitNodeArray<T>(cbNodes: (nodes: Node[]) => T, nodes: Node[]) {
26-
if (nodes) {
26+
if (nodes !== void 0) {
2727
return cbNodes(nodes);
2828
}
2929
}
3030

3131
function visitEachNode<T>(cbNode: (node: Node) => T, nodes: Node[]) {
32-
if (nodes) {
32+
if (nodes !== void 0) {
3333
for (const node of nodes) {
3434
const result = cbNode(node);
3535
if (result) {
@@ -44,7 +44,7 @@ namespace ts {
4444
// embedded arrays are flattened and the 'cbNode' callback is invoked for each element. If a callback returns
4545
// a truthy value, iteration stops and that value is returned. Otherwise, undefined is returned.
4646
export function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T {
47-
if (!node) {
47+
if (node === void 0) {
4848
return;
4949
}
5050
// The visitXXX functions could be written as local functions that close over the cbNode and cbNodeArray

src/server/client.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ namespace ts.server {
4545
return lineMap;
4646
}
4747

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

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

452-
decodeNavigationBarItems(items: protocol.NavigationBarItem[], fileName: string): NavigationBarItem[] {
453+
decodeNavigationBarItems(items: protocol.NavigationBarItem[], fileName: string, lineMap: number[]): NavigationBarItem[] {
453454
if (!items) {
454455
return [];
455456
}
@@ -458,8 +459,11 @@ namespace ts.server {
458459
text: item.text,
459460
kind: item.kind,
460461
kindModifiers: item.kindModifiers || "",
461-
spans: item.spans.map(span => createTextSpanFromBounds(this.lineOffsetToPosition(fileName, span.start), this.lineOffsetToPosition(fileName, span.end))),
462-
childItems: this.decodeNavigationBarItems(item.childItems, fileName),
462+
spans: item.spans.map(span =>
463+
createTextSpanFromBounds(
464+
this.lineOffsetToPosition(fileName, span.start, lineMap),
465+
this.lineOffsetToPosition(fileName, span.end, lineMap))),
466+
childItems: this.decodeNavigationBarItems(item.childItems, fileName, lineMap),
463467
indent: item.indent,
464468
bolded: false,
465469
grayed: false
@@ -474,7 +478,8 @@ namespace ts.server {
474478
const request = this.processRequest<protocol.NavBarRequest>(CommandNames.NavBar, args);
475479
const response = this.processResponse<protocol.NavBarResponse>(request);
476480

477-
return this.decodeNavigationBarItems(response.body, fileName);
481+
const lineMap = this.getLineMap(fileName);
482+
return this.decodeNavigationBarItems(response.body, fileName, lineMap);
478483
}
479484

480485
getNameOrDottedNameSpan(fileName: string, startPos: number, endPos: number): TextSpan {

src/server/editorServices.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,12 +359,16 @@ namespace ts.server {
359359
* @param line 1-based index
360360
* @param offset 1-based index
361361
*/
362-
positionToLineOffset(filename: string, position: number): ILineInfo {
362+
positionToLineOffset(filename: string, position: number, lineIndex?: LineIndex): ILineInfo {
363+
lineIndex = lineIndex || this.getLineIndex(filename);
364+
const lineOffset = lineIndex.charOffsetToLineNumberAndPos(position);
365+
return { line: lineOffset.line, offset: lineOffset.offset + 1 };
366+
}
367+
368+
getLineIndex(filename: string): LineIndex {
363369
const path = toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName);
364370
const script: ScriptInfo = this.filenameToScript.get(path);
365-
const index = script.snap().index;
366-
const lineOffset = index.charOffsetToLineNumberAndPos(position);
367-
return { line: lineOffset.line, offset: lineOffset.offset + 1 };
371+
return script.snap().index;
368372
}
369373
}
370374

@@ -1452,7 +1456,7 @@ namespace ts.server {
14521456
}
14531457

14541458
// if the project is too large, the root files might not have been all loaded if the total
1455-
// program size reached the upper limit. In that case project.projectOptions.files should
1459+
// program size reached the upper limit. In that case project.projectOptions.files should
14561460
// be more precise. However this would only happen for configured project.
14571461
const oldFileNames = project.projectOptions ? project.projectOptions.files : project.compilerService.host.roots.map(info => info.fileName);
14581462
const newFileNames = ts.filter(projectOptions.files, f => this.host.fileExists(f));

src/server/session.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ namespace ts.server {
863863
this.projectService.closeClientFile(file);
864864
}
865865

866-
private decorateNavigationBarItem(project: Project, fileName: string, items: ts.NavigationBarItem[]): protocol.NavigationBarItem[] {
866+
private decorateNavigationBarItem(project: Project, fileName: string, items: ts.NavigationBarItem[], lineIndex: LineIndex): protocol.NavigationBarItem[] {
867867
if (!items) {
868868
return undefined;
869869
}
@@ -875,10 +875,10 @@ namespace ts.server {
875875
kind: item.kind,
876876
kindModifiers: item.kindModifiers,
877877
spans: item.spans.map(span => ({
878-
start: compilerService.host.positionToLineOffset(fileName, span.start),
879-
end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span))
878+
start: compilerService.host.positionToLineOffset(fileName, span.start, lineIndex),
879+
end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span), lineIndex)
880880
})),
881-
childItems: this.decorateNavigationBarItem(project, fileName, item.childItems),
881+
childItems: this.decorateNavigationBarItem(project, fileName, item.childItems, lineIndex),
882882
indent: item.indent
883883
}));
884884
}
@@ -896,7 +896,7 @@ namespace ts.server {
896896
return undefined;
897897
}
898898

899-
return this.decorateNavigationBarItem(project, fileName, items);
899+
return this.decorateNavigationBarItem(project, fileName, items, compilerService.host.getLineIndex(fileName));
900900
}
901901

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

0 commit comments

Comments
 (0)