vorto 0.4.0

A terminal text editor with tree-sitter syntax highlighting and LSP support
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
//! Pure parsers from `serde_json::Value` into the normalised types in
//! [`super::types`]. Everything here is total — invalid/missing input
//! collapses to `None` / empty rather than panicking.

use std::collections::HashMap;

use serde_json::Value;

use super::types::{
    CodeAction, CompletionItem, Hover, Location, Position, Range, TextEdit, WorkspaceEdit,
};

/// Parse a `Location` (LSP shape). Returns `None` on schema mismatch.
fn parse_location(v: &Value) -> Option<Location> {
    let uri = v.get("uri").and_then(|x| x.as_str())?.to_string();
    let range = parse_range(v.get("range")?)?;
    Some(Location { uri, range })
}

/// Parse a `LocationLink` and reduce it to the same shape as `Location`
/// (taking `targetUri` + `targetSelectionRange`).
fn parse_location_link(v: &Value) -> Option<Location> {
    let uri = v.get("targetUri").and_then(|x| x.as_str())?.to_string();
    let range = parse_range(
        v.get("targetSelectionRange")
            .or_else(|| v.get("targetRange"))?,
    )?;
    Some(Location { uri, range })
}

/// `textDocument/definition` may answer with a single `Location`, a
/// single `LocationLink`, an array of either, or `null`. Normalise to a
/// flat `Vec<Location>`.
pub fn parse_locations(v: &Value) -> Vec<Location> {
    if v.is_null() {
        return Vec::new();
    }
    if let Some(arr) = v.as_array() {
        return arr
            .iter()
            .filter_map(|x| parse_location(x).or_else(|| parse_location_link(x)))
            .collect();
    }
    if let Some(loc) = parse_location(v).or_else(|| parse_location_link(v)) {
        return vec![loc];
    }
    Vec::new()
}

pub(super) fn parse_range(v: &Value) -> Option<Range> {
    let start = v.get("start")?;
    let end = v.get("end")?;
    Some(Range {
        start: Position {
            line: start.get("line")?.as_u64().unwrap_or(0) as u32,
            character: start.get("character")?.as_u64().unwrap_or(0) as u32,
        },
        end: Position {
            line: end.get("line")?.as_u64().unwrap_or(0) as u32,
            character: end.get("character")?.as_u64().unwrap_or(0) as u32,
        },
    })
}

/// Parse a `WorkspaceEdit`. Both legacy `changes: { uri: [TextEdit] }`
/// and modern `documentChanges: [TextDocumentEdit]` shapes are
/// flattened into the same map. We ignore document `create`/`rename`/
/// `delete` operations — rename refactors that need them are out of
/// scope for now.
pub fn parse_workspace_edit(v: &Value) -> Option<WorkspaceEdit> {
    if v.is_null() {
        return None;
    }
    let mut out: HashMap<String, Vec<TextEdit>> = HashMap::new();
    if let Some(changes) = v.get("changes").and_then(|c| c.as_object()) {
        for (uri, edits) in changes {
            if let Some(arr) = edits.as_array() {
                let edits = arr.iter().filter_map(parse_text_edit).collect();
                out.insert(uri.clone(), edits);
            }
        }
    }
    if let Some(doc_changes) = v.get("documentChanges").and_then(|c| c.as_array()) {
        for dc in doc_changes {
            // A `TextDocumentEdit` has `textDocument.uri` + `edits[]`. Other
            // operations (`CreateFile`, `RenameFile`, `DeleteFile`) have
            // their own `kind` field — skip those.
            if dc.get("kind").is_some() {
                continue;
            }
            let Some(uri) = dc
                .get("textDocument")
                .and_then(|td| td.get("uri"))
                .and_then(|v| v.as_str())
            else {
                continue;
            };
            let Some(edits) = dc.get("edits").and_then(|e| e.as_array()) else {
                continue;
            };
            let edits: Vec<TextEdit> = edits.iter().filter_map(parse_text_edit).collect();
            out.entry(uri.to_string()).or_default().extend(edits);
        }
    }
    if out.is_empty() {
        return None;
    }
    Some(WorkspaceEdit { changes: out })
}

/// Parse the result of `textDocument/codeAction`. The server returns
/// `null`, a single object (rare), or an array mixing `CodeAction`
/// objects and legacy `Command` objects. We treat everything with a
/// `title` as a candidate; `Command`-only entries (no `edit` and no
/// `data` for resolve) still appear in the picker but do nothing on
/// submit because we don't run `workspace/executeCommand` yet.
pub fn parse_code_actions(v: &Value) -> Vec<CodeAction> {
    let mut out = Vec::new();
    let push = |out: &mut Vec<CodeAction>, item: &Value| {
        let Some(title) = item.get("title").and_then(|t| t.as_str()) else {
            return;
        };
        let edit = item.get("edit").and_then(parse_workspace_edit);
        out.push(CodeAction {
            title: title.to_string(),
            edit,
            raw: item.clone(),
            // Filled in by the coordinator (which knows the originating
            // client). Parse stage is source-agnostic.
            source: String::new(),
        });
    };
    if let Some(arr) = v.as_array() {
        for item in arr {
            push(&mut out, item);
        }
    } else if v.is_object() {
        push(&mut out, v);
    }
    out
}

/// Parse the result of `codeAction/resolve` — same shape as a single
/// `CodeAction` from the list response, just with the previously-missing
/// `edit` filled in (in the typical case).
pub fn parse_code_action(v: &Value) -> Option<CodeAction> {
    let title = v.get("title").and_then(|t| t.as_str())?.to_string();
    let edit = v.get("edit").and_then(parse_workspace_edit);
    Some(CodeAction {
        title,
        edit,
        raw: v.clone(),
        source: String::new(),
    })
}

/// Parse a `textDocument/hover` response. `contents` may arrive as
/// `MarkupContent { kind, value }`, a bare `MarkedString` (string or
/// `{ language, value }`), or an array of `MarkedString`s — collapse all
/// shapes into a single joined string. Returns `None` when `contents`
/// is missing/empty or when the whole response is `null` (servers send
/// `null` to mean "no hover info here").
pub fn parse_hover(v: &Value) -> Option<Hover> {
    if v.is_null() {
        return None;
    }
    let contents = v.get("contents")?;
    let joined = collect_hover_contents(contents);
    let trimmed = joined.trim();
    if trimmed.is_empty() {
        return None;
    }
    Some(Hover {
        contents: trimmed.to_string(),
    })
}

fn collect_hover_contents(v: &Value) -> String {
    // `MarkupContent` — the modern shape.
    if let Some(obj) = v.as_object()
        && let Some(value) = obj.get("value").and_then(|x| x.as_str())
        && obj.get("kind").is_some()
    {
        return value.to_string();
    }
    // Legacy `MarkedString` — either a plain string or
    // `{ language, value }` (a code block).
    if let Some(s) = v.as_str() {
        return s.to_string();
    }
    if let Some(obj) = v.as_object()
        && let Some(value) = obj.get("value").and_then(|x| x.as_str())
    {
        let lang = obj.get("language").and_then(|x| x.as_str()).unwrap_or("");
        return format!("```{}\n{}\n```", lang, value);
    }
    // Array of `MarkedString`s — join with blank lines so distinct
    // entries (signature, doc, examples) stay visually separated.
    if let Some(arr) = v.as_array() {
        let parts: Vec<String> = arr
            .iter()
            .map(collect_hover_contents)
            .filter(|s| !s.trim().is_empty())
            .collect();
        return parts.join("\n\n");
    }
    String::new()
}

/// Parse a `textDocument/completion` response. The result can be:
/// - `null` (no completions),
/// - `CompletionItem[]` (the simple case), or
/// - `{ isIncomplete, items: CompletionItem[] }`.
///
/// All three collapse to a flat `Vec<CompletionItem>`. We don't surface
/// `isIncomplete` — the popup doesn't re-request on every keystroke,
/// so the distinction doesn't pay rent.
pub fn parse_completion(v: &Value) -> Vec<CompletionItem> {
    if v.is_null() {
        return Vec::new();
    }
    let arr = if let Some(a) = v.as_array() {
        a.as_slice()
    } else if let Some(a) = v.get("items").and_then(|x| x.as_array()) {
        a.as_slice()
    } else {
        return Vec::new();
    };
    arr.iter().filter_map(parse_completion_item).collect()
}

fn parse_completion_item(v: &Value) -> Option<CompletionItem> {
    let label = v.get("label")?.as_str()?.to_string();
    let kind = v.get("kind").and_then(|x| x.as_u64()).unwrap_or(0) as u8;
    let text_edit = v
        .get("textEdit")
        .and_then(|te| {
            // Modern servers may send `InsertReplaceEdit { insert, replace, newText }`
            // instead of `TextEdit { range, newText }`. Prefer the replace
            // range — that's the one we'd want when the user accepts.
            let new_text = te.get("newText")?.as_str()?.to_string();
            let range = te
                .get("range")
                .or_else(|| te.get("replace"))
                .or_else(|| te.get("insert"))?;
            let range = parse_range(range)?;
            Some(TextEdit { range, new_text })
        });
    let insert_text = v
        .get("insertText")
        .and_then(|x| x.as_str())
        .map(|s| s.to_string());
    let filter_text = v
        .get("filterText")
        .and_then(|x| x.as_str())
        .map(|s| s.to_string());
    let sort_text = v
        .get("sortText")
        .and_then(|x| x.as_str())
        .map(|s| s.to_string());
    let detail = v
        .get("detail")
        .and_then(|x| x.as_str())
        .map(|s| s.to_string());
    let additional_text_edits = v
        .get("additionalTextEdits")
        .and_then(|x| x.as_array())
        .map(|arr| arr.iter().filter_map(parse_text_edit).collect())
        .unwrap_or_default();
    Some(CompletionItem {
        label,
        kind,
        text_edit,
        insert_text,
        filter_text,
        sort_text,
        detail,
        additional_text_edits,
        raw: v.clone(),
        source: String::new(),
    })
}

/// Parse a `completionItem/resolve` response. The result is a single
/// CompletionItem object — same shape as one element of the array
/// returned by `textDocument/completion`. Returns `None` when the
/// server hands back something we can't make sense of (no `label`).
pub fn parse_completion_resolve(v: &Value) -> Option<CompletionItem> {
    parse_completion_item(v)
}

fn parse_text_edit(v: &Value) -> Option<TextEdit> {
    let range = parse_range(v.get("range")?)?;
    let new_text = v.get("newText")?.as_str()?.to_string();
    Some(TextEdit { range, new_text })
}

/// Parse a `textDocument/formatting` (or `rangeFormatting`) response.
/// The result is `TextEdit[]` or `null`; both collapse to a flat vector.
/// Empty when the server returned nothing or every entry was malformed.
pub fn parse_text_edits(v: &Value) -> Vec<TextEdit> {
    if v.is_null() {
        return Vec::new();
    }
    let Some(arr) = v.as_array() else {
        return Vec::new();
    };
    arr.iter().filter_map(parse_text_edit).collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn parse_locations_handles_all_shapes() {
        // Single Location object.
        let single = json!({
            "uri": "file:///a.rs",
            "range": { "start": {"line": 1, "character": 2}, "end": {"line": 1, "character": 5} }
        });
        let v = parse_locations(&single);
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].uri, "file:///a.rs");

        // Array of Locations.
        let arr = json!([single]);
        assert_eq!(parse_locations(&arr).len(), 1);

        // LocationLink shape.
        let link = json!([{
            "targetUri": "file:///b.rs",
            "targetSelectionRange": {
                "start": {"line": 0, "character": 0},
                "end":   {"line": 0, "character": 3}
            }
        }]);
        let v = parse_locations(&link);
        assert_eq!(v.len(), 1);
        assert_eq!(v[0].uri, "file:///b.rs");

        // null → empty.
        assert!(parse_locations(&Value::Null).is_empty());
    }

    #[test]
    fn parse_workspace_edit_normalises_both_shapes() {
        // Legacy `changes` map.
        let v = json!({
            "changes": {
                "file:///a.rs": [{
                    "range": {
                        "start": {"line": 0, "character": 0},
                        "end":   {"line": 0, "character": 3}
                    },
                    "newText": "X"
                }]
            }
        });
        let edit = parse_workspace_edit(&v).unwrap();
        assert_eq!(edit.changes.len(), 1);
        assert_eq!(edit.changes["file:///a.rs"].len(), 1);

        // Modern `documentChanges` array.
        let v = json!({
            "documentChanges": [{
                "textDocument": { "uri": "file:///b.rs", "version": 1 },
                "edits": [{
                    "range": {
                        "start": {"line": 0, "character": 0},
                        "end":   {"line": 0, "character": 3}
                    },
                    "newText": "Y"
                }]
            }]
        });
        let edit = parse_workspace_edit(&v).unwrap();
        assert_eq!(edit.changes["file:///b.rs"][0].new_text, "Y");

        assert!(parse_workspace_edit(&Value::Null).is_none());
    }

    #[test]
    fn parse_completion_handles_array_and_list_shapes() {
        // Bare CompletionItem[].
        let v = json!([
            { "label": "push", "kind": 2, "detail": "fn push(&mut self, x: T)" },
            { "label": "pop",  "kind": 2 }
        ]);
        let items = parse_completion(&v);
        assert_eq!(items.len(), 2);
        assert_eq!(items[0].label, "push");
        assert_eq!(items[0].kind, 2);
        assert_eq!(items[0].detail.as_deref(), Some("fn push(&mut self, x: T)"));

        // CompletionList { isIncomplete, items }.
        let v = json!({
            "isIncomplete": true,
            "items": [{ "label": "len" }]
        });
        let items = parse_completion(&v);
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].label, "len");

        // null → empty.
        assert!(parse_completion(&Value::Null).is_empty());

        // textEdit honored, falling back to InsertReplaceEdit shape.
        let v = json!([{
            "label": "foo",
            "textEdit": {
                "newText": "foo()",
                "replace": {
                    "start": { "line": 1, "character": 2 },
                    "end":   { "line": 1, "character": 5 }
                },
                "insert": {
                    "start": { "line": 1, "character": 2 },
                    "end":   { "line": 1, "character": 4 }
                }
            }
        }]);
        let items = parse_completion(&v);
        let te = items[0].text_edit.as_ref().unwrap();
        assert_eq!(te.new_text, "foo()");
        // We pick `replace`, not `insert`.
        assert_eq!(te.range.end.character, 5);
    }

    #[test]
    fn parse_completion_preserves_raw_for_resolve_round_trip() {
        let v = json!([{
            "label": "HashMap",
            "data": { "opaque": "server-handle" }
        }]);
        let items = parse_completion(&v);
        assert_eq!(items[0].raw["data"]["opaque"], "server-handle");
    }

    #[test]
    fn parse_completion_resolve_pulls_out_additional_edits() {
        // The resolve response is a single CompletionItem object,
        // not an array — that's the shape distinction from the
        // initial completion request.
        let v = json!({
            "label": "HashMap",
            "additionalTextEdits": [{
                "range": {
                    "start": { "line": 0, "character": 0 },
                    "end":   { "line": 0, "character": 0 }
                },
                "newText": "use std::collections::HashMap;\n"
            }]
        });
        let item = parse_completion_resolve(&v).unwrap();
        assert_eq!(item.additional_text_edits.len(), 1);
        assert_eq!(
            item.additional_text_edits[0].new_text,
            "use std::collections::HashMap;\n"
        );
    }

    #[test]
    fn parse_completion_picks_up_additional_text_edits() {
        // Auto-import shape: the primary insertion is the symbol name
        // and the `additionalTextEdits` carry the `use …;` line.
        let v = json!([{
            "label": "HashMap",
            "additionalTextEdits": [{
                "range": {
                    "start": { "line": 0, "character": 0 },
                    "end":   { "line": 0, "character": 0 }
                },
                "newText": "use std::collections::HashMap;\n"
            }]
        }]);
        let items = parse_completion(&v);
        assert_eq!(items[0].additional_text_edits.len(), 1);
        assert_eq!(
            items[0].additional_text_edits[0].new_text,
            "use std::collections::HashMap;\n"
        );
    }

    #[test]
    fn parse_hover_handles_all_content_shapes() {
        // Modern MarkupContent.
        let v = json!({
            "contents": { "kind": "markdown", "value": "**fn** foo()" }
        });
        let h = parse_hover(&v).unwrap();
        assert_eq!(h.contents, "**fn** foo()");

        // Legacy bare MarkedString string.
        let v = json!({ "contents": "plain text" });
        assert_eq!(parse_hover(&v).unwrap().contents, "plain text");

        // Legacy MarkedString with language fence.
        let v = json!({
            "contents": { "language": "rust", "value": "fn foo()" }
        });
        let h = parse_hover(&v).unwrap();
        assert!(h.contents.contains("```rust"));
        assert!(h.contents.contains("fn foo()"));

        // Array of mixed entries — joined with blank lines.
        let v = json!({
            "contents": [
                { "language": "rust", "value": "fn foo()" },
                "docs go here"
            ]
        });
        let h = parse_hover(&v).unwrap();
        assert!(h.contents.contains("fn foo()"));
        assert!(h.contents.contains("docs go here"));
        assert!(h.contents.contains("\n\n"));

        // Empty / null → None.
        assert!(parse_hover(&Value::Null).is_none());
        assert!(parse_hover(&json!({ "contents": "" })).is_none());
        assert!(parse_hover(&json!({ "contents": [] })).is_none());
    }

    #[test]
    fn parse_code_actions_handles_array_and_unresolved() {
        let v = json!([
            {
                "title": "Quickfix: add semicolon",
                "edit": {
                    "changes": {
                        "file:///a.rs": [{
                            "range": {
                                "start": {"line": 0, "character": 5},
                                "end":   {"line": 0, "character": 5}
                            },
                            "newText": ";"
                        }]
                    }
                }
            },
            {
                "title": "Refactor: extract function",
                "data": "opaque-server-handle"
            }
        ]);
        let actions = parse_code_actions(&v);
        assert_eq!(actions.len(), 2);
        assert!(actions[0].edit.is_some());
        assert!(actions[1].edit.is_none());
        // The raw JSON is preserved for round-tripping through resolve.
        assert_eq!(actions[1].raw["data"], "opaque-server-handle");
    }
}