Skip to content

Commit f72b55b

Browse files
committed
Test for custom properties
Change scopes to be inclusive of length, for the indented dialect. Symbols at the end of the range might get dropped from the scope otherwise.
1 parent c8b21d4 commit f72b55b

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Scope {
88

99
final _symbols = <StylesheetDocumentSymbol>[];
1010

11+
/// Starting at [offset] and going for [lenght] characters, inclusive.
1112
Scope({required this.offset, required this.length});
1213

1314
void addChild(Scope scope) {
@@ -32,7 +33,7 @@ class Scope {
3233
var scopeAtOffset = children.firstWhere(
3334
(scope) =>
3435
scope.offset <= offset &&
35-
scope.offset + scope.length > offset + length,
36+
scope.offset + scope.length >= offset + length,
3637
orElse: () => this);
3738

3839
if (scopeAtOffset == this) {

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,20 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
6565
var isCustomProperty =
6666
node.name.isPlain && node.name.asPlain!.startsWith("--");
6767
if (isCustomProperty) {
68-
_addSymbol(
68+
// Add all custom properties to the global scope.
69+
70+
var range = toRange(node.span);
71+
var selectionRange = toRange(node.name.span);
72+
73+
var symbol = StylesheetDocumentSymbol(
6974
name: node.name.span.text,
70-
kind: ReferenceKind.variable,
71-
symbolRange: toRange(node.span),
72-
nameRange: toRange(node.name.span),
73-
offset: node.span.start.offset,
74-
length: node.span.length,
75+
referenceKind: ReferenceKind.customProperty,
76+
range: range,
77+
children: [],
78+
selectionRange: selectionRange,
7579
);
80+
81+
scope.addSymbol(symbol);
7682
}
7783

7884
super.visitDeclaration(node);

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ 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 name = node.name;
14-
var isCustomProperty = name.isPlain && name.asPlain!.startsWith("--");
15-
12+
} else if (node is sass.StringExpression) {
13+
var isCustomProperty =
14+
node.text.isPlain && node.text.asPlain!.startsWith("--");
1615
if (isCustomProperty) {
1716
return ReferenceKind.customProperty;
1817
}
@@ -46,11 +45,11 @@ String? getNodeName(sass.AstNode node) {
4645
return node.name;
4746
} else if (node is sass.VariableExpression) {
4847
return node.name;
49-
} else if (node is sass.Declaration) {
50-
var name = node.name;
51-
var isCustomProperty = name.isPlain && name.asPlain!.startsWith("--");
48+
} else if (node is sass.StringExpression) {
49+
var isCustomProperty =
50+
node.text.isPlain && node.text.asPlain!.startsWith("--");
5251
if (isCustomProperty) {
53-
return node.name.span.text;
52+
return node.text.asPlain;
5453
}
5554
} else if (node is sass.AtRule) {
5655
var name = node.name;

pkgs/sass_language_services/test/features/go_to_definition/go_to_definition_test.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,30 @@ nav ul {
264264
});
265265
});
266266

267-
group('css variables', () {
267+
group('css', () {
268268
setUp(() {
269269
ls.cache.clear();
270270
});
271+
272+
test('custom properties', () async {
273+
var document = fs.createDocument(r'''
274+
:root
275+
--color-text: #000
276+
277+
.a
278+
color: var(--color-text)
279+
''', uri: 'styles.sass');
280+
281+
var result = await ls.goToDefinition(document, at(line: 4, char: 16));
282+
283+
expect(result, isNotNull);
284+
expect(result!.range, StartsAtLine(1));
285+
expect(result.range, EndsAtLine(1));
286+
expect(result.range, StartsAtCharacter(2));
287+
expect(result.range, EndsAtCharacter(14));
288+
289+
expect(result.uri.toString(), endsWith('styles.sass'));
290+
});
271291
});
272292

273293
group('@extended CSS class', () {

pkgs/sass_language_services/test/features/go_to_definition/scope_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ void main() {
3939
expect(global.findScope(offset: -1), isNull);
4040

4141
expect(global.findScope(offset: 0), equals(global));
42-
expect(global.findScope(offset: 20), equals(global));
42+
expect(global.findScope(offset: 21), equals(global));
4343

4444
expect(global.findScope(offset: 10), equals(first));
45-
expect(global.findScope(offset: 14), equals(first));
46-
expect(global.findScope(offset: 15), equals(second));
47-
expect(global.findScope(offset: 19), equals(second));
45+
expect(global.findScope(offset: 15), equals(first));
46+
expect(global.findScope(offset: 16), equals(second));
47+
expect(global.findScope(offset: 20), equals(second));
4848
});
4949
});
5050
}

0 commit comments

Comments
 (0)