Skip to content

Commit 61f6f29

Browse files
committed
Bugfixing references, definition for custom properties
1 parent 7f0ba31 commit 61f6f29

File tree

4 files changed

+303
-35
lines changed

4 files changed

+303
-35
lines changed

pkgs/sass_language_services/lib/src/features/find_references/find_references_visitor.dart

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@ class FindReferencesVisitor
1717
{bool includeDeclaration = false})
1818
: _includeDeclaration = includeDeclaration;
1919

20+
@override
21+
void visitDeclaration(sass.Declaration node) {
22+
var isCustomPropertyDeclaration =
23+
node.name.isPlain && node.name.asPlain!.startsWith('--');
24+
25+
if (isCustomPropertyDeclaration && _includeDeclaration) {
26+
var name = node.name.asPlain!;
27+
if (!name.contains(_name)) {
28+
return;
29+
}
30+
var location = lsp.Location(
31+
range: toRange(node.name.span),
32+
uri: _document.uri,
33+
);
34+
candidates.add(
35+
Reference(
36+
name: name,
37+
location: location,
38+
kind: ReferenceKind.customProperty,
39+
),
40+
);
41+
}
42+
super.visitDeclaration(node);
43+
}
44+
2045
@override
2146
void visitExtendRule(sass.ExtendRule node) {
2247
var isPlaceholderSelector =
@@ -142,22 +167,45 @@ class FindReferencesVisitor
142167

143168
@override
144169
void visitFunctionExpression(sass.FunctionExpression node) {
145-
var name = node.name;
146-
if (!name.contains(_name)) {
147-
super.visitFunctionExpression(node);
148-
return;
170+
var isCustomProperty =
171+
node.name == 'var' && node.arguments.positional.isNotEmpty;
172+
if (isCustomProperty) {
173+
var expression = node.arguments.positional.first;
174+
if (expression is sass.StringExpression &&
175+
!expression.hasQuotes &&
176+
expression.text.isPlain) {
177+
var name = expression.text.asPlain!;
178+
var location = lsp.Location(
179+
range: toRange(expression.text.span),
180+
uri: _document.uri,
181+
);
182+
candidates.add(
183+
Reference(
184+
name: name,
185+
location: location,
186+
kind: ReferenceKind.customProperty,
187+
),
188+
);
189+
}
190+
} else {
191+
var name = node.name;
192+
if (!name.contains(_name)) {
193+
super.visitFunctionExpression(node);
194+
return;
195+
}
196+
var location = lsp.Location(
197+
range: toRange(node.nameSpan),
198+
uri: _document.uri,
199+
);
200+
candidates.add(
201+
Reference(
202+
name: name,
203+
location: location,
204+
kind: ReferenceKind.function,
205+
),
206+
);
149207
}
150-
var location = lsp.Location(
151-
range: toRange(node.nameSpan),
152-
uri: _document.uri,
153-
);
154-
candidates.add(
155-
Reference(
156-
name: name,
157-
location: location,
158-
kind: ReferenceKind.function,
159-
),
160-
);
208+
161209
super.visitFunctionExpression(node);
162210
}
163211

@@ -232,6 +280,9 @@ class FindReferencesVisitor
232280
super.visitMixinRule(node);
233281
}
234282

283+
@override
284+
void visitStringExpression(sass.StringExpression node) {}
285+
235286
@override
236287
void visitStyleRule(sass.StyleRule node) {
237288
if (!_includeDeclaration) {

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,21 @@ class GoToDefinitionFeature extends LanguageFeature {
160160

161161
// Fall back to "@import-style" lookup on the whole workspace.
162162
for (var document in ls.cache.getDocuments()) {
163-
var symbols = ls.findDocumentSymbols(document);
164-
for (var symbol in symbols) {
165-
for (var kind in kinds) {
166-
if (symbol.name == name && symbol.referenceKind == kind) {
167-
return Definition(
168-
name,
169-
kind,
170-
lsp.Location(uri: document.uri, range: symbol.selectionRange),
171-
);
172-
}
163+
var stylesheet = ls.parseStylesheet(document);
164+
var symbols = ScopedSymbols(stylesheet,
165+
document.languageId == 'sass' ? Dialect.indented : Dialect.scss);
166+
167+
for (var kind in kinds) {
168+
var symbol = symbols.globalScope.getSymbol(
169+
name: name,
170+
referenceKind: kind,
171+
);
172+
if (symbol != null) {
173+
return Definition(
174+
name,
175+
kind,
176+
lsp.Location(uri: document.uri, range: symbol.selectionRange),
177+
);
173178
}
174179
}
175180
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ ReferenceKind? getNodeReferenceKind(sass.AstNode node) {
99
return ReferenceKind.variable;
1010
} else if (node is sass.VariableExpression) {
1111
return ReferenceKind.variable;
12+
} else if (node is sass.Declaration) {
13+
var isCustomProperty =
14+
node.name.isPlain && node.name.asPlain!.startsWith("--");
15+
if (isCustomProperty) {
16+
return ReferenceKind.customProperty;
17+
}
1218
} else if (node is sass.StringExpression) {
1319
var isCustomProperty =
1420
node.text.isPlain && node.text.asPlain!.startsWith("--");
@@ -52,6 +58,12 @@ String? getNodeName(sass.AstNode node) {
5258
return node.name;
5359
} else if (node is sass.VariableExpression) {
5460
return node.name;
61+
} else if (node is sass.Declaration) {
62+
var isCustomProperty =
63+
node.name.isPlain && node.name.asPlain!.startsWith("--");
64+
if (isCustomProperty) {
65+
return node.name.asPlain;
66+
}
5567
} else if (node is sass.StringExpression) {
5668
var isCustomProperty =
5769
node.text.isPlain && node.text.asPlain!.startsWith("--");

0 commit comments

Comments
 (0)