Skip to content

Commit 5a89214

Browse files
committed
Go to definition for variable
1 parent b21c505 commit 5a89214

File tree

5 files changed

+80
-50
lines changed

5 files changed

+80
-50
lines changed

pkgs/sass_language_server/lib/src/language_server.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,15 @@ class LanguageServer {
107107
if (document == null) {
108108
var text = await fileSystemProvider.readFile(uri);
109109
document = TextDocument(
110-
uri,
111-
uri.path.endsWith('.sass')
112-
? 'sass'
113-
: uri.path.endsWith('.css')
114-
? 'css'
115-
: 'scss',
116-
1,
117-
text);
110+
uri,
111+
uri.path.endsWith('.sass')
112+
? 'sass'
113+
: uri.path.endsWith('.css')
114+
? 'css'
115+
: 'scss',
116+
1,
117+
text,
118+
);
118119

119120
_ls.parseStylesheet(document);
120121
}

pkgs/sass_language_services/lib/src/features/go_to_definition/go_to_definition_feature.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class GoToDefinitionFeature extends LanguageFeature {
6565
? '$prefix$name'
6666
: name;
6767

68+
var stylesheet = ls.parseStylesheet(document);
6869
var symbols = ScopedSymbols(stylesheet,
6970
document.languageId == 'sass' ? Dialect.indented : Dialect.scss);
7071
var symbol = symbols.globalScope.getSymbol(
@@ -82,7 +83,7 @@ class GoToDefinitionFeature extends LanguageFeature {
8283
},
8384
);
8485

85-
if (definition != null) {
86+
if (definition != null && definition.isNotEmpty) {
8687
return definition.first;
8788
}
8889

pkgs/sass_language_services/lib/src/features/go_to_definition/scope_visitor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
366366
@override
367367
void visitVariableDeclaration(node) {
368368
_addSymbol(
369-
name: node.nameSpan.text,
369+
name: node.name,
370370
kind: ReferenceKind.variable,
371371
symbolRange: toRange(node.span),
372372
nameRange: lsp.Range(

pkgs/sass_language_services/lib/src/features/go_to_definition/scoped_symbols.dart

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,75 @@ import 'scope.dart';
55
import 'scope_visitor.dart';
66

77
ReferenceKind? getNodeReferenceKind(sass.AstNode node) {
8-
if (node.runtimeType is sass.VariableDeclaration) {
8+
if (node is sass.VariableDeclaration) {
99
return ReferenceKind.variable;
10-
} else if (node.runtimeType is sass.Declaration) {
11-
var name = (node as sass.Declaration).name;
10+
} else if (node is sass.VariableExpression) {
11+
return ReferenceKind.variable;
12+
} else if (node is sass.Declaration) {
13+
var name = node.name;
1214
var isCustomProperty = name.isPlain && name.asPlain!.startsWith("--");
1315

1416
if (isCustomProperty) {
1517
return ReferenceKind.customProperty;
1618
}
17-
} else if (node.runtimeType is sass.AtRule) {
18-
var name = (node as sass.AtRule).name;
19+
} else if (node is sass.AtRule) {
20+
var name = node.name;
1921
var isKeyframe = name.isPlain && name.asPlain!.startsWith('keyframes');
2022
if (isKeyframe) {
2123
return ReferenceKind.keyframe;
2224
}
23-
} else if (node.runtimeType is sass.ComplexSelectorComponent) {
25+
} else if (node is sass.ComplexSelectorComponent) {
2426
return ReferenceKind.selector;
25-
} else if (node.runtimeType is sass.SimpleSelector) {
27+
} else if (node is sass.SimpleSelector) {
2628
return ReferenceKind.selector;
27-
} else if (node.runtimeType is sass.PlaceholderSelector) {
29+
} else if (node is sass.PlaceholderSelector) {
2830
return ReferenceKind.placeholderSelector;
29-
} else if (node.runtimeType is sass.MixinRule) {
31+
} else if (node is sass.MixinRule) {
3032
return ReferenceKind.mixin;
31-
} else if (node.runtimeType is sass.FunctionExpression) {
33+
} else if (node is sass.FunctionExpression) {
34+
return ReferenceKind.function;
35+
} else if (node is sass.FunctionRule) {
3236
return ReferenceKind.function;
3337
}
3438

3539
return null;
3640
}
3741

3842
String? getNodeName(sass.AstNode node) {
39-
if (node.runtimeType is sass.VariableDeclaration) {
40-
return (node as sass.VariableDeclaration).name;
41-
} else if (node.runtimeType is sass.Declaration) {
42-
var name = (node as sass.Declaration).name;
43+
if (node is sass.VariableDeclaration) {
44+
return node.name;
45+
} else if (node is sass.VariableExpression) {
46+
return node.name;
47+
} else if (node is sass.Declaration) {
48+
var name = node.name;
4349
var isCustomProperty = name.isPlain && name.asPlain!.startsWith("--");
4450
if (isCustomProperty) {
4551
return node.name.span.text;
4652
}
47-
} else if (node.runtimeType is sass.AtRule) {
48-
var name = (node as sass.AtRule).name;
53+
} else if (node is sass.AtRule) {
54+
var name = node.name;
4955
var isKeyframe = name.isPlain && name.asPlain!.startsWith('keyframes');
5056
if (isKeyframe) {
5157
var keyframesName = node.span.context.split(' ').elementAtOrNull(1);
5258
return keyframesName;
5359
}
54-
} else if (node.runtimeType is sass.ComplexSelectorComponent) {
60+
} else if (node is sass.ComplexSelectorComponent) {
5561
var rule = node as sass.ComplexSelectorComponent;
5662
return rule.span.text;
57-
} else if (node.runtimeType is sass.SimpleSelector) {
58-
var rule = node as sass.SimpleSelector;
63+
} else if (node is sass.SimpleSelector) {
64+
var rule = node;
5965
return rule.span.text;
60-
} else if (node.runtimeType is sass.PlaceholderSelector) {
61-
var placeholder = node as sass.PlaceholderSelector;
66+
} else if (node is sass.PlaceholderSelector) {
67+
var placeholder = node;
6268
return placeholder.name;
63-
} else if (node.runtimeType is sass.MixinRule) {
64-
var mixin = node as sass.MixinRule;
69+
} else if (node is sass.MixinRule) {
70+
var mixin = node;
6571
return mixin.name;
66-
} else if (node.runtimeType is sass.FunctionExpression) {
67-
var function = node as sass.FunctionExpression;
72+
} else if (node is sass.FunctionExpression) {
73+
var function = node;
74+
return function.name;
75+
} else if (node is sass.FunctionRule) {
76+
var function = node;
6877
return function.name;
6978
}
7079

pkgs/sass_language_services/lib/src/features/language_feature.dart

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ abstract class LanguageFeature {
2424
bool lazy = false,
2525
int depth = 0}) async {
2626
return _findInWorkspace(
27-
callback: callback,
28-
initialDocument: initialDocument,
29-
currentDocument: initialDocument,
30-
depth: depth,
31-
lazy: lazy);
27+
callback: callback,
28+
initialDocument: initialDocument,
29+
currentDocument: initialDocument,
30+
depth: depth,
31+
lazy: lazy,
32+
hiddenMixinsAndFunctions: [],
33+
hiddenVariables: [],
34+
shownMixinsAndFunctions: [],
35+
shownVariables: [],
36+
visited: {},
37+
);
3238
}
3339

3440
Future<List<T>?> _findInWorkspace<T>(
@@ -42,12 +48,12 @@ abstract class LanguageFeature {
4248
}) callback,
4349
required TextDocument initialDocument,
4450
required TextDocument currentDocument,
51+
required List<String> hiddenMixinsAndFunctions,
52+
required List<String> hiddenVariables,
53+
required List<String> shownMixinsAndFunctions,
54+
required List<String> shownVariables,
55+
required Set<String> visited,
4556
String accumulatedPrefix = '',
46-
List<String> hiddenMixinsAndFunctions = const [],
47-
List<String> hiddenVariables = const [],
48-
List<String> shownMixinsAndFunctions = const [],
49-
List<String> shownVariables = const [],
50-
Set<String> visited = const {},
5157
bool lazy = false,
5258
int depth = 0}) async {
5359
if (visited.contains(currentDocument.uri.toString())) {
@@ -95,11 +101,24 @@ abstract class LanguageFeature {
95101
continue;
96102
}
97103

98-
var next = ls.cache.getDocument(link.target!);
104+
var uri = link.target!;
105+
var next = ls.cache.getDocument(uri);
99106
if (next == null) {
100-
// We shouldn't really end up here. If so, the feature's handler in
101-
// the server should await the initial scan.
102-
continue;
107+
// We shouldn't really end up here outside of unit tests.
108+
// The language server's initial scan should have put all
109+
// linked documents in the cache already.
110+
var text = await ls.fs.readFile(uri);
111+
next = TextDocument(
112+
uri,
113+
uri.path.endsWith('.sass')
114+
? 'sass'
115+
: uri.path.endsWith('.css')
116+
? 'css'
117+
: 'scss',
118+
1,
119+
text,
120+
);
121+
ls.parseStylesheet(next);
103122
}
104123

105124
var prefix = accumulatedPrefix;
@@ -126,7 +145,7 @@ abstract class LanguageFeature {
126145
var linkResult = await _findInWorkspace(
127146
callback: callback,
128147
initialDocument: initialDocument,
129-
currentDocument: currentDocument,
148+
currentDocument: next,
130149
accumulatedPrefix: prefix,
131150
hiddenMixinsAndFunctions: hiddenMixinsAndFunctions,
132151
hiddenVariables: hiddenVariables,

0 commit comments

Comments
 (0)