Skip to content

Commit

Permalink
Merge pull request microsoft#10885 from Microsoft/navtosinglefile
Browse files Browse the repository at this point in the history
Implement NavigateTo for single files, instead of the project.
  • Loading branch information
paulvanbrenk committed Sep 14, 2016
2 parents 42515c7 + 84caec3 commit e9178a5
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 19 deletions.
11 changes: 6 additions & 5 deletions src/harness/fourslash.ts
Expand Up @@ -2038,11 +2038,12 @@ namespace FourSlash {
}

/*
Check number of navigationItems which match both searchValue and matchKind.
Check number of navigationItems which match both searchValue and matchKind,
if a filename is passed in, limit the results to that file.
Report an error if expected value and actual value do not match.
*/
public verifyNavigationItemsCount(expected: number, searchValue: string, matchKind?: string) {
const items = this.languageService.getNavigateToItems(searchValue);
public verifyNavigationItemsCount(expected: number, searchValue: string, matchKind?: string, fileName?: string) {
const items = this.languageService.getNavigateToItems(searchValue, /*maxResultCount*/ undefined, fileName);
let actual = 0;
let item: ts.NavigateToItem;

Expand Down Expand Up @@ -3170,8 +3171,8 @@ namespace FourSlashInterface {
this.state.verifyNavigationBar(json);
}

public navigationItemsListCount(count: number, searchValue: string, matchKind?: string) {
this.state.verifyNavigationItemsCount(count, searchValue, matchKind);
public navigationItemsListCount(count: number, searchValue: string, matchKind?: string, fileName?: string) {
this.state.verifyNavigationItemsCount(count, searchValue, matchKind, fileName);
}

public navigationItemsListContains(
Expand Down
5 changes: 5 additions & 0 deletions src/server/protocol.d.ts
Expand Up @@ -1141,6 +1141,11 @@ declare namespace ts.server.protocol {
* Optional limit on the number of items to return.
*/
maxResultCount?: number;
/**
* Optional flag to indicate we want results for just the current file
* or the entire project.
*/
currentFileOnly?: boolean;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/server/session.ts
Expand Up @@ -940,7 +940,7 @@ namespace ts.server {
return this.decorateNavigationBarItem(project, fileName, items, compilerService.host.getLineIndex(fileName));
}

private getNavigateToItems(searchValue: string, fileName: string, maxResultCount?: number): protocol.NavtoItem[] {
private getNavigateToItems(searchValue: string, fileName: string, maxResultCount?: number, currentFileOnly?: boolean): protocol.NavtoItem[] {
const file = ts.normalizePath(fileName);
const info = this.projectService.getScriptInfo(file);
const projects = this.projectService.findReferencingProjects(info);
Expand All @@ -953,7 +953,7 @@ namespace ts.server {
projectsWithLanguageServiceEnabeld,
(project: Project) => {
const compilerService = project.compilerService;
const navItems = compilerService.languageService.getNavigateToItems(searchValue, maxResultCount);
const navItems = compilerService.languageService.getNavigateToItems(searchValue, maxResultCount, currentFileOnly ? fileName : undefined);
if (!navItems) {
return [];
}
Expand Down Expand Up @@ -1188,7 +1188,7 @@ namespace ts.server {
},
[CommandNames.Navto]: (request: protocol.Request) => {
const navtoArgs = <protocol.NavtoRequestArgs>request.arguments;
return { response: this.getNavigateToItems(navtoArgs.searchValue, navtoArgs.file, navtoArgs.maxResultCount), responseRequired: true };
return { response: this.getNavigateToItems(navtoArgs.searchValue, navtoArgs.file, navtoArgs.maxResultCount, navtoArgs.currentFileOnly), responseRequired: true };
},
[CommandNames.Brace]: (request: protocol.Request) => {
const braceArguments = <protocol.FileLocationRequestArgs>request.arguments;
Expand Down
4 changes: 2 additions & 2 deletions src/services/navigateTo.ts
Expand Up @@ -2,15 +2,15 @@
namespace ts.NavigateTo {
type RawNavigateToItem = { name: string; fileName: string; matchKind: PatternMatchKind; isCaseSensitive: boolean; declaration: Declaration };

export function getNavigateToItems(program: Program, checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[] {
export function getNavigateToItems(sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[] {
const patternMatcher = createPatternMatcher(searchValue);
let rawItems: RawNavigateToItem[] = [];

// This means "compare in a case insensitive manner."
const baseSensitivity: Intl.CollatorOptions = { sensitivity: "base" };

// Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[]
forEach(program.getSourceFiles(), sourceFile => {
forEach(sourceFiles, sourceFile => {
cancellationToken.throwIfCancellationRequested();

const nameToDeclarations = sourceFile.getNamedDeclarations();
Expand Down
7 changes: 4 additions & 3 deletions src/services/services.ts
Expand Up @@ -1347,10 +1347,11 @@ namespace ts {
}

/// NavigateTo
function getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[] {
function getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): NavigateToItem[] {
synchronizeHostData();
const checker = getProgram().getTypeChecker();
return ts.NavigateTo.getNavigateToItems(program, checker, cancellationToken, searchValue, maxResultCount);

const sourceFiles = fileName ? [getValidSourceFile(fileName)] : program.getSourceFiles();
return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount);
}

function getEmitOutput(fileName: string): EmitOutput {
Expand Down
8 changes: 4 additions & 4 deletions src/services/shims.ts
Expand Up @@ -209,7 +209,7 @@ namespace ts {
* Returns a JSON-encoded value of the type:
* { name: string; kind: string; kindModifiers: string; containerName: string; containerKind: string; matchKind: string; fileName: string; textSpan: { start: number; length: number}; } [] = [];
*/
getNavigateToItems(searchValue: string, maxResultCount?: number): string;
getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): string;

/**
* Returns a JSON-encoded value of the type:
Expand Down Expand Up @@ -930,10 +930,10 @@ namespace ts {
/// NAVIGATE TO

/** Return a list of symbols that are interesting to navigate to */
public getNavigateToItems(searchValue: string, maxResultCount?: number): string {
public getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): string {
return this.forwardJSONCall(
`getNavigateToItems('${searchValue}', ${maxResultCount})`,
() => this.languageService.getNavigateToItems(searchValue, maxResultCount)
`getNavigateToItems('${searchValue}', ${maxResultCount}, ${fileName})`,
() => this.languageService.getNavigateToItems(searchValue, maxResultCount, fileName)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Expand Up @@ -217,7 +217,7 @@ namespace ts {
/** @deprecated */
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];

getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): NavigateToItem[];
getNavigationBarItems(fileName: string): NavigationBarItem[];

getOutliningSpans(fileName: string): OutliningSpan[];
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/fourslash.ts
Expand Up @@ -208,7 +208,7 @@ declare namespace FourSlashInterface {
noDocCommentTemplate(): void;

navigationBar(json: any): void;
navigationItemsListCount(count: number, searchValue: string, matchKind?: string): void;
navigationItemsListCount(count: number, searchValue: string, matchKind?: string, fileName?: string): void;
navigationItemsListContains(name: string, kind: string, searchValue: string, matchKind: string, fileName?: string, parentName?: string): void;
occurrencesAtPositionContains(range: Range, isWriteAccess?: boolean): void;
occurrencesAtPositionCount(expectedCount: number): void;
Expand Down
19 changes: 19 additions & 0 deletions tests/cases/fourslash/navigateToSingleFileResults.ts
@@ -0,0 +1,19 @@
/// <reference path="fourslash.ts" />

// @Filename: file1.ts
/////*1*/class Greeter {
//// public hello(name: string) { }
////}
////var x = new Greeter();
// @Filename: file2.ts
/////*2*/class MyGreeter {
//// public hello(name: string) { }
////}
////class MyOtherGreeter {
//// public hello(name: string) { }
////}

verify.navigationItemsListCount(3, "hello");
verify.navigationItemsListCount(1, "hello", undefined, test.marker("1").fileName);
verify.navigationItemsListContains("hello", "method", "hello", "exact", test.marker("1").fileName);
verify.navigationItemsListCount(2, "hello", undefined, test.marker("2").fileName);

0 comments on commit e9178a5

Please sign in to comment.