Skip to content

Commit 431b6d8

Browse files
committed
Add workspace symbols feature
1 parent 315213e commit 431b6d8

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

pkgs/sass_language_server/lib/src/language_server.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class LanguageServer {
173173
documentLinkProvider: DocumentLinkOptions(resolveProvider: false),
174174
documentSymbolProvider: Either2.t1(true),
175175
textDocumentSync: Either2.t1(TextDocumentSyncKind.Incremental),
176+
workspaceSymbolProvider: Either2.t1(true),
176177
);
177178

178179
var result = InitializeResult(capabilities: serverCapabilities);
@@ -285,6 +286,22 @@ class LanguageServer {
285286
_connection.peer
286287
.registerMethod('textDocument/documentSymbol', onDocumentSymbol);
287288

289+
// TODO: add this handler upstream
290+
Future<List<WorkspaceSymbol>> onWorkspaceSymbol(dynamic params) async {
291+
try {
292+
var query =
293+
(params.value as Map<String, Object?>)['query'] as String?;
294+
295+
var result = _ls.findWorkspaceSymbols(query);
296+
return result;
297+
} on Exception catch (e) {
298+
_log.debug(e.toString());
299+
return [];
300+
}
301+
}
302+
303+
_connection.peer.registerMethod('workspace/symbol', onWorkspaceSymbol);
304+
288305
_connection.onShutdown(() async {
289306
await _socket?.close();
290307
exitCode = 0;

pkgs/sass_language_services/lib/src/configuration/language_configuration.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ class DocumentLinksConfiguration extends FeatureConfiguration {
1212
DocumentLinksConfiguration({required super.enabled});
1313
}
1414

15+
class WorkspaceSymbolsConfiguration extends FeatureConfiguration {
16+
WorkspaceSymbolsConfiguration({required super.enabled});
17+
}
18+
1519
class LanguageConfiguration {
1620
late final DocumentSymbolsConfiguration documentSymbols;
1721
late final DocumentLinksConfiguration documentLinks;
22+
late final WorkspaceSymbolsConfiguration workspaceSymbols;
1823

1924
LanguageConfiguration.from(dynamic config) {
2025
documentSymbols = DocumentSymbolsConfiguration(
2126
enabled: config?['documentSymbols']?['enabled'] as bool? ?? true);
2227
documentLinks = DocumentLinksConfiguration(
2328
enabled: config?['documentLinks']?['enabled'] as bool? ?? true);
29+
workspaceSymbols = WorkspaceSymbolsConfiguration(
30+
enabled: config?['workspaceSymbols']?['enabled'] as bool? ?? true);
2431
}
2532
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:lsp_server/lsp_server.dart' as lsp;
2+
3+
import '../language_feature.dart';
4+
5+
class WorkspaceSymbolsFeature extends LanguageFeature {
6+
WorkspaceSymbolsFeature({required super.ls});
7+
8+
List<lsp.WorkspaceSymbol> findWorkspaceSymbols(String? query) {
9+
var result = <lsp.WorkspaceSymbol>[];
10+
for (var document in ls.cache.getDocuments()) {
11+
// This is the exception to the rule that this enabled check
12+
// should happen at the server edge. It's only at this point
13+
// we know if the document should be included or not.
14+
var config = getLanguageConfiguration(document);
15+
if (config.workspaceSymbols.enabled) {
16+
var symbols = ls.findDocumentSymbols(document);
17+
for (var symbol in symbols) {
18+
if (query != null && !symbol.name.contains(query)) {
19+
continue;
20+
}
21+
22+
result.add(lsp.WorkspaceSymbol(
23+
kind: symbol.kind,
24+
location: lsp.Either2.t1(
25+
lsp.Location(range: symbol.range, uri: document.uri)),
26+
name: symbol.name,
27+
tags: symbol.tags));
28+
}
29+
}
30+
}
31+
return result;
32+
}
33+
}

pkgs/sass_language_services/lib/src/language_services.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:sass_language_services/sass_language_services.dart';
44

55
import 'features/document_links/document_links_feature.dart';
66
import 'features/document_symbols/document_symbols_feature.dart';
7+
import 'features/workspace_symbols/workspace_symbols_feature.dart';
78
import 'language_services_cache.dart';
89

910
class LanguageServices {
@@ -16,13 +17,15 @@ class LanguageServices {
1617

1718
late final DocumentLinksFeature _documentLinks;
1819
late final DocumentSymbolsFeature _documentSymbols;
20+
late final WorkspaceSymbolsFeature _workspaceSymbols;
1921

2022
LanguageServices({
2123
required this.clientCapabilities,
2224
required this.fs,
2325
}) : cache = LanguageServicesCache() {
2426
_documentLinks = DocumentLinksFeature(ls: this);
2527
_documentSymbols = DocumentSymbolsFeature(ls: this);
28+
_workspaceSymbols = WorkspaceSymbolsFeature(ls: this);
2629
}
2730

2831
void configure(LanguageServerConfiguration configuration) {
@@ -38,6 +41,10 @@ class LanguageServices {
3841
return _documentSymbols.findDocumentSymbols(document);
3942
}
4043

44+
List<lsp.WorkspaceSymbol> findWorkspaceSymbols(String? query) {
45+
return _workspaceSymbols.findWorkspaceSymbols(query);
46+
}
47+
4148
sass.Stylesheet parseStylesheet(TextDocument document) {
4249
return cache.getStylesheet(document);
4350
}

0 commit comments

Comments
 (0)