Skip to content

Commit fb4c94d

Browse files
committed
Go to definition
1 parent f82721c commit fb4c94d

File tree

10 files changed

+438
-259
lines changed

10 files changed

+438
-259
lines changed

pkgs/sass_language_services/lib/src/features/document_symbols/document_symbols_visitor.dart

Lines changed: 113 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
2020
/// other symbols, and if so adds them as children.
2121
void _collect(
2222
{required String name,
23-
required lsp.SymbolKind kind,
23+
required ReferenceKind referenceKind,
2424
required lsp.Range symbolRange,
2525
lsp.Range? nameRange,
2626
String? docComment,
@@ -33,15 +33,16 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
3333
}
3434

3535
var symbol = StylesheetDocumentSymbol(
36-
name: name,
37-
kind: kind,
38-
range: range,
39-
children: [],
40-
selectionRange: selectionRange,
41-
docComment: docComment,
42-
detail: detail,
43-
tags: tags,
44-
deprecated: _isDeprecated(docComment));
36+
name: name,
37+
referenceKind: referenceKind,
38+
range: range,
39+
children: [],
40+
selectionRange: selectionRange,
41+
docComment: docComment,
42+
detail: detail,
43+
tags: tags,
44+
deprecated: _isDeprecated(docComment),
45+
);
4546

4647
// Look to see if this symbol contains other symbols.
4748

@@ -105,40 +106,47 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
105106

106107
if (node.name.asPlain == 'font-face') {
107108
_collect(
108-
name: node.name.span.text,
109-
kind: lsp.SymbolKind.Class,
110-
symbolRange: toRange(node.span),
111-
nameRange: toRange(node.name.span));
109+
name: node.name.span.text,
110+
referenceKind: ReferenceKind.fontFace,
111+
symbolRange: toRange(node.span),
112+
nameRange: toRange(node.name.span),
113+
);
112114
} else if (node.name.asPlain!.startsWith('keyframes')) {
113115
var keyframesName = node.span.context.split(' ').elementAtOrNull(1);
114116
if (keyframesName != null) {
115117
var keyframesNameRange = lsp.Range(
116-
start: lsp.Position(
117-
line: node.name.span.start.line,
118-
character: node.name.span.end.column + 1),
119-
end: lsp.Position(
120-
line: node.name.span.end.line,
121-
character:
122-
node.name.span.end.column + 1 + keyframesName.length));
118+
start: lsp.Position(
119+
line: node.name.span.start.line,
120+
character: node.name.span.end.column + 1,
121+
),
122+
end: lsp.Position(
123+
line: node.name.span.end.line,
124+
character: node.name.span.end.column + 1 + keyframesName.length,
125+
),
126+
);
123127

124128
_collect(
125-
name: keyframesName,
126-
kind: lsp.SymbolKind.Class,
127-
symbolRange: toRange(node.span),
128-
nameRange: keyframesNameRange);
129+
name: keyframesName,
130+
referenceKind: ReferenceKind.keyframe,
131+
symbolRange: toRange(node.span),
132+
nameRange: keyframesNameRange,
133+
);
129134
}
130135
}
131136
}
132137

133138
@override
134139
void visitDeclaration(node) {
135140
super.visitDeclaration(node);
136-
if (node.name.isPlain && node.name.asPlain!.startsWith("--")) {
141+
var isCustomProperty =
142+
node.name.isPlain && node.name.asPlain!.startsWith("--");
143+
if (isCustomProperty) {
137144
_collect(
138-
name: node.name.span.text,
139-
kind: lsp.SymbolKind.Variable,
140-
symbolRange: toRange(node.span),
141-
nameRange: toRange(node.name.span));
145+
name: node.name.span.text,
146+
referenceKind: ReferenceKind.customProperty,
147+
symbolRange: toRange(node.span),
148+
nameRange: toRange(node.name.span),
149+
);
142150
}
143151
}
144152

@@ -147,12 +155,13 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
147155
super.visitFunctionRule(node);
148156

149157
_collect(
150-
name: node.name,
151-
detail: _detail(node),
152-
kind: lsp.SymbolKind.Function,
153-
docComment: node.comment?.docComment,
154-
symbolRange: toRange(node.span),
155-
nameRange: toRange(node.nameSpan));
158+
name: node.name,
159+
detail: _detail(node),
160+
referenceKind: ReferenceKind.function,
161+
docComment: node.comment?.docComment,
162+
symbolRange: toRange(node.span),
163+
nameRange: toRange(node.nameSpan),
164+
);
156165
}
157166

158167
@override
@@ -164,33 +173,38 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
164173

165174
// node.query.span includes whitespace, so the range doesn't match node.query.asPlain
166175
var nameRange = lsp.Range(
167-
start: lsp.Position(
168-
line: node.span.start.line + node.query.span.start.line,
169-
character: node.span.start.column + node.query.span.start.column),
170-
end: lsp.Position(
171-
line: node.span.start.line + node.query.span.end.line,
172-
character: node.span.start.column +
173-
node.query.span.start.column +
174-
node.query.asPlain!.length));
176+
start: lsp.Position(
177+
line: node.span.start.line + node.query.span.start.line,
178+
character: node.span.start.column + node.query.span.start.column,
179+
),
180+
end: lsp.Position(
181+
line: node.span.start.line + node.query.span.end.line,
182+
character: node.span.start.column +
183+
node.query.span.start.column +
184+
node.query.asPlain!.length,
185+
),
186+
);
175187

176188
_collect(
177-
name: '@media ${node.query.asPlain}',
178-
kind: lsp.SymbolKind.Module,
179-
symbolRange: toRange(node.span),
180-
nameRange: nameRange);
189+
name: '@media ${node.query.asPlain}',
190+
referenceKind: ReferenceKind.media,
191+
symbolRange: toRange(node.span),
192+
nameRange: nameRange,
193+
);
181194
}
182195

183196
@override
184197
void visitMixinRule(node) {
185198
super.visitMixinRule(node);
186199

187200
_collect(
188-
name: node.name,
189-
detail: _detail(node),
190-
kind: lsp.SymbolKind.Method,
191-
docComment: node.comment?.docComment,
192-
symbolRange: toRange(node.span),
193-
nameRange: toRange(node.nameSpan));
201+
name: node.name,
202+
detail: _detail(node),
203+
referenceKind: ReferenceKind.mixin,
204+
docComment: node.comment?.docComment,
205+
symbolRange: toRange(node.span),
206+
nameRange: toRange(node.nameSpan),
207+
);
194208
}
195209

196210
@override
@@ -221,38 +235,45 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
221235
if (nameRange == null) {
222236
// The selector span seems to be relative to node, not to the file.
223237
nameRange = lsp.Range(
224-
start: lsp.Position(
225-
line: node.span.start.line + selector.span.start.line,
226-
character:
227-
node.span.start.column + selector.span.start.column),
228-
end: lsp.Position(
229-
line: node.span.start.line + selector.span.end.line,
230-
character:
231-
node.span.start.column + selector.span.end.column));
238+
start: lsp.Position(
239+
line: node.span.start.line + selector.span.start.line,
240+
character: node.span.start.column + selector.span.start.column,
241+
),
242+
end: lsp.Position(
243+
line: node.span.start.line + selector.span.end.line,
244+
character: node.span.start.column + selector.span.end.column,
245+
),
246+
);
232247

233248
// symbolRange: start position of selector's nameRange, end of stylerule (node.span.end).
234249
symbolRange = lsp.Range(
235-
start: lsp.Position(
236-
line: nameRange.start.line,
237-
character: nameRange.start.character),
238-
end: lsp.Position(
239-
line: node.span.end.line, character: node.span.end.column));
250+
start: lsp.Position(
251+
line: nameRange.start.line,
252+
character: nameRange.start.character,
253+
),
254+
end: lsp.Position(
255+
line: node.span.end.line,
256+
character: node.span.end.column,
257+
),
258+
);
240259
} else {
241260
// Move the end of the name range down to include this selector component
242261
nameRange = lsp.Range(
243-
start: nameRange.start,
244-
end: lsp.Position(
245-
line: node.span.start.line + selector.span.end.line,
246-
character:
247-
node.span.start.column + selector.span.end.column));
262+
start: nameRange.start,
263+
end: lsp.Position(
264+
line: node.span.start.line + selector.span.end.line,
265+
character: node.span.start.column + selector.span.end.column,
266+
),
267+
);
248268
}
249269
}
250270

251271
_collect(
252-
name: name!,
253-
kind: lsp.SymbolKind.Class,
254-
symbolRange: symbolRange!,
255-
nameRange: nameRange);
272+
name: name!,
273+
referenceKind: ReferenceKind.selector,
274+
symbolRange: symbolRange!,
275+
nameRange: nameRange,
276+
);
256277
}
257278
} on sass.SassFormatException catch (_) {
258279
// Do nothing.
@@ -263,17 +284,21 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
263284
void visitVariableDeclaration(node) {
264285
super.visitVariableDeclaration(node);
265286
_collect(
266-
name: node.nameSpan.text,
267-
kind: lsp.SymbolKind.Variable,
268-
docComment: node.comment?.docComment,
269-
symbolRange: toRange(node.span),
270-
nameRange: lsp.Range(
271-
start: lsp.Position(
272-
line: node.nameSpan.start.line,
273-
// the span includes $
274-
character: node.nameSpan.start.column),
275-
end: lsp.Position(
276-
line: node.nameSpan.end.line,
277-
character: node.nameSpan.end.column)));
287+
name: node.nameSpan.text,
288+
referenceKind: ReferenceKind.variable,
289+
docComment: node.comment?.docComment,
290+
symbolRange: toRange(node.span),
291+
nameRange: lsp.Range(
292+
start: lsp.Position(
293+
line: node.nameSpan.start.line,
294+
// the span includes $
295+
character: node.nameSpan.start.column,
296+
),
297+
end: lsp.Position(
298+
line: node.nameSpan.end.line,
299+
character: node.nameSpan.end.column,
300+
),
301+
),
302+
);
278303
}
279304
}
Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
1-
import 'package:lsp_server/lsp_server.dart';
1+
import 'package:lsp_server/lsp_server.dart' as lsp;
22

3-
class StylesheetDocumentSymbol extends DocumentSymbol {
3+
class StylesheetDocumentSymbol extends lsp.DocumentSymbol {
44
final String? docComment;
5+
final ReferenceKind referenceKind;
56

67
StylesheetDocumentSymbol({
78
required super.name,
8-
required super.kind,
9+
required this.referenceKind,
910
required super.range,
1011
required super.selectionRange,
1112
super.tags,
1213
super.deprecated,
1314
super.detail,
1415
super.children,
1516
this.docComment,
16-
});
17+
}) : super(kind: _toKind(referenceKind));
18+
}
19+
20+
/// Translate between [ReferenceKind] and the [lsp.SymbolKind] used by language clients.
21+
lsp.SymbolKind _toKind(ReferenceKind kind) {
22+
switch (kind) {
23+
case ReferenceKind.atRule:
24+
return lsp.SymbolKind.Module;
25+
case ReferenceKind.mixin:
26+
return lsp.SymbolKind.Method;
27+
case ReferenceKind.function:
28+
return lsp.SymbolKind.Function;
29+
case ReferenceKind.variable:
30+
return lsp.SymbolKind.Variable;
31+
default:
32+
return lsp.SymbolKind.Class;
33+
}
34+
}
35+
36+
enum ReferenceKind {
37+
atRule,
38+
customProperty,
39+
fontFace,
40+
forward,
41+
forwardVisibility,
42+
function,
43+
keyframe,
44+
mixin,
45+
module,
46+
media,
47+
selector,
48+
placeholderSelector,
49+
property,
50+
unknown,
51+
variable,
1752
}

0 commit comments

Comments
 (0)