@@ -6,13 +6,19 @@ import '../document_symbols/stylesheet_document_symbol.dart';
6
6
import 'scope.dart' ;
7
7
8
8
final deprecated = RegExp (r'///\s*@deprecated' );
9
- final scopeStart = RegExp (r'[\{\n]' );
9
+ final openBracketOrNewline = RegExp (r'[\{\n]' );
10
+
11
+ enum Dialect { scss, indented }
10
12
11
13
/// Builds scopes and a list of symbols in those scopes starting at [scope] (typically the global scope).
12
14
class ScopeVisitor with sass.RecursiveStatementVisitor {
13
15
final Scope scope;
16
+ final Dialect dialect;
14
17
15
- ScopeVisitor (this .scope);
18
+ ScopeVisitor (
19
+ this .scope,
20
+ this .dialect,
21
+ );
16
22
17
23
void _addSymbol ({
18
24
required String name,
@@ -58,7 +64,8 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
58
64
void visitAtRule (node) {
59
65
if (node.children != null ) {
60
66
var nameEndIndex = node.name.span.end.offset - node.span.start.offset;
61
- var scopeIndex = node.span.text.indexOf (scopeStart, nameEndIndex);
67
+ var scopeIndex =
68
+ node.span.text.indexOf (openBracketOrNewline, nameEndIndex);
62
69
63
70
_addScope (
64
71
offset: node.span.start.offset + scopeIndex,
@@ -152,7 +159,7 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
152
159
@override
153
160
void visitForRule (sass.ForRule node) {
154
161
var toEndIndex = node.to.span.end.offset - node.span.start.offset;
155
- var scopeIndex = node.span.text.indexOf (scopeStart , toEndIndex);
162
+ var scopeIndex = node.span.text.indexOf (openBracketOrNewline , toEndIndex);
156
163
var scope = _addScope (
157
164
offset: node.span.start.offset + scopeIndex,
158
165
length: node.span.length - scopeIndex,
@@ -197,7 +204,7 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
197
204
);
198
205
199
206
var argsEndIndex = node.arguments.span.end.offset - node.span.start.offset;
200
- var scopeIndex = node.span.text.indexOf (scopeStart , argsEndIndex);
207
+ var scopeIndex = node.span.text.indexOf (openBracketOrNewline , argsEndIndex);
201
208
var scope = _addScope (
202
209
offset: node.span.start.offset + scopeIndex,
203
210
length: node.span.length - scopeIndex,
@@ -223,10 +230,43 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
223
230
224
231
@override
225
232
void visitIfRule (sass.IfRule node) {
226
- _addScope (
227
- offset: node.span.start.offset,
228
- length: node.span.length,
229
- );
233
+ // TODO: would be nice to have the spans for clauses from sass_api.
234
+ Scope ? previousClause;
235
+ for (var clause in node.clauses) {
236
+ var argsEndIndex =
237
+ clause.expression.span.end.offset - node.span.start.offset;
238
+ var scopeStartIndex =
239
+ node.span.text.indexOf (openBracketOrNewline, argsEndIndex);
240
+
241
+ var clauseChildrenLength = clause.children
242
+ .map <int >((e) => e.span.context.length)
243
+ .reduce ((value, element) => value + element);
244
+
245
+ var toMatch = dialect == Dialect .indented ? '\n ' : '}' ;
246
+
247
+ var scopeEndIndex = node.span.text
248
+ .indexOf (toMatch, scopeStartIndex + clauseChildrenLength);
249
+
250
+ previousClause = _addScope (
251
+ offset: node.span.start.offset + scopeStartIndex,
252
+ length: scopeEndIndex - scopeStartIndex + 1 ,
253
+ );
254
+ }
255
+
256
+ if (previousClause != null && node.lastClause != null ) {
257
+ var scopeIndex = node.span.text.indexOf (
258
+ openBracketOrNewline,
259
+ previousClause.offset -
260
+ node.span.start.offset +
261
+ previousClause.length +
262
+ "@else" .length);
263
+
264
+ _addScope (
265
+ offset: node.span.start.offset + scopeIndex,
266
+ length: node.span.length - scopeIndex,
267
+ );
268
+ }
269
+
230
270
super .visitIfRule (node);
231
271
}
232
272
@@ -242,7 +282,7 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
242
282
);
243
283
244
284
var argsEndIndex = node.arguments.span.end.offset - node.span.start.offset;
245
- var scopeIndex = node.span.text.indexOf (scopeStart , argsEndIndex);
285
+ var scopeIndex = node.span.text.indexOf (openBracketOrNewline , argsEndIndex);
246
286
var scope = _addScope (
247
287
offset: node.span.start.offset + scopeIndex,
248
288
length: node.span.length - scopeIndex,
@@ -349,11 +389,14 @@ class ScopeVisitor with sass.RecursiveStatementVisitor {
349
389
350
390
@override
351
391
void visitWhileRule (sass.WhileRule node) {
352
- var lengthSubtract =
392
+ var conditionEndIndex =
353
393
node.condition.span.end.offset - node.span.start.offset;
394
+ var scopeIndex =
395
+ node.span.text.indexOf (openBracketOrNewline, conditionEndIndex);
396
+
354
397
_addScope (
355
- offset: node.condition. span.end .offset,
356
- length: node.span.length - lengthSubtract ,
398
+ offset: node.span.start .offset + scopeIndex ,
399
+ length: node.span.length - scopeIndex ,
357
400
);
358
401
359
402
super .visitWhileRule (node);
0 commit comments