Skip to content

Commit

Permalink
Refactor to reuse span as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Nov 24, 2024
1 parent 7140762 commit 21bba84
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,16 @@ class DocumentLinkVisitor with sass.RecursiveStatementVisitor {
}

const isSassLink = false;
var urlSpan = staticImport.url.span;
unresolvedLinks.add((
StylesheetDocumentLink(
type: LinkType.import,
target: target,
range: lsp.Range(
end: lsp.Position(
line: staticImport.url.span.end.line,
character: staticImport.url.span.end.column),
line: urlSpan.end.line, character: urlSpan.end.column),
start: lsp.Position(
line: staticImport.url.span.start.line,
character: staticImport.url.span.start.column),
line: urlSpan.start.line, character: urlSpan.start.column),
),
),
isSassLink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,33 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
}

if (node.name.asPlain == 'font-face') {
var nameSpan = node.name.span;
_collect(
name: node.name.span.text,
name: nameSpan.text,
referenceKind: ReferenceKind.fontFace,
symbolRange: toRange(node.span),
nameRange: toRange(node.name.span),
nameRange: toRange(nameSpan),
);
} else if (node.name.asPlain!.startsWith('keyframes')) {
var keyframesName = node.span.context.split(' ').elementAtOrNull(1);
var span = node.span;
var nameSpan = node.name.span;
var keyframesName = span.context.split(' ').elementAtOrNull(1);
if (keyframesName != null) {
var keyframesNameRange = lsp.Range(
start: lsp.Position(
line: node.name.span.start.line,
character: node.name.span.end.column + 1,
line: nameSpan.start.line,
character: nameSpan.end.column + 1,
),
end: lsp.Position(
line: node.name.span.end.line,
character: node.name.span.end.column + 1 + keyframesName.length,
line: nameSpan.end.line,
character: nameSpan.end.column + 1 + keyframesName.length,
),
);

_collect(
name: keyframesName,
referenceKind: ReferenceKind.keyframe,
symbolRange: toRange(node.span),
symbolRange: toRange(span),
nameRange: keyframesNameRange,
);
}
Expand All @@ -141,11 +144,12 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
var isCustomProperty =
node.name.isPlain && node.name.asPlain!.startsWith("--");
if (isCustomProperty) {
var nameSpan = node.name.span;
_collect(
name: node.name.span.text,
name: nameSpan.text,
referenceKind: ReferenceKind.customProperty,
symbolRange: toRange(node.span),
nameRange: toRange(node.name.span),
nameRange: toRange(nameSpan),
);
}
}
Expand All @@ -172,14 +176,15 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
}

// node.query.span includes whitespace, so the range doesn't match node.query.asPlain
var querySpan = node.query.span;
var nameRange = lsp.Range(
start: lsp.Position(
line: node.query.span.start.line,
character: node.query.span.start.column,
line: querySpan.start.line,
character: querySpan.start.column,
),
end: lsp.Position(
line: node.query.span.end.line,
character: node.query.span.start.column + node.query.asPlain!.length,
line: querySpan.end.line,
character: querySpan.start.column + node.query.asPlain!.length,
),
);

Expand Down Expand Up @@ -222,16 +227,17 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
lsp.Range? symbolRange;

for (var component in complexSelector.components) {
var selector = component.selector;
var selectorSpan = component.selector.span;
var span = node.span;

if (name == null) {
name = selector.span.text;
name = selectorSpan.text;
} else {
name = '$name ${selector.span.text}';
name = '$name ${selectorSpan.text}';
}

if (nameRange == null) {
nameRange = selectorNameRange(node, selector);
nameRange = selectorNameRange(node: span, selector: selectorSpan);

// symbolRange: start position of selector's nameRange, end of stylerule (node.span.end).
symbolRange = lsp.Range(
Expand All @@ -240,17 +246,17 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
character: nameRange.start.character,
),
end: lsp.Position(
line: node.span.end.line,
character: node.span.end.column,
line: span.end.line,
character: span.end.column,
),
);
} else {
// Move the end of the name range down to include this selector component
nameRange = lsp.Range(
start: nameRange.start,
end: lsp.Position(
line: node.span.start.line + selector.span.end.line,
character: node.span.start.column + selector.span.end.column,
line: span.start.line + selectorSpan.end.line,
character: span.start.column + selectorSpan.end.column,
),
);
}
Expand All @@ -271,20 +277,21 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
@override
void visitVariableDeclaration(node) {
super.visitVariableDeclaration(node);
var nameSpan = node.nameSpan;
_collect(
name: node.nameSpan.text,
name: nameSpan.text,
referenceKind: ReferenceKind.variable,
docComment: node.comment?.docComment,
symbolRange: toRange(node.span),
nameRange: lsp.Range(
start: lsp.Position(
line: node.nameSpan.start.line,
line: nameSpan.start.line,
// the span includes $
character: node.nameSpan.start.column,
character: nameSpan.start.column,
),
end: lsp.Position(
line: node.nameSpan.end.line,
character: node.nameSpan.end.column,
line: nameSpan.end.line,
character: nameSpan.end.column,
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,16 @@ class FindReferencesVisitor
}

var component = complexSelector.components.first;
var selector = component.selector;
var name = selector.span.text;
var selectorSpan = component.selector.span;
var name = selectorSpan.text;
if (!name.contains(_name)) {
continue;
}

var nameRange = selectorNameRange(node, selector);
var nameRange = selectorNameRange(
node: node.span,
selector: selectorSpan,
);

candidates.add(
Reference(
Expand Down
Loading

0 comments on commit 21bba84

Please sign in to comment.