quarb-code-lsp 0.30.0

Language server for the Quarb code level — outline, definition, references, and hover over source code, on LSP JSON-RPC and kaivrpc
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
//! The protocol-agnostic core: one set of reading gestures, two
//! codecs. Both transports decode into these calls and encode
//! the same answers.
//!
//! The engine is the single source of truth: every answer is a
//! code-level read — the outline is the named-node walk,
//! definition and references are name resolution over the
//! workspace forest (by identifier, fan-out on ambiguity — the
//! honest answer, exactly as ruling #31 resolves
//! `->definition`), hover is `::signature` + `::doc`.
//!
//! This is a READING tool by design: no rename, no completion,
//! no diagnostics — the write side stays the classic language
//! server's job.

use std::cell::RefCell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use quarb::{AstAdapter, NodeId, Value};
use quarb_code::CodeModel;
use quarb_compose::{ComposeAdapter, SourceGraft};
use quarb_fs::{FsAdapter, FsOptions};
use serde::Serialize;

/// One symbol, flat form (kaivrpc rows; the JSON-RPC codec nests
/// them back into DocumentSymbols by `depth`).
#[derive(Debug, Clone, Serialize)]
pub struct Symbol {
    pub name: String,
    /// The vocabulary word (`function`, `type`, `impl`, …).
    pub construct: String,
    /// LSP SymbolKind — the lowering tables' informative column,
    /// live.
    pub kind: u32,
    /// Nesting depth under the file root (0 = top level).
    pub depth: u32,
    /// 1-based lines.
    pub start_line: u32,
    pub end_line: u32,
    /// 0-based byte columns on the start/end lines.
    pub start_col: u32,
    pub end_col: u32,
    pub signature: Option<String>,
}

/// One resolved location in the workspace.
#[derive(Debug, Clone, Serialize)]
pub struct Location {
    pub file: String,
    /// 1-based.
    pub line: u32,
    /// The code-level locator (`/dataclass/wrap/call`) — the
    /// path that tells the story; kaiv-native clients print it.
    pub locator: String,
}

/// An arbitrary code-level query's answer: node results become
/// locations, value results become printed values, and an
/// engine refusal arrives verbatim — what refuses here refuses
/// in `qua`.
pub enum QueryAnswer {
    Locations(Vec<Location>),
    Values(Vec<String>),
    Refused(String),
}

/// A hover answer: the declaration head and its documentation.
#[derive(Debug, Clone, Serialize)]
pub struct HoverRow {
    pub name: String,
    pub construct: String,
    pub signature: Option<String>,
    pub doc: Option<String>,
    pub file: String,
    pub line: u32,
}

/// The server state: overlay documents (editor buffers win over
/// disk) and the lazily-built workspace forest.
pub struct Workspace {
    root: Option<PathBuf>,
    /// uri → (path, model) for open documents.
    docs: HashMap<String, (PathBuf, CodeModel)>,
    forest: RefCell<Option<ComposeAdapter<FsAdapter>>>,
}

impl Workspace {
    pub fn new(root: Option<PathBuf>) -> Self {
        Workspace {
            root,
            docs: HashMap::new(),
            forest: RefCell::new(None),
        }
    }

    pub fn open(&mut self, uri: &str, text: &str) {
        let path = uri_to_path(uri);
        if let Ok(m) = CodeModel::parse(text, ext_of(&path)) {
            self.docs.insert(uri.to_string(), (path, m));
        }
    }

    pub fn close(&mut self, uri: &str) {
        self.docs.remove(uri);
    }

    /// A save invalidates the forest: the next workspace answer
    /// re-reads disk (per-file parses come back from the AST
    /// cache, so the rebuild costs only the changed file).
    pub fn saved(&self) {
        *self.forest.borrow_mut() = None;
    }

    pub fn model(&self, uri: &str) -> Option<&CodeModel> {
        self.docs.get(uri).map(|(_, m)| m)
    }

    /// The document outline: every named node, pre-order, with
    /// its nesting depth — Function becomes Method inside a type,
    /// straight from the lowering tables' LSP column.
    pub fn symbols(&self, uri: &str) -> Vec<Symbol> {
        let Some(model) = self.model(uri) else {
            return Vec::new();
        };
        let lines = LineIndex::new(model.source());
        let mut out = Vec::new();
        walk_symbols(model, model.root(), 0, false, &lines, &mut out);
        out
    }

    /// Declarations of `word` — the open buffer first (overlay
    /// wins), then the workspace forest. Fan-out is the honest
    /// answer for homonyms.
    pub fn definition(&self, uri: &str, word: &str) -> Vec<Location> {
        let mut out = Vec::new();
        if let Some((path, model)) = self.docs.get(uri) {
            for n in named_nodes(model) {
                if model.ident(n) == Some(word) {
                    out.push(Location {
                        file: path.display().to_string(),
                        line: model.line_span(n).0 as u32,
                        locator: model.locator(n),
                    });
                }
            }
        }
        if out.is_empty() {
            out = self.forest_query(&format!("//{}", quoted(word)), uri);
        }
        out
    }

    /// Call sites whose callee resolves to `word`, workspace-wide
    /// — find-references as a filter, not an index.
    pub fn references(&self, uri: &str, word: &str) -> Vec<Location> {
        let re = regex_escape(word);
        self.forest_query(
            &format!("//*<call>[::callee =~ /(^|[^A-Za-z0-9_$]){re}$/]"),
            uri,
        )
    }

    /// Workspace symbols: declarations matching `query` as a
    /// name prefix (empty query lists nothing — the census is a
    /// qua one-liner, not an editor payload).
    pub fn workspace_symbols(&self, query: &str, uri: &str) -> Vec<Location> {
        if query.is_empty() {
            return Vec::new();
        }
        self.forest_query(&format!("//~({}.*)", regex_escape(query)), uri)
    }

    /// Hover: the declaration head and doc of `word`, buffer
    /// first, forest second.
    pub fn hover(&self, uri: &str, word: &str) -> Vec<HoverRow> {
        if let Some((path, model)) = self.docs.get(uri) {
            let rows: Vec<HoverRow> = named_nodes(model)
                .into_iter()
                .filter(|&n| model.ident(n) == Some(word))
                .map(|n| HoverRow {
                    name: word.to_string(),
                    construct: model.construct(n).to_string(),
                    signature: str_prop(model, n, "signature"),
                    doc: str_prop(model, n, "doc"),
                    file: path.display().to_string(),
                    line: model.line_span(n).0 as u32,
                })
                .collect();
            if !rows.is_empty() {
                return rows;
            }
        }
        self.forest_hover(word, uri)
    }

    /// The query door: any code-level query, over the open file
    /// (`file_only`) or the workspace forest. This is the whole
    /// product in one method — the gestures above are the four
    /// questions every editor asks; this is the rest of them.
    pub fn query(&self, uri: &str, q: &str, file_only: bool) -> QueryAnswer {
        if file_only {
            let Some((path, model)) = self.docs.get(uri) else {
                return QueryAnswer::Refused("no file to query".into());
            };
            return match quarb::run(q, model) {
                Err(e) => QueryAnswer::Refused(e.to_string()),
                Ok(quarb::QueryResult::Values(vs)) => {
                    QueryAnswer::Values(vs.iter().map(|v| v.to_string()).collect())
                }
                Ok(quarb::QueryResult::Nodes(ns)) => QueryAnswer::Locations(
                    ns.into_iter()
                        .map(|n| Location {
                            file: path.display().to_string(),
                            line: model.line_span(n).0 as u32,
                            locator: model.locator(n),
                        })
                        .collect(),
                ),
            };
        }
        self.with_forest(uri, |forest| match quarb::run(q, forest) {
            Err(e) => QueryAnswer::Refused(e.to_string()),
            Ok(quarb::QueryResult::Values(vs)) => {
                QueryAnswer::Values(vs.iter().map(|v| v.to_string()).collect())
            }
            Ok(quarb::QueryResult::Nodes(ns)) => QueryAnswer::Locations(
                ns.into_iter()
                    .map(|n| {
                        let locator =
                            forest.locator(n, |o| forest.outer().path(o).display().to_string());
                        let (file, tail) = split_locator(&locator);
                        Location {
                            file,
                            line: int_meta(forest, n, "start-line"),
                            locator: tail,
                        }
                    })
                    .collect(),
            ),
        })
        .unwrap_or(QueryAnswer::Refused("no workspace root".into()))
    }

    // ---- the workspace forest ------------------------------------

    /// The root the forest mounts: the initialize rootUri, else
    /// the open document's parent directory.
    fn forest_root(&self, uri: &str) -> Option<PathBuf> {
        self.root.clone().or_else(|| {
            self.docs
                .get(uri)
                .and_then(|(p, _)| p.parent().map(Path::to_path_buf))
        })
    }

    fn with_forest<T>(
        &self,
        uri: &str,
        f: impl FnOnce(&ComposeAdapter<FsAdapter>) -> T,
    ) -> Option<T> {
        let mut slot = self.forest.borrow_mut();
        if slot.is_none() {
            let root = self.forest_root(uri)?;
            let fs = FsAdapter::with_options(&root, FsOptions::default()).ok()?;
            *slot = Some(
                ComposeAdapter::with_source_paths(fs, |fs, n| Some(fs.path(n)))
                    .with_source_graft(SourceGraft::Code),
            );
        }
        slot.as_ref().map(f)
    }

    fn forest_query(&self, query: &str, uri: &str) -> Vec<Location> {
        self.with_forest(uri, |forest| {
            let Ok(quarb::QueryResult::Nodes(ns)) = quarb::run(query, forest) else {
                return Vec::new();
            };
            ns.into_iter()
                .map(|n| {
                    let locator =
                        forest.locator(n, |o| forest.outer().path(o).display().to_string());
                    let (file, tail) = split_locator(&locator);
                    Location {
                        file,
                        line: int_meta(forest, n, "start-line"),
                        locator: tail,
                    }
                })
                .collect()
        })
        .unwrap_or_default()
    }

    fn forest_hover(&self, word: &str, uri: &str) -> Vec<HoverRow> {
        self.with_forest(uri, |forest| {
            let q = format!("//{}", quoted(word));
            let Ok(quarb::QueryResult::Nodes(ns)) = quarb::run(&q, forest) else {
                return Vec::new();
            };
            ns.into_iter()
                .take(8)
                .map(|n| {
                    let locator =
                        forest.locator(n, |o| forest.outer().path(o).display().to_string());
                    let (file, _) = split_locator(&locator);
                    HoverRow {
                        name: word.to_string(),
                        construct: str_meta(forest, n, "construct"),
                        signature: value_str(forest.property(n, "signature")),
                        doc: value_str(forest.property(n, "doc")),
                        file,
                        line: int_meta(forest, n, "start-line"),
                    }
                })
                .collect()
        })
        .unwrap_or_default()
    }
}

// ---- helpers --------------------------------------------------------

/// The identifier under the cursor: scan the line for the word
/// containing 0-based character `col` (columns as Unicode
/// scalars; identifiers are ASCII in the grammars we lower, so
/// UTF-16 drift cannot split one).
pub fn word_at(text: &str, line: u32, col: u32) -> Option<String> {
    let line = text.lines().nth(line as usize)?;
    let chars: Vec<char> = line.chars().collect();
    let is_word = |c: char| c.is_alphanumeric() || c == '_' || c == '$';
    let mut i = (col as usize).min(chars.len());
    if i >= chars.len() || !is_word(chars[i]) {
        i = i.checked_sub(1)?;
    }
    if !is_word(*chars.get(i)?) {
        return None;
    }
    let mut start = i;
    while start > 0 && is_word(chars[start - 1]) {
        start -= 1;
    }
    let mut end = i;
    while end + 1 < chars.len() && is_word(chars[end + 1]) {
        end += 1;
    }
    Some(chars[start..=end].iter().collect())
}

/// LSP SymbolKind from the lowering tables' informative column.
/// Function is Method inside a type or impl — computed here, by
/// position, exactly as the spec's one sentence says.
pub fn symbol_kind(construct: &str, kind: &str, inside_type: bool) -> u32 {
    match construct {
        "function" | "lambda" if inside_type => 6, // Method
        "function" | "lambda" => 12,               // Function
        "impl" => 19,                              // Object
        "module" => 2,                             // Module
        "constant" => 14,                          // Constant
        "field" => match kind {
            "enum_variant" | "enumerator" => 22, // EnumMember
            _ => 8,                              // Field
        },
        "type" => match kind {
            k if k.starts_with("struct") || k.starts_with("union") => 23, // Struct
            k if k.starts_with("enum") => 10,                             // Enum
            "trait_item" => 11,                                           // Interface
            _ => 5,                                                       // Class
        },
        _ => 13, // Variable — unreachable for vocabulary constructs
    }
}

fn walk_symbols(
    model: &CodeModel,
    node: NodeId,
    depth: u32,
    inside_type: bool,
    lines: &LineIndex,
    out: &mut Vec<Symbol>,
) {
    for child in model.children(node) {
        let named = model.ident(child).is_some();
        let container = matches!(model.construct(child), "type" | "impl");
        if let Some(ident) = model.ident(child) {
            let (sl, el) = model.line_span(child);
            let (start, end) = model.span(child);
            out.push(Symbol {
                name: ident.to_string(),
                construct: model.construct(child).to_string(),
                kind: symbol_kind(
                    model.construct(child),
                    &str_meta(model, child, "kind"),
                    inside_type,
                ),
                depth,
                start_line: sl as u32,
                end_line: el as u32,
                start_col: lines.col(start, sl) as u32,
                end_col: lines.col(end, el) as u32,
                signature: str_prop(model, child, "signature"),
            });
        }
        walk_symbols(
            model,
            child,
            depth + u32::from(named),
            container || (inside_type && !named),
            lines,
            out,
        );
    }
}

fn named_nodes(model: &CodeModel) -> Vec<NodeId> {
    let mut out = Vec::new();
    let mut stack = vec![model.root()];
    while let Some(n) = stack.pop() {
        if model.ident(n).is_some() {
            out.push(n);
        }
        stack.extend(model.children(n));
    }
    out.sort();
    out
}

/// Byte-offset → column resolution for one source text.
struct LineIndex {
    starts: Vec<usize>,
}

impl LineIndex {
    fn new(text: &str) -> Self {
        let mut starts = vec![0];
        for (i, b) in text.bytes().enumerate() {
            if b == b'\n' {
                starts.push(i + 1);
            }
        }
        LineIndex { starts }
    }

    /// 0-based byte column of `offset` on 1-based line `line`.
    fn col(&self, offset: usize, line: usize) -> usize {
        self.starts
            .get(line - 1)
            .map_or(0, |s| offset.saturating_sub(*s))
    }
}

fn ext_of(path: &Path) -> &str {
    path.extension().and_then(|e| e.to_str()).unwrap_or("")
}

pub fn uri_to_path(uri: &str) -> PathBuf {
    let raw = uri.strip_prefix("file://").unwrap_or(uri);
    // Percent-decoding for the common case (spaces); full URI
    // handling is the editor's job — file URIs from real editors
    // are plain paths.
    PathBuf::from(raw.replace("%20", " "))
}

/// Split a forest locator at the graft bang: file, code path.
fn split_locator(locator: &str) -> (String, String) {
    match locator.split_once('!') {
        Some((file, tail)) => (file.to_string(), tail.to_string()),
        None => (locator.to_string(), String::new()),
    }
}

/// An identifier as a quoted literal name step.
fn quoted(word: &str) -> String {
    format!("'{}'", word.replace('\'', ""))
}

fn regex_escape(word: &str) -> String {
    word.chars()
        .flat_map(|c| {
            if c.is_alphanumeric() || c == '_' {
                vec![c]
            } else {
                vec!['\\', c]
            }
        })
        .collect()
}

fn value_str(v: Option<Value>) -> Option<String> {
    match v {
        Some(Value::Str(s)) => Some(s),
        _ => None,
    }
}

fn str_prop(a: &impl AstAdapter, n: NodeId, key: &str) -> Option<String> {
    value_str(a.property(n, key))
}

fn str_meta(a: &impl AstAdapter, n: NodeId, key: &str) -> String {
    match a.metadata(n, key) {
        Some(Value::Str(s)) => s,
        _ => String::new(),
    }
}

fn int_meta(a: &impl AstAdapter, n: NodeId, key: &str) -> u32 {
    match a.metadata(n, key) {
        Some(Value::Int(i)) => i as u32,
        _ => 0,
    }
}