Skip to content
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

Smart Select language service API #31028

Merged
merged 26 commits into from May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
af3d0f0
Start smart select API
andrewbranch Apr 9, 2019
f98c00a
Add more tests, special handling for mapped types
andrewbranch Apr 9, 2019
e62c233
Add support for string literals
andrewbranch Apr 10, 2019
fd88e52
Start imports
andrewbranch Apr 11, 2019
039487c
Also skip TemplateHeads
andrewbranch Apr 11, 2019
0a4ef0f
Distinguish between same-line and different-line braces
andrewbranch Apr 11, 2019
61425cb
Move most logic to separate file
andrewbranch Apr 11, 2019
0f7bc02
Move to language service
andrewbranch Apr 12, 2019
70e2672
Add rules for expanding selection to sibling nodes
andrewbranch Apr 12, 2019
fcb7f01
Rethink sibling expansion by creating fake subtrees
andrewbranch Apr 14, 2019
4ecdc82
Solidify fake tree approach
andrewbranch Apr 15, 2019
74fc84f
Snap to nodes directly behind the cursor, create special rules for Pa…
andrewbranch Apr 16, 2019
d73eabd
Special rules for binding elements, extend brace/whitespace logic to …
andrewbranch Apr 16, 2019
fed910f
Add stop for JSDoc comments
andrewbranch Apr 18, 2019
5479893
Skip lone variable declarations
andrewbranch Apr 18, 2019
f0f7d82
Remove debug info
andrewbranch Apr 18, 2019
d8936fd
Rename to be smarter
andrewbranch Apr 18, 2019
12492a3
Rename test to match
andrewbranch Apr 18, 2019
511cc79
Revert accidental line break added
andrewbranch Apr 19, 2019
99ace03
Revert accidental line ending change
andrewbranch Apr 19, 2019
6177596
Revert accidental submodule change I guess
andrewbranch Apr 19, 2019
f0a3d2b
Filter out zero-width selections
andrewbranch Apr 19, 2019
6fc2e4a
Add custom baseline format for smart selection
andrewbranch Apr 23, 2019
e28b9b2
Copy smartSelect tests to fourslash
andrewbranch Apr 23, 2019
3e30a7c
Remove all but one server unit test
andrewbranch Apr 23, 2019
eff3960
Fix baseline file name changes
andrewbranch Apr 24, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/harness/client.ts
Expand Up @@ -424,6 +424,10 @@ namespace ts.server {
return renameInfo;
}

getSmartSelectionRange() {
return notImplemented();
}

findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] {
if (!this.lastRenameEntry ||
this.lastRenameEntry.inputs.fileName !== fileName ||
Expand Down
3 changes: 3 additions & 0 deletions src/harness/harnessLanguageService.ts
Expand Up @@ -472,6 +472,9 @@ namespace Harness.LanguageService {
getRenameInfo(fileName: string, position: number, options?: ts.RenameInfoOptions): ts.RenameInfo {
return unwrapJSONCallResult(this.shim.getRenameInfo(fileName, position, options));
}
getSmartSelectionRange(fileName: string, position: number): ts.SelectionRange {
return unwrapJSONCallResult(this.shim.getSmartSelectionRange(fileName, position));
}
findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, providePrefixAndSuffixTextForRename?: boolean): ts.RenameLocation[] {
return unwrapJSONCallResult(this.shim.findRenameLocations(fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename));
}
Expand Down
23 changes: 22 additions & 1 deletion src/server/protocol.ts
Expand Up @@ -130,7 +130,10 @@ namespace ts.server.protocol {
GetEditsForFileRename = "getEditsForFileRename",
/* @internal */
GetEditsForFileRenameFull = "getEditsForFileRename-full",
ConfigurePlugin = "configurePlugin"
ConfigurePlugin = "configurePlugin",
SelectionRange = "selectionRange",
/* @internal */
SelectionRangeFull = "selectionRange-full",
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved

// NOTE: If updating this, be sure to also update `allCommandNames` in `harness/unittests/session.ts`.
}
Expand Down Expand Up @@ -1395,6 +1398,24 @@ namespace ts.server.protocol {
export interface ConfigurePluginResponse extends Response {
}

export interface SelectionRangeRequest extends FileRequest {
command: CommandTypes.SelectionRange;
arguments: SelectionRangeRequestArgs;
}

export interface SelectionRangeRequestArgs extends FileRequestArgs {
locations: Location[];
}

export interface SelectionRangeResponse extends Response {
body?: SelectionRange[];
}

export interface SelectionRange {
textSpan: TextSpan;
parent?: SelectionRange;
}

/**
* Information found in an "open" request.
*/
Expand Down
34 changes: 31 additions & 3 deletions src/server/session.ts
Expand Up @@ -1318,11 +1318,11 @@ namespace ts.server {
this.projectService.openClientFileWithNormalizedPath(fileName, fileContent, scriptKind, /*hasMixedContent*/ false, projectRootPath);
}

private getPosition(args: protocol.FileLocationRequestArgs, scriptInfo: ScriptInfo): number {
private getPosition(args: protocol.Location & { position?: number }, scriptInfo: ScriptInfo): number {
return args.position !== undefined ? args.position : scriptInfo.lineOffsetToPosition(args.line, args.offset);
}

private getPositionInFile(args: protocol.FileLocationRequestArgs, file: NormalizedPath): number {
private getPositionInFile(args: protocol.Location & { position?: number }, file: NormalizedPath): number {
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!;
return this.getPosition(args, scriptInfo);
}
Expand Down Expand Up @@ -2059,6 +2059,28 @@ namespace ts.server {
this.projectService.configurePlugin(args);
}

private getSmartSelectionRange(args: protocol.SelectionRangeRequestArgs, simplifiedResult: boolean) {
const { locations } = args;
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
const scriptInfo = Debug.assertDefined(this.projectService.getScriptInfo(file));

return map(locations, location => {
const pos = this.getPosition(location, scriptInfo);
const selectionRange = languageService.getSmartSelectionRange(file, pos);
return simplifiedResult ? this.mapSelectionRange(selectionRange, scriptInfo) : selectionRange;
});
}

private mapSelectionRange(selectionRange: SelectionRange, scriptInfo: ScriptInfo): protocol.SelectionRange {
const result: protocol.SelectionRange = {
textSpan: this.toLocationTextSpan(selectionRange.textSpan, scriptInfo),
};
if (selectionRange.parent) {
result.parent = this.mapSelectionRange(selectionRange.parent, scriptInfo);
}
return result;
}

getCanonicalFileName(fileName: string) {
const name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
return normalizePath(name);
Expand Down Expand Up @@ -2414,7 +2436,13 @@ namespace ts.server {
this.configurePlugin(request.arguments);
this.doOutput(/*info*/ undefined, CommandNames.ConfigurePlugin, request.seq, /*success*/ true);
return this.notRequired();
}
},
[CommandNames.SelectionRange]: (request: protocol.SelectionRangeRequest) => {
return this.requiredResponse(this.getSmartSelectionRange(request.arguments, /*simplifiedResult*/ true));
},
[CommandNames.SelectionRangeFull]: (request: protocol.SelectionRangeRequest) => {
return this.requiredResponse(this.getSmartSelectionRange(request.arguments, /*simplifiedResult*/ false));
},
});

public addProtocolHandler(command: string, handler: (request: protocol.Request) => HandlerResponse) {
Expand Down
5 changes: 5 additions & 0 deletions src/services/services.ts
Expand Up @@ -2087,6 +2087,10 @@ namespace ts {
};
}

function getSmartSelectionRange(fileName: string, position: number): SelectionRange {
return SmartSelectionRange.getSmartSelectionRange(position, syntaxTreeCache.getCurrentSourceFile(fileName));
}

function getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences = emptyOptions): ApplicableRefactorInfo[] {
synchronizeHostData();
const file = getValidSourceFile(fileName);
Expand Down Expand Up @@ -2134,6 +2138,7 @@ namespace ts {
getBreakpointStatementAtPosition,
getNavigateToItems,
getRenameInfo,
getSmartSelectionRange,
findRenameLocations,
getNavigationBarItems,
getNavigationTree,
Expand Down
8 changes: 8 additions & 0 deletions src/services/shims.ts
Expand Up @@ -165,6 +165,7 @@ namespace ts {
* { canRename: boolean, localizedErrorMessage: string, displayName: string, fullDisplayName: string, kind: string, kindModifiers: string, triggerSpan: { start; length } }
*/
getRenameInfo(fileName: string, position: number, options?: RenameInfoOptions): string;
getSmartSelectionRange(fileName: string, position: number): string;

/**
* Returns a JSON-encoded value of the type:
Expand Down Expand Up @@ -838,6 +839,13 @@ namespace ts {
);
}

public getSmartSelectionRange(fileName: string, position: number): string {
return this.forwardJSONCall(
`getSmartSelectionRange('${fileName}', ${position})`,
() => this.languageService.getSmartSelectionRange(fileName, position)
);
}

public findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, providePrefixAndSuffixTextForRename?: boolean): string {
return this.forwardJSONCall(
`findRenameLocations('${fileName}', ${position}, ${findInStrings}, ${findInComments}, ${providePrefixAndSuffixTextForRename})`,
Expand Down