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