Skip to content

Commit d62831b

Browse files
committed
Cache scoped symbols, not document symbols
Designed the cache with the assumption that findDocumentSymbols would be on the hot path for workspace traversal, findReferences etc. Forgot to update once that turned out not to be the case.
1 parent 52ce245 commit d62831b

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

pkgs/sass_language_services/lib/src/features/document_highlights/document_highlights_feature.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,22 @@ class DocumentHighlightsFeature extends LanguageFeature {
2929
}
3030
var kind = getNodeReferenceKind(node);
3131

32-
var symbols = ScopedSymbols(
33-
stylesheet,
34-
document.languageId == 'sass' ? Dialect.indented : Dialect.scss,
35-
);
32+
var symbols = ls.cache.getDocumentSymbols(document) ??
33+
ScopedSymbols(
34+
stylesheet,
35+
document.languageId == 'sass' ? Dialect.indented : Dialect.scss,
36+
);
37+
ls.cache.setDocumentSymbols(document, symbols);
38+
3639
var symbol = symbols.findSymbolFromNode(node);
3740

3841
var result = <lsp.DocumentHighlight>[];
39-
var references = FindReferencesVisitor(document, name);
42+
var references = FindReferencesVisitor(
43+
document,
44+
name,
45+
includeDeclaration: true,
46+
);
47+
4048
for (var reference in references.candidates) {
4149
if (symbol != null) {
4250
if (symbol.referenceKind == reference.kind &&

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@ class DocumentSymbolsFeature extends LanguageFeature {
77
DocumentSymbolsFeature({required super.ls});
88

99
List<StylesheetDocumentSymbol> findDocumentSymbols(TextDocument document) {
10-
final cached = ls.cache.getDocumentSymbols(document);
11-
if (cached != null) {
12-
return cached;
13-
}
14-
1510
var stylesheet = ls.parseStylesheet(document);
1611

1712
var symbolsVisitor = DocumentSymbolsVisitor();
1813
stylesheet.accept(symbolsVisitor);
1914

20-
ls.cache.setDocumentSymbols(document, symbolsVisitor.symbols);
21-
2215
return symbolsVisitor.symbols;
2316
}
2417
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ class GoToDefinitionFeature extends LanguageFeature {
6060

6161
// Look for the symbol in the current document.
6262
// It may be a scoped symbol.
63-
var symbols = ScopedSymbols(stylesheet,
64-
document.languageId == 'sass' ? Dialect.indented : Dialect.scss);
63+
var symbols = ls.cache.getDocumentSymbols(document) ??
64+
ScopedSymbols(
65+
stylesheet,
66+
document.languageId == 'sass' ? Dialect.indented : Dialect.scss,
67+
);
68+
ls.cache.setDocumentSymbols(document, symbols);
6569
var symbol = symbols.findSymbolFromNode(node);
6670
if (symbol != null) {
6771
// Found the definition in the same document.
@@ -128,8 +132,14 @@ class GoToDefinitionFeature extends LanguageFeature {
128132
: name!;
129133

130134
var stylesheet = ls.parseStylesheet(document);
131-
var symbols = ScopedSymbols(stylesheet,
132-
document.languageId == 'sass' ? Dialect.indented : Dialect.scss);
135+
136+
var symbols = ls.cache.getDocumentSymbols(document) ??
137+
ScopedSymbols(
138+
stylesheet,
139+
document.languageId == 'sass' ? Dialect.indented : Dialect.scss,
140+
);
141+
ls.cache.setDocumentSymbols(document, symbols);
142+
133143
var symbol = symbols.globalScope.getSymbol(
134144
name: unprefixedName,
135145
referenceKind: kind,

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ ReferenceKind? getNodeReferenceKind(sass.AstNode node) {
1111
return ReferenceKind.variable;
1212
} else if (node is sass.Declaration) {
1313
var isCustomProperty =
14-
node.name.isPlain && node.name.asPlain!.startsWith("--");
14+
node.name.isPlain && node.name.asPlain!.startsWith('--');
1515
if (isCustomProperty) {
1616
return ReferenceKind.customProperty;
1717
}
1818
} else if (node is sass.StringExpression) {
19-
var isCustomProperty =
20-
node.text.isPlain && node.text.asPlain!.startsWith("--");
19+
var isCustomProperty = node.text.isPlain &&
20+
(node.text.asPlain!.startsWith('--') ||
21+
node.text.asPlain!.startsWith('var(--'));
2122
if (isCustomProperty) {
2223
return ReferenceKind.customProperty;
2324
}
@@ -60,15 +61,21 @@ String? getNodeName(sass.AstNode node) {
6061
return node.name;
6162
} else if (node is sass.Declaration) {
6263
var isCustomProperty =
63-
node.name.isPlain && node.name.asPlain!.startsWith("--");
64+
node.name.isPlain && node.name.asPlain!.startsWith('--');
6465
if (isCustomProperty) {
6566
return node.name.asPlain;
6667
}
6768
} else if (node is sass.StringExpression) {
68-
var isCustomProperty =
69-
node.text.isPlain && node.text.asPlain!.startsWith("--");
69+
var isCustomProperty = node.text.isPlain &&
70+
(node.text.asPlain!.startsWith('--') ||
71+
node.text.asPlain!.startsWith('var(--'));
7072
if (isCustomProperty) {
71-
return node.text.asPlain;
73+
var name = node.text.asPlain!;
74+
if (name.startsWith('var(')) {
75+
name = name.replaceAll('var(', '');
76+
name = name.replaceAll(')', '');
77+
}
78+
return name;
7279
}
7380
} else if (node is sass.AtRule) {
7481
var name = node.name;

pkgs/sass_language_services/lib/src/language_services_cache.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import 'package:sass_api/sass_api.dart' as sass;
22
import 'package:sass_language_services/sass_language_services.dart';
3+
import 'package:sass_language_services/src/features/go_to_definition/scoped_symbols.dart';
34

45
class CacheEntry {
56
TextDocument document;
67
sass.Stylesheet stylesheet;
78
List<StylesheetDocumentLink>? links;
8-
List<StylesheetDocumentSymbol>? symbols;
9+
ScopedSymbols? symbols;
910

1011
CacheEntry({
1112
required this.document,
@@ -53,7 +54,7 @@ class LanguageServicesCache {
5354
return _cache[document.uri.toString()]?.links;
5455
}
5556

56-
List<StylesheetDocumentSymbol>? getDocumentSymbols(TextDocument document) {
57+
ScopedSymbols? getDocumentSymbols(TextDocument document) {
5758
return _cache[document.uri.toString()]?.symbols;
5859
}
5960

@@ -62,8 +63,7 @@ class LanguageServicesCache {
6263
_cache[document.uri.toString()]?.links = links;
6364
}
6465

65-
void setDocumentSymbols(
66-
TextDocument document, List<StylesheetDocumentSymbol> symbols) {
66+
void setDocumentSymbols(TextDocument document, ScopedSymbols symbols) {
6767
_cache[document.uri.toString()]?.symbols = symbols;
6868
}
6969

0 commit comments

Comments
 (0)