Skip to content

Commit 3a00206

Browse files
committed
Clean up unused code, cache
1 parent 0037d95 commit 3a00206

File tree

3 files changed

+25
-36
lines changed

3 files changed

+25
-36
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@ 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+
1015
var stylesheet = ls.parseStylesheet(document);
1116

1217
var symbolsVisitor = DocumentSymbolsVisitor();
1318
stylesheet.accept(symbolsVisitor);
1419

20+
ls.cache.setDocumentSymbols(document, symbolsVisitor.symbols);
21+
1522
return symbolsVisitor.symbols;
1623
}
1724
}

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

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
2323
required lsp.SymbolKind kind,
2424
required lsp.Range symbolRange,
2525
lsp.Range? nameRange,
26-
lsp.Range? bodyRange, // TODO: delete if unused
2726
String? docComment,
2827
String? detail,
2928
List<lsp.SymbolTag>? tags}) {
@@ -49,9 +48,6 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
4948
var keep = <StylesheetDocumentSymbol>[];
5049

5150
for (var other in symbols) {
52-
// This probably scales terribly with document size.
53-
// A tree structure that uses ranges for lookup would probably be the ticket here.
54-
// We can maybe get away with it if we cache the result.
5551
if (_containsRange(symbol.range, other.range)) {
5652
symbol.children!.add(other);
5753
} else {
@@ -90,24 +86,6 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
9086
return false;
9187
}
9288

93-
lsp.Range? _bodyRange(sass.ParentStatement<List<sass.Statement>?> node) {
94-
if (node.children case var children?) {
95-
if (children.isEmpty) {
96-
return null;
97-
}
98-
99-
return lsp.Range(
100-
start: lsp.Position(
101-
line: children.first.span.start.line,
102-
character: children.first.span.start.column),
103-
end: lsp.Position(
104-
line: children.last.span.start.line,
105-
character: children.last.span.start.column));
106-
}
107-
108-
return null;
109-
}
110-
11189
String? _detail(sass.CallableDeclaration node) {
11290
var arguments = node.arguments.arguments
11391
.map<String>((arg) => arg.defaultValue != null
@@ -120,6 +98,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
12098
@override
12199
void visitAtRule(node) {
122100
super.visitAtRule(node);
101+
123102
if (!node.name.isPlain) {
124103
return;
125104
}
@@ -129,8 +108,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
129108
name: node.name.span.text,
130109
kind: lsp.SymbolKind.Class,
131110
symbolRange: toRange(node.span),
132-
nameRange: toRange(node.name.span),
133-
bodyRange: _bodyRange(node));
111+
nameRange: toRange(node.name.span));
134112
} else if (node.name.asPlain!.startsWith('keyframes')) {
135113
var keyframesName = node.span.context.split(' ').elementAtOrNull(1);
136114
if (keyframesName != null) {
@@ -147,8 +125,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
147125
name: keyframesName,
148126
kind: lsp.SymbolKind.Class,
149127
symbolRange: toRange(node.span),
150-
nameRange: keyframesNameRange,
151-
bodyRange: _bodyRange(node));
128+
nameRange: keyframesNameRange);
152129
}
153130
}
154131
}
@@ -161,8 +138,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
161138
name: node.name.span.text,
162139
kind: lsp.SymbolKind.Variable,
163140
symbolRange: toRange(node.span),
164-
nameRange: toRange(node.name.span),
165-
bodyRange: _bodyRange(node));
141+
nameRange: toRange(node.name.span));
166142
}
167143
}
168144

@@ -176,8 +152,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
176152
kind: lsp.SymbolKind.Function,
177153
docComment: node.comment?.docComment,
178154
symbolRange: toRange(node.span),
179-
nameRange: toRange(node.nameSpan),
180-
bodyRange: _bodyRange(node));
155+
nameRange: toRange(node.nameSpan));
181156
}
182157

183158
@override
@@ -202,8 +177,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
202177
name: '@media ${node.query.asPlain}',
203178
kind: lsp.SymbolKind.Module,
204179
symbolRange: toRange(node.span),
205-
nameRange: nameRange,
206-
bodyRange: _bodyRange(node));
180+
nameRange: nameRange);
207181
}
208182

209183
@override
@@ -216,8 +190,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
216190
kind: lsp.SymbolKind.Method,
217191
docComment: node.comment?.docComment,
218192
symbolRange: toRange(node.span),
219-
nameRange: toRange(node.nameSpan),
220-
bodyRange: _bodyRange(node));
193+
nameRange: toRange(node.nameSpan));
221194
}
222195

223196
@override
@@ -279,8 +252,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
279252
name: name!,
280253
kind: lsp.SymbolKind.Class,
281254
symbolRange: symbolRange!,
282-
nameRange: nameRange,
283-
bodyRange: _bodyRange(node));
255+
nameRange: nameRange);
284256
}
285257
} on sass.SassFormatException catch (_) {
286258
// Do nothing.

pkgs/sass_language_services/lib/src/language_services_cache.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class CacheEntry {
55
TextDocument document;
66
sass.Stylesheet stylesheet;
77
List<StylesheetDocumentLink>? links;
8+
List<StylesheetDocumentSymbol>? symbols;
89

910
CacheEntry({
1011
required this.document,
@@ -52,11 +53,20 @@ class LanguageServicesCache {
5253
return _cache[document.uri.toString()]?.links;
5354
}
5455

56+
List<StylesheetDocumentSymbol>? getDocumentSymbols(TextDocument document) {
57+
return _cache[document.uri.toString()]?.symbols;
58+
}
59+
5560
void setDocumentLinks(
5661
TextDocument document, List<StylesheetDocumentLink> links) {
5762
_cache[document.uri.toString()]?.links = links;
5863
}
5964

65+
void setDocumentSymbols(
66+
TextDocument document, List<StylesheetDocumentSymbol> symbols) {
67+
_cache[document.uri.toString()]?.symbols = symbols;
68+
}
69+
6070
Iterable<TextDocument> getDocuments() {
6171
return _cache.values.map((e) => e.document);
6272
}

0 commit comments

Comments
 (0)