forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rs
More file actions
257 lines (239 loc) · 12.5 KB
/
Copy pathserver.rs
File metadata and controls
257 lines (239 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::backend::Backend;
use crate::fallback::WithFallbackService;
use crate::functionality::handlers::{
DocReloadExecute, DocReloadRequest, FileChange, FilesNeedDiagnostics, NeedsDiagnostics,
};
use crate::logging;
use crate::lsp_actor::LspActor;
use crate::lsp_actor::service::{LspActorKey, LspActorService};
use crate::lsp_streams::RouterStreams;
use act_locally::actor::ActorRef;
use act_locally::builder::ActorBuilder;
use async_lsp::ClientSocket;
use async_lsp::lsp_types::notification::{
self, DidChangeTextDocument, DidChangeWatchedFiles, DidOpenTextDocument, DidSaveTextDocument,
Initialized,
};
use async_lsp::lsp_types::request::{
CallHierarchyIncomingCalls, CallHierarchyOutgoingCalls, CallHierarchyPrepare,
CodeActionRequest, CodeLensRequest, Completion, DocumentHighlightRequest,
DocumentSymbolRequest, ExecuteCommand, FoldingRangeRequest, Formatting, GotoDeclaration,
GotoDefinition, GotoImplementation, GotoTypeDefinition, HoverRequest, InlayHintRequest,
References, Rename, SelectionRangeRequest, SemanticTokensFullRequest, Shutdown,
SignatureHelpRequest, TypeHierarchyPrepare, TypeHierarchySubtypes, TypeHierarchySupertypes,
WorkspaceSymbolRequest,
};
use async_std::stream::StreamExt;
use futures_batch::ChunksTimeoutStreamExt;
use tokio::sync::broadcast;
use tracing::instrument::WithSubscriber;
use tracing::{debug, info};
use crate::functionality::{
call_hierarchy, code_actions, code_lens, codegen_view, completion, declaration,
document_symbols, folding_range, goto, handlers, highlight, implementations, inlay_hints,
references, rename, selection_range, semantic_tokens, signature_help, type_definition,
type_hierarchy, workspace_symbols,
};
use async_lsp::lsp_types::request::Initialize;
use async_lsp::router::Router;
/// Spawn the Backend actor. Call once per LS process.
pub(crate) fn spawn_backend(
client: ClientSocket,
name: String,
doc_nav_tx: Option<broadcast::Sender<String>>,
doc_reload_tx: Option<broadcast::Sender<String>>,
docs_url: Option<String>,
) -> ActorRef<Backend, LspActorKey> {
info!("Spawning backend actor");
let client_for_actor = client.clone();
let client_for_logging = client.clone();
ActorBuilder::new()
.with_name(name)
.with_state_init(move || {
Ok(Backend::new(
client_for_actor,
doc_nav_tx,
doc_reload_tx,
docs_url,
))
})
.with_subscriber_init(logging::init_fn(client_for_logging))
.spawn()
.expect("Failed to spawn backend actor")
}
/// Create an LspService wrapping an existing Backend actor.
/// Call once per connection (stdio, WS).
pub(crate) fn setup_service(
actor_ref: ActorRef<Backend, LspActorKey>,
client: ClientSocket,
) -> WithFallbackService<LspActorService<Backend>, Router<()>> {
let mut lsp_actor_service = LspActorService::with(actor_ref);
lsp_actor_service
// mutating handlers
.handle_request_mut::<Initialize>(handlers::initialize)
.handle_request::<GotoDefinition>(goto::handle_goto_definition)
.handle_event_mut::<FileChange>(handlers::handle_file_change)
.handle_event::<FilesNeedDiagnostics>(handlers::handle_files_need_diagnostics)
.handle_event::<DocReloadExecute>(handlers::handle_doc_reload)
// non-mutating handlers
.handle_notification::<Initialized>(handlers::initialized)
.handle_request::<HoverRequest>(handlers::handle_hover_request)
.handle_request::<Completion>(completion::handle_completion)
.handle_request::<SignatureHelpRequest>(signature_help::handle_signature_help)
.handle_request::<CodeActionRequest>(code_actions::handle_code_action)
.handle_request::<References>(references::handle_references)
.handle_request::<DocumentHighlightRequest>(highlight::handle_document_highlight)
.handle_request::<GotoTypeDefinition>(type_definition::handle_goto_type_definition)
.handle_request::<GotoImplementation>(implementations::handle_goto_implementation)
.handle_request::<Rename>(rename::handle_rename)
.handle_request::<SemanticTokensFullRequest>(semantic_tokens::handle_semantic_tokens_full)
.handle_request::<Formatting>(handlers::handle_formatting)
.handle_request::<InlayHintRequest>(inlay_hints::handle_inlay_hints)
.handle_request::<DocumentSymbolRequest>(document_symbols::handle_document_symbols)
.handle_request::<WorkspaceSymbolRequest>(workspace_symbols::handle_workspace_symbols)
// call hierarchy
.handle_request::<CallHierarchyPrepare>(call_hierarchy::handle_prepare)
.handle_request::<CallHierarchyIncomingCalls>(call_hierarchy::handle_incoming_calls)
.handle_request::<CallHierarchyOutgoingCalls>(call_hierarchy::handle_outgoing_calls)
// type hierarchy
.handle_request::<TypeHierarchyPrepare>(type_hierarchy::handle_prepare)
.handle_request::<TypeHierarchySupertypes>(type_hierarchy::handle_supertypes)
.handle_request::<TypeHierarchySubtypes>(type_hierarchy::handle_subtypes)
// code lens
.handle_request::<CodeLensRequest>(code_lens::handle_code_lens)
// execute command (codegen views)
.handle_request_mut::<ExecuteCommand>(codegen_view::handle_execute_command)
// selection range
.handle_request::<SelectionRangeRequest>(selection_range::handle_selection_range)
// folding range
.handle_request::<FoldingRangeRequest>(folding_range::handle_folding_range)
// go to declaration
.handle_request::<GotoDeclaration>(declaration::handle_goto_declaration)
.handle_notification::<DidOpenTextDocument>(handlers::handle_did_open_text_document)
.handle_notification::<DidChangeTextDocument>(handlers::handle_did_change_text_document)
.handle_notification::<DidChangeWatchedFiles>(handlers::handle_did_change_watched_files)
.handle_notification::<DidSaveTextDocument>(handlers::handle_did_save_text_document)
.handle_notification::<notification::Exit>(handlers::handle_exit)
.handle_request::<Shutdown>(handlers::handle_shutdown);
let mut streaming_router = Router::new(());
setup_streams(client, &mut streaming_router);
setup_unhandled(&mut streaming_router);
WithFallbackService::new(lsp_actor_service, streaming_router)
}
/// Create an LspService for a secondary (WS) connection sharing an existing Backend.
///
/// Uses a read-only initialize handler so that browser clients don't overwrite
/// the editor's lsp_workspace_root or definition_link_support on the shared Backend.
pub(crate) fn setup_ws_service(
actor_ref: ActorRef<Backend, LspActorKey>,
client: ClientSocket,
) -> WithFallbackService<LspActorService<Backend>, Router<()>> {
let mut lsp_actor_service = LspActorService::with(actor_ref);
lsp_actor_service
// Read-only initialize: returns capabilities without mutating backend
.handle_request::<Initialize>(handlers::initialize_readonly)
.handle_request::<GotoDefinition>(goto::handle_goto_definition)
.handle_event::<FilesNeedDiagnostics>(handlers::handle_files_need_diagnostics)
.handle_event::<DocReloadExecute>(handlers::handle_doc_reload)
// non-mutating handlers
.handle_notification::<Initialized>(handlers::initialized)
.handle_request::<HoverRequest>(handlers::handle_hover_request)
.handle_request::<Completion>(completion::handle_completion)
.handle_request::<SignatureHelpRequest>(signature_help::handle_signature_help)
.handle_request::<CodeActionRequest>(code_actions::handle_code_action)
.handle_request::<References>(references::handle_references)
.handle_request::<DocumentHighlightRequest>(highlight::handle_document_highlight)
.handle_request::<GotoTypeDefinition>(type_definition::handle_goto_type_definition)
.handle_request::<GotoImplementation>(implementations::handle_goto_implementation)
.handle_request::<Rename>(rename::handle_rename)
.handle_request::<SemanticTokensFullRequest>(semantic_tokens::handle_semantic_tokens_full)
.handle_request::<Formatting>(handlers::handle_formatting)
.handle_request::<InlayHintRequest>(inlay_hints::handle_inlay_hints)
.handle_request::<DocumentSymbolRequest>(document_symbols::handle_document_symbols)
.handle_request::<WorkspaceSymbolRequest>(workspace_symbols::handle_workspace_symbols)
// call hierarchy
.handle_request::<CallHierarchyPrepare>(call_hierarchy::handle_prepare)
.handle_request::<CallHierarchyIncomingCalls>(call_hierarchy::handle_incoming_calls)
.handle_request::<CallHierarchyOutgoingCalls>(call_hierarchy::handle_outgoing_calls)
// type hierarchy
.handle_request::<TypeHierarchyPrepare>(type_hierarchy::handle_prepare)
.handle_request::<TypeHierarchySupertypes>(type_hierarchy::handle_supertypes)
.handle_request::<TypeHierarchySubtypes>(type_hierarchy::handle_subtypes)
// code lens
.handle_request::<CodeLensRequest>(code_lens::handle_code_lens)
// execute command (codegen views)
.handle_request_mut::<ExecuteCommand>(codegen_view::handle_execute_command)
// selection range
.handle_request::<SelectionRangeRequest>(selection_range::handle_selection_range)
// folding range
.handle_request::<FoldingRangeRequest>(folding_range::handle_folding_range)
// go to declaration
.handle_request::<GotoDeclaration>(declaration::handle_goto_declaration)
.handle_notification::<DidOpenTextDocument>(handlers::handle_did_open_text_document)
.handle_notification::<DidChangeTextDocument>(handlers::handle_did_change_text_document)
.handle_notification::<DidChangeWatchedFiles>(handlers::handle_did_change_watched_files)
.handle_notification::<DidSaveTextDocument>(handlers::handle_did_save_text_document)
.handle_notification::<notification::Exit>(handlers::handle_exit)
.handle_request::<Shutdown>(handlers::handle_shutdown);
let mut streaming_router = Router::new(());
setup_streams(client, &mut streaming_router);
setup_unhandled(&mut streaming_router);
WithFallbackService::new(lsp_actor_service, streaming_router)
}
/// Convenience: spawn a backend and create a service in one call.
/// Used by connections that own their own Backend (TCP mode).
pub(crate) fn setup(
client: ClientSocket,
name: String,
) -> WithFallbackService<LspActorService<Backend>, Router<()>> {
let actor_ref = spawn_backend(client.clone(), name, None, None, None);
setup_service(actor_ref, client)
}
fn setup_streams(client: ClientSocket, router: &mut Router<()>) {
info!("setting up streams");
let mut diagnostics_stream = router
.event_stream::<NeedsDiagnostics>()
.chunks_timeout(500, std::time::Duration::from_millis(30))
.map(FilesNeedDiagnostics)
.fuse();
let client_for_diag = client.clone();
tokio::spawn(
async move {
while let Some(files_need_diagnostics) = diagnostics_stream.next().await {
let _ = client_for_diag.emit(files_need_diagnostics);
}
}
.with_current_subscriber(),
);
// Debounced doc reload: coalesce rapid file changes into a single reload
let mut doc_reload_stream = router
.event_stream::<DocReloadRequest>()
.chunks_timeout(100, std::time::Duration::from_millis(200))
.map(|_batch| DocReloadExecute)
.fuse();
tokio::spawn(
async move {
while let Some(reload) = doc_reload_stream.next().await {
let _ = client.emit(reload);
}
}
.with_current_subscriber(),
);
}
fn setup_unhandled(router: &mut Router<()>) {
// These fire for any LSP notification/event that Fe doesn't explicitly
// handle — $/cancelRequest, $/setTrace, workspace/didChangeConfiguration,
// custom experimental/* methods editors send speculatively, etc. That
// traffic is completely normal and logging it at warn level floods the
// client log and drowns out real warnings. DEBUG so it's visible when
// debugging but invisible to end users.
router
.unhandled_notification(|_, params| {
debug!("Unhandled notification: {:?}", params);
std::ops::ControlFlow::Continue(())
})
.unhandled_event(|_, params| {
debug!("Unhandled event: {:?}", params);
std::ops::ControlFlow::Continue(())
});
}