Skip to content

Commit 9009d88

Browse files
authored
(feat) better document symbols (#536)
#532 - remove svelte2tsx transformation artifacts within script - show first 50 characters of reactive statements that are not variable assignments
1 parent 4811635 commit 9009d88

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import {
1313
SymbolInformation,
1414
WorkspaceEdit,
1515
} from 'vscode-languageserver';
16-
import { Document, DocumentManager, mapSymbolInformationToOriginal } from '../../lib/documents';
16+
import {
17+
Document,
18+
DocumentManager,
19+
mapSymbolInformationToOriginal,
20+
getTextInRange,
21+
} from '../../lib/documents';
1722
import { LSConfigManager, LSTypescriptConfig } from '../../ls-config';
1823
import { pathToUrl } from '../../utils';
1924
import {
@@ -122,12 +127,28 @@ export class TypeScriptPlugin
122127
})
123128
.map((symbol) => mapSymbolInformationToOriginal(fragment, symbol))
124129
// Due to svelte2tsx, there will also be some symbols that are unmapped.
125-
// Filter those out to keep the lsp from throwing errors
130+
// Filter those out to keep the lsp from throwing errors.
131+
// Also filter out transformation artifacts
126132
.filter(
127133
(symbol) =>
128134
symbol.location.range.start.line >= 0 &&
129-
symbol.location.range.end.line >= 0,
135+
symbol.location.range.end.line >= 0 &&
136+
!symbol.name.startsWith('__sveltets_'),
130137
)
138+
.map((symbol) => {
139+
if (symbol.name !== '<function>') {
140+
return symbol;
141+
}
142+
143+
let name = getTextInRange(symbol.location.range, document.getText()).trimLeft();
144+
if (name.length > 50) {
145+
name = name.substring(0, 50) + '...';
146+
}
147+
return {
148+
...symbol,
149+
name,
150+
};
151+
})
131152
);
132153

133154
function collectSymbols(

packages/language-server/test/plugins/typescript/TypescriptPlugin.test.ts

+57-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ describe('TypescriptPlugin', () => {
1414
return pathToUrl(filePath);
1515
}
1616

17+
function harmonizeNewLines(input: string) {
18+
return input.replace(/\r\n/g, '~:~').replace(/\n/g, '~:~').replace(/~:~/g, '\n');
19+
}
20+
1721
function setup(filename: string) {
1822
const docManager = new DocumentManager(() => document);
1923
const testDir = path.join(__dirname, 'testfiles');
@@ -30,25 +34,63 @@ describe('TypescriptPlugin', () => {
3034
const symbols = await plugin.getDocumentSymbols(document);
3135

3236
assert.deepStrictEqual(
33-
symbols.find((symbol) => symbol.name === 'bla'),
34-
{
35-
containerName: 'render',
36-
kind: 12,
37-
location: {
38-
range: {
39-
start: {
40-
character: 8,
41-
line: 0,
37+
symbols.map((s) => ({ ...s, name: harmonizeNewLines(s.name) })),
38+
[
39+
{
40+
containerName: 'render',
41+
kind: 12,
42+
location: {
43+
range: {
44+
start: {
45+
line: 6,
46+
character: 3,
47+
},
48+
end: {
49+
line: 8,
50+
character: 5,
51+
},
4252
},
43-
end: {
44-
character: 37,
45-
line: 0,
53+
uri: getUri('documentsymbols.svelte'),
54+
},
55+
name: "$: if (hello) {\n console.log('hi');\n }",
56+
},
57+
{
58+
containerName: 'render',
59+
kind: 12,
60+
location: {
61+
range: {
62+
start: {
63+
line: 1,
64+
character: 4,
65+
},
66+
end: {
67+
line: 3,
68+
character: 5,
69+
},
4670
},
71+
uri: getUri('documentsymbols.svelte'),
4772
},
48-
uri: getUri('documentsymbols.svelte'),
73+
name: 'bla',
4974
},
50-
name: 'bla',
51-
},
75+
{
76+
containerName: 'render',
77+
kind: 13,
78+
location: {
79+
range: {
80+
start: {
81+
line: 5,
82+
character: 7,
83+
},
84+
end: {
85+
line: 5,
86+
character: 16,
87+
},
88+
},
89+
uri: getUri('documentsymbols.svelte'),
90+
},
91+
name: 'hello',
92+
},
93+
],
5294
);
5395
});
5496

Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
<script>function bla() {return true;} bla();</script>
1+
<script>
2+
function bla() {
3+
return true;
4+
}
5+
bla();
6+
$: hello = 1;
7+
$: if (hello) {
8+
console.log('hi');
9+
}
10+
</script>

0 commit comments

Comments
 (0)