on master help sign in
Simplify 'lsp_stop' command
commit58c913ce4567a95c52808d78245eb1dac806971c authorPhilipp Mildenberger <philipp@mildenberger.me> dateSun 19 Mar 2023, 19:26 (4 years ago) parent7d20740
2 files changed+12−48
helix-term/src/commands/typed.rs+5−48 helix-view/src/document.rs+7
helix-term/src/commands/typed.rs+5−48
@@ -1442,62 +1442,19 @@ fn lsp_stop(
14421442 return Ok(());
14431443 }
14441444
1445− let doc = doc!(cx.editor);
1446−
1447− // TODO this stops language servers which may be used in another doc/language type that uses the same language servers
1448− // I'm not sure if this is really what we want
1449− let ls_shutdown_names = doc
1445+ let ls_shutdown_names = doc!(cx.editor)
14501446 .language_servers()
1451− .map(|ls| ls.name())
1447+ .map(|ls| ls.name().to_string())
14521448 .collect::<Vec<_>>();
14531449
14541450 for ls_name in &ls_shutdown_names {
14551451 cx.editor.language_servers.stop(ls_name);
1456− }
14571452
1458− let doc_ids_active_clients: Vec<_> = cx
1459− .editor
1460− .documents()
1461− .filter_map(|doc| {
1462− let doc_active_ls_ids: Vec<_> = doc
1463− .language_servers()
1464− .filter(|ls| !ls_shutdown_names.contains(&ls.name()))
1465− .map(|ls| ls.id())
1466− .collect();
1467−
1468− let active_clients: Vec<_> = cx
1469− .editor
1470− .language_servers
1471− .iter_clients()
1472− .filter(|client| doc_active_ls_ids.contains(&client.id()))
1473− .map(Clone::clone)
1474− .collect();
1475−
1476− if active_clients.len() != doc.language_servers().count() {
1477− Some((doc.id(), active_clients))
1478− } else {
1479− None
1453+ for doc in cx.editor.documents_mut() {
1454+ if let Some(client) = doc.remove_language_server_by_name(ls_name) {
1455+ doc.clear_diagnostics(client.id());
14801456 }
1481− })
1482− .collect();
1483−
1484− for (doc_id, active_clients) in doc_ids_active_clients {
1485− let doc = cx.editor.documents.get_mut(&doc_id).unwrap();
1486−
1487− let stopped_clients: Vec<_> = doc
1488− .language_servers()
1489− .filter(|ls| {
1490− !active_clients
1491− .iter()
1492− .any(|active_ls| active_ls.id() == ls.id())
1493− })
1494− .map(|ls| ls.id())
1495− .collect(); // is necessary because of borrow-checking
1496−
1497− for client_id in stopped_clients {
1498− doc.clear_diagnostics(client_id)
14991457 }
1500− doc.set_language_servers(active_clients);
15011458 }
15021459
15031460 Ok(())
helix-view/src/document.rs+7
@@ -1442,6 +1442,13 @@ impl Document {
14421442 .filter_map(|l| if l.is_initialized() { Some(&**l) } else { None })
14431443 }
14441444
1445+ pub fn remove_language_server_by_name(&mut self, name: &str) -> Option<Arc<Client>> {
1446+ match self.language_servers.iter().position(|l| l.name() == name) {
1447+ Some(index) => Some(self.language_servers.remove(index)),
1448+ None => None,
1449+ }
1450+ }
1451+
14451452 // TODO filter also based on LSP capabilities?
14461453 pub fn language_servers_with_feature(
14471454 &self,