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
|
use std::{borrow::Cow, convert::TryFrom};
use lib::{Report, LINTS};
use rnix::{TextRange, TextSize};
use crate::err::SingleFixErr;
use crate::fix::Source;
pub struct SingleFixResult<'δ> {
pub src: Source<'δ>,
}
fn pos_to_byte(line: usize, col: usize, src: &str) -> Result<TextSize, SingleFixErr> {
let mut byte: TextSize = TextSize::of("");
for (_, l) in src.lines().enumerate().take_while(|(i, _)| i <= &line) {
byte += TextSize::of(l);
}
byte += TextSize::try_from(col).map_err(|_| SingleFixErr::Conversion(col))?;
if usize::from(byte) >= src.len() {
Err(SingleFixErr::OutOfBounds(line, col))
} else {
Ok(byte)
}
}
fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> {
// we don't really need the source to form a completely parsed tree
let parsed = rnix::parse(src);
let elem_at = parsed
.node()
.child_or_token_at_range(TextRange::empty(offset))
.ok_or(SingleFixErr::NoOp)?;
LINTS
.get(&elem_at.kind())
.map(|rules| {
rules
.iter()
.filter_map(|rule| rule.validate(&elem_at))
.filter(|report| report.total_suggestion_range().is_some())
.next()
})
.flatten()
.ok_or(SingleFixErr::NoOp)
}
pub fn single(line: usize, col: usize, src: &str) -> Result<SingleFixResult, SingleFixErr> {
let mut src = Cow::from(src);
let offset = pos_to_byte(line, col, &*src)?;
let report = find(offset, &*src)?;
report.apply(src.to_mut());
Ok(SingleFixResult {
src
})
}
|