Skip to content

Commit

Permalink
Document highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud committed Nov 24, 2024
1 parent 21bba84 commit 5fd5092
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
52 changes: 52 additions & 0 deletions extension/test/electron/definition/document-highlights.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const assert = require('node:assert');
const path = require('node:path');
const vscode = require('vscode');
const { showFile, sleepCI } = require('../util');

const stylesUri = vscode.Uri.file(
path.resolve(__dirname, 'fixtures', 'styles.scss')
);

before(async () => {
await showFile(stylesUri);
await sleepCI();
});

after(async () => {
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});

/**
* @param {import('vscode').Uri} documentUri
* @returns {Promise<Array<import('vscode').DocumentHighlight>>}
*/
async function getHighlights(documentUri, position) {
const result = await vscode.commands.executeCommand(
'vscode.executeDocumentHighlights',
documentUri,
position
);
return result;
}

test('gets document highlights', async () => {
const [first, second] = await getHighlights(
stylesUri,
new vscode.Position(7, 5)
);

assert.ok(first, 'Should have found highlights');
assert.ok(second, 'Should have found two highlights');

assert.equal(first.range.start.line, 7);
assert.equal(first.range.start.character, 2);

assert.equal(first.range.end.line, 7);
assert.equal(first.range.end.character, 14);

assert.equal(second.range.start.line, 11);
assert.equal(second.range.start.character, 13);

assert.equal(second.range.end.line, 11);
assert.equal(second.range.end.character, 25);
});
8 changes: 8 additions & 0 deletions extension/test/electron/definition/fixtures/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
.a {
@extend %brand;
}

:root {
--color-text: #000;
}

body {
color: var(--color-text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:lsp_server/lsp_server.dart' as lsp;
import 'package:sass_api/sass_api.dart' as sass;
import 'package:sass_language_services/sass_language_services.dart';
import 'package:sass_language_services/src/features/find_references/find_references_visitor.dart';
import 'package:sass_language_services/src/utils/sass_lsp_utils.dart';

import '../go_to_definition/scope_visitor.dart';
import '../go_to_definition/scoped_symbols.dart';
Expand Down Expand Up @@ -39,17 +38,21 @@ class DocumentHighlightsFeature extends LanguageFeature {
var symbol = symbols.findSymbolFromNode(node);

var result = <lsp.DocumentHighlight>[];
var references = FindReferencesVisitor(
var visitor = FindReferencesVisitor(
document,
name,
includeDeclaration: true,
);

for (var reference in references.candidates) {
stylesheet.accept(visitor);

for (var reference in visitor.candidates) {
if (symbol != null) {
if (symbol.referenceKind == reference.kind &&
symbol.name == reference.name &&
isSameRange(symbol.range, reference.location.range)) {
if (symbols.matchesSymbol(
reference,
document.offsetAt(reference.location.range.start),
symbol,
)) {
result.add(
lsp.DocumentHighlight(range: reference.location.range),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:sass_api/sass_api.dart' as sass;
import 'package:sass_language_services/src/utils/sass_lsp_utils.dart';

import '../document_symbols/stylesheet_document_symbol.dart';
import '../find_references/reference.dart';
import 'scope.dart';
import 'scope_visitor.dart';

Expand Down Expand Up @@ -123,20 +125,20 @@ class ScopedSymbols {

var referenceKind = getNodeReferenceKind(node);
if (referenceKind != null) {
return _findSymbol(node, referenceKind);
return _findSymbol(
getNodeName(node), node.span.start.offset, referenceKind);
}

return null;
}

StylesheetDocumentSymbol? _findSymbol(
sass.AstNode node, ReferenceKind referenceKind) {
var name = getNodeName(node);
String? name, int offset, ReferenceKind referenceKind) {
if (name == null) {
return null;
}

var scope = globalScope.findScope(offset: node.span.start.offset);
var scope = globalScope.findScope(offset: offset);
while (scope != null) {
var symbol = scope.getSymbol(name: name, referenceKind: referenceKind);
if (symbol != null) {
Expand Down Expand Up @@ -167,4 +169,15 @@ class ScopedSymbols {
}
return result;
}

bool matchesSymbol(
Reference reference, int offset, StylesheetDocumentSymbol symbol) {
var referenceSymbol = _findSymbol(reference.name, offset, reference.kind);
if (referenceSymbol == null) {
return false;
}
return referenceSymbol.name == symbol.name &&
referenceSymbol.kind == symbol.kind &&
isSameRange(referenceSymbol.range, symbol.range);
}
}

0 comments on commit 5fd5092

Please sign in to comment.