Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find references #28

Merged
merged 9 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion pkgs/sass_language_server/lib/src/language_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class LanguageServer {
connection: _connection,
onDidChangeContent: (params) async {
try {
_ls.cache.remove(params.document.uri);
// Reparse the stylesheet to update the cache with the new
// version of the document.
_ls.parseStylesheet(params.document);
if (initialScan != null) {
await initialScan;
}
Expand Down Expand Up @@ -174,6 +176,7 @@ class LanguageServer {
definitionProvider: Either2.t1(true),
documentLinkProvider: DocumentLinkOptions(resolveProvider: false),
documentSymbolProvider: Either2.t1(true),
referencesProvider: Either2.t1(true),
textDocumentSync: Either2.t1(TextDocumentSyncKind.Incremental),
workspaceSymbolProvider: Either2.t1(true),
);
Expand Down Expand Up @@ -313,6 +316,32 @@ class LanguageServer {
_connection.peer
.registerMethod('textDocument/documentSymbol', onDocumentSymbol);

_connection.onReferences((params) async {
try {
var document = _documents.get(params.textDocument.uri);
if (document == null) return [];

var configuration = _getLanguageConfiguration(document);
if (configuration.references.enabled) {
if (initialScan != null) {
await initialScan;
}

var result = await _ls.findReferences(
document,
params.position,
params.context,
);
return result;
} else {
return [];
}
} on Exception catch (e) {
_log.debug(e.toString());
return [];
}
});

// TODO: add this handler upstream
Future<List<WorkspaceSymbol>> onWorkspaceSymbol(dynamic params) async {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class DocumentLinksConfiguration extends FeatureConfiguration {
DocumentLinksConfiguration({required super.enabled});
}

class ReferencesConfiguration extends FeatureConfiguration {
ReferencesConfiguration({required super.enabled});
}

class WorkspaceSymbolsConfiguration extends FeatureConfiguration {
WorkspaceSymbolsConfiguration({required super.enabled});
}
Expand All @@ -24,6 +28,7 @@ class LanguageConfiguration {
late final DefinitionConfiguration definition;
late final DocumentSymbolsConfiguration documentSymbols;
late final DocumentLinksConfiguration documentLinks;
late final ReferencesConfiguration references;
late final WorkspaceSymbolsConfiguration workspaceSymbols;

LanguageConfiguration.from(dynamic config) {
Expand All @@ -33,6 +38,8 @@ class LanguageConfiguration {
enabled: config?['documentSymbols']?['enabled'] as bool? ?? true);
documentLinks = DocumentLinksConfiguration(
enabled: config?['documentLinks']?['enabled'] as bool? ?? true);
references = ReferencesConfiguration(
enabled: config?['references']?['enabled'] as bool? ?? true);
workspaceSymbols = WorkspaceSymbolsConfiguration(
enabled: config?['workspaceSymbols']?['enabled'] as bool? ?? true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,7 @@ class DocumentSymbolsVisitor with sass.RecursiveStatementVisitor {
}

if (nameRange == null) {
// The selector span seems to be relative to node, not to the file.
nameRange = lsp.Range(
start: lsp.Position(
line: node.span.start.line + selector.span.start.line,
character: node.span.start.column + selector.span.start.column,
),
end: lsp.Position(
line: node.span.start.line + selector.span.end.line,
character: node.span.start.column + selector.span.end.column,
),
);
nameRange = selectorNameRange(node, selector);

// symbolRange: start position of selector's nameRange, end of stylerule (node.span.end).
symbolRange = lsp.Range(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import 'package:lsp_server/lsp_server.dart' as lsp;
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/features/go_to_definition/go_to_definition_feature.dart';

import '../../sass/sass_data.dart';
import '../go_to_definition/definition.dart';
import 'reference.dart';

class FindReferencesFeature extends GoToDefinitionFeature {
FindReferencesFeature({required super.ls});

Future<List<lsp.Location>> findReferences(TextDocument document,
lsp.Position position, lsp.ReferenceContext context) async {
var references = await internalFindReferences(document, position, context);
return references.references.map((r) => r.location).toList();
}

Future<({Definition? definition, List<Reference> references})>
internalFindReferences(TextDocument document, lsp.Position position,
lsp.ReferenceContext context) async {
var references = <Reference>[];
var definition = await internalGoToDefinition(document, position);
if (definition == null) {
return (definition: definition, references: references);
}

String? builtin;
if (definition.location == null) {
// If we don't have a location we might be dealing with a built-in.
var sassData = SassData();
for (var module in sassData.modules) {
for (var function in module.functions) {
if (function.name == definition.name) {
builtin = function.name;
break;
}
}
for (var variable in module.variables) {
if (variable.name == definition.name) {
builtin = variable.name;
break;
}
}
if (builtin != null) {
break;
}
}
}

if (definition.location == null && builtin == null) {
return (definition: definition, references: references);
}

var name = builtin ?? definition.name;

var documents = ls.cache.getDocuments();
// Go through all documents with a visitor.
// For each document, collect candidates that match the definition name.
for (var document in documents) {
var stylesheet = ls.parseStylesheet(document);
var visitor = FindReferencesVisitor(
document,
name,
includeDeclaration: context.includeDeclaration,
isBuiltin: builtin != null,
);
stylesheet.accept(visitor);

// Go through all candidates and add matches to references.
// A match is a candidate with the same name, referenceKind,
// and whose definition is the same as the definition of the
// symbol at [position].
var candidates = visitor.candidates;
for (var candidate in candidates) {
if (builtin case var name?) {
if (name.contains(candidate.name)) {
references.add(
Reference(
name: candidate.name,
kind: candidate.kind,
location: candidate.location,
defaultBehavior: true,
),
);
}
} else {
if (candidate.kind != definition.kind) {
continue;
}

var candidateIsDefinition = _isSameLocation(
candidate.location,
definition.location!,
);

if (candidateIsDefinition) {
references.add(candidate);
continue;
}

// Find the definition of the candidate and compare it
// to the definition of the symbol at [position]. If
// the two definitions are the same, we have a reference.
var candidateDefinition = await internalGoToDefinition(
document,
candidate.location.range.start,
);

if (candidateDefinition != null &&
candidateDefinition.location != null) {
if (_isSameLocation(
candidateDefinition.location!,
definition.location!,
)) {
references.add(candidate);
continue;
}
} else {
continue;
}
}
}
}

return (definition: definition, references: references);
}

bool _isSameLocation(lsp.Location a, lsp.Location b) {
return a.uri.toString() == b.uri.toString() &&
a.range.start.line == b.range.start.line &&
a.range.start.character == b.range.start.character &&
a.range.end.line == b.range.end.line &&
a.range.end.character == b.range.end.character;
}
}
Loading