diff options
Diffstat (limited to 'bin/src/fix')
-rw-r--r-- | bin/src/fix/all.rs | 21 | ||||
-rw-r--r-- | bin/src/fix/single.rs | 15 |
2 files changed, 25 insertions, 11 deletions
diff --git a/bin/src/fix/all.rs b/bin/src/fix/all.rs index 7e51d16..d7f7fff 100644 --- a/bin/src/fix/all.rs +++ b/bin/src/fix/all.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::borrow::Cow; | 1 | use std::borrow::Cow; |
2 | 2 | ||
3 | use lib::Report; | 3 | use lib::{session::SessionInfo, Report}; |
4 | use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; | 4 | use rnix::{parser::ParseError as RnixParseErr, WalkEvent}; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
@@ -8,7 +8,11 @@ use crate::{ | |||
8 | LintMap, | 8 | LintMap, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | fn collect_fixes(source: &str, lints: &LintMap) -> Result<Vec<Report>, RnixParseErr> { | 11 | fn collect_fixes( |
12 | source: &str, | ||
13 | lints: &LintMap, | ||
14 | sess: &SessionInfo, | ||
15 | ) -> Result<Vec<Report>, RnixParseErr> { | ||
12 | let parsed = rnix::parse(source).as_result()?; | 16 | let parsed = rnix::parse(source).as_result()?; |
13 | 17 | ||
14 | Ok(parsed | 18 | Ok(parsed |
@@ -18,7 +22,7 @@ fn collect_fixes(source: &str, lints: &LintMap) -> Result<Vec<Report>, RnixParse | |||
18 | WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { | 22 | WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { |
19 | rules | 23 | rules |
20 | .iter() | 24 | .iter() |
21 | .filter_map(|rule| rule.validate(&child)) | 25 | .filter_map(|rule| rule.validate(&child, sess)) |
22 | .filter(|report| report.total_suggestion_range().is_some()) | 26 | .filter(|report| report.total_suggestion_range().is_some()) |
23 | .collect::<Vec<_>>() | 27 | .collect::<Vec<_>>() |
24 | }), | 28 | }), |
@@ -57,7 +61,7 @@ fn reorder(mut reports: Vec<Report>) -> Vec<Report> { | |||
57 | impl<'a> Iterator for FixResult<'a> { | 61 | impl<'a> Iterator for FixResult<'a> { |
58 | type Item = FixResult<'a>; | 62 | type Item = FixResult<'a>; |
59 | fn next(&mut self) -> Option<Self::Item> { | 63 | fn next(&mut self) -> Option<Self::Item> { |
60 | let all_reports = collect_fixes(&self.src, self.lints).ok()?; | 64 | let all_reports = collect_fixes(&self.src, self.lints, &self.sess).ok()?; |
61 | if all_reports.is_empty() { | 65 | if all_reports.is_empty() { |
62 | return None; | 66 | return None; |
63 | } | 67 | } |
@@ -78,13 +82,18 @@ impl<'a> Iterator for FixResult<'a> { | |||
78 | src: self.src.clone(), | 82 | src: self.src.clone(), |
79 | fixed, | 83 | fixed, |
80 | lints: self.lints, | 84 | lints: self.lints, |
85 | sess: self.sess, | ||
81 | }) | 86 | }) |
82 | } | 87 | } |
83 | } | 88 | } |
84 | 89 | ||
85 | pub fn all_with<'a>(src: &'a str, lints: &'a LintMap) -> Option<FixResult<'a>> { | 90 | pub fn all_with<'a>( |
91 | src: &'a str, | ||
92 | lints: &'a LintMap, | ||
93 | sess: &'a SessionInfo, | ||
94 | ) -> Option<FixResult<'a>> { | ||
86 | let src = Cow::from(src); | 95 | let src = Cow::from(src); |
87 | let _ = rnix::parse(&src).as_result().ok()?; | 96 | let _ = rnix::parse(&src).as_result().ok()?; |
88 | let initial = FixResult::empty(src, lints); | 97 | let initial = FixResult::empty(src, lints, sess); |
89 | initial.into_iter().last() | 98 | initial.into_iter().last() |
90 | } | 99 | } |
diff --git a/bin/src/fix/single.rs b/bin/src/fix/single.rs index d95cfda..67b6b8f 100644 --- a/bin/src/fix/single.rs +++ b/bin/src/fix/single.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::{borrow::Cow, convert::TryFrom}; | 1 | use std::{borrow::Cow, convert::TryFrom}; |
2 | 2 | ||
3 | use lib::Report; | 3 | use lib::{session::SessionInfo, Report}; |
4 | use rnix::{TextSize, WalkEvent}; | 4 | use rnix::{TextSize, WalkEvent}; |
5 | 5 | ||
6 | use crate::{err::SingleFixErr, fix::Source, utils}; | 6 | use crate::{err::SingleFixErr, fix::Source, utils}; |
@@ -27,7 +27,7 @@ fn pos_to_byte(line: usize, col: usize, src: &str) -> Result<TextSize, SingleFix | |||
27 | } | 27 | } |
28 | } | 28 | } |
29 | 29 | ||
30 | fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> { | 30 | fn find(offset: TextSize, src: &str, sess: &SessionInfo) -> Result<Report, SingleFixErr> { |
31 | // we don't really need the source to form a completely parsed tree | 31 | // we don't really need the source to form a completely parsed tree |
32 | let parsed = rnix::parse(src); | 32 | let parsed = rnix::parse(src); |
33 | let lints = utils::lint_map(); | 33 | let lints = utils::lint_map(); |
@@ -39,7 +39,7 @@ fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> { | |||
39 | WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { | 39 | WalkEvent::Enter(child) => lints.get(&child.kind()).map(|rules| { |
40 | rules | 40 | rules |
41 | .iter() | 41 | .iter() |
42 | .filter_map(|rule| rule.validate(&child)) | 42 | .filter_map(|rule| rule.validate(&child, sess)) |
43 | .find(|report| report.total_suggestion_range().is_some()) | 43 | .find(|report| report.total_suggestion_range().is_some()) |
44 | }), | 44 | }), |
45 | _ => None, | 45 | _ => None, |
@@ -49,10 +49,15 @@ fn find(offset: TextSize, src: &str) -> Result<Report, SingleFixErr> { | |||
49 | .ok_or(SingleFixErr::NoOp) | 49 | .ok_or(SingleFixErr::NoOp) |
50 | } | 50 | } |
51 | 51 | ||
52 | pub fn single(line: usize, col: usize, src: &str) -> Result<SingleFixResult, SingleFixErr> { | 52 | pub fn single<'a, 'b>( |
53 | line: usize, | ||
54 | col: usize, | ||
55 | src: &'a str, | ||
56 | sess: &'b SessionInfo, | ||
57 | ) -> Result<SingleFixResult<'a>, SingleFixErr> { | ||
53 | let mut src = Cow::from(src); | 58 | let mut src = Cow::from(src); |
54 | let offset = pos_to_byte(line, col, &*src)?; | 59 | let offset = pos_to_byte(line, col, &*src)?; |
55 | let report = find(offset, &*src)?; | 60 | let report = find(offset, &*src, &sess)?; |
56 | 61 | ||
57 | report.apply(src.to_mut()); | 62 | report.apply(src.to_mut()); |
58 | 63 | ||