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
use std::collections::HashMap;
use harn_lexer::{Lexer, TokenKind};
use harn_parser::format_type;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use crate::constants::{
builtin_doc, keyword_doc, BUILTINS, DICT_METHODS, KEYWORDS, LIST_METHODS, STRING_METHODS,
};
use crate::document::DocumentState;
use crate::helpers::{
char_before_position, extract_backtick_name, find_word_in_region, infer_dot_receiver_type,
lsp_position_to_offset, offset_to_position, position_in_span, simplify_bool_comparison,
span_to_full_range, word_at_position,
};
use crate::references::find_references;
use crate::semantic_tokens::{build_semantic_tokens, semantic_token_legend};
use crate::symbols::{format_shape_expanded, HarnSymbolKind, SymbolInfo};
use crate::HarnLsp;
// ---------------------------------------------------------------------------
// LanguageServer trait implementation
// ---------------------------------------------------------------------------
#[tower_lsp::async_trait]
impl tower_lsp::LanguageServer for HarnLsp {
async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult {
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::FULL,
)),
completion_provider: Some(CompletionOptions {
trigger_characters: Some(vec![".".to_string()]),
..Default::default()
}),
definition_provider: Some(OneOf::Left(true)),
references_provider: Some(OneOf::Left(true)),
document_symbol_provider: Some(OneOf::Left(true)),
hover_provider: Some(HoverProviderCapability::Simple(true)),
semantic_tokens_provider: Some(
SemanticTokensServerCapabilities::SemanticTokensOptions(
SemanticTokensOptions {
legend: semantic_token_legend(),
full: Some(SemanticTokensFullOptions::Bool(true)),
range: None,
work_done_progress_options: Default::default(),
},
),
),
signature_help_provider: Some(SignatureHelpOptions {
trigger_characters: Some(vec!["(".to_string(), ",".to_string()]),
retrigger_characters: Some(vec![")".to_string()]),
work_done_progress_options: Default::default(),
}),
workspace_symbol_provider: Some(OneOf::Left(true)),
code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
rename_provider: Some(OneOf::Left(true)),
..Default::default()
},
..Default::default()
})
}
async fn initialized(&self, _: InitializedParams) {
self.client
.log_message(MessageType::INFO, "Harn LSP initialized")
.await;
}
async fn shutdown(&self) -> Result<()> {
Ok(())
}
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let uri = params.text_document.uri.clone();
let source = params.text_document.text.clone();
let state = DocumentState::new(source);
let diagnostics = state.diagnostics.clone();
self.documents.lock().unwrap().insert(uri.clone(), state);
self.client
.publish_diagnostics(uri, diagnostics, None)
.await;
}
async fn did_change(&self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri.clone();
if let Some(change) = params.content_changes.into_iter().last() {
let source = change.text;
let diagnostics;
{
let mut docs = self.documents.lock().unwrap();
let entry = docs
.entry(uri.clone())
.or_insert_with(|| DocumentState::new(String::new()));
entry.update(source);
diagnostics = entry.diagnostics.clone();
}
self.client
.publish_diagnostics(uri, diagnostics, None)
.await;
}
}
async fn did_close(&self, params: DidCloseTextDocumentParams) {
self.documents
.lock()
.unwrap()
.remove(¶ms.text_document.uri);
}
// -----------------------------------------------------------------------
// Completion (scope-aware + method completion)
// -----------------------------------------------------------------------
async fn completion(&self, params: CompletionParams) -> Result<Option<CompletionResponse>> {
let uri = ¶ms.text_document_position.text_document.uri;
let position = params.text_document_position.position;
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(None),
};
let source = state.source.clone();
let symbols = state.symbols.clone();
drop(docs);
let mut items = Vec::new();
// Check if this is a dot-completion
if char_before_position(&source, position) == Some('.') {
let type_name = infer_dot_receiver_type(&source, position, &symbols);
let methods = match type_name.as_deref() {
Some("string") => STRING_METHODS,
Some("list") => LIST_METHODS,
Some("dict") => DICT_METHODS,
_ => {
// Unknown type — offer all methods
for m in STRING_METHODS
.iter()
.chain(LIST_METHODS.iter())
.chain(DICT_METHODS.iter())
{
items.push(CompletionItem {
label: m.to_string(),
kind: Some(CompletionItemKind::METHOD),
..Default::default()
});
}
// Deduplicate by label
items.sort_by(|a, b| a.label.cmp(&b.label));
items.dedup_by(|a, b| a.label == b.label);
return Ok(Some(CompletionResponse::Array(items)));
}
};
for m in methods {
items.push(CompletionItem {
label: m.to_string(),
kind: Some(CompletionItemKind::METHOD),
..Default::default()
});
}
return Ok(Some(CompletionResponse::Array(items)));
}
// Scope-aware: find symbols visible at cursor position
for sym in &symbols {
// A symbol is visible if:
// 1. It has no scope_span (top-level), or
// 2. The cursor is inside its scope_span
let visible = match sym.scope_span {
None => true,
Some(ref scope) => position_in_span(&position, scope, &source),
};
if !visible {
continue;
}
let (kind, detail) = match sym.kind {
HarnSymbolKind::Pipeline => (CompletionItemKind::FUNCTION, "pipeline"),
HarnSymbolKind::Function => (CompletionItemKind::FUNCTION, "function"),
HarnSymbolKind::Variable => (CompletionItemKind::VARIABLE, "variable"),
HarnSymbolKind::Parameter => (CompletionItemKind::VARIABLE, "parameter"),
HarnSymbolKind::Enum => (CompletionItemKind::ENUM, "enum"),
HarnSymbolKind::Struct => (CompletionItemKind::STRUCT, "struct"),
HarnSymbolKind::Interface => (CompletionItemKind::INTERFACE, "interface"),
};
items.push(CompletionItem {
label: sym.name.clone(),
kind: Some(kind),
detail: Some(sym.signature.as_deref().unwrap_or(detail).to_string()),
..Default::default()
});
}
// Add builtins
for &(name, detail) in BUILTINS {
items.push(CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::FUNCTION),
detail: Some(detail.to_string()),
..Default::default()
});
}
// Add keywords
for kw in KEYWORDS {
items.push(CompletionItem {
label: kw.to_string(),
kind: Some(CompletionItemKind::KEYWORD),
..Default::default()
});
}
Ok(Some(CompletionResponse::Array(items)))
}
// -----------------------------------------------------------------------
// Go-to-definition (AST-based symbol table)
// -----------------------------------------------------------------------
async fn goto_definition(
&self,
params: GotoDefinitionParams,
) -> Result<Option<GotoDefinitionResponse>> {
let uri = ¶ms.text_document_position_params.text_document.uri;
let position = params.text_document_position_params.position;
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(None),
};
let source = state.source.clone();
let symbols = state.symbols.clone();
drop(docs);
let word = match word_at_position(&source, position) {
Some(w) => w,
None => return Ok(None),
};
// Look up the name in the symbol table — find the first definition-like symbol
for sym in &symbols {
if sym.name == word
&& matches!(
sym.kind,
HarnSymbolKind::Pipeline
| HarnSymbolKind::Function
| HarnSymbolKind::Variable
| HarnSymbolKind::Parameter
| HarnSymbolKind::Enum
| HarnSymbolKind::Struct
| HarnSymbolKind::Interface
)
{
let range = span_to_full_range(&sym.def_span, &source);
return Ok(Some(GotoDefinitionResponse::Scalar(Location {
uri: uri.clone(),
range,
})));
}
}
Ok(None)
}
// -----------------------------------------------------------------------
// Find references (AST-based)
// -----------------------------------------------------------------------
async fn references(&self, params: ReferenceParams) -> Result<Option<Vec<Location>>> {
let uri = ¶ms.text_document_position.text_document.uri;
let position = params.text_document_position.position;
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(None),
};
let source = state.source.clone();
let ast = state.ast.clone();
drop(docs);
let word = match word_at_position(&source, position) {
Some(w) => w,
None => return Ok(None),
};
let program = match ast {
Some(p) => p,
None => return Ok(None),
};
let ref_spans = find_references(&program, &word);
if ref_spans.is_empty() {
return Ok(None);
}
let locations: Vec<Location> = ref_spans
.iter()
.map(|span| Location {
uri: uri.clone(),
range: span_to_full_range(span, &source),
})
.collect();
Ok(Some(locations))
}
// -----------------------------------------------------------------------
// Document symbols (AST-based with proper spans)
// -----------------------------------------------------------------------
#[allow(deprecated)]
async fn document_symbol(
&self,
params: DocumentSymbolParams,
) -> Result<Option<DocumentSymbolResponse>> {
let uri = ¶ms.text_document.uri;
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(None),
};
let source = state.source.clone();
let symbols = state.symbols.clone();
drop(docs);
let mut doc_symbols = Vec::new();
for sym in &symbols {
// Only include top-level definitions for document symbols
let kind = match sym.kind {
HarnSymbolKind::Pipeline => SymbolKind::FUNCTION,
HarnSymbolKind::Function => SymbolKind::FUNCTION,
HarnSymbolKind::Variable => SymbolKind::VARIABLE,
HarnSymbolKind::Enum => SymbolKind::ENUM,
HarnSymbolKind::Struct => SymbolKind::STRUCT,
HarnSymbolKind::Interface => SymbolKind::INTERFACE,
HarnSymbolKind::Parameter => continue, // skip params from outline
};
// Only show top-level and direct-child symbols
if sym.scope_span.is_some()
&& !matches!(
sym.kind,
HarnSymbolKind::Function | HarnSymbolKind::Variable
)
{
continue;
}
let range = span_to_full_range(&sym.def_span, &source);
let detail = match sym.kind {
HarnSymbolKind::Pipeline => "pipeline",
HarnSymbolKind::Function => "function",
HarnSymbolKind::Variable => "variable",
HarnSymbolKind::Enum => "enum",
HarnSymbolKind::Struct => "struct",
HarnSymbolKind::Interface => "interface",
HarnSymbolKind::Parameter => "parameter",
};
doc_symbols.push(DocumentSymbol {
name: sym.name.clone(),
detail: Some(detail.to_string()),
kind,
range,
selection_range: range,
tags: None,
deprecated: None,
children: None,
});
}
Ok(Some(DocumentSymbolResponse::Nested(doc_symbols)))
}
// -----------------------------------------------------------------------
// Hover (with type information)
// -----------------------------------------------------------------------
async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
let uri = ¶ms.text_document_position_params.text_document.uri;
let position = params.text_document_position_params.position;
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(None),
};
let source = state.source.clone();
let symbols = state.symbols.clone();
drop(docs);
let word = match word_at_position(&source, position) {
Some(w) => w,
None => return Ok(None),
};
// Check builtins first
if let Some(doc) = builtin_doc(&word) {
return Ok(Some(Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value: doc,
}),
range: None,
}));
}
// Check keywords
if let Some(doc) = keyword_doc(&word) {
return Ok(Some(Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value: doc,
}),
range: None,
}));
}
// Check user-defined symbols — prefer the innermost scope that
// contains the cursor position so that shadowed bindings resolve
// to the closest definition.
let cursor_offset = lsp_position_to_offset(&source, position);
let mut best: Option<&SymbolInfo> = None;
for sym in &symbols {
if sym.name != word {
continue;
}
// Methods inside impl blocks are globally visible (called via
// dot syntax), so skip the scope check for them.
let in_scope = if sym.impl_type.is_some() {
true
} else {
match sym.scope_span {
Some(sp) => cursor_offset >= sp.start && cursor_offset <= sp.end,
None => true, // top-level symbol is always visible
}
};
if !in_scope {
continue;
}
// Prefer the symbol with the narrowest (innermost) scope.
match best {
None => best = Some(sym),
Some(prev) => {
let prev_scope_size = match prev.scope_span {
Some(sp) => sp.end.saturating_sub(sp.start),
None => usize::MAX,
};
let this_scope_size = match sym.scope_span {
Some(sp) => sp.end.saturating_sub(sp.start),
None => usize::MAX,
};
if this_scope_size < prev_scope_size {
best = Some(sym);
}
}
}
}
if let Some(sym) = best {
let mut hover_text = String::new();
// Show signature if available (functions, pipelines, structs, enums)
if let Some(ref sig) = sym.signature {
// For methods, prefix with the impl type name
let display_sig = if let Some(ref impl_ty) = sym.impl_type {
format!("impl {impl_ty}\n{sig}")
} else {
sig.clone()
};
hover_text.push_str(&format!("```harn\n{display_sig}\n```\n"));
} else {
// For variables/parameters, build a code-block declaration
// with the type annotation when known.
let keyword = match sym.kind {
HarnSymbolKind::Variable => "let",
HarnSymbolKind::Parameter => "param",
_ => "",
};
if let Some(ref ty) = sym.type_info {
hover_text.push_str(&format!(
"```harn\n{keyword} {}: {}\n```\n",
sym.name,
format_type(ty)
));
} else {
let kind_str = match sym.kind {
HarnSymbolKind::Pipeline => "pipeline",
HarnSymbolKind::Function => "function",
HarnSymbolKind::Variable => "variable",
HarnSymbolKind::Parameter => "parameter",
HarnSymbolKind::Enum => "enum",
HarnSymbolKind::Struct => "struct",
HarnSymbolKind::Interface => "interface",
};
hover_text.push_str(&format!("**{kind_str}** `{}`", sym.name));
}
}
// For functions with a return type, show it below the signature
// (signatures already include "-> type", so only add for
// variables/params where the type is a shape and worth expanding).
if sym.signature.is_none() {
if let Some(ref ty) = sym.type_info {
if matches!(ty, harn_parser::TypeExpr::Shape(_)) {
// Already shown in the code block above; add a
// human-readable breakdown for complex shapes.
let expanded = format_shape_expanded(ty, 0);
if !expanded.is_empty() {
hover_text.push_str(&format!("\n{expanded}"));
}
}
}
}
// Append doc comment if present
if let Some(ref doc) = sym.doc_comment {
hover_text.push_str(&format!("\n---\n\n{doc}"));
}
return Ok(Some(Hover {
contents: HoverContents::Markup(MarkupContent {
kind: MarkupKind::Markdown,
value: hover_text,
}),
range: None,
}));
}
Ok(None)
}
// -----------------------------------------------------------------------
// Signature help (shows parameter info as you type)
// -----------------------------------------------------------------------
async fn signature_help(&self, params: SignatureHelpParams) -> Result<Option<SignatureHelp>> {
let uri = ¶ms.text_document_position_params.text_document.uri;
let position = params.text_document_position_params.position;
let source = {
let docs = self.documents.lock().unwrap();
match docs.get(uri) {
Some(s) => s.source.clone(),
None => return Ok(None),
}
};
let lines: Vec<&str> = source.lines().collect();
let line = match lines.get(position.line as usize) {
Some(l) => *l,
None => return Ok(None),
};
let col = position.character as usize;
let prefix = if col <= line.len() {
&line[..col]
} else {
line
};
// Walk backwards to find the matching `(` and the function name
let mut depth = 0i32;
let mut comma_count = 0u32;
let mut open_paren_pos = None;
for (i, ch) in prefix.char_indices().rev() {
match ch {
')' => depth += 1,
'(' => {
if depth == 0 {
open_paren_pos = Some(i);
break;
}
depth -= 1;
}
',' if depth == 0 => comma_count += 1,
_ => {}
}
}
let paren_pos = match open_paren_pos {
Some(p) => p,
None => return Ok(None),
};
// Extract function name before the `(`
let before = &prefix[..paren_pos];
let name: String = before
.chars()
.rev()
.take_while(|c| c.is_alphanumeric() || *c == '_')
.collect::<String>()
.chars()
.rev()
.collect();
if name.is_empty() {
return Ok(None);
}
// Look up in BUILTINS
let sig_str = match BUILTINS.iter().find(|(n, _)| *n == name.as_str()) {
Some((_, sig)) => *sig,
None => return Ok(None),
};
// Parse parameters from "name(param1, param2, ...) -> ret"
let params_str = sig_str
.split('(')
.nth(1)
.and_then(|s| s.split(')').next())
.unwrap_or("");
let params_list: Vec<ParameterInformation> = if params_str.is_empty() {
vec![]
} else {
params_str
.split(',')
.map(|p| ParameterInformation {
label: ParameterLabel::Simple(p.trim().to_string()),
documentation: None,
})
.collect()
};
Ok(Some(SignatureHelp {
signatures: vec![SignatureInformation {
label: sig_str.to_string(),
documentation: builtin_doc(&name).map(|d| {
Documentation::MarkupContent(MarkupContent {
kind: MarkupKind::Markdown,
value: d,
})
}),
parameters: Some(params_list.clone()),
active_parameter: Some(if params_list.is_empty() {
0
} else {
comma_count.min(params_list.len() as u32 - 1)
}),
}],
active_signature: Some(0),
active_parameter: Some(if params_list.is_empty() {
0
} else {
comma_count.min(params_list.len() as u32 - 1)
}),
}))
}
// -----------------------------------------------------------------------
// Workspace symbol search (cross-file pipeline/function search)
// -----------------------------------------------------------------------
async fn symbol(
&self,
params: WorkspaceSymbolParams,
) -> Result<Option<Vec<SymbolInformation>>> {
let query = params.query.to_lowercase();
let docs = self.documents.lock().unwrap();
let mut results = Vec::new();
for (uri, state) in docs.iter() {
for sym in &state.symbols {
let kind = match sym.kind {
HarnSymbolKind::Pipeline => SymbolKind::FUNCTION,
HarnSymbolKind::Function => SymbolKind::FUNCTION,
HarnSymbolKind::Variable => SymbolKind::VARIABLE,
HarnSymbolKind::Enum => SymbolKind::ENUM,
HarnSymbolKind::Struct => SymbolKind::STRUCT,
HarnSymbolKind::Interface => SymbolKind::INTERFACE,
HarnSymbolKind::Parameter => continue,
};
let name_lower = sym.name.to_lowercase();
if !query.is_empty() && !name_lower.contains(&query) {
continue;
}
let range = span_to_full_range(&sym.def_span, &state.source);
#[allow(deprecated)]
results.push(SymbolInformation {
name: sym.name.clone(),
kind,
tags: None,
deprecated: None,
location: Location {
uri: uri.clone(),
range,
},
container_name: None,
});
}
}
Ok(Some(results))
}
// -----------------------------------------------------------------------
// Code actions (quick-fix for lint diagnostics)
// -----------------------------------------------------------------------
async fn code_action(&self, params: CodeActionParams) -> Result<Option<CodeActionResponse>> {
let uri = ¶ms.text_document.uri;
let mut actions = Vec::new();
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(Some(actions)),
};
let source = state.source.clone();
drop(docs);
for diag in ¶ms.context.diagnostics {
let msg = &diag.message;
// --- [mutable-never-reassigned]: replace `var` with `let` ---
if msg.contains("[mutable-never-reassigned]") {
// Find the `var` keyword at the start of the diagnostic range
let offset = lsp_position_to_offset(&source, diag.range.start);
// Scan forward from the line start to find "var" keyword
if let Some(var_pos) = source[offset..].find("var") {
let abs_pos = offset + var_pos;
// Verify it's actually the `var` keyword (next char is space or newline)
if abs_pos + 3 <= source.len()
&& (abs_pos == 0 || !source.as_bytes()[abs_pos - 1].is_ascii_alphanumeric())
&& (abs_pos + 3 == source.len()
|| !source.as_bytes()[abs_pos + 3].is_ascii_alphanumeric())
{
let start = offset_to_position(&source, abs_pos);
let end = offset_to_position(&source, abs_pos + 3);
let edit_range = Range { start, end };
let mut changes = HashMap::new();
changes.insert(
uri.clone(),
vec![TextEdit {
range: edit_range,
new_text: "let".to_string(),
}],
);
actions.push(CodeActionOrCommand::CodeAction(CodeAction {
title: "Change `var` to `let`".to_string(),
kind: Some(CodeActionKind::QUICKFIX),
diagnostics: Some(vec![diag.clone()]),
edit: Some(WorkspaceEdit {
changes: Some(changes),
..Default::default()
}),
..Default::default()
}));
}
}
}
// --- [unused-variable] / [unused-parameter]: add `_` prefix ---
if msg.contains("[unused-variable]") || msg.contains("[unused-parameter]") {
// Extract the variable name from the message: "variable `foo` is declared..."
// or "parameter `foo` is declared..."
if let Some(name) = extract_backtick_name(msg) {
// Find the name in the source around the diagnostic range
let offset = lsp_position_to_offset(&source, diag.range.start);
let end_offset = lsp_position_to_offset(&source, diag.range.end)
.max(offset + 1)
.min(source.len());
let search_region = &source[offset..end_offset];
if let Some(name_pos) = find_word_in_region(search_region, &name) {
let abs_pos = offset + name_pos;
let start = offset_to_position(&source, abs_pos);
let end = offset_to_position(&source, abs_pos + name.len());
let edit_range = Range { start, end };
let mut changes = HashMap::new();
changes.insert(
uri.clone(),
vec![TextEdit {
range: edit_range,
new_text: format!("_{name}"),
}],
);
let label = if msg.contains("[unused-variable]") {
"variable"
} else {
"parameter"
};
actions.push(CodeActionOrCommand::CodeAction(CodeAction {
title: format!("Prefix {label} `{name}` with `_`"),
kind: Some(CodeActionKind::QUICKFIX),
diagnostics: Some(vec![diag.clone()]),
edit: Some(WorkspaceEdit {
changes: Some(changes),
..Default::default()
}),
..Default::default()
}));
}
}
}
// --- [comparison-to-bool]: simplify boolean comparison ---
if msg.contains("[comparison-to-bool]") {
let offset = lsp_position_to_offset(&source, diag.range.start);
let end_offset = lsp_position_to_offset(&source, diag.range.end)
.max(offset + 1)
.min(source.len());
let expr_text = &source[offset..end_offset];
// Try to produce a simplified replacement
let replacement = simplify_bool_comparison(expr_text);
if let Some(new_text) = replacement {
let mut changes = HashMap::new();
changes.insert(
uri.clone(),
vec![TextEdit {
range: diag.range,
new_text,
}],
);
actions.push(CodeActionOrCommand::CodeAction(CodeAction {
title: "Simplify boolean comparison".to_string(),
kind: Some(CodeActionKind::QUICKFIX),
diagnostics: Some(vec![diag.clone()]),
edit: Some(WorkspaceEdit {
changes: Some(changes),
..Default::default()
}),
..Default::default()
}));
}
}
}
Ok(Some(actions))
}
// -----------------------------------------------------------------------
// Rename (document-wide symbol rename)
// -----------------------------------------------------------------------
async fn rename(&self, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
let uri = ¶ms.text_document_position.text_document.uri;
let position = params.text_document_position.position;
let new_name = ¶ms.new_name;
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(None),
};
let source = state.source.clone();
let ast = state.ast.clone();
let symbols = state.symbols.clone();
drop(docs);
let old_name = match word_at_position(&source, position) {
Some(w) => w,
None => return Ok(None),
};
// Verify the name refers to a known symbol (definition or builtin).
// Builtins should not be renamed.
if BUILTINS.iter().any(|(n, _)| *n == old_name) {
return Ok(None);
}
// Check that the symbol exists in the symbol table
let symbol_exists = symbols.iter().any(|s| s.name == old_name);
if !symbol_exists {
return Ok(None);
}
// Collect all references from the AST
let program = match ast {
Some(p) => p,
None => return Ok(None),
};
let ref_spans = find_references(&program, &old_name);
if ref_spans.is_empty() {
return Ok(None);
}
// For each reference span, find the exact position of the name within
// the span text. Definition nodes have spans covering the whole
// declaration, so we search within each span for the identifier token.
let mut edits = Vec::new();
let mut seen_offsets = std::collections::HashSet::new();
// Also scan lexer tokens for precise identifier positions
let mut lexer = Lexer::new(&source);
if let Ok(tokens) = lexer.tokenize() {
for token in &tokens {
if let TokenKind::Identifier(ref name) = token.kind {
if name == &old_name && !seen_offsets.contains(&token.span.start) {
// Verify this token falls within one of the reference spans
let in_ref = ref_spans
.iter()
.any(|rs| token.span.start >= rs.start && token.span.end <= rs.end);
if in_ref {
seen_offsets.insert(token.span.start);
let start = offset_to_position(&source, token.span.start);
let end = offset_to_position(&source, token.span.end);
edits.push(TextEdit {
range: Range { start, end },
new_text: new_name.clone(),
});
}
}
}
}
}
if edits.is_empty() {
return Ok(None);
}
// Sort edits by position (bottom-up) to avoid offset shifting issues
edits.sort_by(|a, b| {
b.range
.start
.line
.cmp(&a.range.start.line)
.then(b.range.start.character.cmp(&a.range.start.character))
});
let mut changes = HashMap::new();
changes.insert(uri.clone(), edits);
Ok(Some(WorkspaceEdit {
changes: Some(changes),
..Default::default()
}))
}
// -----------------------------------------------------------------------
// Semantic tokens (lexer-based with symbol table enhancement)
// -----------------------------------------------------------------------
async fn semantic_tokens_full(
&self,
params: SemanticTokensParams,
) -> Result<Option<SemanticTokensResult>> {
let uri = ¶ms.text_document.uri;
let docs = self.documents.lock().unwrap();
let state = match docs.get(uri) {
Some(s) => s,
None => return Ok(None),
};
let source = state.source.clone();
let symbols = state.symbols.clone();
drop(docs);
// Tokenize (lexer never fails fatally for semantic tokens — if it
// errors we still have partial tokens up to the error point, but
// the simple API returns Err. Re-lex and collect what we can.)
let mut lexer = Lexer::new(&source);
let tokens = match lexer.tokenize() {
Ok(t) => t,
Err(_) => {
// On lex error, we can't produce tokens reliably
return Ok(None);
}
};
let semantic_tokens = build_semantic_tokens(&tokens, &symbols, &source);
Ok(Some(SemanticTokensResult::Tokens(SemanticTokens {
result_id: None,
data: semantic_tokens,
})))
}
}