seq-lsp 5.6.3

Language Server Protocol implementation for Seq
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
//! Binary entry point for `seq-lsp`. Implements the `LanguageServer` trait
//! over a per-URI document cache and dispatches to the sibling `completion`,
//! `diagnostics`, and `includes` modules.

use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
use std::sync::RwLock;

use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
use tracing::{info, warn};

mod completion;
mod diagnostics;
mod includes;

use diagnostics::QuotationInfo;
use includes::{IncludeResolution, LocalWord};

/// State for a single document
struct DocumentState {
    /// Document content
    content: String,
    /// File path (for resolving relative includes)
    file_path: Option<PathBuf>,
    /// Words and union types from includes (cached)
    includes: IncludeResolution,
    /// Words defined in this document
    local_words: Vec<LocalWord>,
    /// Quotation info for hover support
    quotations: Vec<QuotationInfo>,
}

struct SeqLanguageServer {
    client: Client,
    /// Document state cache
    documents: RwLock<HashMap<String, DocumentState>>,
}

impl SeqLanguageServer {
    fn new(client: Client) -> Self {
        Self {
            client,
            documents: RwLock::new(HashMap::new()),
        }
    }

    /// Run `f` against the cached `DocumentState` for `uri`. Returns `None`
    /// if the document is unknown or the read lock cannot be acquired.
    fn with_document<T>(&self, uri: &str, f: impl FnOnce(&DocumentState) -> T) -> Option<T> {
        match self.documents.read() {
            Ok(docs) => docs.get(uri).map(f),
            Err(e) => {
                warn!("documents lock poisoned: {}", e);
                None
            }
        }
    }

    /// Update document state, resolve includes, and return diagnostics
    fn update_document(
        &self,
        uri: &str,
        content: String,
        file_path: Option<PathBuf>,
    ) -> Vec<tower_lsp::lsp_types::Diagnostic> {
        // Parse document to extract includes and local words
        let (include_stmts, local_words) = includes::parse_document(&content);

        info!(
            "Parsed document: {} includes, {} local words, file_path={:?}",
            include_stmts.len(),
            local_words.len(),
            file_path
        );

        // Resolve includes to get words and union types from included files
        // Uses embedded stdlib for std: includes, filesystem for relative includes
        let resolved = includes::resolve_includes(&include_stmts, file_path.as_deref());

        info!(
            "Document has {} local words, {} included words, {} included unions from {} includes",
            local_words.len(),
            resolved.words.len(),
            resolved.union_names.len(),
            include_stmts.len()
        );

        // Check document for diagnostics and collect quotation info
        let (diagnostics, quotations) =
            diagnostics::check_document_with_quotations(&content, &resolved, file_path.as_deref());

        info!("Found {} quotations with type info", quotations.len());

        let state = DocumentState {
            content,
            file_path,
            includes: resolved,
            local_words,
            quotations,
        };

        if let Ok(mut docs) = self.documents.write() {
            docs.insert(uri.to_string(), state);
        }

        diagnostics
    }
}

#[tower_lsp::async_trait]
impl LanguageServer for SeqLanguageServer {
    async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
        info!("Seq LSP server initializing");

        // Check if inlay hints are enabled via initialization options
        let inlay_hints_enabled = params
            .initialization_options
            .as_ref()
            .and_then(|opts| opts.get("inlay_hints"))
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        let inlay_hint_provider = if inlay_hints_enabled {
            info!("Inlay hints enabled");
            Some(OneOf::Right(InlayHintServerCapabilities::Options(
                InlayHintOptions {
                    resolve_provider: Some(false),
                    work_done_progress_options: Default::default(),
                },
            )))
        } else {
            None
        };

        Ok(InitializeResult {
            capabilities: ServerCapabilities {
                // Declare UTF-8 position encoding since we use byte offsets
                position_encoding: Some(PositionEncodingKind::UTF8),
                text_document_sync: Some(TextDocumentSyncCapability::Kind(
                    TextDocumentSyncKind::FULL,
                )),
                completion_provider: Some(CompletionOptions {
                    trigger_characters: Some(vec![
                        " ".to_string(),
                        "\n".to_string(),
                        ":".to_string(),
                        ".".to_string(),
                    ]),
                    ..Default::default()
                }),
                hover_provider: Some(HoverProviderCapability::Simple(true)),
                definition_provider: Some(OneOf::Left(true)),
                document_symbol_provider: Some(OneOf::Left(true)),
                code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
                inlay_hint_provider,
                ..Default::default()
            },
            server_info: Some(ServerInfo {
                name: "seq-lsp".to_string(),
                version: Some(env!("CARGO_PKG_VERSION").to_string()),
            }),
        })
    }

    async fn initialized(&self, _: InitializedParams) {
        info!("Seq LSP server initialized");
        self.client
            .log_message(MessageType::INFO, "Seq LSP server ready")
            .await;
    }

    async fn shutdown(&self) -> Result<()> {
        info!("Seq LSP server shutting down");
        Ok(())
    }

    async fn did_open(&self, params: DidOpenTextDocumentParams) {
        let uri = params.text_document.uri;
        let text = params.text_document.text;

        info!("Document opened: {}", uri);

        let file_path = includes::uri_to_path(uri.as_str());
        info!("File path: {:?}", file_path);

        // Update document state (parses includes) and get diagnostics
        let diagnostics = self.update_document(uri.as_str(), text, file_path);

        self.client
            .publish_diagnostics(uri, diagnostics, None)
            .await;
    }

    async fn did_change(&self, params: DidChangeTextDocumentParams) {
        let uri = params.text_document.uri;

        // With FULL sync, we get the entire document
        if let Some(change) = params.content_changes.into_iter().next() {
            let text = change.text;

            info!("Document changed: {}", uri);

            // Get existing file path from cache
            let file_path = self
                .with_document(uri.as_str(), |s| s.file_path.clone())
                .flatten();

            // Update document state (re-parses includes) and get diagnostics
            let diagnostics = self.update_document(uri.as_str(), text, file_path);

            self.client
                .publish_diagnostics(uri, diagnostics, None)
                .await;
        }
    }

    async fn did_close(&self, params: DidCloseTextDocumentParams) {
        let uri = params.text_document.uri;
        info!("Document closed: {}", uri);

        if let Ok(mut docs) = self.documents.write() {
            docs.remove(uri.as_str());
        }

        // Clear diagnostics when document is closed
        self.client.publish_diagnostics(uri, vec![], None).await;
    }

    async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
        let uri = params.text_document_position_params.text_document.uri;
        let position = params.text_document_position_params.position;

        let Some((word, local_words, included_words, quotations)) =
            self.with_document(uri.as_str(), |state| {
                (
                    get_word_at_position(&state.content, position),
                    state.local_words.clone(),
                    state.includes.words.clone(),
                    state.quotations.clone(),
                )
            })
        else {
            return Ok(None);
        };

        // Check if cursor is inside a quotation
        let line = position.line as usize;
        let col = position.character as usize;
        for q in &quotations {
            if q.span.contains(line, col) {
                let type_str = format_quotation_type(&q.inferred_type);
                return Ok(Some(Hover {
                    contents: HoverContents::Markup(MarkupContent {
                        kind: MarkupKind::Markdown,
                        value: format!("```seq\n{}\n```\n\n*Quotation*", type_str),
                    }),
                    range: Some(Range {
                        start: Position {
                            line: q.span.start_line as u32,
                            character: q.span.start_column as u32,
                        },
                        end: Position {
                            line: q.span.end_line as u32,
                            character: q.span.end_column as u32,
                        },
                    }),
                }));
            }
        }

        let Some(word) = word else {
            return Ok(None);
        };

        // Look up the word in local words, included words, or builtins
        if let Some(hover) = lookup_word_hover(&word, &local_words, &included_words) {
            return Ok(Some(hover));
        }

        Ok(None)
    }

    async fn goto_definition(
        &self,
        params: GotoDefinitionParams,
    ) -> Result<Option<GotoDefinitionResponse>> {
        let uri = params.text_document_position_params.text_document.uri;
        let position = params.text_document_position_params.position;

        let Some((word, local_words, included_words)) = self.with_document(uri.as_str(), |state| {
            (
                get_word_at_position(&state.content, position),
                state.local_words.clone(),
                state.includes.words.clone(),
            )
        }) else {
            return Ok(None);
        };

        let Some(word) = word else {
            return Ok(None);
        };

        // Check local words first - jump to definition in current file
        for local in &local_words {
            if local.name == word {
                let location = Location {
                    uri: uri.clone(),
                    range: make_definition_range(local.start_line, &local.name),
                };
                return Ok(Some(GotoDefinitionResponse::Scalar(location)));
            }
        }

        // Check included words - jump to definition in included file
        for included in &included_words {
            if included.name == word
                && let Some(ref file_path) = included.file_path
                && file_path.exists()
                && let Ok(file_uri) = Url::from_file_path(file_path)
            {
                let location = Location {
                    uri: file_uri,
                    range: make_definition_range(included.start_line, &included.name),
                };
                return Ok(Some(GotoDefinitionResponse::Scalar(location)));
            }
        }

        // Builtins don't have a definition location
        Ok(None)
    }

    async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> {
        let uri = params.text_document.uri;
        let range = params.range;

        let Some((content, file_path)) = self.with_document(uri.as_str(), |state| {
            (state.content.clone(), state.file_path.clone())
        }) else {
            return Ok(None);
        };

        let actions = diagnostics::get_code_actions(&content, range, &uri, file_path.as_deref());

        if actions.is_empty() {
            Ok(None)
        } else {
            Ok(Some(
                actions
                    .into_iter()
                    .map(CodeActionOrCommand::CodeAction)
                    .collect(),
            ))
        }
    }

    async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> {
        let uri = params.text_document_position.text_document.uri;
        let position = params.text_document_position.position;

        let (line_prefix, included_words, local_words) = self
            .with_document(uri.as_str(), |state| {
                let prefix = state
                    .content
                    .lines()
                    .nth(position.line as usize)
                    .map(|line| {
                        let end = (position.character as usize).min(line.len());
                        line[..end].to_string()
                    });
                (
                    prefix,
                    state.includes.words.clone(),
                    state.local_words.clone(),
                )
            })
            .unwrap_or_default();

        let context = line_prefix
            .as_ref()
            .map(|prefix| completion::CompletionContext {
                line_prefix: prefix,
                included_words: &included_words,
                local_words: &local_words,
            });

        let items = completion::get_completions(context);
        Ok(Some(CompletionResponse::Array(items)))
    }

    async fn document_symbol(
        &self,
        params: DocumentSymbolParams,
    ) -> Result<Option<DocumentSymbolResponse>> {
        let uri = params.text_document.uri;

        let Some((local_words, content)) = self.with_document(uri.as_str(), |state| {
            (state.local_words.clone(), state.content.clone())
        }) else {
            return Ok(None);
        };

        // Pre-compute line lengths for accurate end positions
        let line_lengths: Vec<u32> = content.lines().map(|l| l.len() as u32).collect();

        // Convert local words to document symbols for breadcrumbs
        // The range spans the entire word definition so editors show the symbol
        // when cursor is anywhere inside the definition
        // Note: We don't add child symbols for word calls because most breadcrumb
        // implementations don't do column-level matching within a line
        let symbols: Vec<DocumentSymbol> = local_words
            .iter()
            .map(|word| {
                let end_char = line_lengths.get(word.end_line).copied().unwrap_or(0);

                #[allow(deprecated)]
                DocumentSymbol {
                    name: word.name.clone(),
                    detail: None,
                    kind: SymbolKind::FUNCTION,
                    tags: None,
                    deprecated: None,
                    range: Range {
                        start: Position {
                            line: word.start_line as u32,
                            character: 0,
                        },
                        end: Position {
                            line: word.end_line as u32,
                            character: end_char,
                        },
                    },
                    selection_range: Range {
                        start: Position {
                            line: word.start_line as u32,
                            character: 0,
                        },
                        end: Position {
                            line: word.start_line as u32,
                            character: word.name.len() as u32 + 2, // `: name`
                        },
                    },
                    children: None,
                }
            })
            .collect();

        Ok(Some(DocumentSymbolResponse::Nested(symbols)))
    }

    async fn inlay_hint(&self, params: InlayHintParams) -> Result<Option<Vec<InlayHint>>> {
        let uri = params.text_document.uri;
        let range = params.range;

        let Some((content, local_words, included_words)) =
            self.with_document(uri.as_str(), |state| {
                (
                    state.content.clone(),
                    state.local_words.clone(),
                    state.includes.words.clone(),
                )
            })
        else {
            return Ok(None);
        };

        let mut hints = Vec::new();

        // Process lines in the visible range
        for (line_num, line) in content.lines().enumerate() {
            let line_num = line_num as u32;
            if line_num < range.start.line || line_num > range.end.line {
                continue;
            }

            // Skip comments and include lines
            let trimmed = line.trim();
            if trimmed.starts_with('\\') || trimmed.starts_with("include ") {
                continue;
            }

            for hint in find_word_calls_in_line(line, line_num, &local_words, &included_words) {
                hints.push(hint);
            }
        }

        Ok(Some(hints))
    }
}

/// Find word calls in a line and return inlay hints for their signatures
fn find_word_calls_in_line(
    line: &str,
    line_num: u32,
    local_words: &[includes::LocalWord],
    included_words: &[includes::IncludedWord],
) -> Vec<InlayHint> {
    let mut hints = Vec::new();

    // Skip if this is a word definition line (starts with ":")
    if line.trim().starts_with(':') {
        return hints;
    }

    // Skip strings when looking for words
    let is_word_char = |c: char| c.is_alphanumeric() || "-_>?!<+=*/:".contains(c);

    let mut in_string = false;
    let mut word_start: Option<usize> = None;

    for (i, c) in line.char_indices() {
        if c == '"' {
            in_string = !in_string;
            continue;
        }

        if in_string {
            continue;
        }

        if is_word_char(c) {
            if word_start.is_none() {
                word_start = Some(i);
            }
        } else if let Some(start) = word_start {
            let word = &line[start..i];
            if let Some(hint) =
                make_inlay_hint_for_word(word, line_num, i as u32, local_words, included_words)
            {
                hints.push(hint);
            }
            word_start = None;
        }
    }

    // Handle word at end of line
    if let Some(start) = word_start {
        let word = &line[start..];
        if let Some(hint) = make_inlay_hint_for_word(
            word,
            line_num,
            line.len() as u32,
            local_words,
            included_words,
        ) {
            hints.push(hint);
        }
    }

    hints
}

/// Build a TYPE-kind inlay hint that shows an effect label after a word.
fn make_type_inlay_hint(line: u32, end_col: u32, effect: &seqc::Effect) -> InlayHint {
    let effect_str = completion::format_effect(effect);
    InlayHint {
        position: Position {
            line,
            character: end_col,
        },
        label: InlayHintLabel::String(format!(": {}", effect_str)),
        kind: Some(InlayHintKind::TYPE),
        text_edits: None,
        tooltip: None,
        padding_left: Some(false),
        padding_right: Some(true),
        data: None,
    }
}

/// Create an inlay hint for a word if it has a known signature
fn make_inlay_hint_for_word(
    word: &str,
    line: u32,
    end_col: u32,
    local_words: &[includes::LocalWord],
    included_words: &[includes::IncludedWord],
) -> Option<InlayHint> {
    // Skip common control flow and simple words that would be noisy
    let skip_words = [
        "if", "else", "then", "do", "loop", "begin", "dup", "drop", "swap", "over", "rot", "nip",
        "tuck", "pick", "+", "-", "*", "/", "=", "<", ">", "<=", ">=", "<>", "and", "or", "not",
        "true", "false",
    ];
    if skip_words.contains(&word) {
        return None;
    }

    for local in local_words {
        if local.name == word
            && let Some(ref effect) = local.effect
        {
            return Some(make_type_inlay_hint(line, end_col, effect));
        }
    }

    for included in included_words {
        if included.name == word
            && let Some(ref effect) = included.effect
        {
            return Some(make_type_inlay_hint(line, end_col, effect));
        }
    }

    for (name, effect) in seqc::builtins::builtin_signatures() {
        if name == word {
            return Some(make_type_inlay_hint(line, end_col, &effect));
        }
    }

    None
}

/// Get the word at a given position in the document
fn get_word_at_position(content: &str, position: Position) -> Option<String> {
    let line = content.lines().nth(position.line as usize)?;
    let col = position.character as usize;

    // Find word boundaries - Seq words can contain special chars like -, >, ?, !
    let is_word_char = |c: char| c.is_alphanumeric() || "-_>?!<+=*/:".contains(c);

    let start = line[..col.min(line.len())]
        .char_indices()
        .rev()
        .find(|(_, c)| !is_word_char(*c))
        .map(|(i, _)| i + 1)
        .unwrap_or(0);

    let end = line[col.min(line.len())..]
        .char_indices()
        .find(|(_, c)| !is_word_char(*c))
        .map(|(i, _)| col + i)
        .unwrap_or(line.len());

    if start >= end {
        return None;
    }

    Some(line[start..end].to_string())
}

/// Create a Range for a word definition (`: word-name`)
/// Uses character count (not byte length) for proper UTF-8 support
fn make_definition_range(start_line: usize, name: &str) -> Range {
    Range {
        start: Position {
            line: start_line as u32,
            character: 0,
        },
        end: Position {
            line: start_line as u32,
            // +2 for `: ` prefix, use chars().count() for UTF-8 correctness
            character: (name.chars().count() + 2) as u32,
        },
    }
}

/// Format a quotation type for display
fn format_quotation_type(typ: &seqc::types::Type) -> String {
    completion::format_type(typ)
}

/// Look up a word and return hover information
fn lookup_word_hover(
    word: &str,
    local_words: &[includes::LocalWord],
    included_words: &[includes::IncludedWord],
) -> Option<Hover> {
    use tower_lsp::lsp_types::{HoverContents, MarkupContent, MarkupKind};

    // Check local words first
    for local in local_words {
        if local.name == word {
            let effect = local
                .effect
                .as_ref()
                .map(completion::format_effect)
                .unwrap_or_else(|| "( ? )".to_string());

            return Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: MarkupKind::Markdown,
                    value: format!(
                        "```seq\n: {} {}\n```\n\n*Defined in this file*",
                        word, effect
                    ),
                }),
                range: None,
            });
        }
    }

    // Check included words
    for included in included_words {
        if included.name == word {
            let effect = included
                .effect
                .as_ref()
                .map(completion::format_effect)
                .unwrap_or_else(|| "( ? )".to_string());

            return Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: MarkupKind::Markdown,
                    value: format!(
                        "```seq\n: {} {}\n```\n\n*From {}*",
                        word, effect, included.source
                    ),
                }),
                range: None,
            });
        }
    }

    // Check builtins
    for (name, effect) in seqc::builtins::builtin_signatures() {
        if name == word {
            let signature = completion::format_effect(&effect);
            let doc = seqc::builtins::builtin_doc(word).unwrap_or("");
            let doc_section = if doc.is_empty() {
                String::new()
            } else {
                format!("\n\n{}", doc)
            };
            return Some(Hover {
                contents: HoverContents::Markup(MarkupContent {
                    kind: MarkupKind::Markdown,
                    value: format!(
                        "```seq\n{} {}\n```{}\n\n*Built-in*",
                        word, signature, doc_section
                    ),
                }),
                range: None,
            });
        }
    }

    None
}

#[tokio::main]
async fn main() {
    // Handle --version flag
    let args: Vec<String> = env::args().collect();
    if args.iter().any(|a| a == "--version" || a == "-V") {
        println!("seq-lsp {}", env!("CARGO_PKG_VERSION"));
        return;
    }

    // Set up logging
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive("seq_lsp=info".parse().unwrap()),
        )
        .with_writer(std::io::stderr)
        .init();

    info!("Starting Seq LSP server");

    let stdin = tokio::io::stdin();
    let stdout = tokio::io::stdout();

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