strop-editor 0.8.0

strop — a modal text editor in Rust: see the cut before you make it
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
//! LSP glue (0009): the editor drains typed events; tokio never touches
//! the input path. Diagnostics merge into the git gutter (severity wins),
//! Space d is the diagnostics picker, Space k hover, gd goto-definition.

use std::path::{Path, PathBuf};
use std::sync::mpsc::{channel, Receiver};
use std::sync::OnceLock;

use strop_lsp::languages::Languages;
use strop_lsp::registry::{self, ServerSpec};
use strop_lsp::{Client, LspEvent};

use super::Editor;

/// One pooled server connection (0014 wave 2): the pool keys on
/// (workspace root, server name) so a polyglot session runs rust-analyzer
/// and clangd and pyright side by side.
pub(crate) struct LspServer {
    pub key: (PathBuf, String),
    pub client: strop_lsp::Client,
    pub rx: Receiver<strop_lsp::LspEvent>,
}

impl Editor {
    /// Spawn a server for the current buffer if the merged languages
    /// config (0012: project > XDG > embedded) resolves one. One client
    /// per workspace root; buffers did_open on it.
    pub(crate) fn lsp_maybe_attach(&mut self) {
        if cfg!(test) {
            return; // hermetic test builds never spawn servers
        }
        let Some(path) = self.buf().path.clone() else {
            return;
        };
        let ext = Path::new(&path)
            .extension()
            .map(|e| format!(".{}", e.to_string_lossy()));
        let Some(ext) = ext else { return };
        let abs = if Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        let languages: &'static Languages = merged_languages(&abs);
        let warn = languages.warnings();
        let Some(spec) = registry::for_extension(&ext, languages) else {
            if !warn.is_empty() {
                self.message = format!("languages.toml: {}", warn.join("; "));
            }
            return;
        };
        // already pooled for this (root, server)? just did_open — the
        // root resolve repeats the languages/git walk, cheap and rare
        let root_known = self.lsp_server_root(&abs, languages);
        let key = (root_known, spec.name.to_string());
        if self.lsp_servers.iter().any(|s| s.key == key) {
            self.lsp_did_open_current();
            return;
        }
        // PATH probe only for bare command names — a config-provided
        // absolute path is its own existence check (0012 §5)
        if !spec.absolute_command()
            && std::process::Command::new(spec.command)
                .arg("--version")
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .spawn()
                .is_err()
        {
            self.lsp_hint_once(spec);
            return;
        }
        let (tx, rx) = channel();
        match Client::spawn(&spec, &key.0.clone(), tx) {
            Some(client) => {
                self.lsp_servers.push(LspServer { key, client, rx });
                self.message = format!("lsp: {} starting", spec.name);
                if !warn.is_empty() {
                    self.message
                        .push_str(&format!(" — languages.toml: {}", warn.join("; ")));
                }
                self.lsp_did_open_current();
            }
            None => self.lsp_hint_once(spec),
        }
    }

    fn lsp_hint_once(&mut self, spec: ServerSpec<'static>) {
        if self.lsp_hints_shown.insert(spec.name) {
            let hint = spec
                .install_hint
                .unwrap_or("install it or fix the command in languages.toml");
            self.message = format!("lsp: {} not available — {}", spec.name, hint);
        }
    }

    /// The buffer's workspace root (project layer, git walk, cwd).
    fn lsp_server_root(&self, abs: &Path, languages: &Languages) -> PathBuf {
        languages
            .project_root
            .as_deref()
            .map(Path::to_path_buf)
            .unwrap_or_else(|| {
                let fallback = self
                    .git
                    .as_ref()
                    .map(|g| g.workdir().to_path_buf())
                    .unwrap_or_else(|| self.cwd.clone());
                registry::workspace_root(abs, &fallback)
            })
    }

    /// The pooled server for the current buffer, resolved through its
    /// extension + root (None when nothing serves this file).
    fn lsp_current(&self) -> Option<&LspServer> {
        let path = self.buf().path.as_deref()?;
        let abs = if Path::new(path).is_absolute() {
            PathBuf::from(path)
        } else {
            self.cwd.join(path)
        };
        let ext = format!(".{}", abs.extension()?.to_string_lossy());
        let languages: &'static Languages = merged_languages(&abs);
        let spec = registry::for_extension(&ext, languages)?;
        let root = self.lsp_server_root(&abs, languages);
        let key = (root, spec.name.to_string());
        self.lsp_servers.iter().find(|s| s.key == key)
    }

    fn lsp_did_open_current(&mut self) {
        let Some(path) = self.buf().path.clone() else {
            return;
        };
        let abs = if std::path::Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        if !self.lsp_opened.insert(abs.clone()) {
            return;
        }
        let Some(client) = self.lsp_current().map(|s| &s.client) else {
            return;
        };
        let lang = lang_id(&abs);
        client.did_open(&abs, lang, &self.buf().rope.to_string());
    }

    /// didChange when the buffer epoch moved (debounced by epoch — one
    /// full sync per edit burst; incremental sync is the perf follow-up).
    pub fn lsp_sync_changed(&mut self) {
        // buffers can be empty post-quit (the TUI breaks first, but
        // scripted/headless callers may drain past the last :q)
        let Some(buf) = self.docs.get(self.current()).map(|d| &d.buf) else {
            return;
        };
        let Some(path) = buf.path.clone() else {
            return;
        };
        let abs = if std::path::Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        let epoch = buf.epoch;
        if self.lsp_sent_epochs.get(&abs) == Some(&epoch) {
            return;
        }
        self.lsp_sent_epochs.insert(abs.clone(), epoch);
        let text = buf.rope.to_string();
        let Some(client) = self.lsp_current().map(|s| &s.client) else {
            return;
        };
        client.did_change(&abs, &text);
    }

    /// Drain server events (event loop tick + headless settle).
    pub fn drain_lsp(&mut self) {
        // drain every pooled server, remembering which one each event
        // came from (its negotiated encoding converts the columns)
        let mut events: Vec<(strop_lsp::PositionEncoding, LspEvent)> = Vec::new();
        for srv in &self.lsp_servers {
            while let Ok(ev) = srv.rx.try_recv() {
                events.push((srv.client.encoding(), ev));
            }
        }
        for (enc, event) in events {
            match event {
                LspEvent::Diagnostics { path, mut diags } => {
                    // server columns → byte columns against the open
                    // buffer's text (unopened files keep wire values)
                    {
                        if let Some(buf) = self.buffer_for_path(&path) {
                            for d in &mut diags {
                                let line = buf.line_text(d.line);
                                d.col = strop_lsp::to_byte_col(&line, d.col, enc);
                                let end_line = buf.line_text(d.end_line);
                                d.end_col = strop_lsp::to_byte_col(&end_line, d.end_col, enc);
                            }
                        }
                    }
                    self.diags.insert(path, diags);
                }
                LspEvent::Ready { server } => {
                    self.message = format!("lsp: {server} ready");
                }
                LspEvent::Failed { server, hint } => {
                    self.message = format!("lsp: {server} failed — {hint}");
                }
                LspEvent::Note { text } => self.message = text,
                LspEvent::HoverText { text } => self.hover_card = Some(text),
                LspEvent::Locations { kind, items } => match items.len() {
                    0 => self.message = format!("lsp: no {}", kind.label()),
                    1 => {
                        let (path, line, col) = items.into_iter().next().unwrap();
                        self.jump_to_location(path, line, col, enc);
                    }
                    n => {
                        use strop_picker::{Item, Kind, Payload};
                        let label = kind.label();
                        let items: Vec<Item> = items
                            .into_iter()
                            .map(|(path, line, col)| Item {
                                text: format!("{}:{}:{}", path.display(), line + 1, col + 1),
                                payload: Payload::Grep {
                                    path,
                                    line: line + 1,
                                    col: col + 1,
                                    match_len: 1,
                                    line_text: String::new(),
                                },
                            })
                            .collect();
                        let picker = strop_picker::Picker::new(Kind::Locations, items, false);
                        self.picker = Some(crate::editor::PickerGlue::diagnostics(picker));
                        self.message = format!("{n} {label}");
                    }
                },
                LspEvent::GotoLocation { path, line, col } => {
                    if std::env::var_os("STROP_LSP_LOG").is_some() {
                        eprintln!("strop: goto {}:{}:{}", path.display(), line, col);
                    }
                    self.jump_to_location(path, line, col, enc);
                }
            }
        }
    }

    /// Open the target and land on the position (server col → byte
    /// col against the target line).
    fn jump_to_location(
        &mut self,
        path: PathBuf,
        line: usize,
        col: usize,
        enc: strop_lsp::PositionEncoding,
    ) {
        let path_s = path.display().to_string();
        if let Err(e) = self.open_buffer(&path_s) {
            self.message = format!("open {path_s}: {e}");
            return;
        }
        // IntelliJ's external-libraries rule without a list: following a
        // definition OUT of the workspace is reading, not editing —
        // the buffer opens readonly (0009's mutation boundary enforces
        // it); `:set noro` unlocks deliberately.
        // workspace_root takes a FILE path (walks from its parent) —
        // cwd/x's parent is the cwd itself
        let probe = self.cwd.join("x");
        let root = registry::workspace_root(&probe, &self.cwd);
        if !path.starts_with(&root) && !self.buf().readonly {
            self.buf_mut().readonly = true;
            self.message = format!("{path_s} [readonly — outside workspace; :set noro to edit]");
        }
        let col = {
            let line_idx = line.min(self.buf().len_lines().saturating_sub(1));
            let text = self.buf().line_text(line_idx);
            strop_lsp::to_byte_col(&text, col, enc)
        };
        let start = self.buf().line_start(line.min(self.buf().len_lines() - 1));
        self.set_head(self.buf().clamp_boundary(start + col));
        self.clamp_cursor();
        self.scroll_to_cursor(self.view_rows());
    }

    /// gr / gI / gy / gD: references, implementation, type definition,
    /// declaration — one request shape, four LSP methods.
    pub(crate) fn lsp_locations(&mut self, kind: strop_lsp::LocKind) {
        let Some(client) = self.lsp_current().map(|s| &s.client) else {
            self.message = "no language server — install it or fix languages.toml".into();
            return;
        };
        let Some(path) = self.buf().path.clone() else {
            return;
        };
        let abs = if std::path::Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        let line = self.buf().line_of(self.head());
        let col = self.server_col(client, self.buf().col_of(self.head()));
        let label = kind.label();
        client.locations(kind, &abs, line, col);
        self.message = format!("lsp: {label}");
    }

    /// `]d` / `[d`: jump to the next/previous diagnostic in this
    /// buffer, wrapping (vim's diagnostic traversal).
    pub(crate) fn jump_diagnostic(&mut self, forward: bool) {
        let Some(path) = self.buf().path.clone() else {
            return;
        };
        let abs = if std::path::Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        let Some(diags) = self.diags.get(&abs).filter(|d| !d.is_empty()) else {
            self.message = "no diagnostics".into();
            return;
        };
        let cur = self.buf().line_of(self.head());
        // diags arrive sorted by position from the server; wrap at ends
        let target = if forward {
            diags
                .iter()
                .find(|d| d.line > cur || (d.line == cur && d.col > self.buf().col_of(self.head())))
                .or(diags.first())
        } else {
            diags
                .iter()
                .rev()
                .find(|d| d.line < cur || (d.line == cur && d.col < self.buf().col_of(self.head())))
                .or(diags.last())
        };
        let Some(d) = target else { return };
        let (line, col, msg) = (d.line, d.col, d.message.clone());
        let start = self.buf().line_start(line.min(self.buf().len_lines() - 1));
        self.set_head(self.buf().clamp_boundary(start + col));
        self.clamp_cursor();
        self.scroll_to_cursor(self.view_rows());
        self.message = msg;
    }

    /// `Space d`: diagnostics picker over the current buffer's diags.
    pub(crate) fn open_diagnostics_picker(&mut self) {
        use strop_picker::{Item, Kind, Payload};
        let items: Vec<Item> = self
            .diags
            .iter()
            .flat_map(|(path, diags)| {
                diags.iter().map(move |d| Item {
                    text: format!(
                        "{}:{} {} {}",
                        path.display(),
                        d.line + 1,
                        d.severity_char(),
                        d.message
                    ),
                    payload: Payload::Grep {
                        path: path.clone(),
                        line: d.line + 1,
                        col: d.col + 1,
                        match_len: 1,
                        line_text: d.message.clone(),
                    },
                })
            })
            .collect();
        if items.is_empty() {
            self.message = "no diagnostics".into();
            return;
        }
        let picker = strop_picker::Picker::new(Kind::Diagnostics, items, false);
        self.picker = Some(crate::editor::PickerGlue::diagnostics(picker));
    }

    /// `Space k`: hover at the cursor.
    pub(crate) fn lsp_hover(&mut self) {
        let Some(client) = self.lsp_current().map(|s| &s.client) else {
            self.message = "no language server — install it or fix languages.toml".into();
            return;
        };
        let Some(path) = self.buf().path.clone() else {
            return;
        };
        let abs = if std::path::Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        let line = self.buf().line_of(self.head());
        let col = self.buf().col_of(self.head());
        let col = self.server_col(client, col);
        client.hover(&abs, line, col);
    }

    /// The open buffer backing an absolute path, if any.
    fn buffer_for_path(&self, abs: &std::path::Path) -> Option<&strop_core::Buffer> {
        self.docs.iter().map(|(_, d)| &d.buf).find(|b| {
            b.path.as_deref().is_some_and(|p| {
                let p = std::path::Path::new(p);
                let buf_abs = if p.is_absolute() {
                    p.to_path_buf()
                } else {
                    self.cwd.join(p)
                };
                buf_abs == abs || buf_abs.canonicalize().ok().as_deref() == Some(abs)
            })
        })
    }

    /// byte col → the server's negotiated column for the current line.
    fn server_col(&self, client: &strop_lsp::Client, byte_col: usize) -> usize {
        let line = self.buf().line_of(self.head());
        let text = self.buf().line_text(line);
        strop_lsp::to_server_col(&text, byte_col, client.encoding())
    }

    /// `gd`: goto definition at the cursor.
    pub(crate) fn lsp_goto_definition(&mut self) {
        let Some(client) = self.lsp_current().map(|s| &s.client) else {
            self.message = "no language server — install it or fix languages.toml".into();
            return;
        };
        let Some(path) = self.buf().path.clone() else {
            return;
        };
        let abs = if std::path::Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        let line = self.buf().line_of(self.head());
        let col = self.buf().col_of(self.head());
        let col = self.server_col(client, col);
        client.goto_definition(&abs, line, col);
    }

    /// `gs`: switch between source and header (clangd's extension).
    pub(crate) fn lsp_switch_source_header(&mut self) {
        let Some(client) = self.lsp_current().map(|s| &s.client) else {
            self.message = "no language server — install it or fix languages.toml".into();
            return;
        };
        let Some(path) = self.buf().path.clone() else {
            return;
        };
        let abs = if std::path::Path::new(&path).is_absolute() {
            PathBuf::from(&path)
        } else {
            self.cwd.join(&path)
        };
        client.switch_source_header(&abs);
    }
}

/// The merged languages.toml layers (0012: project > XDG > embedded),
/// loaded once per process at first attach — never on the input path.
/// One project layer per session matches the one-server-per-workspace
/// model; the buffer anchors the project-file walk. OnceLock (not
/// LazyLock) because the initializer needs that runtime buffer path.
fn merged_languages(buffer: &Path) -> &'static Languages {
    static MERGED: OnceLock<Languages> = OnceLock::new();
    MERGED.get_or_init(|| {
        let xdg = strop_lsp::languages::xdg_path();
        let project = strop_lsp::languages::project_path(buffer);
        Languages::load(xdg.as_deref(), project.as_deref())
    })
}

/// LSP language id for a path (the registry's languages, lsp-named).
fn lang_id(path: &std::path::Path) -> &'static str {
    match path.extension().and_then(|e| e.to_str()) {
        Some("rs") => "rust",
        Some("py") | Some("pyi") => "python",
        Some("go") => "go",
        Some("js") | Some("jsx") | Some("mjs") | Some("cjs") => "javascript",
        Some("ts") => "typescript",
        Some("tsx") => "typescriptreact",
        Some("json") => "json",
        Some("sh") | Some("bash") => "shellscript",
        Some("c") | Some("h") => "c",
        Some("cpp") | Some("cc") | Some("cxx") | Some("hpp") | Some("hh") => "cpp",
        _ => "plaintext",
    }
}

impl Editor {
    /// Table shims (0008 stage 2).
    pub(crate) fn lsp_goto_definition_pub(&mut self) {
        self.lsp_goto_definition();
    }
    pub(crate) fn lsp_switch_source_header_pub(&mut self) {
        self.lsp_switch_source_header();
    }
    pub(crate) fn lsp_hover_pub(&mut self) {
        self.lsp_hover();
    }
    pub fn lsp_locations_pub(&mut self, kind: strop_lsp::LocKind) {
        self.lsp_locations(kind);
    }

    pub fn jump_diagnostic_pub(&mut self, forward: bool) {
        self.jump_diagnostic(forward);
    }
}