forked from microsoft/TypeScript
/
smartSelection.ts
71 lines (68 loc) · 3.33 KB
/
smartSelection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace ts.projectSystem {
function setup(fileName: string, content: string) {
const file: File = { path: fileName, content };
const host = createServerHost([file, libFile]);
const session = createSession(host);
openFilesForSession([file], session);
return function getSmartSelectionRange(locations: protocol.SelectionRangeRequestArgs["locations"]) {
return executeSessionRequest<protocol.SelectionRangeRequest, protocol.SelectionRangeResponse>(
session,
CommandNames.SelectionRange,
{ file: fileName, locations });
};
}
// More tests in fourslash/smartSelection_*
describe("unittests:: tsserver:: smartSelection", () => {
it("works for simple JavaScript", () => {
const getSmartSelectionRange = setup("/file.js", `
class Foo {
bar(a, b) {
if (a === b) {
return true;
}
return false;
}
}`);
const locations = getSmartSelectionRange([
{ line: 4, offset: 13 }, // a === b
]);
assert.deepEqual(locations, [{
textSpan: { // a
start: { line: 4, offset: 13 },
end: { line: 4, offset: 14 } },
parent: {
textSpan: { // a === b
start: { line: 4, offset: 13 },
end: { line: 4, offset: 20 } },
parent: {
textSpan: { // IfStatement
start: { line: 4, offset: 9 },
end: { line: 6, offset: 10 } },
parent: {
textSpan: { // SyntaxList + whitespace (body of method)
start: { line: 3, offset: 16 },
end: { line: 8, offset: 5 }
},
parent: {
textSpan: { // {}
start: { line: 3, offset: 15 },
end: { line: 8, offset: 6 } },
parent: {
textSpan: { // MethodDeclaration
start: { line: 3, offset: 5 },
end: { line: 8, offset: 6 } },
parent: {
textSpan: { // SyntaxList + whitespace (body of class)
start: { line: 2, offset: 12 },
end: { line: 9, offset: 1 } },
parent: {
textSpan: { // ClassDeclaration
start: { line: 2, offset: 1 },
end: { line: 9, offset: 2 } },
parent: {
textSpan: { // SourceFile (all text)
start: { line: 1, offset: 1 },
end: { line: 9, offset: 2 } } } } } } } } } } }]);
});
});
}