Repository: helix-editor/helix
File: helix-view/src/editor.rs (Rust)

Code before:
            doc.ensure_view_init(current_view.id);
            current_view.id
        }
    }

    pub fn add_diagnostics(
        &mut self,
        diagnostics: Vec<(lsp::Diagnostic, LanguageServerId)>,
        server_id: LanguageServerId,
        uri: helix_core::Uri,
        document_version: Option<i32>,
        result_id: Option<String>,
    ) {
        let doc = self.documents.values_mut()
                            .find(|doc| doc.uri().is_some_and(|u| u == uri))
                            .filter(|doc| {
                                if let Some(version) = document_version{
                                    if version != doc.version() {
                                        log::info!("Version ({version}) is out of date for {uri:?} (expected ({}), dropping PublishDiagnostic notification", doc.version());
                                        return false;
                                    }
                                }
                                true
                            });

        if let Some(doc) = doc {
            let mut unchanged_diag_sources = Vec::new();
            if let Some(old_diagnostics) = self.diagnostics.get(&uri) {
                if let Some(lang_conf) = doc.language_config() {
                    for source in &lang_conf.persistent_diagnostic_sources {
                        let new_diagnostics = diagnostics
                            .iter()
                            .filter(|d| d.0.source.as_ref() == Some(source));
                        let old_diagnostics = old_diagnostics
                            .iter()
                            .filter(|(d, d_server)| {
                                *d_server == server_id && d.source.as_ref() == Some(source)
                            })
                            .map(|(d, _)| d);
                        if new_diagnostics.map(|x| &x.0).eq(old_diagnostics) {
                            unchanged_diag_sources.push(source.clone())
                        }
                    }
                }
            }

            // Insert the original lsp::Diagnostics here because we may have no open document
            // for diagnosic message and so we can't calculate the exact position.
            // When using them later in the diagnostics picker, we calculate them on-demand.
            let diagnostics = match self.diagnostics.entry(uri) {
                std::collections::btree_map::Entry::Occupied(o) => {

Review comment (refactor):
While we're moving this block let's take the opportunity to refactor some of the code here:

```rust
let Some(doc) = self
    .documents
    .values_mut()
    .find(|doc| doc.uri().is_some_and(|u| u == uri))
else {
    return;
};
if let Some(version) = document_version {
    if version != doc.version() {
        log::info!("Version ({version}) is out of date for {uri:?} (expected ({}), dropping PublishDiagnostic notification", doc.version());
        return;
    }
}
```

That eliminates the extra nesting from `if let Some(doc) = doc` and IMO the `find` + `filter` is a little hard to follow in its current form.

Code after:

    /// Closes language servers with timeout. The default timeout is 10000 ms, use
    /// `timeout` parameter to override this.
    pub async fn close_language_servers(
        &self,
        timeout: Option<u64>,
    ) -> Result<(), tokio::time::error::Elapsed> {
        // Remove all language servers from the file event handler.
        // Note: this is non-blocking.
        for client in self.language_servers.iter_clients() {
            self.language_servers
                .file_event_handler
                .remove_client(client.id());
        }

        tokio::time::timeout(
            Duration::from_millis(timeout.unwrap_or(3000)),
            future::join_all(
                self.language_servers
                    .iter_clients()
                    .map(|client| client.force_shutdown()),
            ),
        )
        .await
        .map(|_| ())
    }

    pub async fn wait_event(&mut self) -> EditorEvent {
        // the loop only runs once or twice and would be better implemented with a recursion + const generic
        // however due to limitations with async functions that can not be implemented right now
        loop {
            tokio::select! {
                biased;

                Some(event) = self.save_queue.next() => {
                    self.write_count -= 1;
                    return EditorEvent::DocumentSaved(event)
                }
                Some(config_event) = self.config_events.1.recv() => {
                    return EditorEvent::ConfigEvent(config_event)
                }
                Some(message) = self.language_servers.incoming.next() => {
                    return EditorEvent::LanguageServerMessage(message)
                }
                Some(event) = self.debug_adapters.incoming.next() => {
                    return EditorEvent::DebuggerEvent(event)
                }

                _ = helix_event::redraw_requested() => {
                    if  !self.needs_redraw{
                        self.needs_redraw = true;