Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 4 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ namespace ts {
}

function visitNode<T>(cbNode: (node: Node) => T, node: Node): T {
if (node) {
if (node !== void 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned it in person, but how does this compare to an always undefined (never passed) function argument? (IE, visitNode<T>(cbNode: (node: Node) => T, node: Node, missing?: undefined): T).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After running some more tests, the difference between implicit casts and void 0 appears to exist entirely within the imagination of the Chrome profiler. When running normally, it doesn't seem to make any difference.

return cbNode(node);
}
}

function visitNodeArray<T>(cbNodes: (nodes: Node[]) => T, nodes: Node[]) {
if (nodes) {
if (nodes !== void 0) {
return cbNodes(nodes);
}
}

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