Try to gracefully terminate the LSP server when requested

This commit is contained in:
Simon Hausmann 2021-04-06 11:10:30 +02:00
parent 121a098f74
commit d9db07db2a
2 changed files with 38 additions and 26 deletions

View file

@ -45,7 +45,15 @@ impl<'a> DocumentCache<'a> {
}
fn main() {
start_lsp_thread();
std::thread::spawn(|| {
match run_lsp_server() {
Ok(_) => {}
Err(error) => {
eprintln!("Error running LSP server: {}", error);
}
}
preview::quit_ui_event_loop();
});
// TODO: Don't terminate the event loop when the window is closed with Qt
// TODO: There's a race condition where theoretically the LSP could receive a preview
// request before the gui event loop has started, which would cause post_event to panic.
@ -54,32 +62,30 @@ fn main() {
preview::start_ui_event_loop();
}
fn start_lsp_thread() {
std::thread::spawn(|| {
let (connection, io_threads) = Connection::stdio();
let capabilities = ServerCapabilities {
completion_provider: Some(CompletionOptions {
resolve_provider: Some(true),
trigger_characters: None,
work_done_progress_options: WorkDoneProgressOptions::default(),
}),
hover_provider: Some(HoverProviderCapability::Simple(true)),
document_highlight_provider: Some(OneOf::Left(true)),
document_symbol_provider: Some(OneOf::Left(true)),
workspace_symbol_provider: Some(OneOf::Left(true)),
definition_provider: Some(OneOf::Left(true)),
text_document_sync: Some(TextDocumentSyncCapability::Kind(
lsp_types::TextDocumentSyncKind::Full,
)),
fn run_lsp_server() -> Result<(), Error> {
let (connection, io_threads) = Connection::stdio();
let capabilities = ServerCapabilities {
completion_provider: Some(CompletionOptions {
resolve_provider: Some(true),
trigger_characters: None,
work_done_progress_options: WorkDoneProgressOptions::default(),
}),
hover_provider: Some(HoverProviderCapability::Simple(true)),
document_highlight_provider: Some(OneOf::Left(true)),
document_symbol_provider: Some(OneOf::Left(true)),
workspace_symbol_provider: Some(OneOf::Left(true)),
definition_provider: Some(OneOf::Left(true)),
text_document_sync: Some(TextDocumentSyncCapability::Kind(
lsp_types::TextDocumentSyncKind::Full,
)),
..ServerCapabilities::default()
};
let server_capabilities = serde_json::to_value(&capabilities).unwrap();
let initialization_params = connection.initialize(server_capabilities).unwrap();
main_loop(&connection, initialization_params).unwrap();
io_threads.join().unwrap();
// TODO: notify the gui thread to gracefully terminate the event loop
});
..ServerCapabilities::default()
};
let server_capabilities = serde_json::to_value(&capabilities).unwrap();
let initialization_params = connection.initialize(server_capabilities)?;
main_loop(&connection, initialization_params)?;
io_threads.join()?;
Ok(())
}
fn main_loop(connection: &Connection, params: serde_json::Value) -> Result<(), Error> {