Skip to content

Commit d8936fd

Browse files
committed
Rename to be smarter
1 parent f0f7d82 commit d8936fd

File tree

14 files changed

+76
-49
lines changed

14 files changed

+76
-49
lines changed

src/harness/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ namespace ts.server {
424424
return renameInfo;
425425
}
426426

427-
getSelectionRange() {
427+
getSmartSelectionRange() {
428428
return notImplemented();
429429
}
430430

src/harness/harnessLanguageService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ namespace Harness.LanguageService {
472472
getRenameInfo(fileName: string, position: number, options?: ts.RenameInfoOptions): ts.RenameInfo {
473473
return unwrapJSONCallResult(this.shim.getRenameInfo(fileName, position, options));
474474
}
475-
getSelectionRange(fileName: string, position: number): ts.SelectionRange {
476-
return unwrapJSONCallResult(this.shim.getSelectionRange(fileName, position));
475+
getSmartSelectionRange(fileName: string, position: number): ts.SelectionRange {
476+
return unwrapJSONCallResult(this.shim.getSmartSelectionRange(fileName, position));
477477
}
478478
findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, providePrefixAndSuffixTextForRename?: boolean): ts.RenameLocation[] {
479479
return unwrapJSONCallResult(this.shim.findRenameLocations(fileName, position, findInStrings, findInComments, providePrefixAndSuffixTextForRename));

src/server/session.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,14 +2059,14 @@ namespace ts.server {
20592059
this.projectService.configurePlugin(args);
20602060
}
20612061

2062-
private getSelectionRange(args: protocol.SelectionRangeRequestArgs, simplifiedResult: boolean) {
2062+
private getSmartSelectionRange(args: protocol.SelectionRangeRequestArgs, simplifiedResult: boolean) {
20632063
const { locations } = args;
20642064
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
20652065
const scriptInfo = Debug.assertDefined(this.projectService.getScriptInfo(file));
20662066

20672067
return map(locations, location => {
20682068
const pos = this.getPosition(location, scriptInfo);
2069-
const selectionRange = languageService.getSelectionRange(file, pos);
2069+
const selectionRange = languageService.getSmartSelectionRange(file, pos);
20702070
return simplifiedResult ? this.mapSelectionRange(selectionRange, scriptInfo) : selectionRange;
20712071
});
20722072
}
@@ -2438,10 +2438,10 @@ namespace ts.server {
24382438
return this.notRequired();
24392439
},
24402440
[CommandNames.SelectionRange]: (request: protocol.SelectionRangeRequest) => {
2441-
return this.requiredResponse(this.getSelectionRange(request.arguments, /*simplifiedResult*/ true));
2441+
return this.requiredResponse(this.getSmartSelectionRange(request.arguments, /*simplifiedResult*/ true));
24422442
},
24432443
[CommandNames.SelectionRangeFull]: (request: protocol.SelectionRangeRequest) => {
2444-
return this.requiredResponse(this.getSelectionRange(request.arguments, /*simplifiedResult*/ false));
2444+
return this.requiredResponse(this.getSmartSelectionRange(request.arguments, /*simplifiedResult*/ false));
24452445
},
24462446
});
24472447

src/services/services.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,8 +2087,8 @@ namespace ts {
20872087
};
20882088
}
20892089

2090-
function getSelectionRange(fileName: string, position: number): SelectionRange {
2091-
return SelectionRange.getSelectionRange(position, syntaxTreeCache.getCurrentSourceFile(fileName));
2090+
function getSmartSelectionRange(fileName: string, position: number): SelectionRange {
2091+
return SmartSelectionRange.getSmartSelectionRange(position, syntaxTreeCache.getCurrentSourceFile(fileName));
20922092
}
20932093

20942094
function getApplicableRefactors(fileName: string, positionOrRange: number | TextRange, preferences: UserPreferences = emptyOptions): ApplicableRefactorInfo[] {
@@ -2138,7 +2138,7 @@ namespace ts {
21382138
getBreakpointStatementAtPosition,
21392139
getNavigateToItems,
21402140
getRenameInfo,
2141-
getSelectionRange,
2141+
getSmartSelectionRange,
21422142
findRenameLocations,
21432143
getNavigationBarItems,
21442144
getNavigationTree,

src/services/shims.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ namespace ts {
165165
* { canRename: boolean, localizedErrorMessage: string, displayName: string, fullDisplayName: string, kind: string, kindModifiers: string, triggerSpan: { start; length } }
166166
*/
167167
getRenameInfo(fileName: string, position: number, options?: RenameInfoOptions): string;
168-
getSelectionRange(fileName: string, position: number): string;
168+
getSmartSelectionRange(fileName: string, position: number): string;
169169

170170
/**
171171
* Returns a JSON-encoded value of the type:
@@ -839,10 +839,10 @@ namespace ts {
839839
);
840840
}
841841

842-
public getSelectionRange(fileName: string, position: number): string {
842+
public getSmartSelectionRange(fileName: string, position: number): string {
843843
return this.forwardJSONCall(
844-
`getSelectionRange('${fileName}', ${position})`,
845-
() => this.languageService.getSelectionRange(fileName, position)
844+
`getSmartSelectionRange('${fileName}', ${position})`,
845+
() => this.languageService.getSmartSelectionRange(fileName, position)
846846
);
847847
}
848848

src/services/selectionRange.ts renamed to src/services/smartSelection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @internal */
2-
namespace ts.SelectionRange {
3-
export function getSelectionRange(pos: number, sourceFile: SourceFile): SelectionRange {
2+
namespace ts.SmartSelectionRange {
3+
export function getSmartSelectionRange(pos: number, sourceFile: SourceFile): SelectionRange {
44
let selectionRange: SelectionRange = {
55
textSpan: createTextSpanFromBounds(sourceFile.getFullStart(), sourceFile.getEnd())
66
};

src/services/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"patternMatcher.ts",
2929
"preProcess.ts",
3030
"rename.ts",
31-
"selectionRange.ts",
31+
"smartSelection.ts",
3232
"signatureHelp.ts",
3333
"sourcemaps.ts",
3434
"suggestionDiagnostics.ts",

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ namespace ts {
297297
getRenameInfo(fileName: string, position: number, options?: RenameInfoOptions): RenameInfo;
298298
findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, providePrefixAndSuffixTextForRename?: boolean): ReadonlyArray<RenameLocation> | undefined;
299299

300-
getSelectionRange(fileName: string, position: number): SelectionRange;
300+
getSmartSelectionRange(fileName: string, position: number): SelectionRange;
301301

302302
getDefinitionAtPosition(fileName: string, position: number): ReadonlyArray<DefinitionInfo> | undefined;
303303
getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan | undefined;

src/testRunner/unittests/tsserver/selectionRange.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace ts.projectSystem {
44
const host = createServerHost([file, libFile]);
55
const session = createSession(host);
66
openFilesForSession([file], session);
7-
return function getSelectionRange(locations: protocol.SelectionRangeRequestArgs["locations"]) {
7+
return function getSmartSelectionRange(locations: protocol.SelectionRangeRequestArgs["locations"]) {
88
return executeSessionRequest<protocol.SelectionRangeRequest, protocol.SelectionRangeResponse>(
99
session,
1010
CommandNames.SelectionRange,
@@ -14,7 +14,7 @@ namespace ts.projectSystem {
1414

1515
describe("unittests:: tsserver:: selectionRange", () => {
1616
it("works for simple JavaScript", () => {
17-
const getSelectionRange = setup("/file.js", `
17+
const getSmartSelectionRange = setup("/file.js", `
1818
class Foo {
1919
bar(a, b) {
2020
if (a === b) {
@@ -24,7 +24,7 @@ class Foo {
2424
}
2525
}`);
2626

27-
const locations = getSelectionRange([
27+
const locations = getSmartSelectionRange([
2828
{
2929
line: 4,
3030
offset: 13,
@@ -87,14 +87,14 @@ class Foo {
8787
});
8888

8989
it("works for simple TypeScript", () => {
90-
const getSelectionRange = setup("/file.ts", `
90+
const getSmartSelectionRange = setup("/file.ts", `
9191
export interface IService {
9292
_serviceBrand: any;
9393
9494
open(host: number, data: any): Promise<any>;
9595
bar(): void
9696
}`);
97-
const locations = getSelectionRange([
97+
const locations = getSmartSelectionRange([
9898
{ line: 5, offset: 12 }, // ho/**/st
9999
{ line: 6, offset: 16 }, // void/**/
100100
]);
@@ -136,10 +136,10 @@ export interface IService {
136136
});
137137

138138
it("works for complex TypeScript", () => {
139-
const getSelectionRange = setup("/file.ts", `
139+
const getSmartSelectionRange = setup("/file.ts", `
140140
type X<T, P> = IsExactlyAny<P> extends true ? T : ({ [K in keyof P]: IsExactlyAny<P[K]> extends true ? K extends keyof T ? T[K] : P[K] : P[K]; } & Pick<T, Exclude<keyof T, keyof P>>)
141141
`);
142-
const locations = getSelectionRange([
142+
const locations = getSmartSelectionRange([
143143
{
144144
line: 2,
145145
offset: 133,
@@ -195,13 +195,13 @@ type X<T, P> = IsExactlyAny<P> extends true ? T : ({ [K in keyof P]: IsExactlyAn
195195
});
196196

197197
it("works for object types", () => {
198-
const getSelectionRange = setup("/file.js", `
198+
const getSmartSelectionRange = setup("/file.js", `
199199
type X = {
200200
foo?: string;
201201
readonly bar: { x: number };
202202
meh
203203
}`);
204-
const locations = getSelectionRange([
204+
const locations = getSmartSelectionRange([
205205
{ line: 3, offset: 5 },
206206
{ line: 4, offset: 5 },
207207
{ line: 4, offset: 14 },
@@ -285,8 +285,8 @@ type X = {
285285

286286
it("works for string literals and template strings", () => {
287287
// tslint:disable-next-line:no-invalid-template-strings
288-
const getSelectionRange = setup("/file.ts", "`a b ${\n 'c'\n} d`");
289-
const locations = getSelectionRange([
288+
const getSmartSelectionRange = setup("/file.ts", "`a b ${\n 'c'\n} d`");
289+
const locations = getSmartSelectionRange([
290290
{ line: 2, offset: 4 },
291291
{ line: 1, offset: 4 },
292292
]);
@@ -327,13 +327,13 @@ type X = {
327327
});
328328

329329
it("works for ES2015 import lists", () => {
330-
const getSelectionRange = setup("/file.ts", `
330+
const getSmartSelectionRange = setup("/file.ts", `
331331
import { x as y, z } from './z';
332332
import { b } from './';
333333
334334
console.log(1);`);
335335

336-
const locations = getSelectionRange([{ line: 2, offset: 10 }]);
336+
const locations = getSmartSelectionRange([{ line: 2, offset: 10 }]);
337337
assert.deepEqual(locations, [
338338
{
339339
textSpan: { // x
@@ -367,10 +367,10 @@ console.log(1);`);
367367
});
368368

369369
it("works for complex mapped types", () => {
370-
const getSelectionRange = setup("/file.ts", `
370+
const getSmartSelectionRange = setup("/file.ts", `
371371
type M = { -readonly [K in keyof any]-?: any };`);
372372

373-
const locations = getSelectionRange([
373+
const locations = getSmartSelectionRange([
374374
{ line: 2, offset: 12 }, // -readonly
375375
{ line: 2, offset: 14 }, // eadonly
376376
{ line: 2, offset: 22 }, // [
@@ -476,10 +476,10 @@ type M = { -readonly [K in keyof any]-?: any };`);
476476
});
477477

478478
it("works for parameters", () => {
479-
const getSelectionRange = setup("/file.ts", `
479+
const getSmartSelectionRange = setup("/file.ts", `
480480
function f(p, q?, ...r: any[] = []) {}`);
481481

482-
const locations = getSelectionRange([
482+
const locations = getSmartSelectionRange([
483483
{ line: 2, offset: 12 }, // p
484484
{ line: 2, offset: 15 }, // q
485485
{ line: 2, offset: 19 }, // ...
@@ -537,9 +537,9 @@ function f(p, q?, ...r: any[] = []) {}`);
537537
});
538538

539539
it("works for binding elements", () => {
540-
const getSelectionRange = setup("/file.ts", `
540+
const getSmartSelectionRange = setup("/file.ts", `
541541
const { x, y: a, ...zs = {} } = {};`);
542-
const locations = getSelectionRange([
542+
const locations = getSmartSelectionRange([
543543
{ line: 2, offset: 9 }, // x
544544
{ line: 2, offset: 15 }, // a
545545
{ line: 2, offset: 21 }, // zs
@@ -578,12 +578,12 @@ const { x, y: a, ...zs = {} } = {};`);
578578
});
579579

580580
it("consumes all whitespace in a multi-line function parameter list", () => {
581-
const getSelectionRange = setup("/file.ts", `
581+
const getSmartSelectionRange = setup("/file.ts", `
582582
function f(
583583
a,
584584
b
585585
) {}`);
586-
const locations = getSelectionRange([{ line: 4, offset: 5 }]); // b
586+
const locations = getSmartSelectionRange([{ line: 4, offset: 5 }]); // b
587587
assert.deepEqual(locations, [{
588588
textSpan: { // b
589589
start: { line: 4, offset: 5 },
@@ -604,24 +604,24 @@ function f(
604604
});
605605

606606
it("snaps to nodes directly behind the cursor instead of trivia ahead of the cursor", () => {
607-
const getSelectionRange = setup("/file.ts", `let x: string`);
608-
const locations = getSelectionRange([{ line: 1, offset: 4 }]);
607+
const getSmartSelectionRange = setup("/file.ts", `let x: string`);
608+
const locations = getSmartSelectionRange([{ line: 1, offset: 4 }]);
609609
assert.deepEqual(locations![0].textSpan, {
610610
start: { line: 1, offset: 1 },
611611
end: { line: 1, offset: 4 },
612612
});
613613
});
614614

615615
it("creates a stop for JSDoc ranges", () => {
616-
const getSelectionRange = setup("/file.js", "" +
616+
const getSmartSelectionRange = setup("/file.js", "" +
617617
`// Not a JSDoc comment
618618
/**
619619
* @param {number} x The number to square
620620
*/
621621
function square(x) {
622622
return x * x;
623623
}`);
624-
const locations = getSelectionRange([{ line: 5, offset: 10 }]); // square(x)
624+
const locations = getSmartSelectionRange([{ line: 5, offset: 10 }]); // square(x)
625625
assert.deepEqual(locations, [{
626626
textSpan: { // square
627627
start: { line: 5 , offset: 10 },
@@ -641,8 +641,8 @@ function square(x) {
641641
});
642642

643643
it("skips lone VariableDeclarations in a declaration list", () => {
644-
const getSelectionRange = setup("/file.ts", `const x = 3;`);
645-
const locations = getSelectionRange([{ line: 1, offset: 7 }]); // x
644+
const getSmartSelectionRange = setup("/file.ts", `const x = 3;`);
645+
const locations = getSmartSelectionRange([{ line: 1, offset: 7 }]); // x
646646
assert.deepEqual(locations, [{
647647
textSpan: {
648648
start: { line: 1, offset: 7 },

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4788,6 +4788,7 @@ declare namespace ts {
47884788
getSignatureHelpItems(fileName: string, position: number, options: SignatureHelpItemsOptions | undefined): SignatureHelpItems | undefined;
47894789
getRenameInfo(fileName: string, position: number, options?: RenameInfoOptions): RenameInfo;
47904790
findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean, providePrefixAndSuffixTextForRename?: boolean): ReadonlyArray<RenameLocation> | undefined;
4791+
getSmartSelectionRange(fileName: string, position: number): SelectionRange;
47914792
getDefinitionAtPosition(fileName: string, position: number): ReadonlyArray<DefinitionInfo> | undefined;
47924793
getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan | undefined;
47934794
getTypeDefinitionAtPosition(fileName: string, position: number): ReadonlyArray<DefinitionInfo> | undefined;
@@ -5239,6 +5240,10 @@ declare namespace ts {
52395240
displayParts: SymbolDisplayPart[];
52405241
isOptional: boolean;
52415242
}
5243+
interface SelectionRange {
5244+
textSpan: TextSpan;
5245+
parent?: SelectionRange;
5246+
}
52425247
/**
52435248
* Represents a single signature to show in signature help.
52445249
* The id is used for subsequent calls into the language service to ask questions about the
@@ -5790,7 +5795,8 @@ declare namespace ts.server.protocol {
57905795
GetEditsForRefactor = "getEditsForRefactor",
57915796
OrganizeImports = "organizeImports",
57925797
GetEditsForFileRename = "getEditsForFileRename",
5793-
ConfigurePlugin = "configurePlugin"
5798+
ConfigurePlugin = "configurePlugin",
5799+
SelectionRange = "selectionRange",
57945800
}
57955801
/**
57965802
* A TypeScript Server message
@@ -6753,6 +6759,20 @@ declare namespace ts.server.protocol {
67536759
}
67546760
interface ConfigurePluginResponse extends Response {
67556761
}
6762+
interface SelectionRangeRequest extends FileRequest {
6763+
command: CommandTypes.SelectionRange;
6764+
arguments: SelectionRangeRequestArgs;
6765+
}
6766+
interface SelectionRangeRequestArgs extends FileRequestArgs {
6767+
locations: Location[];
6768+
}
6769+
interface SelectionRangeResponse extends Response {
6770+
body?: SelectionRange[];
6771+
}
6772+
interface SelectionRange {
6773+
textSpan: TextSpan;
6774+
parent?: SelectionRange;
6775+
}
67566776
/**
67576777
* Information found in an "open" request.
67586778
*/
@@ -9026,6 +9046,8 @@ declare namespace ts.server {
90269046
private getBraceMatching;
90279047
private getDiagnosticsForProject;
90289048
private configurePlugin;
9049+
private getSmartSelectionRange;
9050+
private mapSelectionRange;
90299051
getCanonicalFileName(fileName: string): string;
90309052
exit(): void;
90319053
private notRequired;

0 commit comments

Comments
 (0)