Skip to content

Commit

Permalink
[css] Random red highlighting on code (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli authored Jun 24, 2024
1 parent 028c5be commit f6e7034
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 56 deletions.
29 changes: 12 additions & 17 deletions src/parser/cssNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ export enum NodeType {
LayerNameList,
LayerName,
PropertyAtRule,
Container
Container,
ModuleConfig,
}

export enum ReferenceType {
Expand Down Expand Up @@ -1042,16 +1043,17 @@ export class Import extends Node {
export class Use extends Node {

public identifier?: Identifier;
public parameters?: Nodelist;
public parameters?: Node;

public get type(): NodeType {
return NodeType.Use;
}

public getParameters(): Nodelist {
if (!this.parameters) {
this.parameters = new Nodelist(this);
}
public setParameters(value: Node | null): value is Node{
return this.setNode('parameters', value);
}

public getParameters(): Node | undefined {
return this.parameters;
}

Expand Down Expand Up @@ -1097,8 +1099,7 @@ export class ModuleConfiguration extends Node {
export class Forward extends Node {

public identifier?: Node;
public members?: Nodelist;
public parameters?: Nodelist;
public parameters?: Node;

public get type(): NodeType {
return NodeType.Forward;
Expand All @@ -1112,17 +1113,11 @@ export class Forward extends Node {
return this.identifier;
}

public getMembers(): Nodelist {
if (!this.members) {
this.members = new Nodelist(this);
}
return this.members;
public setParameters(value: Node | null): value is Node{
return this.setNode('parameters', value);
}

public getParameters(): Nodelist {
if (!this.parameters) {
this.parameters = new Nodelist(this);
}
public getParameters(): Node | undefined {
return this.parameters;
}

Expand Down
66 changes: 28 additions & 38 deletions src/parser/scssParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class SCSSParser extends cssParser.Parser {
return this._parseInterpolation() || super._parseMediaCondition();
}

public _parseMediaFeatureRangeOperator() : boolean {
public _parseMediaFeatureRangeOperator(): boolean {
return this.accept(scssScanner.SmallerEqualsOperator) || this.accept(scssScanner.GreaterEqualsOperator) || super._parseMediaFeatureRangeOperator();
}

Expand Down Expand Up @@ -816,33 +816,41 @@ export class SCSSParser extends cssParser.Parser {
}

if (this.acceptIdent('with')) {
if (!this.accept(TokenType.ParenthesisL)) {
if (!node.setParameters(this._parseModuleConfig())) {
return this.finish(node, ParseError.LeftParenthesisExpected, [TokenType.ParenthesisR]);
}
}
}

// First variable statement, no comma.
if (!node.getParameters().addChild(this._parseModuleConfigDeclaration())) {
return this.finish(node, ParseError.VariableNameExpected);
}
if (!this.accept(TokenType.SemiColon) && !this.accept(TokenType.EOF)) {
return this.finish(node, ParseError.SemiColonExpected);
}

while (this.accept(TokenType.Comma)) {
if (this.peek(TokenType.ParenthesisR)) {
break;
}
if (!node.getParameters().addChild(this._parseModuleConfigDeclaration())) {
return this.finish(node, ParseError.VariableNameExpected);
}
}
return this.finish(node);
}

if (!this.accept(TokenType.ParenthesisR)) {
return this.finish(node, ParseError.RightParenthesisExpected);
}
public _parseModuleConfig(): nodes.Node | null {
const node = this.createNode(nodes.NodeType.ModuleConfig);
if (!this.accept(TokenType.ParenthesisL)) {
return null;
}

// First variable statement, no comma.
if (!node.addChild(this._parseModuleConfigDeclaration())) {
return this.finish(node, ParseError.VariableNameExpected);
}

while (this.accept(TokenType.Comma)) {
if (this.peek(TokenType.ParenthesisR)) {
break;
}
if (!node.addChild(this._parseModuleConfigDeclaration())) {
return this.finish(node, ParseError.VariableNameExpected);
}
}

if (!this.accept(TokenType.SemiColon) && !this.accept(TokenType.EOF)) {
return this.finish(node, ParseError.SemiColonExpected);
if (!this.accept(TokenType.ParenthesisR)) {
return this.finish(node, ParseError.RightParenthesisExpected);
}

return this.finish(node);
Expand Down Expand Up @@ -894,28 +902,10 @@ export class SCSSParser extends cssParser.Parser {
}

if (this.acceptIdent('with')) {
if (!this.accept(TokenType.ParenthesisL)) {
if (!node.setParameters(this._parseModuleConfig())) {
return this.finish(node, ParseError.LeftParenthesisExpected, [TokenType.ParenthesisR]);
}

// First variable statement, no comma.
if (!node.getParameters().addChild(this._parseModuleConfigDeclaration())) {
return this.finish(node, ParseError.VariableNameExpected);
}

while (this.accept(TokenType.Comma)) {
if (this.peek(TokenType.ParenthesisR)) {
break;
}
if (!node.getParameters().addChild(this._parseModuleConfigDeclaration())) {
return this.finish(node, ParseError.VariableNameExpected);
}
}

if (!this.accept(TokenType.ParenthesisR)) {
return this.finish(node, ParseError.RightParenthesisExpected);
}

} else if (this.peekIdent('hide') || this.peekIdent('show')) {
if (!node.addChild(this._parseForwardVisibility())) {
return this.finish(node, ParseError.IdentifierOrVariableExpected);
Expand Down
2 changes: 1 addition & 1 deletion src/services/cssNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class CSSNavigation {
private getHighlightNode(document: TextDocument, position: Position, stylesheet: nodes.Stylesheet): nodes.Node | undefined {
const offset = document.offsetAt(position);
let node = nodes.getNodeAtOffset(stylesheet, offset);
if (!node || node.type === nodes.NodeType.Stylesheet || node.type === nodes.NodeType.Declarations) {
if (!node || node.type === nodes.NodeType.Stylesheet || node.type === nodes.NodeType.Declarations || node.type === nodes.NodeType.ModuleConfig) {
return;
}
if (node.type === nodes.NodeType.Identifier && node.parent && node.parent.type === nodes.NodeType.ClassSelector) {
Expand Down

0 comments on commit f6e7034

Please sign in to comment.