1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
//! Main Language Server Protocol server implementation for rumdl
//!
//! This module implements the core LSP server following Ruff's architecture.
//! It provides real-time markdown linting, diagnostics, and code actions.
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::{RwLock, mpsc};
use tower_lsp::jsonrpc::Result as JsonRpcResult;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer};
use crate::config::{Config, ConfigValidated, SourcedConfig, is_valid_rule_name};
use crate::discovery::{ExcludeMatchers, is_markdown_extension};
use crate::lsp::index_worker::{IndexWorker, SharedIndexState};
use crate::lsp::types::{IndexState, IndexUpdate, LspRuleSettings, RelintRequest, RumdlLspConfig};
use crate::workspace_index::WorkspaceIndex;
/// Maximum number of rules in enable/disable lists (DoS protection)
const MAX_RULE_LIST_SIZE: usize = 100;
/// Maximum allowed line length value (DoS protection)
const MAX_LINE_LENGTH: usize = 10_000;
/// Merge the keys present in a `workspace/didChangeConfiguration` payload onto the
/// current LSP config, returning the merged config.
///
/// Only the keys the client actually sent are changed; every other field keeps its
/// current value, so a partial payload (e.g. just `{"enableSymbols": false}`) never
/// resets omitted fields to their defaults. A client that sends a full snapshot
/// still fully applies. Returns `None` only if `incoming` is not a JSON object, the
/// current config cannot be represented as one, or the merged object fails to
/// deserialize -- all unreachable for the current field types, which round-trip
/// through serde JSON; the caller treats `None` as "leave the config unchanged"
/// rather than clobbering omitted fields.
fn merge_lsp_config(current: &RumdlLspConfig, incoming: &serde_json::Value) -> Option<RumdlLspConfig> {
let serde_json::Value::Object(incoming) = incoming else {
return None;
};
let serde_json::Value::Object(mut base) = serde_json::to_value(current).ok()? else {
return None;
};
for (key, value) in incoming {
base.insert(key.clone(), value.clone());
}
serde_json::from_value(serde_json::Value::Object(base)).ok()
}
/// Represents a document in the LSP server's cache
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DocumentEntry {
/// The document content
pub(crate) content: String,
/// Version number from the editor (None for disk-loaded documents)
pub(crate) version: Option<i32>,
/// Whether the document was loaded from disk (true) or opened in editor (false)
pub(crate) from_disk: bool,
}
/// Cache entry for resolved configuration
#[derive(Clone, Debug)]
pub(crate) struct ConfigCacheEntry {
/// The resolved configuration
pub(crate) config: Config,
/// The same configuration with provenance intact, kept only when it opts
/// into `.editorconfig` reading. That layering is per file (a section glob
/// can match one file in a directory and not its neighbour) while this cache
/// is per directory, so the sourced form has to survive the cache hit.
pub(crate) sourced: Option<Arc<SourcedConfig<ConfigValidated>>>,
/// Config file path that was loaded (for invalidation)
pub(crate) config_file: Option<PathBuf>,
/// True if this entry came from the global/user fallback (no project config)
pub(crate) from_global_fallback: bool,
}
/// Shared per-file configuration resolution used by request handlers and the
/// background workspace index.
///
/// The server keeps the individual handles as part of its established state
/// surface; this value holds clones of those same `Arc`s, so both consumers use
/// one cache and observe the same reloads and invalidations.
#[derive(Clone)]
pub(crate) struct ConfigResolver {
pub(super) config: Arc<RwLock<RumdlLspConfig>>,
pub(super) rumdl_config: Arc<RwLock<Config>>,
pub(super) rumdl_sourced: Arc<RwLock<Option<Arc<SourcedConfig<ConfigValidated>>>>>,
pub(super) workspace_roots: Arc<RwLock<Vec<PathBuf>>>,
pub(super) config_cache: Arc<RwLock<HashMap<PathBuf, ConfigCacheEntry>>>,
pub(super) cli_config_path: Option<String>,
}
/// Main LSP server for rumdl
///
/// Following Ruff's pattern, this server provides:
/// - Real-time diagnostics as users type
/// - Code actions for automatic fixes
/// - Configuration management
/// - Multi-file support
/// - Multi-root workspace support with per-file config resolution
/// - Cross-file analysis with workspace indexing
#[derive(Clone)]
pub struct RumdlLanguageServer {
pub(crate) client: Client,
/// Configuration for the LSP server
pub(crate) config: Arc<RwLock<RumdlLspConfig>>,
/// Rumdl core configuration (fallback/default)
pub(crate) rumdl_config: Arc<RwLock<Config>>,
/// `rumdl_config` with provenance intact, kept only when it opts into
/// `.editorconfig` reading; written wherever `rumdl_config` is.
pub(crate) rumdl_sourced: Arc<RwLock<Option<Arc<SourcedConfig<ConfigValidated>>>>>,
/// Document store for open files and cached disk files
pub(crate) documents: Arc<RwLock<HashMap<Url, DocumentEntry>>>,
/// Maps a document's resolved URI to every open spelling that names it.
///
/// The store is keyed by the editor's spelling, because that is the spelling
/// diagnostics must be published against. Navigation asks for a document by
/// its resolved spelling, which differs only when the editor reached the file
/// through a symlinked ancestor, so this stays empty for most workspaces.
/// Without it such a request would read the file on disk and miss the buffer.
///
/// One resolved path can have several spellings open at once (two symlinks
/// to the same directory, each opened), so this holds all of them rather
/// than the latest. A single slot would let the second open displace the
/// first and the first close strand the second.
pub(crate) document_aliases: Arc<RwLock<HashMap<Url, Vec<Url>>>>,
/// Workspace root folders from the client
pub(crate) workspace_roots: Arc<RwLock<Vec<PathBuf>>>,
/// Configuration cache: maps directory path to resolved config
/// Key is the directory where config search started (file's parent dir)
pub(crate) config_cache: Arc<RwLock<HashMap<PathBuf, ConfigCacheEntry>>>,
/// Shared resolver consumed by document requests and workspace indexing.
pub(crate) config_resolver: ConfigResolver,
/// Workspace index for cross-file analysis (MD051)
pub(crate) workspace_index: Arc<RwLock<WorkspaceIndex>>,
/// Current state of the workspace index (building/ready/error)
pub(crate) index_state: Arc<RwLock<IndexState>>,
/// Channel to send updates to the background index worker.
///
/// `None` on the copy a background task holds (see
/// [`Self::detached_for_background`]), which must not be able to queue
/// index work: it would keep the index worker waiting on a channel that
/// can never close, so neither task would stop when the editor goes away.
/// Queue through [`Self::queue_index_update`] rather than reading it.
update_tx: Option<mpsc::Sender<IndexUpdate>>,
/// Whether the client supports pull diagnostics (textDocument/diagnostic)
/// When true, we skip pushing diagnostics to avoid duplicates
pub(crate) client_supports_pull_diagnostics: Arc<RwLock<bool>>,
/// Whether the client supports hierarchical (nested) document symbols.
/// When false, `textDocument/documentSymbol` must return the flat
/// `SymbolInformation[]` form instead of a `DocumentSymbol` tree.
pub(crate) client_supports_hierarchical_symbols: Arc<RwLock<bool>>,
/// Config path supplied via `rumdl server --config <path>`.
///
/// Held in an immutable field (not in `self.config`) so that client-driven
/// updates -- `initialize` initialization options or `workspace/didChangeConfiguration`
/// notifications -- cannot drop it. Treated as the highest-priority config source:
/// it outranks both client-supplied `configPath` and per-file discovery, mirroring
/// the CLI semantics where an explicit `--config` is standalone.
pub(crate) cli_config_path: Option<String>,
}
impl RumdlLanguageServer {
pub fn new(client: Client, cli_config_path: Option<&str>) -> Self {
let initial_config = RumdlLspConfig::default();
let cli_config_path = cli_config_path.map(str::to_string);
// Create shared state for workspace indexing
let workspace_index = Arc::new(RwLock::new(WorkspaceIndex::new()));
let index_state = Arc::new(RwLock::new(IndexState::default()));
let workspace_roots = Arc::new(RwLock::new(Vec::new()));
let config = Arc::new(RwLock::new(initial_config));
let rumdl_config = Arc::new(RwLock::new(Config::default()));
let rumdl_sourced = Arc::new(RwLock::new(None));
let config_cache = Arc::new(RwLock::new(HashMap::new()));
let documents = Arc::new(RwLock::new(HashMap::new()));
let config_resolver = ConfigResolver {
config: config.clone(),
rumdl_config: rumdl_config.clone(),
rumdl_sourced: rumdl_sourced.clone(),
workspace_roots: workspace_roots.clone(),
config_cache: config_cache.clone(),
cli_config_path: cli_config_path.clone(),
};
// Create channels for index worker communication
let (update_tx, update_rx) = mpsc::channel::<IndexUpdate>(100);
let (relint_tx, relint_rx) = mpsc::channel::<RelintRequest>(100);
let server = Self {
client,
config,
rumdl_config,
rumdl_sourced,
documents,
document_aliases: Arc::new(RwLock::new(HashMap::new())),
workspace_roots,
config_cache,
config_resolver: config_resolver.clone(),
workspace_index,
index_state,
update_tx: Some(update_tx),
client_supports_pull_diagnostics: Arc::new(RwLock::new(false)),
client_supports_hierarchical_symbols: Arc::new(RwLock::new(false)),
cli_config_path,
};
// Spawn the background index worker after every shared configuration
// handle exists, so indexing and request handling receive the same
// resolver rather than parallel snapshots.
let worker = IndexWorker::new(
update_rx,
server.client.clone(),
relint_tx,
SharedIndexState {
workspace_index: server.workspace_index.clone(),
index_state: server.index_state.clone(),
workspace_roots: server.workspace_roots.clone(),
config_resolver,
documents: server.documents.clone(),
},
);
tokio::spawn(worker.run());
// Consume the index worker's re-lint requests. Cross-file diagnostics are
// computed from the workspace index, so the events that change an answer
// reach this server rather than the editor: another file's headings moved,
// or the initial scan finished after a document was already linted.
tokio::spawn(server.detached_for_background().run_relint_worker(relint_rx));
server
}
/// A copy of this server for a background task, holding the same state but
/// not the connection's claim on the index worker.
///
/// A task parked on a channel holds its copy for as long as it runs, and
/// the index worker runs until every sender is dropped. A plain clone would
/// therefore make the two keep each other alive: the worker waiting on a
/// channel the re-lint task holds open, the re-lint task waiting on a
/// channel the worker holds open, with the whole server state behind them.
/// A client that closes its connection without sending `shutdown` is what
/// reaches that.
fn detached_for_background(&self) -> Self {
Self {
update_tx: None,
..self.clone()
}
}
/// Queue work for the background index worker.
///
/// Answers whether the worker took it. `false` means the worker is gone,
/// which is the normal state after shutdown and on a background copy of the
/// server; a caller that wants to report it decides what that is worth.
pub(crate) async fn queue_index_update(&self, update: IndexUpdate) -> bool {
let Some(update_tx) = &self.update_tx else {
return false;
};
update_tx.send(update).await.is_ok()
}
/// Get document content, either from cache or by reading from disk
///
/// This method first checks if the document is in the cache (opened in editor).
/// If not found, it attempts to read the file from disk and caches it for
/// future requests.
pub(super) async fn get_document_content(&self, uri: &Url) -> Option<String> {
let uri = &self.store_uri(uri).await;
// First check the cache
{
let docs = self.documents.read().await;
if let Some(entry) = docs.get(uri) {
return Some(entry.content.clone());
}
}
// If not in cache and it's a file URI, try to read from disk
if let Ok(path) = uri.to_file_path() {
if let Ok(content) = tokio::fs::read_to_string(&path).await {
// Cache the document for future requests
let entry = DocumentEntry {
content: content.clone(),
version: None,
from_disk: true,
};
let mut docs = self.documents.write().await;
docs.insert(uri.clone(), entry);
log::debug!("Loaded document from disk and cached: {uri}");
return Some(content);
} else {
log::debug!("Failed to read file from disk: {uri}");
}
}
None
}
/// Get document content only if the document is currently open in the editor.
///
/// We intentionally do not read from disk here because diagnostics should be
/// scoped to open documents. This avoids lingering diagnostics after a file
/// is closed when clients use pull diagnostics.
async fn get_open_document_content(&self, uri: &Url) -> Option<String> {
let uri = self.store_uri(uri).await;
let docs = self.documents.read().await;
docs.get(&uri)
.and_then(|entry| (!entry.from_disk).then(|| entry.content.clone()))
}
/// The URI a document is stored under, given any spelling that names it.
///
/// Answers with the request's own URI, except when the file is open only
/// under a different spelling of the same path: an alias then finds the
/// editor's buffer instead of falling through to the file on disk.
///
/// An open buffer under the requested spelling wins over any alias, because
/// one file can be open under several spellings at once and the editor holds
/// a separate buffer for each. A disk copy cached under the requested
/// spelling does not win: it was read before the document was opened
/// elsewhere, and the buffer an alias names has since become the truth.
async fn store_uri(&self, uri: &Url) -> Url {
let Some(spellings) = self.document_aliases.read().await.get(uri).cloned() else {
return uri.clone();
};
let docs = self.documents.read().await;
let is_open = |u: &Url| matches!(docs.get(u), Some(entry) if !entry.from_disk);
if is_open(uri) {
return uri.clone();
}
// The most recently opened spelling, so a reopen supersedes an older one.
spellings
.iter()
.rev()
.find(|u| is_open(u))
.cloned()
.unwrap_or_else(|| uri.clone())
}
/// Resolve the Markdown flavor for a document, mirroring the per-file flavor
/// resolution used by diagnostics and formatting so symbol parsing matches.
pub(super) async fn resolve_flavor_for_uri(&self, uri: &Url) -> crate::config::MarkdownFlavor {
match super::resolve_uri(uri) {
Some(path) => self.resolve_config_for_file(&path).await.get_flavor_for_file(&path),
None => self.rumdl_config.read().await.markdown_flavor(),
}
}
}
#[tower_lsp::async_trait]
impl LanguageServer for RumdlLanguageServer {
async fn initialize(&self, params: InitializeParams) -> JsonRpcResult<InitializeResult> {
log::info!("Initializing rumdl Language Server");
// Parse client capabilities and configuration
if let Some(options) = params.initialization_options
&& let Ok(config) = serde_json::from_value::<RumdlLspConfig>(options)
{
*self.config.write().await = config;
}
// Detect if client supports pull diagnostics (textDocument/diagnostic)
// When the client supports pull, we avoid pushing to prevent duplicate diagnostics
let supports_pull = params
.capabilities
.text_document
.as_ref()
.and_then(|td| td.diagnostic.as_ref())
.is_some();
if supports_pull {
log::info!("Client supports pull diagnostics - disabling push to avoid duplicates");
*self.client_supports_pull_diagnostics.write().await = true;
} else {
log::info!("Client does not support pull diagnostics - using push model");
}
// Detect hierarchical document symbol support; without it the client expects
// the legacy flat `SymbolInformation[]` form.
let supports_hierarchical_symbols = params
.capabilities
.text_document
.as_ref()
.and_then(|td| td.document_symbol.as_ref())
.and_then(|ds| ds.hierarchical_document_symbol_support)
.unwrap_or(false);
*self.client_supports_hierarchical_symbols.write().await = supports_hierarchical_symbols;
// Extract and store workspace roots
let mut roots = Vec::new();
if let Some(workspace_folders) = params.workspace_folders {
for folder in workspace_folders {
if let Ok(path) = folder.uri.to_file_path() {
let path = super::resolve_workspace_root(&path);
log::info!("Workspace root: {}", path.display());
roots.push(path);
}
}
} else if let Some(root_uri) = params.root_uri
&& let Ok(path) = root_uri.to_file_path()
{
let path = super::resolve_workspace_root(&path);
log::info!("Workspace root: {}", path.display());
roots.push(path);
}
*self.workspace_roots.write().await = roots;
// Load rumdl configuration with auto-discovery (fallback/default)
self.load_configuration(false).await;
let (enable_link_navigation, enable_link_completions, enable_symbols) = {
let config = self.config.read().await;
(
config.enable_link_navigation,
config.enable_link_completions,
config.enable_symbols,
)
};
Ok(InitializeResult {
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Options(TextDocumentSyncOptions {
open_close: Some(true),
change: Some(TextDocumentSyncKind::FULL),
will_save: Some(false),
will_save_wait_until: Some(true),
save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
include_text: Some(false),
})),
})),
code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions {
code_action_kinds: Some(vec![
CodeActionKind::QUICKFIX,
CodeActionKind::SOURCE_FIX_ALL,
CodeActionKind::new("source.fixAll.rumdl"),
]),
work_done_progress_options: WorkDoneProgressOptions::default(),
resolve_provider: None,
})),
document_formatting_provider: Some(OneOf::Left(true)),
document_range_formatting_provider: Some(OneOf::Left(true)),
document_symbol_provider: enable_symbols.then_some(OneOf::Left(true)),
workspace_symbol_provider: enable_symbols.then_some(OneOf::Left(true)),
diagnostic_provider: Some(DiagnosticServerCapabilities::Options(DiagnosticOptions {
identifier: Some("rumdl".to_string()),
inter_file_dependencies: true,
workspace_diagnostics: false,
work_done_progress_options: WorkDoneProgressOptions::default(),
})),
// Completion always stays available for fenced code-block language
// labels (backtick trigger). The link-target triggers (`(` `#` `/`
// `.` `-`) are only registered when link completions are enabled, so
// a client with its own link-completion source (e.g. a PKM-focused
// LSP) is not invoked on those characters when the feature is off.
completion_provider: Some(CompletionOptions {
trigger_characters: Some(if enable_link_completions {
vec![
"`".to_string(),
"(".to_string(),
"#".to_string(),
"/".to_string(),
".".to_string(),
"-".to_string(),
]
} else {
vec!["`".to_string()]
}),
resolve_provider: Some(false),
work_done_progress_options: WorkDoneProgressOptions::default(),
all_commit_characters: None,
completion_item: None,
}),
definition_provider: enable_link_navigation.then_some(OneOf::Left(true)),
references_provider: enable_link_navigation.then_some(OneOf::Left(true)),
hover_provider: enable_link_navigation.then_some(HoverProviderCapability::Simple(true)),
rename_provider: enable_link_navigation.then_some(OneOf::Right(RenameOptions {
prepare_provider: Some(true),
work_done_progress_options: WorkDoneProgressOptions::default(),
})),
workspace: Some(WorkspaceServerCapabilities {
workspace_folders: Some(WorkspaceFoldersServerCapabilities {
supported: Some(true),
change_notifications: Some(OneOf::Left(true)),
}),
file_operations: None,
}),
..Default::default()
},
server_info: Some(ServerInfo {
name: "rumdl".to_string(),
version: Some(env!("CARGO_PKG_VERSION").to_string()),
}),
})
}
async fn initialized(&self, _: InitializedParams) {
let version = env!("CARGO_PKG_VERSION");
// Get binary path and build time
let (binary_path, build_time) = std::env::current_exe().ok().map_or_else(
|| ("unknown".to_string(), "unknown".to_string()),
|path| {
let path_str = path.to_str().unwrap_or("unknown").to_string();
let build_time = std::fs::metadata(&path)
.ok()
.and_then(|metadata| metadata.modified().ok())
.and_then(|modified| modified.duration_since(std::time::UNIX_EPOCH).ok())
.and_then(|duration| {
let secs = duration.as_secs();
chrono::DateTime::from_timestamp(secs as i64, 0)
.map(|dt| dt.format("%Y-%m-%d %H:%M:%S UTC").to_string())
})
.unwrap_or_else(|| "unknown".to_string());
(path_str, build_time)
},
);
let working_dir = std::env::current_dir()
.ok()
.and_then(|p| p.to_str().map(std::string::ToString::to_string))
.unwrap_or_else(|| "unknown".to_string());
log::info!("rumdl Language Server v{version} initialized (built: {build_time}, binary: {binary_path})");
log::info!("Working directory: {working_dir}");
self.client
.log_message(MessageType::INFO, format!("rumdl v{version} Language Server started"))
.await;
// Trigger initial workspace indexing for cross-file analysis
if !self.queue_index_update(IndexUpdate::FullRescan).await {
log::warn!("Failed to trigger initial workspace indexing");
} else {
log::info!("Triggered initial workspace indexing for cross-file analysis");
}
// Register file watchers for markdown files and config files
let markdown_patterns = [
"**/*.md",
"**/*.markdown",
"**/*.mdx",
"**/*.mkd",
"**/*.mkdn",
"**/*.mdown",
"**/*.mdwn",
"**/*.qmd",
"**/*.rmd",
];
// `.editorconfig` is subscribed to unconditionally: a project can opt in
// after the client registered these, and the handler decides whether an
// event counts.
let config_patterns = [
"**/.rumdl.toml",
"**/rumdl.toml",
"**/pyproject.toml",
"**/.markdownlint.json",
"**/.markdownlint-cli2.yaml",
"**/.markdownlint-cli2.jsonc",
"**/.editorconfig",
];
let watchers: Vec<_> = markdown_patterns
.iter()
.chain(config_patterns.iter())
.map(|pattern| FileSystemWatcher {
glob_pattern: GlobPattern::String((*pattern).to_string()),
kind: Some(WatchKind::all()),
})
.collect();
let registration = Registration {
id: "markdown-watcher".to_string(),
method: "workspace/didChangeWatchedFiles".to_string(),
register_options: Some(
serde_json::to_value(DidChangeWatchedFilesRegistrationOptions { watchers }).unwrap(),
),
};
if self.client.register_capability(vec![registration]).await.is_err() {
log::debug!("Client does not support file watching capability");
}
}
async fn completion(&self, params: CompletionParams) -> JsonRpcResult<Option<CompletionResponse>> {
let uri = params.text_document_position.text_document.uri;
let position = params.text_document_position.position;
// Get document content
let Some(text) = self.get_document_content(&uri).await else {
return Ok(None);
};
// Code fence language completion (backtick trigger)
if let Some((start_col, current_text)) = Self::detect_code_fence_language_position(&text, position) {
log::debug!(
"Code fence completion triggered at {}:{}, current text: '{}'",
position.line,
position.character,
current_text
);
let items = self
.get_language_completions(&uri, ¤t_text, start_col, position)
.await;
if !items.is_empty() {
return Ok(Some(CompletionResponse::Array(items)));
}
}
// Link target completion: file paths and heading anchors
if self.config.read().await.enable_link_completions {
// For trigger characters that fire on many non-link contexts (`.`, `-`),
// skip the full parse when there is no `](` on the current line before
// the cursor. This avoids needless work on list items and contractions.
let trigger = params.context.as_ref().and_then(|c| c.trigger_character.as_deref());
let skip_link_check = matches!(trigger, Some("." | "-")) && {
let line_num = position.line as usize;
// Scan the whole line — no byte-slicing at a UTF-16 offset needed.
// A line without `](` anywhere cannot contain a link target.
!text.lines().nth(line_num).is_some_and(|line| line.contains("]("))
};
if !skip_link_check && let Some(link_info) = Self::detect_link_target_position(&text, position) {
if let Some((partial_anchor, anchor_start_col)) = link_info.anchor {
log::debug!(
"Anchor completion triggered at {}:{}, file: '{}', partial: '{}'",
position.line,
position.character,
link_info.file_path,
partial_anchor
);
let items = self
.get_anchor_completions(&uri, &link_info.file_path, &partial_anchor, anchor_start_col, position)
.await;
if !items.is_empty() {
return Ok(Some(CompletionResponse::Array(items)));
}
} else {
log::debug!(
"File path completion triggered at {}:{}, partial: '{}'",
position.line,
position.character,
link_info.file_path
);
let list = self
.get_file_completions(&uri, &link_info.file_path, link_info.path_start_col, position)
.await;
if !list.items.is_empty() {
return Ok(Some(CompletionResponse::List(list)));
}
}
}
}
Ok(None)
}
async fn did_change_workspace_folders(&self, params: DidChangeWorkspaceFoldersParams) {
// Update workspace roots
let mut roots = self.workspace_roots.write().await;
// Resolved the same way `initialize` resolves a root, so a folder added
// or removed later is comparable with the ones already recorded.
// Remove deleted workspace folders
for removed in ¶ms.event.removed {
if let Ok(path) = removed.uri.to_file_path() {
let path = super::resolve_workspace_root(&path);
roots.retain(|r| r != &path);
log::info!("Removed workspace root: {}", path.display());
}
}
// Add new workspace folders
for added in ¶ms.event.added {
if let Ok(path) = added.uri.to_file_path()
&& let path = super::resolve_workspace_root(&path)
&& !roots.contains(&path)
{
log::info!("Added workspace root: {}", path.display());
roots.push(path);
}
}
drop(roots);
// Clear config cache as workspace structure changed
self.config_cache.write().await.clear();
// Reload fallback configuration
self.reload_configuration().await;
// Trigger full workspace rescan for cross-file index
if !self.queue_index_update(IndexUpdate::FullRescan).await {
log::warn!("Failed to trigger workspace rescan after folder change");
}
}
async fn did_change_configuration(&self, params: DidChangeConfigurationParams) {
log::debug!("Configuration changed: {:?}", params.settings);
// Parse settings from the notification
// Neovim sends: { "rumdl": { "MD013": {...}, ... } }
// VSCode might send the full RumdlLspConfig or similar structure
let settings_value = params.settings;
// Try to extract "rumdl" key from settings (Neovim style)
let rumdl_settings = if let serde_json::Value::Object(ref obj) = settings_value {
obj.get("rumdl").cloned().unwrap_or(settings_value.clone())
} else {
settings_value
};
// A settings payload that carries `linkCompletionContentRoots` is a full
// RumdlLspConfig even when the list is empty, so clearing it back to the
// workspace-root default applies instead of being treated as unknown.
let has_content_roots_key = matches!(
&rumdl_settings,
serde_json::Value::Object(obj) if obj.contains_key("linkCompletionContentRoots")
);
// `enableSymbols` is detected by key presence (not just a non-default value)
// so that a bare payload applies symmetrically: both `{"enableSymbols": false}`
// and a later `{"enableSymbols": true}` re-enable take effect, rather than the
// re-enable deserializing to the default and being dropped as an unknown key.
let has_symbols_key = matches!(
&rumdl_settings,
serde_json::Value::Object(obj) if obj.contains_key("enableSymbols")
);
// Track if we successfully applied any configuration
let mut config_applied = false;
let mut warnings: Vec<String> = Vec::new();
// Try to parse as LspRuleSettings first (Neovim style with "disable", "enable", rule keys)
// We check this first because RumdlLspConfig with #[serde(default)] will accept any JSON
// and just ignore unknown fields, which would lose the Neovim-style settings
if let Ok(rule_settings) = serde_json::from_value::<LspRuleSettings>(rumdl_settings.clone())
&& (rule_settings.disable.is_some()
|| rule_settings.enable.is_some()
|| rule_settings.line_length.is_some()
|| (!rule_settings.rules.is_empty() && rule_settings.rules.keys().all(|k| is_valid_rule_name(k))))
{
// Validate rule names in disable/enable lists
if let Some(ref disable) = rule_settings.disable {
for rule in disable {
if !is_valid_rule_name(rule) {
warnings.push(format!("Unknown rule in disable list: {rule}"));
}
}
}
if let Some(ref enable) = rule_settings.enable {
for rule in enable {
if !is_valid_rule_name(rule) {
warnings.push(format!("Unknown rule in enable list: {rule}"));
}
}
}
// Validate rule-specific settings
for rule_name in rule_settings.rules.keys() {
if !is_valid_rule_name(rule_name) {
warnings.push(format!("Unknown rule in settings: {rule_name}"));
}
}
log::info!("Applied rule settings from configuration (Neovim style)");
let mut config = self.config.write().await;
config.settings = Some(rule_settings);
drop(config);
config_applied = true;
} else if let Ok(full_config) = serde_json::from_value::<RumdlLspConfig>(rumdl_settings.clone())
&& (full_config.config_path.is_some()
|| full_config.enable_rules.is_some()
|| full_config.disable_rules.is_some()
|| full_config.settings.is_some()
|| !full_config.enable_linting
|| full_config.enable_auto_fix
|| !full_config.enable_link_completions
|| !full_config.enable_link_navigation
|| has_symbols_key
|| has_content_roots_key)
{
// Validate rule names
if let Some(ref rules) = full_config.enable_rules {
for rule in rules {
if !is_valid_rule_name(rule) {
warnings.push(format!("Unknown rule in enableRules: {rule}"));
}
}
}
if let Some(ref rules) = full_config.disable_rules {
for rule in rules {
if !is_valid_rule_name(rule) {
warnings.push(format!("Unknown rule in disableRules: {rule}"));
}
}
}
// Merge only the keys the client sent onto the current config (see
// `merge_lsp_config`), so a partial payload never clobbers previously-set
// fields. The write lock is held across the merge so the read-modify-write
// is atomic; the merge is synchronous and `.await`-free, so it cannot
// deadlock or stall the executor. `full_config` was already validated above
// and is no longer needed here (a merge failure leaves the config unchanged
// rather than falling back to a clobbering whole-struct replace).
{
let mut config = self.config.write().await;
if let Some(merged) = merge_lsp_config(&config, &rumdl_settings) {
*config = merged;
drop(config);
log::info!("Merged LSP configuration from client settings");
config_applied = true;
} else {
drop(config);
warnings.push("Could not merge LSP configuration update; keeping current settings".to_string());
}
}
} else if let serde_json::Value::Object(obj) = rumdl_settings {
// Otherwise, treat as per-rule settings with manual parsing
// Format: { "MD013": { "lineLength": 80 }, "disable": ["MD009"] }
let mut config = self.config.write().await;
// Manual parsing for Neovim format
let mut rules = std::collections::HashMap::new();
let mut disable = Vec::new();
let mut enable = Vec::new();
let mut line_length = None;
for (key, value) in obj {
match key.as_str() {
"disable" => match serde_json::from_value::<Vec<String>>(value.clone()) {
Ok(d) => {
if d.len() > MAX_RULE_LIST_SIZE {
warnings.push(format!(
"Too many rules in 'disable' ({} > {}), truncating",
d.len(),
MAX_RULE_LIST_SIZE
));
}
for rule in d.iter().take(MAX_RULE_LIST_SIZE) {
if !is_valid_rule_name(rule) {
warnings.push(format!("Unknown rule in disable: {rule}"));
}
}
disable = d.into_iter().take(MAX_RULE_LIST_SIZE).collect();
}
Err(_) => {
warnings.push(format!(
"Invalid 'disable' value: expected array of strings, got {value}"
));
}
},
"enable" => match serde_json::from_value::<Vec<String>>(value.clone()) {
Ok(e) => {
if e.len() > MAX_RULE_LIST_SIZE {
warnings.push(format!(
"Too many rules in 'enable' ({} > {}), truncating",
e.len(),
MAX_RULE_LIST_SIZE
));
}
for rule in e.iter().take(MAX_RULE_LIST_SIZE) {
if !is_valid_rule_name(rule) {
warnings.push(format!("Unknown rule in enable: {rule}"));
}
}
enable = e.into_iter().take(MAX_RULE_LIST_SIZE).collect();
}
Err(_) => {
warnings.push(format!(
"Invalid 'enable' value: expected array of strings, got {value}"
));
}
},
"lineLength" | "line_length" | "line-length" => {
if let Some(l) = value.as_u64() {
match usize::try_from(l) {
Ok(len) if len <= MAX_LINE_LENGTH => line_length = Some(len),
Ok(len) => warnings.push(format!(
"Invalid 'lineLength' value: {len} exceeds maximum ({MAX_LINE_LENGTH})"
)),
Err(_) => warnings.push(format!("Invalid 'lineLength' value: {l} is too large")),
}
} else {
warnings.push(format!("Invalid 'lineLength' value: expected number, got {value}"));
}
}
// Rule-specific settings (e.g., "MD013": { "lineLength": 80 })
_ if key.starts_with("MD") || key.starts_with("md") => {
let normalized = key.to_uppercase();
if !is_valid_rule_name(&normalized) {
warnings.push(format!("Unknown rule: {key}"));
}
rules.insert(normalized, value);
}
_ => {
// Unknown key - warn and ignore
warnings.push(format!("Unknown configuration key: {key}"));
}
}
}
let settings = LspRuleSettings {
line_length,
disable: if disable.is_empty() { None } else { Some(disable) },
enable: if enable.is_empty() { None } else { Some(enable) },
rules,
};
log::info!("Applied Neovim-style rule settings (manual parse)");
config.settings = Some(settings);
drop(config);
config_applied = true;
} else {
log::warn!("Could not parse configuration settings: {rumdl_settings:?}");
}
// Log warnings for invalid configuration
for warning in &warnings {
log::warn!("{warning}");
}
// Notify client of configuration warnings via window/logMessage
if !warnings.is_empty() {
let message = if warnings.len() == 1 {
format!("rumdl: {}", warnings[0])
} else {
format!("rumdl configuration warnings:\n{}", warnings.join("\n"))
};
self.client.log_message(MessageType::WARNING, message).await;
}
if !config_applied {
log::debug!("No configuration changes applied");
}
// Clear config cache to pick up new settings
self.config_cache.write().await.clear();
// Reload the global rumdl config so a runtime change to `configPath`
// (handled by the parser branches above) takes effect on the next
// resolve. Without this, `resolve_config_for_file` would keep returning
// the previously-loaded `rumdl_config`, silently ignoring the new path.
// Skip the client notification: the diagnostics refresh below already
// surfaces the result, and notifying here can stall when a test or
// misbehaving client isn't draining the LSP message channel.
if config_applied {
self.load_configuration(false).await;
// Rebuild the workspace index under the reloaded config: a new
// configPath can change exclude patterns or respect_gitignore,
// which the scan reads from the shared config.
if !self.queue_index_update(IndexUpdate::FullRescan).await {
log::warn!("Failed to request workspace rescan after configuration change");
}
}
// Collect all open documents first (to avoid holding lock during async
// operations). Files cached from disk to answer a request are not open:
// publishing for one puts diagnostics on screen for a document the
// editor never opened, and no `didClose` will ever clear them.
let doc_list: Vec<_> = {
let documents = self.documents.read().await;
documents
.iter()
.filter(|(_, entry)| !entry.from_disk)
.map(|(uri, entry)| (uri.clone(), entry.content.clone()))
.collect()
};
// Refresh diagnostics for all open documents concurrently. Collecting the
// handles is what starts every task: a lazy iterator would spawn each one
// only as the loop below awaits it, running them one at a time.
let tasks: Vec<_> = doc_list
.into_iter()
.map(|(uri, text)| {
let server = self.clone();
tokio::spawn(async move {
server.update_diagnostics(uri, text, true).await;
})
})
.collect();
// Wait for all diagnostics to complete
for task in tasks {
let _ = task.await;
}
}
async fn shutdown(&self) -> JsonRpcResult<()> {
log::info!("Shutting down rumdl Language Server");
// Signal the index worker to shut down
self.queue_index_update(IndexUpdate::Shutdown).await;
Ok(())
}
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let uri = params.text_document.uri;
let text = params.text_document.text;
let version = params.text_document.version;
let entry = DocumentEntry {
content: text.clone(),
version: Some(version),
from_disk: false,
};
self.documents.write().await.insert(uri.clone(), entry);
// Make the document reachable by the spelling navigation resolves it to.
let resolved = super::resolve_uri_spelling(&uri);
if resolved != uri {
let mut aliases = self.document_aliases.write().await;
let spellings = aliases.entry(resolved).or_default();
if !spellings.contains(&uri) {
spellings.push(uri.clone());
}
}
// Send update to index worker for cross-file analysis
if let Some(path) = super::resolve_uri(&uri) {
self.queue_index_update(IndexUpdate::FileChanged {
path,
content: text.clone(),
})
.await;
}
self.update_diagnostics(uri, text, true).await;
}
async fn did_change(&self, params: DidChangeTextDocumentParams) {
let uri = params.text_document.uri;
let version = params.text_document.version;
if let Some(change) = params.content_changes.into_iter().next() {
let text = change.text;
let entry = DocumentEntry {
content: text.clone(),
version: Some(version),
from_disk: false,
};
self.documents.write().await.insert(uri.clone(), entry);
// Send update to index worker for cross-file analysis
if let Some(path) = super::resolve_uri(&uri) {
self.queue_index_update(IndexUpdate::FileChanged {
path,
content: text.clone(),
})
.await;
}
self.update_diagnostics(uri, text, false).await;
}
}
async fn will_save_wait_until(&self, params: WillSaveTextDocumentParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
// Only apply fixes on manual saves (Cmd+S / Ctrl+S), not on autosave
// This respects VSCode's editor.formatOnSave: "explicit" setting
if params.reason != TextDocumentSaveReason::MANUAL {
return Ok(None);
}
let config_guard = self.config.read().await;
let enable_auto_fix = config_guard.enable_auto_fix;
drop(config_guard);
if !enable_auto_fix {
return Ok(None);
}
// Get the current document content
let Some(text) = self.get_document_content(¶ms.text_document.uri).await else {
return Ok(None);
};
// Apply all fixes
match self.apply_all_fixes(¶ms.text_document.uri, &text).await {
Ok(Some(fixed_text)) => {
// Return a single edit that replaces the entire document
Ok(Some(vec![TextEdit {
range: Range {
start: Position { line: 0, character: 0 },
end: self.get_end_position(&text),
},
new_text: fixed_text,
}]))
}
Ok(None) => Ok(None),
Err(e) => {
log::error!("Failed to generate fixes in will_save_wait_until: {e}");
Ok(None)
}
}
}
async fn did_save(&self, params: DidSaveTextDocumentParams) {
// Re-lint the document after save
// Note: Auto-fixing is now handled by will_save_wait_until which runs before the save
if let Some(entry) = self.documents.read().await.get(¶ms.text_document.uri) {
self.update_diagnostics(params.text_document.uri, entry.content.clone(), true)
.await;
}
}
async fn did_close(&self, params: DidCloseTextDocumentParams) {
// Remove document from storage
self.documents.write().await.remove(¶ms.text_document.uri);
// Drop only this spelling. Another one naming the same file can still be
// open, and it stays reachable under the resolved URI.
let resolved = super::resolve_uri_spelling(¶ms.text_document.uri);
if resolved != params.text_document.uri {
let mut aliases = self.document_aliases.write().await;
if let Some(spellings) = aliases.get_mut(&resolved) {
spellings.retain(|u| u != ¶ms.text_document.uri);
if spellings.is_empty() {
aliases.remove(&resolved);
}
}
}
// Always clear diagnostics on close to ensure cleanup
// (Ruff does this unconditionally as a defensive measure)
self.client
.publish_diagnostics(params.text_document.uri, Vec::new(), None)
.await;
}
async fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {
// Check if any of the changed files are config files
const CONFIG_FILES: &[&str] = &[
".rumdl.toml",
"rumdl.toml",
"pyproject.toml",
".markdownlint.json",
".markdownlint-cli2.jsonc",
".markdownlint-cli2.yaml",
".markdownlint-cli2.yml",
];
let mut config_changed = false;
// An `.editorconfig` supplies settings only while a config opts into
// reading it, so it is a config file here only in a workspace that did.
let reads_editorconfig = self.reads_editorconfig().await;
for change in ¶ms.changes {
// Resolved like every other path the server records, so a watch event
// is comparable with the workspace roots and with the index keys the
// scan produced. A deleted file still resolves: only its directory is.
if let Some(path) = super::resolve_uri(&change.uri) {
let file_name = path.file_name().and_then(|f| f.to_str());
// Handle config file changes
if let Some(name) = file_name
&& (CONFIG_FILES.contains(&name) || (reads_editorconfig && name == ".editorconfig"))
&& !config_changed
{
log::info!("Config file changed: {}, invalidating config cache", path.display());
// Clear the entire config cache when any config file changes.
// Fallback entries (no config_file) become stale when a new config file
// is created, and directory-scoped entries may resolve differently after edits.
let mut cache = self.config_cache.write().await;
cache.clear();
// Also reload the global fallback configuration
drop(cache);
self.reload_configuration().await;
config_changed = true;
}
// Handle markdown file changes for workspace index
if let Some(ext) = path.extension()
&& is_markdown_extension(ext)
{
match change.typ {
FileChangeType::CREATED | FileChangeType::CHANGED => {
// The filesystem does not speak for a document an editor
// holds: what is on disk is the last save, and opening a
// document indexes it whatever discovery says. Re-queue
// the buffer rather than skipping the event, so a file
// deleted and recreated underneath the editor (a branch
// switch) keeps the version the user is looking at. The
// lookup goes through the spelling the server identifies
// documents by, because a watch event words the path the
// way the filesystem does and not the way the editor did.
if let Some(content) = self
.get_open_document_content(&super::resolve_uri_spelling(&change.uri))
.await
{
self.queue_index_update(IndexUpdate::FileChanged {
path: path.clone(),
content,
})
.await;
continue;
}
// Skip files the full scan would ignore (e.g. generated
// output) so filesystem-watch events don't reintroduce
// them.
let roots = self.workspace_roots.read().await.clone();
let (options, includes, excludes) = {
let config = self.rumdl_config.read().await;
(
crate::lsp::index_worker::index_walk_options(&config),
config.global.include.clone(),
ExcludeMatchers::new(&config.global.exclude),
)
};
if crate::lsp::index_worker::path_is_ignored_for_index(
&roots, &path, &options, &includes, &excludes,
) {
// A file that was indexed before an ignore rule began
// matching it (e.g. just added to .gitignore) must be
// evicted so completions and navigation stop surfacing
// it. The message is a no-op when it was never indexed.
self.queue_index_update(IndexUpdate::FileRemoved { path: path.clone() })
.await;
continue;
}
// Read file content and update index
if let Ok(content) = tokio::fs::read_to_string(&path).await {
self.queue_index_update(IndexUpdate::FileChanged {
path: path.clone(),
content,
})
.await;
}
}
FileChangeType::DELETED => {
self.queue_index_update(IndexUpdate::FileRemoved { path: path.clone() })
.await;
}
_ => {}
}
}
}
}
// Re-lint all open documents if config changed
if config_changed {
// Rebuild the workspace index: discovery-relevant settings
// (exclude patterns, respect_gitignore) may have changed, and the
// scan reads them from the shared config.
if !self.queue_index_update(IndexUpdate::FullRescan).await {
log::warn!("Failed to request workspace rescan after config change");
}
let docs_to_update: Vec<(Url, String)> = {
let docs = self.documents.read().await;
docs.iter()
.filter(|(_, entry)| !entry.from_disk)
.map(|(uri, entry)| (uri.clone(), entry.content.clone()))
.collect()
};
for (uri, text) in docs_to_update {
self.update_diagnostics(uri, text, true).await;
}
}
}
async fn code_action(&self, params: CodeActionParams) -> JsonRpcResult<Option<CodeActionResponse>> {
let uri = params.text_document.uri;
let range = params.range;
let requested_kinds = params.context.only;
if let Some(text) = self.get_document_content(&uri).await {
match self.get_code_actions(&uri, &text, range).await {
Ok(actions) => {
// Filter actions by requested kinds (if specified and non-empty)
// LSP spec: "If provided with no kinds, all supported kinds are returned"
// LSP code action kinds are hierarchical: source.fixAll.rumdl matches source.fixAll
let filtered_actions = if let Some(ref kinds) = requested_kinds
&& !kinds.is_empty()
{
actions
.into_iter()
.filter(|action| {
action.kind.as_ref().is_some_and(|action_kind| {
let action_kind_str = action_kind.as_str();
kinds.iter().any(|requested| {
let requested_str = requested.as_str();
// Match if action kind starts with requested kind
// e.g., "source.fixAll.rumdl" matches "source.fixAll"
action_kind_str.starts_with(requested_str)
})
})
})
.collect()
} else {
actions
};
let response: Vec<CodeActionOrCommand> = filtered_actions
.into_iter()
.map(CodeActionOrCommand::CodeAction)
.collect();
Ok(Some(response))
}
Err(e) => {
log::error!("Failed to get code actions: {e}");
Ok(None)
}
}
} else {
Ok(None)
}
}
async fn range_formatting(&self, params: DocumentRangeFormattingParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
// For markdown linting, we format the entire document because:
// 1. Many markdown rules have document-wide implications (e.g., heading hierarchy, list consistency)
// 2. Fixes often need surrounding context to be applied correctly
// 3. This approach is common among linters (ESLint, rustfmt, etc. do similar)
log::debug!(
"Range formatting requested for {:?}, formatting entire document due to rule interdependencies",
params.range
);
let formatting_params = DocumentFormattingParams {
text_document: params.text_document,
options: params.options,
work_done_progress_params: params.work_done_progress_params,
};
self.formatting(formatting_params).await
}
async fn formatting(&self, params: DocumentFormattingParams) -> JsonRpcResult<Option<Vec<TextEdit>>> {
let uri = params.text_document.uri;
let options = params.options;
log::debug!("Formatting request for: {uri}");
log::debug!(
"FormattingOptions: insert_final_newline={:?}, trim_final_newlines={:?}, trim_trailing_whitespace={:?}",
options.insert_final_newline,
options.trim_final_newlines,
options.trim_trailing_whitespace
);
if let Some(text) = self.get_document_content(&uri).await {
// Phase 1: Apply lint rule fixes, iterating to a fixpoint through the
// same `FixCoordinator` engine as `rumdl check --fix` and the editor's
// fix-all action. A single fix pass can leave cascading fixes
// unapplied — e.g. MD030 widening a list marker, which then requires
// MD007 to re-indent the nested content and its continuation lines —
// which forced "Format Document" to be run several times to converge
// (rvben/rumdl-vscode#145). `apply_all_fixes` also handles config
// resolution, rule filtering, LSP overrides and excludes for the URI.
let mut result = match self.apply_all_fixes(&uri, &text).await {
Ok(Some(fixed)) => fixed,
Ok(None) => text.clone(),
Err(e) => {
log::error!("Failed to apply fixes during formatting: {e}");
text.clone()
}
};
// Phase 2: Apply FormattingOptions (standard LSP behavior)
// This ensures we respect editor preferences even if lint rules don't catch everything
result = Self::apply_formatting_options(result, &options);
// Return edit if content changed
if result != text {
log::debug!("Returning formatting edits");
let end_position = self.get_end_position(&text);
let edit = TextEdit {
range: Range {
start: Position { line: 0, character: 0 },
end: end_position,
},
new_text: result,
};
return Ok(Some(vec![edit]));
}
Ok(Some(Vec::new()))
} else {
log::warn!("Document not found: {uri}");
Ok(None)
}
}
async fn goto_definition(&self, params: GotoDefinitionParams) -> JsonRpcResult<Option<GotoDefinitionResponse>> {
if !self.config.read().await.enable_link_navigation {
return Ok(None);
}
let uri = params.text_document_position_params.text_document.uri;
let position = params.text_document_position_params.position;
log::debug!("Go-to-definition at {uri} {}:{}", position.line, position.character);
Ok(self.handle_goto_definition(&uri, position).await)
}
async fn references(&self, params: ReferenceParams) -> JsonRpcResult<Option<Vec<Location>>> {
if !self.config.read().await.enable_link_navigation {
return Ok(None);
}
let uri = params.text_document_position.text_document.uri;
let position = params.text_document_position.position;
log::debug!("Find references at {uri} {}:{}", position.line, position.character);
Ok(self.handle_references(&uri, position).await)
}
async fn hover(&self, params: HoverParams) -> JsonRpcResult<Option<Hover>> {
if !self.config.read().await.enable_link_navigation {
return Ok(None);
}
let uri = params.text_document_position_params.text_document.uri;
let position = params.text_document_position_params.position;
log::debug!("Hover at {uri} {}:{}", position.line, position.character);
Ok(self.handle_hover(&uri, position).await)
}
async fn prepare_rename(&self, params: TextDocumentPositionParams) -> JsonRpcResult<Option<PrepareRenameResponse>> {
if !self.config.read().await.enable_link_navigation {
return Ok(None);
}
let uri = params.text_document.uri;
let position = params.position;
log::debug!("Prepare rename at {uri} {}:{}", position.line, position.character);
Ok(self.handle_prepare_rename(&uri, position).await)
}
async fn rename(&self, params: RenameParams) -> JsonRpcResult<Option<WorkspaceEdit>> {
if !self.config.read().await.enable_link_navigation {
return Ok(None);
}
let uri = params.text_document_position.text_document.uri;
let position = params.text_document_position.position;
let new_name = params.new_name;
log::debug!("Rename at {uri} {}:{} → {new_name}", position.line, position.character);
Ok(self.handle_rename(&uri, position, &new_name).await)
}
async fn diagnostic(&self, params: DocumentDiagnosticParams) -> JsonRpcResult<DocumentDiagnosticReportResult> {
let uri = params.text_document.uri;
if let Some(text) = self.get_open_document_content(&uri).await {
match self.lint_document(&uri, &text, true).await {
Ok(diagnostics) => Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
RelatedFullDocumentDiagnosticReport {
related_documents: None,
full_document_diagnostic_report: FullDocumentDiagnosticReport {
result_id: None,
items: diagnostics,
},
},
))),
Err(e) => {
log::error!("Failed to get diagnostics: {e}");
Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
RelatedFullDocumentDiagnosticReport {
related_documents: None,
full_document_diagnostic_report: FullDocumentDiagnosticReport {
result_id: None,
items: Vec::new(),
},
},
)))
}
}
} else {
Ok(DocumentDiagnosticReportResult::Report(DocumentDiagnosticReport::Full(
RelatedFullDocumentDiagnosticReport {
related_documents: None,
full_document_diagnostic_report: FullDocumentDiagnosticReport {
result_id: None,
items: Vec::new(),
},
},
)))
}
}
async fn document_symbol(&self, params: DocumentSymbolParams) -> JsonRpcResult<Option<DocumentSymbolResponse>> {
if !self.config.read().await.enable_symbols {
return Ok(None);
}
let uri = params.text_document.uri;
let Some(text) = self.get_document_content(&uri).await else {
return Ok(None);
};
let flavor = self.resolve_flavor_for_uri(&uri).await;
let ctx = crate::lint_context::LintContext::new(&text, flavor, None);
if *self.client_supports_hierarchical_symbols.read().await {
let symbols = super::symbols::document_symbols(&ctx);
Ok((!symbols.is_empty()).then_some(DocumentSymbolResponse::Nested(symbols)))
} else {
let symbols = super::symbols::document_symbols_flat(&ctx, &uri);
Ok((!symbols.is_empty()).then_some(DocumentSymbolResponse::Flat(symbols)))
}
}
async fn symbol(&self, params: WorkspaceSymbolParams) -> JsonRpcResult<Option<Vec<SymbolInformation>>> {
if !self.config.read().await.enable_symbols {
return Ok(None);
}
let query = params.query.to_lowercase();
let index = self.workspace_index.read().await;
let symbols = super::symbols::workspace_symbols(&index, &query);
Ok(if symbols.is_empty() { None } else { Some(symbols) })
}
}
#[cfg(test)]
#[path = "tests.rs"]
mod tests;