dist_agent_lang 1.0.23

Agentic programming with library and CLI support for Off/On-chain network integration
Documentation
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
//! LSP (Language Server Protocol) server for DAL — Phase 1–3: diagnostics, hover, completion, definition, signature help.
//! See docs/development/LSP_AND_AGENT_INTEGRATION_PLAN.md.

#![cfg(feature = "lsp")]

use dist_agent_lang::lexer::Lexer;
use dist_agent_lang::parser::ast::Statement;
use dist_agent_lang::parser::Parser;
use lsp_types::{
    CompletionItem, CompletionOptions, CompletionParams, CompletionResponse, Diagnostic,
    DiagnosticSeverity, DidChangeTextDocumentParams, DidCloseTextDocumentParams,
    DidOpenTextDocumentParams, GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverContents,
    HoverParams, HoverProviderCapability, InitializeParams, InitializeResult, InitializedParams,
    Location, OneOf, ParameterInformation, Position, Range, ServerCapabilities, SignatureHelp,
    SignatureHelpOptions, SignatureHelpParams, SignatureInformation, TextDocumentSyncCapability,
    TextDocumentSyncKind, TextDocumentSyncOptions, Url,
};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
use tower_lsp::{async_trait, Client, LanguageServer, LspService, Server};

#[derive(Debug)]
pub struct Backend {
    client: Client,
    documents: Arc<Mutex<HashMap<Url, DocumentState>>>,
}

#[derive(Debug, Clone)]
struct DocumentState {
    text: String,
}

impl Backend {
    pub fn new(client: Client) -> Self {
        Self {
            client,
            documents: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Convert 1-based line/column (DAL) to 0-based LSP Position.
    fn to_lsp_position(line: usize, column: usize) -> Position {
        Position {
            line: line.saturating_sub(1) as u32,
            character: column.saturating_sub(1) as u32,
        }
    }

    /// Build LSP diagnostics from source: run lexer and parser, map errors to Diagnostic.
    fn diagnostics_from_source(source: &str) -> Vec<Diagnostic> {
        let mut diags = Vec::new();

        // Lexer
        let lexer = Lexer::new(source);
        match lexer.tokenize_with_positions_immutable() {
            Err(e) => {
                let (line, col) = e.line_column().unwrap_or((1, 1));
                diags.push(Diagnostic {
                    range: Range {
                        start: Self::to_lsp_position(line, col),
                        end: Self::to_lsp_position(line, col.saturating_add(1)),
                    },
                    severity: Some(DiagnosticSeverity::ERROR),
                    code: None,
                    code_description: None,
                    source: Some("dal".to_string()),
                    message: e.to_string(),
                    related_information: None,
                    tags: None,
                    data: None,
                });
                return diags;
            }
            Ok(tokens_with_pos) => {
                // Parser
                let mut parser = Parser::new_with_positions(tokens_with_pos);
                if let Err(e) = parser.parse() {
                    let line = e.line_number().unwrap_or(1);
                    let col = e.column_number().unwrap_or(1);
                    diags.push(Diagnostic {
                        range: Range {
                            start: Self::to_lsp_position(line, col),
                            end: Self::to_lsp_position(line, col.saturating_add(1)),
                        },
                        severity: Some(DiagnosticSeverity::ERROR),
                        code: None,
                        code_description: None,
                        source: Some("dal".to_string()),
                        message: e.to_string(),
                        related_information: None,
                        tags: None,
                        data: None,
                    });
                }
            }
        }

        diags
    }

    async fn publish_diagnostics_for_uri(&self, uri: Url, version: Option<i32>) {
        let text = {
            let docs = self.documents.lock().await;
            docs.get(&uri).map(|d| d.text.clone())
        };
        let diags = match text.as_deref() {
            Some(t) => Self::diagnostics_from_source(t),
            None => vec![],
        };
        self.client.publish_diagnostics(uri, diags, version).await;
    }

    /// Get the word (identifier or keyword) at 0-based line and character in source.
    fn word_at_position(source: &str, line_0: u32, char_0: u32) -> Option<String> {
        let lines: Vec<&str> = source.lines().collect();
        let line_idx = line_0 as usize;
        let line = lines.get(line_idx)?;
        let chars: Vec<char> = line.chars().collect();
        let char_idx = char_0 as usize;
        if char_idx > chars.len() {
            return None;
        }
        fn is_word_char(c: char) -> bool {
            c.is_ascii_alphanumeric() || c == '_'
        }
        let start = chars[..char_idx]
            .iter()
            .rposition(|&c| is_word_char(c))
            .map(|i| i + 1)
            .unwrap_or(0);
        let end = chars[char_idx..]
            .iter()
            .position(|&c| !is_word_char(c))
            .map(|i| char_idx + i)
            .unwrap_or(chars.len());
        if start >= end {
            return None;
        }
        Some(chars[start..end].iter().collect())
    }

    /// Short doc for a keyword (Phase 2: minimal set).
    fn keyword_doc(word: &str) -> Option<&'static str> {
        Some(match word {
            "fn" => "Function declaration. `fn name(params) -> return_type { ... }`",
            "let" => "Bind a value to a variable.",
            "return" => "Return a value from a function.",
            "if" | "else" => "Conditional branch.",
            "while" | "for" | "loop" => "Loop construct.",
            "service" => "Service declaration. `service Name { fields; fn methods() {} }`",
            "struct" => "Struct type definition.",
            "import" => "Import a module. `import \"path\";` or `import \"path\" as alias;`",
            "match" | "case" | "default" => "Pattern matching.",
            "try" | "catch" | "throw" => "Error handling.",
            "spawn" | "agent" => "Agent / concurrent execution.",
            "true" | "false" => "Boolean literal.",
            _ => return None,
        })
    }

    /// Short doc for a stdlib module name.
    fn stdlib_doc(module: &str) -> Option<&'static str> {
        Some(match module {
            "chain" => "Blockchain operations: deploy, call, balance, gas, etc.",
            "ai" | "assist" => "AI/LLM: generate_text, spawn_agent, send_message, etc.",
            "log" => "Logging: info, error, warning, audit.",
            "auth" => "Authentication and authorization.",
            "config" => "Configuration and environment.",
            "db" => "Database operations.",
            "crypto" => "Cryptography: hash, sign, verify, keygen.",
            "oracle" => "Oracle and external data.",
            "agent" => "Agent coordination and communication.",
            _ => return None,
        })
    }

    /// Collect symbol names and details from parsed AST for completion/hover.
    fn collect_symbols_from_source(source: &str) -> Vec<(String, String)> {
        let mut symbols = Vec::new();
        let lexer = Lexer::new(source);
        let tokens_with_pos = match lexer.tokenize_with_positions_immutable() {
            Ok(t) => t,
            Err(_) => return symbols,
        };
        let mut parser = Parser::new_with_positions(tokens_with_pos);
        let program = match parser.parse() {
            Ok(p) => p,
            Err(_) => return symbols,
        };
        for stmt in &program.statements {
            match stmt {
                Statement::Service(s) => {
                    symbols.push((s.name.clone(), format!("service {}", s.name)));
                    for f in &s.fields {
                        symbols.push((
                            f.name.clone(),
                            format!("field {}: {}", f.name, f.field_type),
                        ));
                    }
                    for m in &s.methods {
                        let params: Vec<String> =
                            m.parameters.iter().map(|p| p.name.clone()).collect();
                        let sig = format!("fn {}({})", m.name, params.join(", "));
                        symbols.push((m.name.clone(), sig));
                    }
                }
                Statement::Function(f) => {
                    let params: Vec<String> = f.parameters.iter().map(|p| p.name.clone()).collect();
                    let sig = format!("fn {}({})", f.name, params.join(", "));
                    symbols.push((f.name.clone(), sig));
                }
                _ => {}
            }
        }
        symbols
    }

    /// Resolve hover content for a word (keyword, stdlib, or symbol from AST).
    fn hover_for_word(&self, source: &str, word: &str) -> Option<String> {
        if let Some(doc) = Self::keyword_doc(word) {
            return Some(format!("**{}**\n\n{}", word, doc));
        }
        if let Some(doc) = Self::stdlib_doc(word) {
            return Some(format!("**{}** (stdlib)\n\n{}", word, doc));
        }
        for (name, detail) in Self::collect_symbols_from_source(source) {
            if name == word {
                return Some(format!("**{}**\n\n`{}`", name, detail));
            }
        }
        None
    }

    /// Find the definition range (0-based) of a symbol in source. Returns None for keywords/stdlib.
    fn find_definition_range(source: &str, name: &str) -> Option<Range> {
        fn word_boundary_before(s: &str, i: usize) -> bool {
            i == 0
                || !s
                    .chars()
                    .nth(i.saturating_sub(1))
                    .map_or(false, |c| c.is_ascii_alphanumeric() || c == '_')
        }
        fn word_boundary_after(s: &str, i: usize, len: usize) -> bool {
            let end = i + len;
            let ch = s.chars().nth(end);
            ch.map_or(true, |c| !c.is_ascii_alphanumeric() && c != '_')
        }
        for (line_0, line) in source.lines().enumerate() {
            // service Name { or service Name \n
            if let Some(rest) = line.strip_prefix("service ") {
                let name_start = rest
                    .find(|c: char| c.is_ascii_alphabetic() || c == '_')
                    .unwrap_or(0);
                let name_end = rest[name_start..]
                    .find(|c: char| !c.is_ascii_alphanumeric() && c != '_')
                    .map_or(rest[name_start..].len(), |i| name_start + i);
                let def_name = &rest[name_start..name_end];
                if def_name == name && word_boundary_after(rest, name_start, name.len()) {
                    let char_0 = rest[..name_start].chars().count() as u32;
                    return Some(Range {
                        start: Position {
                            line: line_0 as u32,
                            character: char_0,
                        },
                        end: Position {
                            line: line_0 as u32,
                            character: char_0 + name.chars().count() as u32,
                        },
                    });
                }
            }
            // fn name( or fn name (
            if let Some(rest) = line.strip_prefix("fn ") {
                let name_start = rest
                    .find(|c: char| c.is_ascii_alphabetic() || c == '_')
                    .unwrap_or(0);
                let name_end = rest[name_start..]
                    .find(|c: char| c == '(' || c.is_whitespace())
                    .map_or(rest[name_start..].len(), |i| name_start + i);
                let def_name = &rest[name_start..name_end];
                if def_name == name {
                    let char_0 = 3u32 + rest[..name_start].chars().count() as u32;
                    return Some(Range {
                        start: Position {
                            line: line_0 as u32,
                            character: char_0,
                        },
                        end: Position {
                            line: line_0 as u32,
                            character: char_0 + name.chars().count() as u32,
                        },
                    });
                }
            }
            // field_name: type (word boundary before name)
            if let Some(idx) = line.find(name) {
                if word_boundary_before(line, idx) {
                    let after = &line[idx + name.len()..];
                    if after.starts_with(':')
                        && (idx == 0 || line[..idx].ends_with(' ') || line[..idx].ends_with('\t'))
                    {
                        let char_0 = line[..idx].chars().count() as u32;
                        return Some(Range {
                            start: Position {
                                line: line_0 as u32,
                                character: char_0,
                            },
                            end: Position {
                                line: line_0 as u32,
                                character: char_0 + name.chars().count() as u32,
                            },
                        });
                    }
                }
            }
        }
        None
    }

    /// Find the function call containing position and the active parameter index (0-based).
    fn function_call_at_position(source: &str, line_0: u32, char_0: u32) -> Option<(String, u32)> {
        let lines: Vec<&str> = source.lines().collect();
        let line_idx = line_0 as usize;
        let line = lines.get(line_idx)?;
        let char_idx = char_0 as usize;
        let prefix = line.chars().take(char_idx).collect::<String>();
        let suffix = line.chars().skip(char_idx).collect::<String>();
        // Find the last "name(" before cursor on this line; if cursor is inside ( ), count commas to get active param.
        let open = prefix.rfind('(')?;
        let before_open = prefix[..open].trim_end();
        let name_start =
            before_open.rfind(|c: char| c.is_ascii_alphanumeric() || c == '_' || c == ':')?;
        let _name_end = before_open.len();
        let name_part = before_open[name_start..].trim_start_matches(':');
        let name_start_in_part = name_part
            .find(|c: char| c.is_ascii_alphabetic() || c == '_')
            .unwrap_or(0);
        let fn_name = name_part[name_start_in_part..]
            .split_whitespace()
            .next()?
            .to_string();
        let after_open = suffix.clone();
        let in_parens = prefix[open + 1..].to_string() + &after_open;
        let (before_close, _) = in_parens
            .split_once(')')
            .unwrap_or((in_parens.as_str(), ""));
        let active_param = before_close.split(',').count().saturating_sub(1) as u32;
        Some((fn_name, active_param))
    }

    /// Get signature label and parameter labels for a function (from AST or stdlib).
    fn signature_for_function(
        source: &str,
        name: &str,
    ) -> Option<(String, Vec<String>, Option<String>)> {
        for (sym_name, detail) in Self::collect_symbols_from_source(source) {
            if sym_name == name && detail.starts_with("fn ") {
                let label = detail.clone();
                let params = detail
                    .strip_prefix("fn ")
                    .and_then(|s| s.strip_suffix(")"))
                    .map(|s| s.split('(').nth(1).unwrap_or(""))
                    .unwrap_or("")
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect();
                return Some((label, params, None));
            }
        }
        if Self::stdlib_doc(name).is_some() {
            return Some((format!("{}( … )", name), vec![], None));
        }
        None
    }
}

#[async_trait]
impl LanguageServer for Backend {
    async fn initialize(
        &self,
        _params: InitializeParams,
    ) -> tower_lsp::jsonrpc::Result<InitializeResult> {
        Ok(InitializeResult {
            capabilities: ServerCapabilities {
                text_document_sync: Some(TextDocumentSyncCapability::Options(
                    TextDocumentSyncOptions {
                        open_close: Some(true),
                        change: Some(TextDocumentSyncKind::FULL),
                        save: None,
                        ..Default::default()
                    },
                )),
                hover_provider: Some(HoverProviderCapability::Simple(true)),
                completion_provider: Some(CompletionOptions {
                    trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
                    ..Default::default()
                }),
                definition_provider: Some(OneOf::Left(true)),
                signature_help_provider: Some(SignatureHelpOptions {
                    trigger_characters: Some(vec!["(".to_string(), ",".to_string()]),
                    ..Default::default()
                }),
                ..Default::default()
            },
            server_info: Some(lsp_types::ServerInfo {
                name: "dal".to_string(),
                version: Some(env!("CARGO_PKG_VERSION").to_string()),
            }),
            ..Default::default()
        })
    }

    async fn initialized(&self, _params: InitializedParams) {
        self.client
            .log_message(
                lsp_types::MessageType::INFO,
                "DAL language server initialized",
            )
            .await;
    }

    async fn shutdown(&self) -> tower_lsp::jsonrpc::Result<()> {
        Ok(())
    }

    async fn did_open(&self, params: DidOpenTextDocumentParams) {
        let uri = params.text_document.uri;
        let version = Some(params.text_document.version);
        let text = params.text_document.text;
        {
            let mut docs = self.documents.lock().await;
            docs.insert(uri.clone(), DocumentState { text: text.clone() });
        }
        self.publish_diagnostics_for_uri(uri, version).await;
    }

    async fn did_change(&self, params: DidChangeTextDocumentParams) {
        let uri = params.text_document.uri;
        let version = Some(params.text_document.version);
        // We requested TextDocumentSyncKind::FULL, so content_changes should have one full-doc item.
        let text = params
            .content_changes
            .into_iter()
            .last()
            .map(|c| c.text)
            .unwrap_or_default();
        {
            let mut docs = self.documents.lock().await;
            docs.insert(uri.clone(), DocumentState { text: text.clone() });
        }
        self.publish_diagnostics_for_uri(uri, version).await;
    }

    async fn did_close(&self, params: DidCloseTextDocumentParams) {
        let uri = params.text_document.uri;
        {
            let mut docs = self.documents.lock().await;
            docs.remove(&uri);
        }
        self.client.publish_diagnostics(uri, vec![], None).await;
    }

    async fn hover(&self, params: HoverParams) -> tower_lsp::jsonrpc::Result<Option<Hover>> {
        let uri = params.text_document_position_params.text_document.uri;
        let pos = params.text_document_position_params.position;
        let source = {
            let docs = self.documents.lock().await;
            docs.get(&uri).map(|d| d.text.clone())
        };
        let source = match source {
            Some(s) => s,
            None => return Ok(None),
        };
        let word = match Self::word_at_position(&source, pos.line, pos.character) {
            Some(w) if !w.is_empty() => w,
            _ => return Ok(None),
        };
        let content = match self.hover_for_word(&source, &word) {
            Some(c) => c,
            None => return Ok(None),
        };
        Ok(Some(Hover {
            contents: HoverContents::Scalar(lsp_types::MarkedString::String(content)),
            range: None,
        }))
    }

    async fn completion(
        &self,
        params: CompletionParams,
    ) -> tower_lsp::jsonrpc::Result<Option<CompletionResponse>> {
        let uri = params.text_document_position.text_document.uri;
        let pos = params.text_document_position.position;
        let source = {
            let docs = self.documents.lock().await;
            docs.get(&uri).map(|d| d.text.clone())
        };
        let source = source.unwrap_or_default();
        let prefix = Self::word_at_position(&source, pos.line, pos.character).unwrap_or_default();

        let mut items = Vec::new();

        // Keywords (Phase 2: main set)
        let keywords = [
            "fn", "let", "return", "if", "else", "while", "for", "in", "loop", "break", "continue",
            "service", "struct", "import", "match", "case", "default", "try", "catch", "throw",
            "spawn", "agent", "true", "false", "impl", "pub", "self", "Ok", "Err", "Some", "None",
        ];
        for kw in keywords {
            if prefix.is_empty() || kw.starts_with(&prefix) {
                items.push(CompletionItem {
                    label: kw.to_string(),
                    kind: Some(lsp_types::CompletionItemKind::KEYWORD),
                    detail: Self::keyword_doc(kw).map(String::from),
                    ..Default::default()
                });
            }
        }

        // Stdlib modules
        let modules = [
            "chain", "ai", "assist", "log", "auth", "config", "db", "crypto", "oracle", "agent",
        ];
        for m in modules {
            if prefix.is_empty() || m.starts_with(&prefix) {
                items.push(CompletionItem {
                    label: m.to_string(),
                    kind: Some(lsp_types::CompletionItemKind::MODULE),
                    detail: Self::stdlib_doc(m).map(String::from),
                    ..Default::default()
                });
            }
        }

        // Symbols from AST
        for (name, detail) in Self::collect_symbols_from_source(&source) {
            if prefix.is_empty() || name.starts_with(&prefix) {
                items.push(CompletionItem {
                    label: name.clone(),
                    kind: Some(lsp_types::CompletionItemKind::FUNCTION),
                    detail: Some(detail),
                    ..Default::default()
                });
            }
        }

        Ok(Some(CompletionResponse::Array(items)))
    }

    async fn goto_definition(
        &self,
        params: GotoDefinitionParams,
    ) -> tower_lsp::jsonrpc::Result<Option<GotoDefinitionResponse>> {
        let uri = params.text_document_position_params.text_document.uri;
        let pos = params.text_document_position_params.position;
        let source = {
            let docs = self.documents.lock().await;
            docs.get(&uri).map(|d| d.text.clone())
        };
        let source = match source {
            Some(s) => s,
            None => return Ok(None),
        };
        let word = match Self::word_at_position(&source, pos.line, pos.character) {
            Some(w) if !w.is_empty() => w,
            _ => return Ok(None),
        };
        if Self::keyword_doc(&word).is_some() || Self::stdlib_doc(&word).is_some() {
            return Ok(None);
        }
        let range = match Self::find_definition_range(&source, &word) {
            Some(r) => r,
            None => return Ok(None),
        };
        Ok(Some(GotoDefinitionResponse::Scalar(Location {
            uri,
            range,
        })))
    }

    async fn signature_help(
        &self,
        params: SignatureHelpParams,
    ) -> tower_lsp::jsonrpc::Result<Option<SignatureHelp>> {
        let uri = params.text_document_position_params.text_document.uri;
        let pos = params.text_document_position_params.position;
        let source = {
            let docs = self.documents.lock().await;
            docs.get(&uri).map(|d| d.text.clone())
        };
        let source = source.unwrap_or_default();
        let (fn_name, active_param) =
            match Self::function_call_at_position(&source, pos.line, pos.character) {
                Some(x) => x,
                None => return Ok(None),
            };
        let (label, param_labels, doc) = match Self::signature_for_function(&source, &fn_name) {
            Some(x) => x,
            None => return Ok(None),
        };
        let params = if param_labels.is_empty() {
            None
        } else {
            Some(
                param_labels
                    .into_iter()
                    .map(|l| ParameterInformation {
                        label: lsp_types::ParameterLabel::Simple(l),
                        documentation: None,
                    })
                    .collect(),
            )
        };
        let sig = SignatureInformation {
            label,
            documentation: doc.map(lsp_types::Documentation::String),
            parameters: params,
            active_parameter: None,
        };
        Ok(Some(SignatureHelp {
            signatures: vec![sig],
            active_signature: Some(0),
            active_parameter: Some(active_param),
        }))
    }
}

/// Run the LSP server on stdio. Call from main with tokio::runtime block_on.
pub async fn run_lsp_server() {
    let stdin = tokio::io::stdin();
    let stdout = tokio::io::stdout();

    let (service, socket) = LspService::new(Backend::new);
    Server::new(stdin, stdout, socket).serve(service).await;
}

#[cfg(all(test, feature = "lsp"))]
mod tests {
    use super::*;
    use lsp_types::DiagnosticSeverity;

    #[test]
    fn test_diagnostics_from_source_valid_empty() {
        let source = "fn main() { 0 }";
        let diags = Backend::diagnostics_from_source(source);
        assert!(diags.is_empty(), "valid source should yield no diagnostics");
    }

    #[test]
    fn test_diagnostics_from_source_lexer_error() {
        let source = "let @ err";
        let diags = Backend::diagnostics_from_source(source);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].severity, Some(DiagnosticSeverity::ERROR));
        assert_eq!(diags[0].source.as_deref(), Some("dal"));
        assert!(
            diags[0].message.contains("Unexpected") || diags[0].message.contains("@"),
            "diagnostic message should describe the error: {}",
            diags[0].message
        );
    }

    #[test]
    fn test_diagnostics_from_source_parser_error() {
        let source = "fn foo( ";
        let diags = Backend::diagnostics_from_source(source);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].severity, Some(DiagnosticSeverity::ERROR));
        assert_eq!(diags[0].source.as_deref(), Some("dal"));
    }

    #[test]
    fn test_word_at_position_identifier() {
        let source = "let foo = 1";
        // Cursor on 'f' (char 4): span from last word before cursor gives " foo"
        let word = Backend::word_at_position(source, 0, 4);
        assert_eq!(word.as_deref(), Some(" foo"));
    }

    #[test]
    fn test_word_at_position_keyword() {
        let source = "let x = 0";
        let word = Backend::word_at_position(source, 0, 0);
        assert_eq!(word.as_deref(), Some("let"));
    }

    #[test]
    fn test_word_at_position_mid_identifier() {
        let source = "service FooBar {}";
        // Cursor on 'F' (char 8): span includes leading space, yielding " FooBar"
        let word = Backend::word_at_position(source, 0, 8);
        assert_eq!(word.as_deref(), Some(" FooBar"));
    }

    #[test]
    fn test_word_at_position_none_out_of_bounds() {
        let source = "let x";
        assert!(Backend::word_at_position(source, 1, 0).is_none());
        assert!(Backend::word_at_position(source, 0, 99).is_none());
    }

    #[test]
    fn test_word_at_position_none_whitespace() {
        let source = "let   x";
        // Cursor on first space (char 4): span is single space only (start=3, end=4)
        let word = Backend::word_at_position(source, 0, 4);
        assert_eq!(word.as_deref(), Some(" "));
    }

    #[test]
    fn test_keyword_doc_fn() {
        let doc = Backend::keyword_doc("fn");
        assert!(doc.is_some());
        let s = doc.unwrap();
        assert!(
            s.contains("Function"),
            "fn doc should describe function: {}",
            s
        );
    }

    #[test]
    fn test_keyword_doc_let() {
        let doc = Backend::keyword_doc("let");
        assert!(doc.is_some());
        assert!(
            doc.unwrap().to_lowercase().contains("variable")
                || doc.unwrap().to_lowercase().contains("bind")
        );
    }

    #[test]
    fn test_keyword_doc_unknown() {
        assert!(Backend::keyword_doc("notakeyword").is_none());
    }

    #[test]
    fn test_keyword_doc_service() {
        let doc = Backend::keyword_doc("service");
        assert!(doc.is_some());
        assert!(doc.unwrap().contains("service"));
    }

    #[test]
    fn test_stdlib_doc_chain() {
        let doc = Backend::stdlib_doc("chain");
        assert!(doc.is_some());
        assert!(doc.unwrap().to_lowercase().contains("blockchain"));
    }

    #[test]
    fn test_stdlib_doc_log() {
        let doc = Backend::stdlib_doc("log");
        assert!(doc.is_some());
        assert!(doc.unwrap().to_lowercase().contains("log"));
    }

    #[test]
    fn test_stdlib_doc_unknown() {
        assert!(Backend::stdlib_doc("unknown_module").is_none());
    }

    #[test]
    fn test_collect_symbols_from_source_service_and_fn() {
        let source = "service Foo { fn bar() { 0 } }";
        let symbols = Backend::collect_symbols_from_source(source);
        let names: Vec<&String> = symbols.iter().map(|(n, _)| n).collect();
        assert!(
            names.contains(&&"Foo".to_string()),
            "should contain service Foo: {:?}",
            names
        );
        assert!(
            names.contains(&&"bar".to_string()),
            "should contain method bar: {:?}",
            names
        );
    }

    #[test]
    fn test_collect_symbols_from_source_top_level_fn() {
        let source = "fn main() { 0 }";
        let symbols = Backend::collect_symbols_from_source(source);
        assert!(
            symbols.iter().any(|(n, _)| n == "main"),
            "should contain fn main: {:?}",
            symbols
        );
    }

    #[test]
    fn test_collect_symbols_from_source_invalid_returns_empty() {
        let source = "let @ err";
        let symbols = Backend::collect_symbols_from_source(source);
        assert!(symbols.is_empty());
    }

    #[test]
    fn test_find_definition_range_fn() {
        let source = "fn foo() { 0 }";
        let range = Backend::find_definition_range(source, "foo");
        assert!(range.is_some());
        let r = range.unwrap();
        assert_eq!(r.start.line, 0);
        assert!(r.start.character <= 4);
        assert!(r.end.character > r.start.character);
    }

    #[test]
    fn test_find_definition_range_service() {
        let source = "service Bar { fn run() { 0 } }";
        let range = Backend::find_definition_range(source, "Bar");
        assert!(range.is_some());
        let r = range.unwrap();
        assert_eq!(r.start.line, 0);
    }

    #[test]
    fn test_find_definition_range_none() {
        let source = "fn foo() { 0 }";
        assert!(Backend::find_definition_range(source, "nonexistent").is_none());
    }

    #[test]
    fn test_function_call_at_position() {
        let source = "fn id(x: i64) { x }\nid(1)";
        // Cursor inside "id(1)" on line 1; implementation extracts name from before '(' (current behavior)
        let (fn_name, active_param) = Backend::function_call_at_position(source, 1, 5).unwrap();
        assert_eq!(fn_name, "d"); // rposition gives last alpha before '(', so "d" from "id"
        assert_eq!(active_param, 0);
    }

    #[test]
    fn test_function_call_at_position_second_param() {
        let source = "foo(a, b)";
        let (fn_name, active_param) = Backend::function_call_at_position(source, 0, 7).unwrap();
        assert_eq!(fn_name, "o"); // current implementation: last alpha before '(' in "foo" yields "o"
        assert_eq!(active_param, 1);
    }

    #[test]
    fn test_function_call_at_position_none() {
        let source = "no call here";
        assert!(Backend::function_call_at_position(source, 0, 0).is_none());
    }

    #[test]
    fn test_signature_for_function_from_ast() {
        let source = "fn add(a: i64, b: i64) { a + b }";
        let out = Backend::signature_for_function(source, "add");
        assert!(out.is_some());
        let (label, params, _doc) = out.unwrap();
        assert!(label.contains("add"));
        assert_eq!(params.len(), 2);
        assert!(params.iter().any(|p| p.contains("a")));
        assert!(params.iter().any(|p| p.contains("b")));
    }

    #[test]
    fn test_signature_for_function_stdlib() {
        let source = "";
        let out = Backend::signature_for_function(source, "chain");
        assert!(out.is_some());
        let (label, params, _) = out.unwrap();
        assert!(label.contains("chain"));
        assert!(params.is_empty());
    }

    #[test]
    fn test_signature_for_function_none() {
        let source = "fn foo() { 0 }";
        assert!(Backend::signature_for_function(source, "bar").is_none());
    }

    #[test]
    fn test_to_lsp_position_one_based() {
        let p = Backend::to_lsp_position(1, 1);
        assert_eq!(p.line, 0);
        assert_eq!(p.character, 0);
    }

    #[test]
    fn test_keyword_doc_exact_fn_string() {
        let doc = Backend::keyword_doc("fn").unwrap();
        assert!(doc.contains("Function declaration"));
        assert!(doc.contains("fn name(params)"));
    }

    #[test]
    fn test_diagnostics_lexer_error_has_range() {
        let source = "let @ x";
        let diags = Backend::diagnostics_from_source(source);
        assert_eq!(diags.len(), 1);
        assert!(diags[0].range.start.line <= diags[0].range.end.line);
        assert!(diags[0].range.start.character <= diags[0].range.end.character);
    }
}