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
|
use crate::Next;
use crate::linter::mlint_report::{
LintSpan, LintSpanLine, LintSuggestion, MlintLevel, MlintReport, StateLintReports,
};
use mingling::Routable;
use mingling::macros::{buffer, chain, dispatcher, pack, r_eprintln, renderer, routeify};
use mingling::res::{ResCurrentDir, ResExitCode};
use std::ops::Range;
use std::path::PathBuf;
const OVERRIDE_KEY: &str = "check.overrideCommand";
const EXPECTED_LINE: &str = r#"check.overrideCommand = ["mling", "ra-lint-check"]"#;
const VALID_FIRST: &[&str] = &["mling", "mingling-cli"];
const RA_CONFIG_TEMPLATE: &str = include_str!("../../tmpls/rust-analyzer.toml");
// Rust-analyzer TOML section
const RA_TABLE: &str = "rust-analyzer";
// Key names
const KEY_CHECK_ON_SAVE: &str = "checkOnSave";
const VALUE_CHECK_ON_SAVE_TRUE: &str = "true";
const DISPLAY_CHECK_ON_SAVE_TRUE: &str = "checkOnSave = true";
// File names
const SOURCE_FILE_NAME: &str = "rust-analyzer.toml";
const MSG_ALREADY_CORRECT: &str = "`.rust-analyzer.toml` already has the correct mling settings";
const MSG_NON_EMPTY_ARRAY: &str = "`check.overrideCommand`: expected a non-empty array";
const MSG_FIRST_ARG_INVALID: &str =
"`check.overrideCommand`: first argument should be `mling` or `mingling-cli`";
const MSG_MISSING_SECOND: &str = "`check.overrideCommand`: missing second argument";
const MSG_SECOND_ARG_INVALID: &str =
"`check.overrideCommand`: second argument should be a `ra-lint-*` subcommand or `lint`";
const MSG_MESSAGE_FORMAT_REQUIRED: &str =
"`check.overrideCommand`: `lint` subcommand needs `--message-format=json`";
// Suggestions / replacements
const SUGGEST_RA_LINT_CHECK_ARRAY: &str = r#"["mling", "ra-lint-check"]"#;
const SUGGEST_MLING_QUOTED: &str = r#""mling""#;
const SUGGEST_RA_LINT_CHECK_QUOTED: &str = r#""ra-lint-check""#;
const SUGGEST_MESSAGE_FORMAT_JSON: &str = ", \"--message-format=json\"]";
// Subcommand constants
const SUB_CMD_LINT: &str = "lint";
const MESSAGE_FORMAT_FLAG: &str = "--message-format=json";
dispatcher!("lint-install", CMDLintInstall => EntryLintInstall);
pack!(StateWriteMlingLinterConfig = PathBuf);
pack!(StateSuggestMlingLinterSetup = ());
pack!(ResultMlingLinterConfigInstalled = PathBuf);
#[chain]
pub fn handle_lint_install(_: EntryLintInstall, current_dir: &ResCurrentDir) -> Next {
let cfg_file_path = current_dir.join(SOURCE_FILE_NAME);
if !cfg_file_path.exists() {
return StateWriteMlingLinterConfig::new(cfg_file_path).to_chain();
}
StateSuggestMlingLinterSetup::new(()).to_chain()
}
#[chain(routeify)]
pub fn handle_state_write_mling_linter_config(prev: StateWriteMlingLinterConfig) -> Next {
let cfg_file_path = prev.inner;
std::fs::write(&cfg_file_path, RA_CONFIG_TEMPLATE)?;
ResultMlingLinterConfigInstalled::new(cfg_file_path).into()
}
#[renderer(buffer)]
pub fn render_mling_linter_config_installed(result: ResultMlingLinterConfigInstalled) {
let cfg_file_path = result.inner;
r_eprintln!(
"info: created `{}` with mling lint-integrated settings",
cfg_file_path.display()
);
}
#[chain]
pub fn handle_state_suggest_mling_linter_setup(
_: StateSuggestMlingLinterSetup,
current_dir: &ResCurrentDir,
ec: &mut ResExitCode,
) -> StateLintReports {
ec.exit_code = 1;
let cfg_file_path = current_dir.join(SOURCE_FILE_NAME);
let file_name = cfg_file_path.to_string_lossy().to_string();
let content = match std::fs::read_to_string(&cfg_file_path) {
Ok(c) => c,
Err(e) => {
return StateLintReports::new(vec![MlintReport {
level: MlintLevel::Error,
message: format!("failed to read `{file_name}`: {e}"),
..Default::default()
}]);
}
};
let mut reports: Vec<MlintReport> = vec![];
reports.extend(check_simple_key_in_section(
&content,
KEY_CHECK_ON_SAVE,
VALUE_CHECK_ON_SAVE_TRUE,
DISPLAY_CHECK_ON_SAVE_TRUE,
SOURCE_FILE_NAME,
RA_TABLE,
));
reports.extend(check_override_command_in_section(
&content,
SOURCE_FILE_NAME,
RA_TABLE,
));
if reports.is_empty() {
reports.push(MlintReport {
level: MlintLevel::Note,
message: MSG_ALREADY_CORRECT.to_string(),
..Default::default()
});
}
StateLintReports::new(reports)
}
/// A `MlintReport` at `Help` level with the given message, file, and source.
fn report_help(file_name: &str, source_code: &str, message: String) -> MlintReport {
MlintReport {
file_name: file_name.to_string(),
source_code: source_code.to_string(),
level: MlintLevel::Help,
message,
..Default::default()
}
}
/// Attach a single-line span + replace suggestion to a report.
fn with_replace_suggestion(
report: MlintReport,
line: usize,
line_text: &str,
byte_range: Range<usize>,
replacement: String,
label: Option<String>,
) -> MlintReport {
let span = LintSpan {
line_start: line,
line_end: line,
column_start: byte_range.start + 1,
column_end: byte_range.end + 1,
text: vec![LintSpanLine {
text: line_text.to_string(),
highlight_start: byte_range.start + 1,
highlight_end: byte_range.end + 1,
}],
label,
};
let suggestion = LintSuggestion {
source: line_text.to_string(),
line_start: line,
byte_range,
replacement,
};
MlintReport {
spans: vec![span],
suggestions: vec![suggestion],
..report
}
}
/// Attach an "insert new content" suggestion (byte_range 0..0) to a report.
fn with_insert_suggestion(report: MlintReport, line: usize, new_content: String) -> MlintReport {
let suggestion = LintSuggestion {
source: new_content.clone(),
line_start: line,
byte_range: 0..0,
replacement: new_content,
};
MlintReport {
suggestions: vec![suggestion],
..report
}
}
/// Result of looking up a key=value pair in TOML content.
type FoundKey = Option<(usize, String)>;
/// Search for a key=value pair within a TOML section (e.g. `[rust-analyzer]`).
///
/// Scans the content between `[section_name]` (and its child tables like
/// `[section_name.check]`) and the next sibling section.
///
/// `dotted_key` can be a simple name like `"checkOnSave"` or a dotted path
/// like `"check.overrideCommand"`. In the latter case it matches both the
/// dotted form (`check.overrideCommand = ...`) and the bare form inside a
/// child table (`overrideCommand = ...` under `[rust-analyzer.check]`).
fn find_key_in_section(content: &str, dotted_key: &str, section_name: &str) -> FoundKey {
let parts: Vec<&str> = dotted_key.split('.').collect();
let field = parts.last().copied().unwrap_or(dotted_key);
let section_header = format!("[{section_name}]");
let mut in_section = false;
for (i, line) in content.lines().enumerate() {
let trimmed = line.trim();
if trimmed.starts_with('[') && trimmed.ends_with(']') {
let header = &trimmed[1..trimmed.len() - 1];
if trimmed == section_header {
in_section = true;
continue;
}
if in_section {
// Child table like [rust-analyzer.check] stays within section
if header.starts_with(&format!("{section_name}.")) {
continue;
}
// Any other table means the section has ended
in_section = false;
continue;
}
continue;
}
if !in_section {
continue;
}
let without_comment = trimmed.split('#').next().unwrap_or("").trim();
if without_comment.is_empty() {
continue;
}
if let Some(eq_pos) = without_comment.find('=') {
let k = without_comment[..eq_pos].trim();
let v = without_comment[eq_pos + 1..].trim();
// Match both dotted key (check.overrideCommand) and bare field (overrideCommand)
if k == dotted_key || k == field {
return Some((i + 1, v.to_string()));
}
}
}
None
}
/// Find the 1-based line number of a TOML section header like `[section]`.
fn find_section_header(content: &str, section_name: &str) -> Option<usize> {
let target = format!("[{section_name}]");
content
.lines()
.position(|line| line.trim() == target)
.map(|i| i + 1)
}
/// Find the last 1-based line number *within* a TOML section.
///
/// Returns the last content line (including blank lines) before the next
/// sibling section begins, or `None` if `[section_name]` is not found.
fn find_section_last_line(content: &str, section_name: &str) -> Option<usize> {
let section_header = format!("[{section_name}]");
let mut in_section = false;
let mut last = None;
for (i, line) in content.lines().enumerate() {
let trimmed = line.trim();
if trimmed.starts_with('[') && trimmed.ends_with(']') {
let header = &trimmed[1..trimmed.len() - 1];
if trimmed == section_header {
in_section = true;
continue;
}
if in_section {
if header.starts_with(&format!("{section_name}.")) {
// Child table, still in section
last = Some(i + 1);
continue;
}
// A sibling section means the end of the current section
return last;
}
continue;
}
if in_section {
last = Some(i + 1);
}
}
last
}
/// Find the TOML table header whose dotted path shares the longest common
/// prefix with `expected_path`.
///
/// Returns `(line_number, matched_prefix, remaining_suffix)`:
/// - `line_number`: 1-based line of the best-match header (0 if none)
/// - `matched_prefix`: path segments that matched (e.g. `["rust-analyzer"]`)
/// - `remaining_suffix`: path segments not yet matched (e.g. `["check"]`)
fn find_longest_toml_header<'a>(
content: &str,
expected_path: &[&'a str],
) -> (usize, Vec<&'a str>, Vec<&'a str>) {
let mut best_match_len = 0usize;
let mut best_line = 0usize;
for (i, line) in content.lines().enumerate() {
let trimmed = line.trim();
if !trimmed.starts_with('[') || !trimmed.ends_with(']') {
continue;
}
let header = &trimmed[1..trimmed.len() - 1];
let header_parts: Vec<&str> = header.split('.').map(|s| s.trim()).collect();
// Count how many leading segments of header_parts match expected_path
let match_len = header_parts
.iter()
.zip(expected_path.iter())
.take_while(|(h, e)| *h == *e)
.count();
// Only consider matches that don't exceed expected_path
if match_len > best_match_len && match_len <= expected_path.len() {
best_match_len = match_len;
best_line = i + 1;
}
}
let matched = expected_path[..best_match_len].to_vec();
let remaining = expected_path[best_match_len..].to_vec();
(best_line, matched, remaining)
}
/// Decide the best insert position and content for adding `check.overrideCommand`
/// inside a TOML section (typically `rust-analyzer`).
///
/// Scans all `[table]` headers matching the expected path `[section_name, "check"]`
/// and picks the longest prefix match:
/// - Full match (`[rust-analyzer.check]`) → bare key `overrideCommand = [...]` inside it.
/// - Partial match (`[rust-analyzer]` only) → dotted key `check.overrideCommand = [...]` inside it.
/// - No match → create the full table hierarchy at end of file.
fn build_override_insert(content: &str, section_name: &str) -> (usize, String) {
// Full config line: check.overrideCommand = ["mling", "ra-lint-check"]
let value = r#"["mling", "ra-lint-check"]"#;
// Expected TOML path segments: e.g. ["rust-analyzer", "check"]
let expected_path: Vec<&str> = vec![section_name, "check"];
let dotted_key = "check.overrideCommand";
let bare_key = "overrideCommand";
let (_line, matched, remaining) = find_longest_toml_header(content, &expected_path);
if matched.is_empty() {
// No matching table at all — create full hierarchy at end
let total_lines = content.lines().count().max(1);
(
total_lines + 1,
format!("\n[{section_name}]\n{dotted_key} = {value}\n"),
)
} else if remaining.is_empty() {
// Full match — e.g. [rust-analyzer.check] exists, insert bare key
let table_name = matched.join(".");
let section_end = find_section_last_line(content, &table_name)
.unwrap_or_else(|| content.lines().count().max(1));
(section_end + 1, format!("{bare_key} = {value}\n"))
} else {
// Partial match — e.g. only [rust-analyzer] exists, insert dotted key
let table_name = matched.join(".");
let section_end = find_section_last_line(content, &table_name)
.unwrap_or_else(|| content.lines().count().max(1));
(section_end + 1, format!("{dotted_key} = {value}\n"))
}
}
/// Check a simple key=value pair inside a TOML section.
fn check_simple_key_in_section(
content: &str,
key: &str,
expected_val: &str,
display_line: &str,
source_file: &str,
section_name: &str,
) -> Vec<MlintReport> {
let found = find_key_in_section(content, key, section_name);
let matches = found
.as_ref()
.is_some_and(|(_, v)| collapse_whitespace(v) == collapse_whitespace(expected_val));
if matches {
return vec![];
}
let msg = format!("expected `{display_line}` in `[{section_name}]` in `rust-analyzer.toml`");
let report = report_help(source_file, content, msg);
match found {
Some((ln, val)) => {
let line_text = nth_line(content, ln);
let byte_start = line_text.find(&val).unwrap_or(0);
let byte_end = byte_start + val.len();
vec![with_replace_suggestion(
report,
ln,
&line_text,
byte_start..byte_end,
expected_val.to_string(),
Some(format!("expected {expected_val}")),
)]
}
None => {
let insert_line = find_section_header(content, section_name)
.map(|h| h + 1)
.unwrap_or_else(|| content.lines().count().max(1) + 1);
let new_content = format!("{display_line}\n");
vec![with_insert_suggestion(report, insert_line, new_content)]
}
}
}
/// Check `check.overrideCommand` inside the given TOML section.
fn check_override_command_in_section(
content: &str,
source_file: &str,
section_name: &str,
) -> Vec<MlintReport> {
let mut reports = Vec::new();
let Some((ln, val)) = find_key_in_section(content, OVERRIDE_KEY, section_name) else {
// Setting entirely missing — build smart insert suggestion
let report = report_help(
source_file,
content,
format!("expected `{EXPECTED_LINE}` in `[{section_name}]` in `rust-analyzer.toml`"),
);
let (insert_line, new_content) = build_override_insert(content, section_name);
reports.push(with_insert_suggestion(report, insert_line, new_content));
return reports;
};
let line_text = nth_line(content, ln);
let args = parse_array_items(&val);
// First: must be `mling` or `mingling-cli`
if !args
.first()
.is_some_and(|a| VALID_FIRST.contains(&a.as_str()))
{
let Some(first) = args.first() else {
let report = report_help(source_file, content, MSG_NON_EMPTY_ARRAY.into());
reports.push(with_replace_suggestion(
report,
ln,
&line_text,
0..val.len(),
SUGGEST_RA_LINT_CHECK_ARRAY.into(),
None,
));
return reports;
};
let quoted = format!("\"{first}\"");
let byte_start = line_text.find("ed).unwrap_or(0);
let byte_end = byte_start + quoted.len();
let report = report_help(source_file, content, MSG_FIRST_ARG_INVALID.into());
reports.push(with_replace_suggestion(
report,
ln,
&line_text,
byte_start..byte_end,
SUGGEST_MLING_QUOTED.into(),
None,
));
return reports;
}
// Second: must be `ra-lint-*` or `lint`
let Some(second) = args.get(1) else {
let report = report_help(source_file, content, MSG_MISSING_SECOND.into());
reports.push(with_replace_suggestion(
report,
ln,
&line_text,
0..val.len(),
SUGGEST_RA_LINT_CHECK_ARRAY.into(),
None,
));
return reports;
};
if !second.starts_with("ra-lint-") && second != SUB_CMD_LINT {
let quoted = format!("\"{second}\"");
let byte_start = line_text.find("ed).unwrap_or(0);
let byte_end = byte_start + quoted.len();
let report = report_help(source_file, content, MSG_SECOND_ARG_INVALID.into());
reports.push(with_replace_suggestion(
report,
ln,
&line_text,
byte_start..byte_end,
SUGGEST_RA_LINT_CHECK_QUOTED.into(),
None,
));
return reports;
}
// If second arg is `lint`, it must be followed by --message-format=json
if second == SUB_CMD_LINT && !has_message_format_json(&args[2..]) {
let byte_start = line_text
.rfind(']')
.unwrap_or(line_text.len().saturating_sub(1));
let byte_end = byte_start + 1;
let report = report_help(source_file, content, MSG_MESSAGE_FORMAT_REQUIRED.into());
reports.push(with_replace_suggestion(
report,
ln,
&line_text,
byte_start..byte_end,
SUGGEST_MESSAGE_FORMAT_JSON.into(),
None,
));
}
reports
}
fn has_message_format_json(rest: &[String]) -> bool {
rest.contains(&MESSAGE_FORMAT_FLAG.to_string())
|| rest
.windows(2)
.any(|w| w[0] == "--message-format" && w[1] == "json")
}
fn parse_array_items(s: &str) -> Vec<String> {
let s = s.trim();
if !s.starts_with('[') || !s.ends_with(']') {
return vec![];
}
let inner = s[1..s.len() - 1].trim();
if inner.is_empty() {
return vec![];
}
let mut items = Vec::new();
let mut current = String::new();
let mut in_quote = false;
for ch in inner.chars() {
match ch {
'"' => in_quote = !in_quote,
',' if !in_quote => {
let trimmed = current.trim().trim_matches('"').to_string();
if !trimmed.is_empty() {
items.push(trimmed);
}
current.clear();
}
_ => current.push(ch),
}
}
let trimmed = current.trim().trim_matches('"').to_string();
if !trimmed.is_empty() {
items.push(trimmed);
}
items
}
fn nth_line(content: &str, n: usize) -> String {
content
.lines()
.nth(n.saturating_sub(1))
.unwrap_or("")
.to_string()
}
fn collapse_whitespace(s: &str) -> String {
let mut out = String::with_capacity(s.len());
let mut in_space = false;
for ch in s.chars() {
if ch.is_whitespace() {
if !in_space {
out.push(' ');
in_space = true;
}
} else {
out.push(ch);
in_space = false;
}
}
out.trim().to_string()
}
|