vorto 0.15.3

A Vim-flavored modal terminal editor with batteries included: tree-sitter, LSP, fuzzy pickers, vim-surround, multi-cursor, and optional Copilot.
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Owns LSP client state and the request/response bookkeeping.
//!
//! `App` resolves a language and asks the coordinator to attach / sync /
//! request things. The coordinator drives the wire-level protocol and
//! reports back via [`LspEventOutcome`] — App turns outcomes into
//! user-visible side effects (status messages, file opens, buffer edits).
//!
//! Multi-server support: a buffer can have more than one LSP attached
//! (e.g. `vtsls` + `typescript-language-server`). Each spawned client
//! gets a unique `client_key` of the form `"<lang>::<server-name>"`.
//! Outgoing requests fan out to every client active for the current
//! document, and responses are accumulated in a per-request `Group`
//! before a single merged [`LspEventOutcome`] is surfaced.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;
use std::thread;
use std::time::Duration;

use anyhow::{Context, Result};
use serde_json::Value;

use crate::editor::Cursor;
use crate::event::AppEvent;
use crate::lsp::{
    self, CodeAction, CompletionItem, Diagnostic, Hover, Location, LspClient, LspEvent,
    SignatureHelp, TextEdit, WorkspaceEdit,
};

mod group;
mod requests;
mod sync;

use group::{Group, GroupAccum, accumulate, diagnostic_to_json, finalize, signature_help_to_json};

/// Build the canonical client identifier from a language name and a
/// server name. The same recipe runs everywhere `client_key` is needed
/// so spawn / attach / lookup stay in lockstep.
pub fn client_key(lang: &str, server: &str) -> String {
    format!("{}::{}", lang, server)
}

/// What an outstanding LSP request was for. Stored under
/// `pending[(client_key, id)]` so a response on the shared event
/// channel can be routed to the right accumulator. Per-request
/// context (labels, prefix positions, new names, request-time URIs)
/// lives on the [`GroupAccum`] for that request instead — pending
/// entries only need a discriminator.
#[derive(Debug, Clone, Copy)]
pub enum LspRequestKind {
    Jump,
    References,
    Rename,
    CodeAction,
    CodeActionResolve,
    Hover,
    Completion,
    CompletionResolve,
    SignatureHelp,
}

/// What [`LspCoordinator::handle_event`] wants the caller to do.
/// Diagnostics events are absorbed internally; everything that requires
/// UI action surfaces here.
pub enum LspEventOutcome {
    /// No user-visible side effect required.
    Nothing,
    InfoMessage(String),
    ErrorMessage(String),
    Jump {
        label: &'static str,
        locations: Vec<Location>,
    },
    References(Vec<Location>),
    Rename {
        new_name: String,
        edit: Option<WorkspaceEdit>,
    },
    CodeActions(Vec<CodeAction>),
    CodeActionResolved(Option<CodeAction>),
    Hover(Option<Hover>),
    Completion {
        prefix_start: Cursor,
        items: Vec<CompletionItem>,
    },
    CompletionResolved {
        uri: String,
        /// `Some(idx)` when the resolve was fired from the open popup to
        /// fill in detail/documentation for the item at that index in
        /// `CompletionState.items`. `None` when the resolve was fired
        /// from `accept_completion` to pull `additionalTextEdits` (auto-
        /// imports) after the user already committed to an item.
        item_index: Option<usize>,
        /// The full resolved item (or `None` when the server returned
        /// something we couldn't parse). The handler picks `detail` /
        /// `documentation` off this for popup display, and
        /// `additional_text_edits` for the accept-time path.
        item: Option<CompletionItem>,
    },
    SignatureHelp {
        /// Row the request was made on. The handler closes the popup
        /// when the cursor has crossed to a different row in the
        /// meantime (stale response).
        anchor_row: usize,
        /// `None` when the server said we're no longer in a callable
        /// context — the handler treats this as "close any open popup".
        help: Option<SignatureHelp>,
    },
}

/// Per-client snapshot for the `:lsp` status modal. Produced by
/// [`LspCoordinator::running_clients`]; the UI formats one row from
/// each entry.
pub struct RunningLspInfo {
    pub client_key: String,
    pub pid: u32,
    pub root_uri: String,
    pub language_id: String,
    /// How many URIs the client currently holds `didOpen`'d.
    pub open_count: usize,
}

/// Result of applying a [`WorkspaceEdit`]. Other-file edits are written
/// to disk by the coordinator; the active buffer's edits are returned
/// for the caller to apply through its own `Buffer` (with undo, version
/// bump, etc.).
pub struct WorkspaceEditResult {
    pub current_buffer_edits: Vec<TextEdit>,
    pub files_touched: usize,
    pub total_edits: usize,
}

/// Per-pending-request bookkeeping. Held under
/// `pending[(client_key, request_id)]`.
pub(super) struct Pending {
    pub(super) group: u64,
    pub(super) kind: LspRequestKind,
}

pub struct LspCoordinator {
    /// Live LSP clients, keyed by `client_key` (see [`client_key`]).
    clients: HashMap<String, LspClient>,
    /// Every URI we've sent `textDocument/didOpen` for, mapped to the
    /// list of `client_key`s currently holding it open. Buffer switches
    /// no longer `didClose` — we keep the server's view of every
    /// visited buffer alive so workspace-wide diagnostics survive
    /// while the buffer sleeps in vorto's parked/sleeping pool. Only
    /// `:bd` (or the client dying) sends `didClose` and removes the
    /// entry here. Used by `did_open` to skip a no-op re-open and by
    /// the workspace diagnostics picker via `all_diagnostics`.
    open_uris: HashMap<String, Vec<String>>,
    /// Diagnostics keyed first by URI, then by the client that
    /// published them. Merged across clients on read so the UI sees
    /// every server's findings at once. `publishDiagnostics` is
    /// authoritative per `(client, uri)` — an empty `items` from one
    /// client only clears that client's slice.
    diagnostics: HashMap<String, HashMap<String, Vec<Diagnostic>>>,
    /// Outstanding LSP request bookkeeping. Keyed by `(client_key, id)`
    /// so a response on the shared event channel routes back to the
    /// right pending entry.
    pending: HashMap<(String, u64), Pending>,
    /// Fan-out accumulators keyed by a per-request group id.
    groups: HashMap<u64, Group>,
    next_group_id: u64,
    /// URI of the document currently considered "open".
    current_uri: Option<String>,
    /// Language name of the currently-open document. Mostly cosmetic —
    /// `current_clients` is what drives request dispatch.
    current_language: Option<String>,
    /// Client keys attached to the current document. All fan-out
    /// requests dispatch to every entry here.
    current_clients: Vec<String>,
    last_synced_version: u64,
    event_tx: Sender<AppEvent>,
    startup_cwd: PathBuf,
}

impl LspCoordinator {
    pub fn new(event_tx: Sender<AppEvent>, startup_cwd: PathBuf) -> Self {
        Self {
            clients: HashMap::new(),
            open_uris: HashMap::new(),
            diagnostics: HashMap::new(),
            pending: HashMap::new(),
            groups: HashMap::new(),
            next_group_id: 0,
            current_uri: None,
            current_language: None,
            current_clients: Vec::new(),
            last_synced_version: 0,
            event_tx,
            startup_cwd,
        }
    }

    pub fn last_synced_version(&self) -> u64 {
        self.last_synced_version
    }

    pub fn set_last_synced_version(&mut self, v: u64) {
        self.last_synced_version = v;
    }

    /// True when at least one client is attached to the current
    /// document. Drives the "no LSP for this buffer" guard in the App.
    pub fn has_lsp(&self) -> bool {
        self.current_uri.is_some() && !self.current_clients.is_empty()
    }

    /// True when any client attached to the current document declared
    /// `c` as a completion trigger character. Used by insert mode to
    /// auto-fire `textDocument/completion` on language-specific
    /// punctuation (e.g. `:` for Rust paths, `<` for TypeScript JSX)
    /// without hardcoding it on our side.
    pub fn is_completion_trigger_char(&self, c: char) -> bool {
        let mut buf = [0u8; 4];
        let needle = c.encode_utf8(&mut buf);
        self.current_clients.iter().any(|key| {
            self.clients
                .get(key)
                .map(|client| {
                    client
                        .completion_trigger_characters()
                        .iter()
                        .any(|t| t == needle)
                })
                .unwrap_or(false)
        })
    }

    /// True when any client attached to the current document declared
    /// `c` as a signature-help trigger character (typically `(`). The
    /// insert layer uses this to fire `textDocument/signatureHelp` on
    /// the specific punctuation each server cares about.
    pub fn is_signature_help_trigger_char(&self, c: char) -> bool {
        let mut buf = [0u8; 4];
        let needle = c.encode_utf8(&mut buf);
        self.current_clients.iter().any(|key| {
            self.clients
                .get(key)
                .map(|client| {
                    client
                        .signature_help_trigger_characters()
                        .iter()
                        .any(|t| t == needle)
                })
                .unwrap_or(false)
        })
    }

    /// Every (uri, diagnostic) pair the coordinator currently holds,
    /// merged across clients and sorted within each URI. Used by the
    /// workspace diagnostics picker; cloned because storage is keyed
    /// per-client and the caller wants a flat owned list.
    ///
    /// URI ordering is alphabetical so the picker order is stable
    /// across runs (`HashMap` iteration would otherwise reshuffle
    /// every restart).
    pub fn all_diagnostics(&self) -> Vec<(String, Vec<Diagnostic>)> {
        let mut out: Vec<(String, Vec<Diagnostic>)> = self
            .diagnostics
            .iter()
            .filter_map(|(uri, per_client)| {
                let mut merged: Vec<Diagnostic> = per_client
                    .values()
                    .flat_map(|v| v.iter().cloned())
                    .collect();
                if merged.is_empty() {
                    return None;
                }
                merged.sort_by_key(|d| (d.range.start.line, d.range.start.character));
                Some((uri.clone(), merged))
            })
            .collect();
        out.sort_by(|a, b| a.0.cmp(&b.0));
        out
    }

    /// Merged diagnostics across all clients for the current buffer's
    /// URI, if any. Cloned into an owned Vec because the underlying
    /// storage is per-client and we have to fold across that on read.
    pub fn current_diagnostics(&self) -> Option<Vec<Diagnostic>> {
        let uri = self.current_uri.as_ref()?;
        let per_client = self.diagnostics.get(uri)?;
        let mut out: Vec<Diagnostic> = per_client
            .values()
            .flat_map(|v| v.iter().cloned())
            .collect();
        if out.is_empty() {
            return None;
        }
        // Sort for a stable presentation regardless of which server
        // happened to publish first.
        out.sort_by_key(|d| (d.range.start.line, d.range.start.character));
        Some(out)
    }

    /// Drop the "current document" pointers without telling the
    /// servers anything. The URI stays in `open_uris` so the server's
    /// view of the buffer (and its diagnostics) survives the switch —
    /// `<space>D` then sees every visited file, not just the active
    /// one. Use [`Self::close_uri`] when the buffer is actually being
    /// destroyed (`:bd`).
    pub fn detach_current(&mut self) {
        self.current_uri = None;
        self.current_clients.clear();
        self.current_language = None;
    }

    /// Explicitly tear down a URI's didOpen state across every client
    /// that has it open. Sends `didClose`, removes the URI from
    /// `open_uris`, and drops its diagnostics. Called by `:bd` so the
    /// server can release its copy when the buffer is gone for good.
    ///
    /// Once the `didClose` is sent, any client that no longer holds
    /// *any* URI open is shut down — `:bd`-ing the last Rust file
    /// reaps rust-analyzer instead of leaving it resident for the rest
    /// of the session. Sleeping/parked buffers still count as "open"
    /// here (their URIs stay in `open_uris` across buffer switches by
    /// design — see [`Self::detach_current`]), so a client only dies
    /// when every visited buffer for it has been explicitly deleted.
    pub fn close_uri(&mut self, uri: &str) {
        let client_keys = self.open_uris.remove(uri).unwrap_or_default();
        for key in &client_keys {
            if let Some(client) = self.clients.get_mut(key) {
                let _ = client.did_close(uri);
            }
        }
        self.diagnostics.remove(uri);
        if self.current_uri.as_deref() == Some(uri) {
            self.detach_current();
        }
        for key in &client_keys {
            let still_holding = self
                .open_uris
                .values()
                .any(|keys| keys.iter().any(|k| k == key));
            if !still_holding {
                self.drop_client(key);
            }
        }
    }

    /// Returns `true` when a client for `client_key` is already attached.
    pub fn has_client(&self, client_key: &str) -> bool {
        self.clients.contains_key(client_key)
    }

    /// Snapshot of every currently-running client for the `:lsp`
    /// status modal. Order is unspecified — the caller sorts.
    pub fn running_clients(&self) -> Vec<RunningLspInfo> {
        self.clients
            .iter()
            .map(|(key, client)| {
                let open_count = self
                    .open_uris
                    .values()
                    .filter(|keys| keys.iter().any(|k| k == key))
                    .count();
                RunningLspInfo {
                    client_key: key.clone(),
                    pid: client.pid(),
                    root_uri: client.root_uri().to_string(),
                    language_id: client.language_id().to_string(),
                    open_count,
                }
            })
            .collect()
    }

    /// Adopt a pre-spawned `LspClient`. Used by the file-open worker
    /// thread. Returns false (and the freshly-spawned client is dropped
    /// by the caller) when the same `client_key` is already attached —
    /// e.g. a parallel open of another file with the same language
    /// won the race.
    pub fn attach_client(&mut self, client_key: &str, client: LspClient) -> bool {
        if self.clients.contains_key(client_key) {
            return false;
        }
        self.clients.insert(client_key.to_string(), client);
        true
    }

    /// Mark `client_key` as one of the active clients for the current
    /// document. Idempotent. The caller (worker) is also responsible
    /// for firing `didOpen` against the new client.
    pub fn add_current_client(&mut self, client_key: &str) {
        if !self.current_clients.iter().any(|k| k == client_key) {
            self.current_clients.push(client_key.to_string());
        }
    }

    /// Build the `emit` closure passed to `LspClient::spawn`.
    pub fn make_emit(&self) -> Box<dyn Fn(LspEvent) + Send + 'static> {
        let tx = self.event_tx.clone();
        Box::new(move |ev| {
            let _ = tx.send(AppEvent::Lsp(ev));
        })
    }

    pub fn startup_cwd(&self) -> &Path {
        &self.startup_cwd
    }

    /// Synchronously request `textDocument/formatting` from the first
    /// attached client. Returns the parsed edits on success, `Ok(None)`
    /// when no client is attached (so the caller can fall through to
    /// an external formatter / no-op without inventing a sentinel
    /// error). `timeout` caps how long save will block before giving
    /// up — past that, the save proceeds un-formatted.
    ///
    /// We try only the first client rather than fanning out: format
    /// responses from two servers would need a merge strategy we
    /// haven't designed (and `vtsls` + `typescript-language-server`
    /// formatting the same file twice would just clobber each other).
    pub fn format_first_client(
        &mut self,
        options: Value,
        timeout: Duration,
    ) -> Result<Option<Vec<lsp::TextEdit>>> {
        let Some(uri) = self.current_uri.clone() else {
            return Ok(None);
        };
        let Some(key) = self.current_clients.first().cloned() else {
            return Ok(None);
        };
        let Some(client) = self.clients.get_mut(&key) else {
            return Ok(None);
        };
        let edits = client.formatting(&uri, options, timeout)?;
        Ok(Some(edits))
    }

    /// Request `textDocument/formatting` from the configured formatter
    /// servers in priority order. Tries each named server that is
    /// attached to the current document; returns the first success.
    /// `Ok(None)` when no named server is attached. On error, tries the
    /// next server and surfaces the last error only if all fail.
    pub fn format_with_servers(
        &mut self,
        servers: &[String],
        options: Value,
        timeout: Duration,
    ) -> Result<Option<Vec<lsp::TextEdit>>> {
        let Some(uri) = self.current_uri.clone() else {
            return Ok(None);
        };
        let mut last_err = None;
        for name in servers {
            let key = self
                .current_clients
                .iter()
                .find(|k| k.rsplit_once("::").map(|(_, s)| s) == Some(name.as_str()))
                .cloned();
            let Some(key) = key else { continue };
            let Some(client) = self.clients.get_mut(&key) else {
                continue;
            };
            match client.formatting(&uri, options.clone(), timeout) {
                Ok(edits) => return Ok(Some(edits)),
                Err(e) => last_err = Some(e),
            }
        }
        match last_err {
            Some(e) => Err(e),
            None => Ok(None),
        }
    }

    pub fn current_uri(&self) -> Option<&str> {
        self.current_uri.as_deref()
    }

    /// Consume an LSP event. Diagnostics / messages are absorbed here;
    /// responses fold into their request group and an outcome is
    /// surfaced once every fanned-out client has reported back.
    pub fn handle_event(&mut self, ev: LspEvent) -> LspEventOutcome {
        match ev {
            LspEvent::Diagnostics { client, uri, items } => {
                let entry = self.diagnostics.entry(uri.clone()).or_default();
                if items.is_empty() {
                    entry.remove(&client);
                    if entry.is_empty() {
                        self.diagnostics.remove(&uri);
                    }
                } else {
                    entry.insert(client, items);
                }
                LspEventOutcome::Nothing
            }
            LspEvent::Message { level, text } => {
                if level == 1 {
                    LspEventOutcome::ErrorMessage(text)
                } else {
                    LspEventOutcome::InfoMessage(text)
                }
            }
            LspEvent::Error { client, message } => {
                // Reader-thread EOF after we proactively closed the
                // last URI for this client (see [`Self::close_uri`])
                // is expected, not an error. The client is already
                // gone from `self.clients` at that point — skip both
                // the redundant drop and the toast.
                if !self.clients.contains_key(&client) {
                    return LspEventOutcome::Nothing;
                }
                self.drop_client(&client);
                LspEventOutcome::ErrorMessage(format!("lsp: {}", message))
            }
            LspEvent::Response {
                client,
                id,
                result,
                error,
            } => self.handle_response(client, id, result, error),
        }
    }

    /// Route a `Response` back to its `Group` and, if every client in
    /// that group has now reported, emit the merged outcome.
    fn handle_response(
        &mut self,
        client: String,
        id: u64,
        result: Option<Value>,
        error: Option<String>,
    ) -> LspEventOutcome {
        let Some(pending) = self.pending.remove(&(client.clone(), id)) else {
            return LspEventOutcome::Nothing;
        };
        let group_id = pending.group;
        let result = result.unwrap_or(Value::Null);

        // Errors collapse to "empty result" — we still count this
        // client toward the group so the merge eventually completes,
        // but the user shouldn't see a per-server error for each
        // server in the fan-out. Genuine failures (every server
        // errored) leave the merged outcome empty, which downstream
        // handlers already report as "no results".
        let had_error = error.is_some();

        if let Some(group) = self.groups.get_mut(&group_id) {
            if !had_error {
                accumulate(&mut group.accum, &client, &result, &pending.kind);
            }
            group.remaining = group.remaining.saturating_sub(1);
            if group.remaining == 0 {
                let group = self.groups.remove(&group_id).unwrap();
                return finalize(group.accum);
            }
        }
        LspEventOutcome::Nothing
    }

    /// Drop a client whose reader thread is dead, or which no URI
    /// references anymore (see [`Self::close_uri`]). Pending requests
    /// against it count as already-responded (empty); groups that
    /// finalise as a result of this are surfaced via the event channel
    /// so the caller sees the merged outcome without a follow-up
    /// response event.
    fn drop_client(&mut self, client_key: &str) {
        let removed = self.clients.remove(client_key);
        self.current_clients.retain(|k| k != client_key);
        // Decrement remaining counts for every pending request against
        // this client. Groups that hit zero are dropped on the floor —
        // the user already sees an `ErrorMessage` for the reader-thread
        // failure that triggered us, which is informative enough; the
        // alternative (a second outcome for the merged-but-incomplete
        // result) would need a new AppEvent variant.
        let dead_keys: Vec<(String, u64)> = self
            .pending
            .keys()
            .filter(|(k, _)| k == client_key)
            .cloned()
            .collect();
        for k in dead_keys {
            if let Some(pending) = self.pending.remove(&k)
                && let Some(group) = self.groups.get_mut(&pending.group)
            {
                group.remaining = group.remaining.saturating_sub(1);
                if group.remaining == 0 {
                    self.groups.remove(&pending.group);
                }
            }
        }
        // Drop diagnostics this client owned across all URIs so the
        // status bar doesn't show stale entries from a dead server.
        for slices in self.diagnostics.values_mut() {
            slices.remove(client_key);
        }
        self.diagnostics.retain(|_, slices| !slices.is_empty());
        // The dead client no longer holds anything open — drop its
        // entries so a future `did_open` for a previously-visited URI
        // re-fires (against whichever client picks the language up
        // next) instead of being skipped as a duplicate.
        for keys in self.open_uris.values_mut() {
            keys.retain(|k| k != client_key);
        }
        self.open_uris.retain(|_, keys| !keys.is_empty());
        // Dispose off-thread: `LspClient::Drop` does the
        // `shutdown`/`exit` handshake and reaps the child, which can
        // block ~800ms for servers like rust-analyzer. Running it
        // inline would stall `:bd` of the last buffer for a language.
        if let Some(client) = removed {
            thread::spawn(move || drop(client));
        }
    }

    pub fn apply_workspace_edit(&self, edit: WorkspaceEdit) -> Result<WorkspaceEditResult> {
        let mut current_buffer_edits = Vec::new();
        let files_touched = edit.changes.len();
        let mut total_edits = 0usize;
        let current_uri = self.current_uri.clone();
        for (uri, edits) in edit.changes {
            total_edits += edits.len();
            if Some(&uri) == current_uri.as_ref() {
                current_buffer_edits = edits;
                continue;
            }
            let Some(path) = lsp::uri_to_path(&uri) else {
                continue;
            };
            let text = std::fs::read_to_string(&path)
                .with_context(|| format!("reading {}", path.display()))?;
            let mut lines: Vec<String> = text.split('\n').map(|s| s.to_string()).collect();
            if lines.is_empty() {
                lines.push(String::new());
            }
            lsp::apply_text_edits(&mut lines, edits);
            std::fs::write(&path, lines.join("\n"))
                .with_context(|| format!("writing {}", path.display()))?;
        }
        Ok(WorkspaceEditResult {
            current_buffer_edits,
            files_touched,
            total_edits,
        })
    }
}