glacier-web-cli 0.5.0

Command-line tool for creating and running Glacier applications.
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
//! `glacier lsp`: a language server for `.glacier` files that proxies
//! rust-analyzer, the way templ (Go) and Volar (Vue) proxy their host
//! language's server.
//!
//! - The editor's `.glacier` documents are transpiled in memory and shown
//!   to rust-analyzer as their compiled twins in `.glacier/`.
//! - Positions in requests are mapped to the Rust file; every range and
//!   location in responses and diagnostics is mapped back.
//! - Everything else (Rust files, workspace requests, progress) passes
//!   through untouched, so one server handles a mixed project.
//!
//! Formatting `.glacier` documents is answered here, with `glacier fmt`'s
//! formatter, rather than by rustfmt on the compiled twin. So are symbols
//! (a document's outline, a workspace search), from the same index the MCP
//! server uses, so models, fields, callbacks and actions show as written.
//!
//! Lines map one to one by construction; columns go through the
//! transpiler's source map. Point any LSP client at `glacier lsp` for the
//! `glacier` file type. `GLACIER_RUST_ANALYZER` overrides the binary.

use std::collections::HashMap;
use std::io::{BufRead, BufReader, Write};
use std::process::{ChildStdin, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;

use glacier_dialect::{Mode, SourceMap, transpile_as};
use serde_json::{Value, json};

struct Doc {
    rust_uri: String,
    map: SourceMap,
}

#[derive(Default)]
struct State {
    /// Open `.glacier` documents by URI.
    docs: HashMap<String, Doc>,
    /// Generated `.rs` URI → `.glacier` URI.
    twins: HashMap<String, String>,
    /// The latest text of each open `.glacier` document, valid or not.
    texts: HashMap<String, String>,
    /// Requests sent on behalf of a `.glacier` document, by request id.
    /// (`.glacier` URI, method), by request id.
    pending: HashMap<String, (String, String)>,
    initialize_id: Option<String>,
    /// Whether the user has been told the project's dialect differs.
    warned_stale: bool,
    /// The project's directory, from `initialize`.
    root: Option<std::path::PathBuf>,
}

type Shared<T> = Arc<Mutex<T>>;

pub fn run() -> Result<(), String> {
    let binary = std::env::var("GLACIER_RUST_ANALYZER").unwrap_or_else(|_| "rust-analyzer".into());
    let mut child = Command::new(&binary)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .map_err(|e| format!("starting {binary}: {e}"))?;

    let state: Shared<State> = Arc::default();
    let server_in: Shared<ChildStdin> = Arc::new(Mutex::new(child.stdin.take().expect("piped")));
    let client_out: Shared<std::io::Stdout> = Arc::new(Mutex::new(std::io::stdout()));

    // rust-analyzer → editor
    let server_out = child.stdout.take().expect("piped");
    let (s, out) = (state.clone(), client_out.clone());
    let from_server = thread::spawn(move || {
        let mut reader = BufReader::new(server_out);
        while let Some(mut message) = read_message(&mut reader) {
            log(&json!({ "from_rust_analyzer": &message }));
            from_rust_analyzer(&s, &mut message);
            send(&out, &message);
        }
    });

    // editor → rust-analyzer
    let mut reader = BufReader::new(std::io::stdin());
    while let Some(message) = read_message(&mut reader) {
        let exit = message["method"] == "exit";
        for forward in from_editor(&state, &client_out, message) {
            send_to(&server_in, &forward);
        }
        if exit {
            break;
        }
    }
    drop(server_in);
    let _ = child.wait();
    let _ = from_server.join();
    Ok(())
}

fn read_message(reader: &mut impl BufRead) -> Option<Value> {
    let mut length = None;
    loop {
        let mut header = String::new();
        if reader.read_line(&mut header).ok()? == 0 {
            return None;
        }
        let header = header.trim_end();
        if header.is_empty() {
            break;
        }
        if let Some(value) = header.strip_prefix("Content-Length:") {
            length = value.trim().parse::<usize>().ok();
        }
    }
    let mut body = vec![0; length?];
    reader.read_exact(&mut body).ok()?;
    serde_json::from_slice(&body).ok()
}

fn frame(message: &Value) -> Vec<u8> {
    let body = message.to_string();
    let mut out = format!("Content-Length: {}\r\n\r\n", body.len()).into_bytes();
    out.extend(body.into_bytes());
    out
}

fn send(out: &Shared<std::io::Stdout>, message: &Value) {
    let mut out = out.lock().unwrap();
    let _ = out.write_all(&frame(message));
    let _ = out.flush();
}

/// `GLACIER_LSP_LOG=path` logs what the proxy sends rust-analyzer.
fn log(message: &Value) {
    if let Ok(path) = std::env::var("GLACIER_LSP_LOG")
        && let Ok(mut file) = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)
    {
        let _ = writeln!(file, "{message}");
    }
}

fn send_to(input: &Shared<ChildStdin>, message: &Value) {
    log(message);
    let mut input = input.lock().unwrap();
    let _ = input.write_all(&frame(message));
    let _ = input.flush();
}

fn is_glacier(uri: &str) -> bool {
    uri.ends_with(".glacier")
}

/// `file:///app/src/x/posts.glacier` → `file:///app/.glacier/x/posts.rs`,
/// its compiled twin in the app's build directory.
fn rust_twin(uri: &str) -> String {
    let path = uri.strip_prefix("file://").unwrap_or(uri);
    match glacier_dialect::build::output_path(std::path::Path::new(path)) {
        Some(output) => format!("file://{}", output.display()),
        None => format!("{uri}.rs"),
    }
}

/// The `.glacier` source of a generated file that isn't open in the editor
/// (go to definition into a model, say), with its source map.
fn closed_source(uri: &str) -> Option<(String, SourceMap)> {
    let path = std::path::Path::new(uri.strip_prefix("file://")?);
    let source = glacier_dialect::build::source_path(path)?;
    let text = std::fs::read_to_string(&source).ok()?;
    let name = source.file_name()?.to_string_lossy().into_owned();
    let output = transpile_as(&text, &name, Mode::for_path(&source)).ok()?;
    Some((format!("file://{}", source.display()), output.map))
}

fn id_key(id: &Value) -> String {
    id.to_string()
}

/// Handles a message from the editor, returning what to send on to
/// rust-analyzer (possibly nothing, possibly a rewritten message).
fn from_editor(
    state: &Shared<State>,
    client: &Shared<std::io::Stdout>,
    mut message: Value,
) -> Vec<Value> {
    let method = message["method"].as_str().unwrap_or_default().to_owned();
    let uri = message["params"]["textDocument"]["uri"]
        .as_str()
        .unwrap_or_default()
        .to_owned();

    if method == "initialize" {
        let mut s = state.lock().unwrap();
        s.initialize_id = Some(id_key(&message["id"]));
        let root = message["params"]["rootUri"]
            .as_str()
            .or_else(|| message["params"]["workspaceFolders"][0]["uri"].as_str());
        s.root = root.and_then(path_of);
        return vec![message];
    }
    // Symbols across the project: the `.glacier` index.
    if method == "workspace/symbol" {
        let root = state.lock().unwrap().root.clone();
        let query = message["params"]["query"].as_str().unwrap_or_default();
        send(
            client,
            &workspace_symbols(&message["id"], root.as_deref(), query),
        );
        return Vec::new();
    }
    // `completionItem/resolve` and friends carry no document, only the
    // item rust-analyzer made, whose opaque `data` names the Rust twin.
    if method.ends_with("/resolve") && !message["id"].is_null() {
        let s = &mut *state.lock().unwrap();
        if let Some(glacier_uri) = twin_in(&message["params"], &s.twins) {
            s.pending
                .insert(id_key(&message["id"]), (glacier_uri, method));
        }
        return vec![message];
    }
    if !is_glacier(&uri) {
        return vec![message];
    }

    match method.as_str() {
        "textDocument/didOpen" | "textDocument/didChange" => {
            if method == "textDocument/didOpen" {
                warn_if_stale(state, client, &uri);
            }
            let text = if method == "textDocument/didOpen" {
                message["params"]["textDocument"]["text"].as_str()
            } else {
                // Full sync is forced in `initialize`, so the last change
                // holds the whole document.
                message["params"]["contentChanges"]
                    .as_array()
                    .and_then(|c| c.last())
                    .and_then(|c| c["text"].as_str())
            }
            .unwrap_or_default()
            .to_owned();
            let version = message["params"]["textDocument"]["version"].clone();
            let name = uri.rsplit('/').next().unwrap_or_default().to_owned();
            let rust_uri = rust_twin(&uri);

            let mode = Mode::for_path(std::path::Path::new(&uri));
            state
                .lock()
                .unwrap()
                .texts
                .insert(uri.clone(), text.clone());
            match transpile_as(&text, &name, mode) {
                Ok(output) => {
                    let opening = {
                        let mut s = state.lock().unwrap();
                        let opening = !s.docs.contains_key(&uri);
                        s.twins.insert(rust_uri.clone(), uri.clone());
                        s.docs.insert(
                            uri.clone(),
                            Doc {
                                rust_uri: rust_uri.clone(),
                                map: output.map,
                            },
                        );
                        opening
                    };
                    let forward = if opening {
                        json!({"jsonrpc": "2.0", "method": "textDocument/didOpen", "params": {
                            "textDocument": {"uri": rust_uri, "languageId": "rust", "version": version, "text": output.rust}
                        }})
                    } else {
                        json!({"jsonrpc": "2.0", "method": "textDocument/didChange", "params": {
                            "textDocument": {"uri": rust_uri, "version": version},
                            "contentChanges": [{"text": output.rust}]
                        }})
                    };
                    vec![forward]
                }
                Err(error) => {
                    // A dialect syntax error: report it ourselves, and keep
                    // rust-analyzer on the last version that transpiled.
                    send(
                        client,
                        &json!({"jsonrpc": "2.0", "method": "textDocument/publishDiagnostics", "params": {
                            "uri": uri,
                            "diagnostics": [{
                                "range": {"start": {"line": error.line, "character": error.col},
                                          "end": {"line": error.line, "character": error.col + 1}},
                                "severity": 1,
                                "source": "glacier",
                                "message": error.message,
                                // As `glacier check` and the MCP give it.
                                "data": crate::analysis::glacier_error(&uri, &error),
                            }]
                        }}),
                    );
                    Vec::new()
                }
            }
        }
        "textDocument/didClose" | "textDocument/didSave" => {
            let mut s = state.lock().unwrap();
            // A document that never transpiled was never opened in
            // rust-analyzer, so there's nothing to close or save there.
            let known = s.docs.contains_key(&uri);
            if method == "textDocument/didClose" {
                s.docs.remove(&uri);
                s.texts.remove(&uri);
                s.twins.remove(&rust_twin(&uri));
            }
            if !known {
                return Vec::new();
            }
            message["params"]["textDocument"]["uri"] = Value::String(rust_twin(&uri));
            vec![message]
        }
        "textDocument/documentSymbol" => {
            let text = state
                .lock()
                .unwrap()
                .texts
                .get(&uri)
                .cloned()
                .unwrap_or_default();
            let symbols = document_symbols(&crate::symbols::glacier_symbols(&text, &uri));
            send(
                client,
                &json!({"jsonrpc": "2.0", "id": message["id"], "result": symbols}),
            );
            Vec::new()
        }
        "textDocument/formatting" => {
            let text = state.lock().unwrap().texts.get(&uri).cloned();
            send(client, &format_response(&message["id"], &uri, text));
            Vec::new()
        }
        // Formatting is whole-file; rustfmt's partial edits would land on
        // the compiled twin, so these get no edits.
        "textDocument/rangeFormatting" | "textDocument/onTypeFormatting" => {
            send(
                client,
                &json!({"jsonrpc": "2.0", "id": message["id"], "result": null}),
            );
            Vec::new()
        }
        _ => {
            // A request about a position in a .glacier document.
            let s = &mut *state.lock().unwrap();
            let Some(doc) = s.docs.get(&uri) else {
                // Not transpiled yet (a syntax error since it opened), so
                // rust-analyzer has no twin to ask: answer empty here.
                if !message["id"].is_null() {
                    send(client, &unknown_document_response(&message["id"], &method));
                }
                return Vec::new();
            };
            message["params"]["textDocument"]["uri"] = Value::String(doc.rust_uri.clone());
            // `get_mut`: indexing to write would add missing keys as null.
            if let Some(position) = message["params"].get_mut("position") {
                map_position(position, &doc.map, true);
            }
            if let Some(range) = message["params"].get_mut("range") {
                map_range(range, &doc.map, true);
            }
            if !message["id"].is_null() {
                s.pending.insert(id_key(&message["id"]), (uri, method));
            }
            vec![message]
        }
    }
}

/// A filesystem path from a `file://` URI.
fn path_of(uri: &str) -> Option<std::path::PathBuf> {
    let path = uri.strip_prefix("file://")?;
    let decoded = path.replace("%20", " ");
    Some(std::path::PathBuf::from(decoded))
}

/// A symbol as LSP's `SymbolInformation`, in `uri`.
fn symbol_information(symbol: &crate::symbols::Symbol, uri: &str) -> Value {
    // LSP's SymbolKind numbers.
    let kind = match symbol.kind {
        "model" | "controller" | "serializer" | "mailer" | "client" | "middleware" => 5,
        "module" => 2,
        "service" => 2,
        "field" => 8,
        "association" => 7,
        "callback" => 24,
        "method" | "action" | "scope" | "changeset" => 6,
        "job" | "struct" => 23,
        "factory" => 9,
        _ => 12,
    };
    let start = json!({"line": symbol.line - 1, "character": symbol.column - 1});
    let end_line = symbol.end_line.unwrap_or(symbol.line) - 1;
    let mut out = json!({
        "name": symbol.qualified(),
        "kind": kind,
        "location": {"uri": uri, "range": {"start": start, "end": {"line": end_line, "character": 0}}},
    });
    if let Some(parent) = &symbol.parent {
        out["containerName"] = parent.clone().into();
    }
    out
}

/// A document's outline as LSP `DocumentSymbol`s: each model, controller
/// or service with its members inside it.
fn document_symbols(symbols: &[crate::symbols::Symbol]) -> Vec<Value> {
    let item = |symbol: &crate::symbols::Symbol| {
        let indent = symbol.column - 1;
        // The name's own span on its line, for the selection range.
        let name_at = symbol
            .text
            .find(symbol.name.as_str())
            .map_or(indent, |at| indent + at);
        let line = symbol.line - 1;
        let end_line = symbol.end_line.unwrap_or(symbol.line) - 1;
        let information = symbol_information(symbol, "");
        json!({
            "name": symbol.name,
            "kind": information["kind"],
            "range": {
                "start": {"line": line, "character": indent},
                "end": {"line": end_line, "character": indent + 3},
            },
            "selectionRange": {
                "start": {"line": line, "character": name_at},
                "end": {"line": line, "character": name_at + symbol.name.len()},
            },
            "children": [],
        })
    };
    let mut out: Vec<Value> = Vec::new();
    for symbol in symbols {
        let parent = symbol
            .parent
            .as_ref()
            .and_then(|parent| out.iter_mut().find(|o| o["name"] == parent.as_str()));
        match parent {
            Some(parent) => parent["children"]
                .as_array_mut()
                .expect("children")
                .push(item(symbol)),
            None => out.push(item(symbol)),
        }
    }
    out
}

/// The reply to `workspace/symbol`: the project's symbols whose name holds
/// the query (any case).
fn workspace_symbols(id: &Value, root: Option<&std::path::Path>, query: &str) -> Value {
    let query = query.to_lowercase();
    let found: Vec<Value> = root
        .map(crate::symbols::index)
        .unwrap_or_default()
        .iter()
        .filter(|s| s.qualified().to_lowercase().contains(&query))
        .take(500)
        .map(|s| {
            let uri = format!(
                "file://{}",
                root.unwrap_or(std::path::Path::new("."))
                    .join(&s.file)
                    .display()
            );
            symbol_information(s, &uri)
        })
        .collect();
    json!({"jsonrpc": "2.0", "id": id, "result": found})
}

/// The empty reply to a request about a `.glacier` document rust-analyzer
/// hasn't seen. Pull diagnostics need a report; everything else takes null.
fn unknown_document_response(id: &Value, method: &str) -> Value {
    let result = if method == "textDocument/diagnostic" {
        json!({"kind": "full", "items": []})
    } else {
        Value::Null
    };
    json!({"jsonrpc": "2.0", "id": id, "result": result})
}

/// Tells the user, once, when the project was compiled by a different
/// version of the dialect than this `glacier` (a stale install, say), as
/// its syntax may differ from what the editor understands.
fn warn_if_stale(state: &Shared<State>, client: &Shared<std::io::Stdout>, uri: &str) {
    let path = std::path::Path::new(uri.strip_prefix("file://").unwrap_or(uri));
    let Some(project) = glacier_dialect::build::project_fingerprint(path) else {
        return;
    };
    if project == glacier_dialect::FINGERPRINT {
        return;
    }
    {
        let mut s = state.lock().unwrap();
        if s.warned_stale {
            return;
        }
        s.warned_stale = true;
    }
    send(client, &stale_warning());
}

fn stale_warning() -> Value {
    json!({"jsonrpc": "2.0", "method": "window/showMessage", "params": {
        "type": 2,
        "message": "This project was compiled by a different version of the .glacier dialect \
                    than the installed `glacier`, so the editor may not understand its syntax. \
                    Reinstall the CLI (`cargo install glacier-web-cli`, or \
                    `cargo install --path crates/glacier-cli` in a checkout) to match."
    }})
}

/// The reply to `textDocument/formatting`: one edit replacing the whole
/// document, none when it's already formatted, or an error explaining why
/// it can't be formatted.
fn format_response(id: &Value, uri: &str, text: Option<String>) -> Value {
    let Some(text) = text else {
        return json!({"jsonrpc": "2.0", "id": id, "result": null});
    };
    let mode = Mode::for_path(std::path::Path::new(uri));
    match glacier_dialect::format::format(&text, mode) {
        Ok(formatted) if formatted == text => json!({"jsonrpc": "2.0", "id": id, "result": []}),
        Ok(formatted) => {
            let lines = text.split('\n').count();
            json!({"jsonrpc": "2.0", "id": id, "result": [{
                "range": {"start": {"line": 0, "character": 0},
                          "end": {"line": lines, "character": 0}},
                "newText": formatted,
            }]})
        }
        Err(error) => json!({"jsonrpc": "2.0", "id": id, "error": {
            "code": -32603,
            "message": format!("can't format this file: {error}"),
        }}),
    }
}

/// Rewrites a message from rust-analyzer for the editor.
fn from_rust_analyzer(state: &Shared<State>, message: &mut Value) {
    let s = &mut *state.lock().unwrap();

    if message.get("id").is_some() && message.get("method").is_none() {
        let key = id_key(&message["id"]);
        if s.initialize_id.as_deref() == Some(key.as_str()) {
            // Full document sync keeps the proxy simple: every change
            // carries the whole text, which is retranspiled.
            let sync = &mut message["result"]["capabilities"]["textDocumentSync"];
            if sync.is_object() {
                sync["change"] = json!(1);
            } else {
                *sync = json!(1);
            }
            return;
        }
        if let Some((uri, method)) = s.pending.remove(&key) {
            let context = s.docs.get(&uri).map(|d| &d.map);
            tidy(&method, &mut message["result"], context);
            remap(&mut message["result"], context, s);
        }
        return;
    }

    if message["method"] == "textDocument/publishDiagnostics" {
        let rust_uri = message["params"]["uri"]
            .as_str()
            .unwrap_or_default()
            .to_owned();
        if let Some(uri) = s.twins.get(&rust_uri).cloned() {
            message["params"]["uri"] = Value::String(uri.clone());
            let map = s.docs.get(&uri).map(|d| &d.map);
            remap(&mut message["params"]["diagnostics"], map, s);
        }
    }
}

/// Maps every range in `value` back to `.glacier` coordinates. `context` is
/// the map for ranges not tied to a URI (they belong to the request's
/// document); objects carrying a generated URI switch to that document.
/// The `.glacier` URI whose Rust twin `value` mentions anywhere.
fn twin_in(value: &Value, twins: &HashMap<String, String>) -> Option<String> {
    match value {
        Value::String(text) => twins.get(text).cloned(),
        Value::Array(items) => items.iter().find_map(|v| twin_in(v, twins)),
        Value::Object(object) => object.values().find_map(|v| twin_in(v, twins)),
        _ => None,
    }
}

/// Drops what can't apply to `.glacier` source from a response to `method`
/// (in Rust coordinates, before `remap`).
///
/// - Code actions: their edits are Rust code, which would corrupt the
///   `.glacier` file. None are offered.
/// - Completions' `additionalTextEdits`: Rust `use` lines for auto-imports.
/// - Inlay hints placed in generated code (between source tokens), which
///   have no sensible place in the source.
fn tidy(method: &str, result: &mut Value, map: Option<&SourceMap>) {
    match method {
        "textDocument/codeAction" => *result = json!([]),
        "textDocument/completion" | "completionItem/resolve" => {
            let items = match result {
                Value::Object(list) if list.contains_key("items") => &mut list["items"],
                other => other,
            };
            match items {
                Value::Array(items) => items.iter_mut().for_each(drop_imports),
                item => drop_imports(item),
            }
        }
        "textDocument/inlayHint" => {
            if let (Value::Array(hints), Some(map)) = (result, map) {
                hints.retain(|hint| {
                    let (Some(line), Some(col)) = (
                        hint["position"]["line"].as_u64(),
                        hint["position"]["character"].as_u64(),
                    ) else {
                        return false;
                    };
                    map.to_glacier_exact(line as usize, col as usize).is_some()
                });
            }
        }
        _ => {}
    }
}

fn drop_imports(item: &mut Value) {
    if let Value::Object(item) = item {
        item.remove("additionalTextEdits");
    }
}

fn remap(value: &mut Value, context: Option<&SourceMap>, s: &State) {
    match value {
        Value::Object(object) => {
            let mut context = context;
            // The map of a generated file whose source isn't open.
            let mut closed: Option<SourceMap> = None;
            for key in ["uri", "targetUri"] {
                let Some(uri) = object.get(key).and_then(Value::as_str).map(str::to_owned) else {
                    continue;
                };
                match s.twins.get(&uri) {
                    Some(glacier_uri) => {
                        context = s.docs.get(glacier_uri).map(|d| &d.map);
                        object.insert(key.into(), Value::String(glacier_uri.clone()));
                    }
                    None => match closed_source(&uri) {
                        Some((glacier_uri, map)) => {
                            closed = Some(map);
                            object.insert(key.into(), Value::String(glacier_uri));
                        }
                        // A location in some other file: leave its ranges be.
                        None => context = None,
                    },
                }
            }
            if closed.is_some() {
                context = closed.as_ref();
            }
            if is_range(object) {
                if let Some(map) = context {
                    let mut range = Value::Object(std::mem::take(object));
                    map_range(&mut range, map, false);
                    if let Value::Object(mapped) = range {
                        *object = mapped;
                    }
                }
                return;
            }
            for (key, child) in object.iter_mut() {
                match key.as_str() {
                    // Opaque to the editor: rust-analyzer gets it back as is
                    // (e.g. in `completionItem/resolve`).
                    "data" => {}
                    // A lone position, e.g. an inlay hint's.
                    "position" if is_position(child) => {
                        if let Some(map) = context {
                            map_position(child, map, false);
                        }
                    }
                    _ => remap(child, context, s),
                }
            }
            if context.is_some() {
                contain_selection(object);
            }
        }
        Value::Array(items) => {
            for item in items {
                remap(item, context, s);
            }
        }
        _ => {}
    }
}

/// Symbols (and call or type hierarchy items) must have their
/// `selectionRange` (the name) inside their `range`, or VS Code rejects them.
/// Columns are mapped separately, and code like `controller Posts do`
/// compiles to two Rust items on one line, so mapped ranges may not nest:
/// grow `range` to cover the name.
fn contain_selection(object: &mut serde_json::Map<String, Value>) {
    let (Some(range), Some(selection)) = (object.get("range"), object.get("selectionRange")) else {
        return;
    };
    let (Some(start), Some(end), Some(sel_start), Some(sel_end)) = (
        point(&range["start"]),
        point(&range["end"]),
        point(&selection["start"]),
        point(&selection["end"]),
    ) else {
        return;
    };
    let (start, end) = (start.min(sel_start), end.max(sel_end));
    object.insert(
        "range".into(),
        json!({"start": {"line": start.0, "character": start.1},
               "end": {"line": end.0, "character": end.1}}),
    );
}

/// A position as (line, character), which orders like positions do.
fn point(position: &Value) -> Option<(u64, u64)> {
    Some((position["line"].as_u64()?, position["character"].as_u64()?))
}

fn is_range(object: &serde_json::Map<String, Value>) -> bool {
    object.len() == 2
        && object.get("start").is_some_and(is_position)
        && object.get("end").is_some_and(is_position)
}

fn is_position(value: &Value) -> bool {
    value["line"].is_u64() && value["character"].is_u64()
}

fn map_range(range: &mut Value, map: &SourceMap, to_rust: bool) {
    if !range.is_object() {
        return;
    }
    map_position(&mut range["start"], map, to_rust);
    map_position(&mut range["end"], map, to_rust);
}

fn map_position(position: &mut Value, map: &SourceMap, to_rust: bool) {
    let (Some(line), Some(col)) = (position["line"].as_u64(), position["character"].as_u64())
    else {
        return;
    };
    let (line, col) = (line as usize, col as usize);
    let mapped = if to_rust {
        map.to_rust(line, col)
    } else {
        map.to_glacier(line, col)
    };
    position["character"] = json!(mapped);
}

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

    #[test]
    fn symbols_come_from_the_glacier_index() {
        let dir = std::env::temp_dir().join(format!("glacier-lsp-symbols-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("src/models")).unwrap();
        std::fs::write(
            dir.join("src/models/post.glacier"),
            "model Post do\n  field title: String\n\n  before_save do\n  end\nend\n",
        )
        .unwrap();
        let reply = workspace_symbols(&json!(7), Some(&dir), "post.TIT");
        assert_eq!(reply["id"], 7);
        let found = reply["result"].as_array().unwrap();
        assert_eq!(found.len(), 1);
        assert_eq!(found[0]["name"], "Post.title");
        assert_eq!(found[0]["kind"], 8, "a field");
        assert_eq!(found[0]["containerName"], "Post");
        assert_eq!(
            found[0]["location"]["range"]["start"],
            json!({"line": 1, "character": 2})
        );
        assert!(
            found[0]["location"]["uri"]
                .as_str()
                .unwrap()
                .ends_with("src/models/post.glacier")
        );

        let all = workspace_symbols(&json!(8), Some(&dir), "");
        let names: Vec<&str> = all["result"]
            .as_array()
            .unwrap()
            .iter()
            .map(|s| s["name"].as_str().unwrap())
            .collect();
        assert_eq!(names, ["Post", "Post.title", "Post.before_save"]);
        assert_eq!(
            path_of("file:///a%20b/c").unwrap(),
            std::path::PathBuf::from("/a b/c")
        );
        std::fs::remove_dir_all(&dir).ok();
    }

    fn state_with(glacier: &str) -> State {
        let uri = "file:///app/src/controllers/a.glacier".to_owned();
        let rust_uri = "file:///app/.glacier/controllers/a.rs".to_owned();
        let map = glacier_dialect::transpile(glacier, "a.glacier")
            .unwrap()
            .map;
        let mut state = State::default();
        state.twins.insert(rust_uri.clone(), uri.clone());
        state.docs.insert(uri, Doc { rust_uri, map });
        state
    }

    #[test]
    fn inlay_hints_land_after_source_tokens() {
        let glacier = "controller A do\n  def x do\n    posts = Post.all()!\n  end\nend\n";
        let s = state_with(glacier);
        let map = &s.docs.values().next().unwrap().map;
        // `    let mut posts = Post::all().await?;`: a type hint after `posts`
        // (col 17) and one in generated code (inside `let`, col 5).
        let mut hints = json!([
            {"position": {"line": 2, "character": 17}, "label": ": Vec<Post>"},
            {"position": {"line": 2, "character": 5}, "label": "nonsense"},
        ]);
        tidy("textDocument/inlayHint", &mut hints, Some(map));
        remap(&mut hints, Some(map), &s);
        assert_eq!(
            hints,
            json!([{"position": {"line": 2, "character": 9}, "label": ": Vec<Post>"}])
        );
    }

    #[test]
    fn opaque_data_and_rust_edits_stay_out_of_glacier_files() {
        let s = state_with("controller A do\nend\n");
        let twin = "file:///app/.glacier/controllers/a.rs";
        let mut completion = json!({"items": [{
            "label": "find",
            "additionalTextEdits": [{"range": {"start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 0}}, "newText": "use x;\n"}],
            "data": {"position": {"textDocument": {"uri": twin}, "position": {"line": 0, "character": 3}}},
        }]});
        tidy("textDocument/completion", &mut completion, None);
        remap(&mut completion, None, &s);
        assert!(completion["items"][0].get("additionalTextEdits").is_none());
        assert_eq!(
            completion["items"][0]["data"]["position"]["textDocument"]["uri"], twin,
            "rust-analyzer needs its own data back to resolve the item"
        );
        // A resolve request names the twin only inside that data.
        assert_eq!(
            twin_in(&completion["items"][0], &s.twins).as_deref(),
            Some("file:///app/src/controllers/a.glacier")
        );

        let mut actions = json!([{"title": "Fill match arms", "data": {}}]);
        tidy("textDocument/codeAction", &mut actions, None);
        assert_eq!(actions, json!([]));
    }

    #[test]
    fn symbol_names_stay_inside_their_ranges() {
        // `controller Posts do` → `pub struct Posts; #[glacier::controller] impl Posts {`:
        // the impl's range starts at `impl`, which has no source column.
        let mut symbol = json!({
            "name": "impl Posts",
            "range": {"start": {"line": 2, "character": 20}, "end": {"line": 9, "character": 3}},
            "selectionRange": {"start": {"line": 2, "character": 11}, "end": {"line": 2, "character": 16}},
        });
        contain_selection(symbol.as_object_mut().unwrap());
        assert_eq!(
            symbol["range"]["start"],
            json!({"line": 2, "character": 11})
        );
        assert_eq!(symbol["range"]["end"], json!({"line": 9, "character": 3}));

        // Already nested: unchanged.
        let nested = json!({
            "range": {"start": {"line": 1, "character": 0}, "end": {"line": 3, "character": 5}},
            "selectionRange": {"start": {"line": 1, "character": 4}, "end": {"line": 1, "character": 8}},
        });
        let mut copy = nested.clone();
        contain_selection(copy.as_object_mut().unwrap());
        assert_eq!(copy, nested);
    }

    #[test]
    fn documents_that_never_transpiled_stay_away_from_rust_analyzer() {
        let state: Shared<State> = Arc::default();
        let client = Arc::new(Mutex::new(std::io::stdout()));
        let uri = "file:///app/src/controllers/broken.glacier";
        let open = json!({"jsonrpc": "2.0", "method": "textDocument/didOpen", "params": {
            "textDocument": {"uri": uri, "languageId": "glacier", "version": 1, "text": "def (\n"}
        }});
        assert!(from_editor(&state, &client, open).is_empty());
        let hover = json!({"jsonrpc": "2.0", "id": 7, "method": "textDocument/hover", "params": {
            "textDocument": {"uri": uri}, "position": {"line": 0, "character": 1}
        }});
        assert!(from_editor(&state, &client, hover).is_empty());
        let close = json!({"jsonrpc": "2.0", "method": "textDocument/didClose", "params": {
            "textDocument": {"uri": uri}
        }});
        assert!(from_editor(&state, &client, close).is_empty());
        assert_eq!(
            unknown_document_response(&json!(3), "textDocument/diagnostic")["result"]["kind"],
            "full"
        );
    }

    #[test]
    fn warns_once_when_the_project_was_compiled_by_another_dialect() {
        let app = std::env::temp_dir().join(format!("glacier-lsp-stale-{}", std::process::id()));
        std::fs::create_dir_all(app.join("src/models")).unwrap();
        std::fs::create_dir_all(app.join(".glacier")).unwrap();
        std::fs::write(app.join(".glacier/.dialect"), "0000000000000000").unwrap();
        let uri = format!("file://{}/src/models/post.glacier", app.display());
        let state: Shared<State> = Arc::default();
        let client = Arc::new(Mutex::new(std::io::stdout()));
        warn_if_stale(&state, &client, &uri);
        assert!(state.lock().unwrap().warned_stale);
        std::fs::write(app.join(".glacier/.dialect"), glacier_dialect::FINGERPRINT).unwrap();
        let fresh: Shared<State> = Arc::default();
        warn_if_stale(&fresh, &client, &uri);
        assert!(
            !fresh.lock().unwrap().warned_stale,
            "same dialect: no warning"
        );
        assert_eq!(stale_warning()["params"]["type"], 2);
        std::fs::remove_dir_all(app).ok();
    }

    #[test]
    fn formats_glacier_documents_whole() {
        let id = json!(7);
        let uri = "file:///app/src/controllers/a.glacier";
        let messy = "controller A do\ndef x do\n1\nend\nend\n".to_owned();
        let reply = format_response(&id, uri, Some(messy));
        assert_eq!(reply["result"][0]["range"]["end"]["line"], 6);
        assert_eq!(
            reply["result"][0]["newText"],
            "controller A do\n  def x do\n    1\n  end\nend\n"
        );

        let tidy = "controller A do\nend\n".to_owned();
        assert_eq!(format_response(&id, uri, Some(tidy))["result"], json!([]));

        let broken = "controller A do\n".to_owned();
        let reply = format_response(&id, uri, Some(broken));
        assert!(
            reply["error"]["message"]
                .as_str()
                .unwrap()
                .contains("missing its `end`")
        );

        // Test files format in test mode.
        let test = "test \"a\" do\nassert 1 == 1\nend\n".to_owned();
        let reply = format_response(&id, "file:///app/tests/a.test.glacier", Some(test));
        assert_eq!(
            reply["result"][0]["newText"],
            "test \"a\" do\n  assert 1 == 1\nend\n"
        );
    }
}