|
| 1 | +import 'package:lsp_server/lsp_server.dart' as lsp; |
| 2 | +import 'package:sass_language_services/sass_language_services.dart'; |
| 3 | +import 'package:sass_language_services/src/features/find_references/find_references_visitor.dart'; |
| 4 | +import 'package:sass_language_services/src/features/go_to_definition/go_to_definition_feature.dart'; |
| 5 | + |
| 6 | +import '../../sass/sass_data.dart'; |
| 7 | +import '../go_to_definition/definition.dart'; |
| 8 | +import 'reference.dart'; |
| 9 | + |
| 10 | +class FindReferencesFeature extends GoToDefinitionFeature { |
| 11 | + FindReferencesFeature({required super.ls}); |
| 12 | + |
| 13 | + Future<List<lsp.Location>> findReferences(TextDocument document, |
| 14 | + lsp.Position position, lsp.ReferenceContext context) async { |
| 15 | + var references = await internalFindReferences(document, position, context); |
| 16 | + return references.references.map((r) => r.location).toList(); |
| 17 | + } |
| 18 | + |
| 19 | + Future<({Definition? definition, List<Reference> references})> |
| 20 | + internalFindReferences(TextDocument document, lsp.Position position, |
| 21 | + lsp.ReferenceContext context) async { |
| 22 | + var references = <Reference>[]; |
| 23 | + var definition = await internalGoToDefinition(document, position); |
| 24 | + if (definition == null) { |
| 25 | + return (definition: definition, references: references); |
| 26 | + } |
| 27 | + |
| 28 | + String? builtin; |
| 29 | + if (definition.location == null) { |
| 30 | + // If we don't have a location we might be dealing with a built-in. |
| 31 | + var sassData = SassData(); |
| 32 | + for (var module in sassData.modules) { |
| 33 | + for (var function in module.functions) { |
| 34 | + if (function.name == definition.name) { |
| 35 | + builtin = function.name; |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + for (var variable in module.variables) { |
| 40 | + if (variable.name == definition.name) { |
| 41 | + builtin = variable.name; |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + if (builtin != null) { |
| 46 | + break; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (definition.location == null && builtin == null) { |
| 52 | + return (definition: definition, references: references); |
| 53 | + } |
| 54 | + |
| 55 | + var name = builtin ?? definition.name; |
| 56 | + |
| 57 | + var candidates = <Reference>[]; |
| 58 | + // Go through all documents with a visitor. |
| 59 | + // For each document, collect candidates that match the definition name. |
| 60 | + for (var document in ls.cache.getDocuments()) { |
| 61 | + var stylesheet = ls.parseStylesheet(document); |
| 62 | + var visitor = FindReferencesVisitor( |
| 63 | + document, |
| 64 | + name, |
| 65 | + includeDeclaration: context.includeDeclaration, |
| 66 | + ); |
| 67 | + stylesheet.accept(visitor); |
| 68 | + candidates.addAll(visitor.candidates); |
| 69 | + |
| 70 | + // Go through all candidates and add matches to references. |
| 71 | + // A match is a candidate with the same name, referenceKind, |
| 72 | + // and whose definition is the same as the definition of the |
| 73 | + // symbol at [position]. |
| 74 | + for (var candidate in candidates) { |
| 75 | + if (builtin case var name?) { |
| 76 | + if (name.contains(candidate.name)) { |
| 77 | + references.add( |
| 78 | + Reference( |
| 79 | + name: candidate.name, |
| 80 | + kind: candidate.kind, |
| 81 | + location: candidate.location, |
| 82 | + defaultBehavior: true, |
| 83 | + ), |
| 84 | + ); |
| 85 | + } |
| 86 | + } else { |
| 87 | + if (candidate.kind != definition.kind) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + var candidateIsDefinition = _isSameLocation( |
| 92 | + candidate.location, |
| 93 | + definition.location!, |
| 94 | + ); |
| 95 | + |
| 96 | + if (!context.includeDeclaration && candidateIsDefinition) { |
| 97 | + continue; |
| 98 | + } else if (candidateIsDefinition) { |
| 99 | + references.add(candidate); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + // Find the definition of the candidate and compare it |
| 104 | + // to the definition of the symbol at [position]. If |
| 105 | + // the two definitions are the same, we have a reference. |
| 106 | + var candidateDefinition = await internalGoToDefinition( |
| 107 | + document, |
| 108 | + position, |
| 109 | + ); |
| 110 | + |
| 111 | + if (candidateDefinition != null && |
| 112 | + candidateDefinition.location != null) { |
| 113 | + if (_isSameLocation( |
| 114 | + candidateDefinition.location!, |
| 115 | + definition.location!, |
| 116 | + )) { |
| 117 | + references.add(candidate); |
| 118 | + continue; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return (definition: definition, references: references); |
| 126 | + } |
| 127 | + |
| 128 | + bool _isSameLocation(lsp.Location a, lsp.Location b) { |
| 129 | + return a.uri.toString() == b.uri.toString() && |
| 130 | + a.range.start.line == b.range.start.line && |
| 131 | + a.range.start.character == b.range.start.character && |
| 132 | + a.range.end.line == b.range.end.line && |
| 133 | + a.range.end.character == b.range.end.character; |
| 134 | + } |
| 135 | +} |
0 commit comments