Skip to content

Commit

Permalink
Multiple variables in @each: support in scope (for microsoft/vscode#1758
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aeschli committed Jan 23, 2017
1 parent 505085c commit 3fe805a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/parser/cssNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ export class ForStatement extends BodyDeclaration {
}

export class EachStatement extends BodyDeclaration {
public variable: Variable;
public variables: Nodelist;

constructor(offset: number, length: number) {
super(offset, length);
Expand All @@ -840,8 +840,11 @@ export class EachStatement extends BodyDeclaration {
return NodeType.Each;
}

public setVariable(node: Variable): boolean {
return this.setNode('variable', node, 0);
public getVariables(): Nodelist {
if (!this.variables) {
this.variables = new Nodelist(this);
}
return this.variables;
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/parser/cssSymbolScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'use strict';

import * as nodes from './cssNodes';
import {findFirst} from '../utils/arrays';
import { findFirst } from '../utils/arrays';

export class Scope {

Expand Down Expand Up @@ -160,11 +160,20 @@ export class ScopeBuilder implements nodes.IVisitor {
this.addScope(node);
return true;
case nodes.NodeType.For:
let forNode = <nodes.ForStatement>node;
let scopeNode = forNode.getDeclarations();
if (scopeNode) {
this.addSymbolToChildScope(scopeNode, forNode.variable, forNode.variable.getName(), null, nodes.ReferenceType.Variable);
}
return true;
case nodes.NodeType.Each: {
let forOrEachNode = <nodes.ForStatement | nodes.EachStatement>node;
let scopeNode = forOrEachNode.getDeclarations();
let eachNode = <nodes.EachStatement>node;
let scopeNode = eachNode.getDeclarations();
if (scopeNode) {
this.addSymbolToChildScope(scopeNode, forOrEachNode.variable, forOrEachNode.variable.getName(), null, nodes.ReferenceType.Variable);
let variables = <nodes.Variable[]>eachNode.getVariables().getChildren();
for (let variable of variables) {
this.addSymbolToChildScope(scopeNode, variable, variable.getName(), null, nodes.ReferenceType.Variable);
}
}
return true;
}
Expand Down
6 changes: 4 additions & 2 deletions src/parser/scssParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,16 @@ export class SCSSParser extends cssParser.Parser {

let node = <nodes.EachStatement>this.create(nodes.EachStatement);
this.consumeToken(); // @each
if (!node.setVariable(this._parseVariable())) {
let variables = node.getVariables();
if (!variables.addChild(this._parseVariable())) {
return this.finish(node, ParseError.VariableNameExpected, [TokenType.CurlyR]);
}
while (this.accept(TokenType.Comma)) {
if (!node.setVariable(this._parseVariable())) {
if (!variables.addChild(this._parseVariable())) {
return this.finish(node, ParseError.VariableNameExpected, [TokenType.CurlyR]);
}
}
this.finish(variables);
if (!this.accept(TokenType.Ident, 'in')) {
return this.finish(node, SCSSParseError.InExpected, [TokenType.CurlyR]);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/scss/scssNavigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ suite('SCSS - Symbols', () => {
assertScopesAndSymbols(p, '$var1: 3; @while $var1 < 2 { #rule { a: b; } }', '$var1,[#rule,[]]');
assertScopesAndSymbols(p, '$i:0; @each $name in f1, f2, f3 { $i:$i+1; }', '$i,[$name,$i]');
assertScopesAndSymbols(p, '$i:0; @for $x from $i to 5 { }', '$i,[$x]');
assertScopesAndSymbols(p, '@each $i, $j, $k in f1, f2, f3 { }', '[$i,$j,$k]');
});

test('mark highlights', function(testDone) {
Expand Down

0 comments on commit 3fe805a

Please sign in to comment.