Skip to content

Commit

Permalink
Bugfixing references, definition for custom properties
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Nov 23, 2024
1 parent 7f0ba31 commit 61f6f29
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ class FindReferencesVisitor
{bool includeDeclaration = false})
: _includeDeclaration = includeDeclaration;

@override
void visitDeclaration(sass.Declaration node) {
var isCustomPropertyDeclaration =
node.name.isPlain && node.name.asPlain!.startsWith('--');

if (isCustomPropertyDeclaration && _includeDeclaration) {
var name = node.name.asPlain!;
if (!name.contains(_name)) {
return;
}
var location = lsp.Location(
range: toRange(node.name.span),
uri: _document.uri,
);
candidates.add(
Reference(
name: name,
location: location,
kind: ReferenceKind.customProperty,
),
);
}
super.visitDeclaration(node);
}

@override
void visitExtendRule(sass.ExtendRule node) {
var isPlaceholderSelector =
Expand Down Expand Up @@ -142,22 +167,45 @@ class FindReferencesVisitor

@override
void visitFunctionExpression(sass.FunctionExpression node) {
var name = node.name;
if (!name.contains(_name)) {
super.visitFunctionExpression(node);
return;
var isCustomProperty =
node.name == 'var' && node.arguments.positional.isNotEmpty;
if (isCustomProperty) {
var expression = node.arguments.positional.first;
if (expression is sass.StringExpression &&
!expression.hasQuotes &&
expression.text.isPlain) {
var name = expression.text.asPlain!;
var location = lsp.Location(
range: toRange(expression.text.span),
uri: _document.uri,
);
candidates.add(
Reference(
name: name,
location: location,
kind: ReferenceKind.customProperty,
),
);
}
} else {
var name = node.name;
if (!name.contains(_name)) {
super.visitFunctionExpression(node);
return;
}
var location = lsp.Location(
range: toRange(node.nameSpan),
uri: _document.uri,
);
candidates.add(
Reference(
name: name,
location: location,
kind: ReferenceKind.function,
),
);
}
var location = lsp.Location(
range: toRange(node.nameSpan),
uri: _document.uri,
);
candidates.add(
Reference(
name: name,
location: location,
kind: ReferenceKind.function,
),
);

super.visitFunctionExpression(node);
}

Expand Down Expand Up @@ -232,6 +280,9 @@ class FindReferencesVisitor
super.visitMixinRule(node);
}

@override
void visitStringExpression(sass.StringExpression node) {}

@override
void visitStyleRule(sass.StyleRule node) {
if (!_includeDeclaration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,21 @@ class GoToDefinitionFeature extends LanguageFeature {

// Fall back to "@import-style" lookup on the whole workspace.
for (var document in ls.cache.getDocuments()) {
var symbols = ls.findDocumentSymbols(document);
for (var symbol in symbols) {
for (var kind in kinds) {
if (symbol.name == name && symbol.referenceKind == kind) {
return Definition(
name,
kind,
lsp.Location(uri: document.uri, range: symbol.selectionRange),
);
}
var stylesheet = ls.parseStylesheet(document);
var symbols = ScopedSymbols(stylesheet,
document.languageId == 'sass' ? Dialect.indented : Dialect.scss);

for (var kind in kinds) {
var symbol = symbols.globalScope.getSymbol(
name: name,
referenceKind: kind,
);
if (symbol != null) {
return Definition(
name,
kind,
lsp.Location(uri: document.uri, range: symbol.selectionRange),
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ ReferenceKind? getNodeReferenceKind(sass.AstNode node) {
return ReferenceKind.variable;
} else if (node is sass.VariableExpression) {
return ReferenceKind.variable;
} else if (node is sass.Declaration) {
var isCustomProperty =
node.name.isPlain && node.name.asPlain!.startsWith("--");
if (isCustomProperty) {
return ReferenceKind.customProperty;
}
} else if (node is sass.StringExpression) {
var isCustomProperty =
node.text.isPlain && node.text.asPlain!.startsWith("--");
Expand Down Expand Up @@ -52,6 +58,12 @@ String? getNodeName(sass.AstNode node) {
return node.name;
} else if (node is sass.VariableExpression) {
return node.name;
} else if (node is sass.Declaration) {
var isCustomProperty =
node.name.isPlain && node.name.asPlain!.startsWith("--");
if (isCustomProperty) {
return node.name.asPlain;
}
} else if (node is sass.StringExpression) {
var isCustomProperty =
node.text.isPlain && node.text.asPlain!.startsWith("--");
Expand Down
Loading

0 comments on commit 61f6f29

Please sign in to comment.