Skip to content

Commit e3f12c5

Browse files
authored
refactor: pass around &str instead of &[u8] (#20)
Closes #10.
2 parents 4975df5 + 72e2293 commit e3f12c5

File tree

20 files changed

+65
-87
lines changed

20 files changed

+65
-87
lines changed

src/helpers/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ impl<'src> QueryHelper<'src> {
5050
///
5151
/// Panics if Tree-sitter fails to parse the query.
5252
#[must_use]
53-
pub fn new(query_src: &str, tree: &'src Tree, code: &'src [u8]) -> Self {
53+
pub fn new(query_src: &str, tree: &'src Tree, code: &'src str) -> Self {
5454
let query =
5555
Query::new(&tree_sitter_c::LANGUAGE.into(), query_src).expect("Failed to parse query");
56-
Self { query, tree, code }
56+
Self {
57+
query,
58+
tree,
59+
code: code.as_bytes(),
60+
}
5761
}
5862

5963
/// Returns a reference to this helper's query.
@@ -222,7 +226,7 @@ impl<'src> QueryHelper<'src> {
222226
/// - the node's text is not valid UTF-8
223227
///
224228
#[must_use]
225-
pub fn function_definition_name<'code>(node: Node, code: &'code [u8]) -> &'code str {
229+
pub fn function_definition_name<'code>(node: Node, code: &'code str) -> &'code str {
226230
assert_eq!(
227231
"function_definition",
228232
node.kind(),
@@ -235,7 +239,7 @@ pub fn function_definition_name<'code>(node: Node, code: &'code [u8]) -> &'code
235239
.child_by_field_name("declarator")
236240
.expect("Expected node to have a `declarator' field");
237241
}
238-
node.utf8_text(code).expect("Code is not valid UTF-8")
242+
&code[node.byte_range()]
239243
}
240244

241245
/// Gets the number of columns by which this line is indented. Tab characters (U+0009 or `'\t'`)
@@ -434,14 +438,10 @@ mod test {
434438
let mut parser = Parser::new();
435439
parser.set_language(&tree_sitter_c::LANGUAGE.into()).unwrap();
436440
let tree = parser.parse(code.as_bytes(), None).unwrap();
437-
let helper =
438-
QueryHelper::new("(function_definition) @function", &tree, code.as_bytes());
441+
let helper = QueryHelper::new("(function_definition) @function", &tree, code);
439442
helper.for_each_capture(|label, capture| {
440443
assert_eq!("function", label);
441-
assert_eq!(
442-
expected_name,
443-
super::function_definition_name(capture.node, code.as_bytes())
444-
);
444+
assert_eq!(expected_name, super::function_definition_name(capture.node, code));
445445
});
446446
}
447447
}
@@ -478,7 +478,7 @@ mod test {
478478
let mut parser = Parser::new();
479479
parser.set_language(&tree_sitter_c::LANGUAGE.into()).unwrap();
480480
let tree = parser.parse(code.as_bytes(), None).unwrap();
481-
let helper = QueryHelper::new("(preproc_def) @define", &tree, code.as_bytes());
481+
let helper = QueryHelper::new("(preproc_def) @define", &tree, code);
482482
helper.for_each_capture(|_label, capture| ranges.push(capture.node.range()));
483483
let mut collapser = RangeCollapser::from(ranges.clone());
484484
let group1 = collapser.next().unwrap();

src/helpers/testing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub fn test_captures(query: &str, input: &str) -> ExitCode {
113113
let mut parser = Parser::new();
114114
parser.set_language(&tree_sitter_c::LANGUAGE.into()).unwrap();
115115
let tree = parser.parse(&code, None).unwrap();
116-
let helper = QueryHelper::new(query, &tree, code.as_bytes());
116+
let helper = QueryHelper::new(query, &tree, &code);
117117
let mut failed = false;
118118
helper.for_each_capture(|label, capture| {
119119
let start = capture.node.start_position();

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn main() -> ExitCode {
146146
// Do checks
147147
let rules: Vec<Box<dyn Rule>> = crate::rules::get_rules();
148148
for rule in rules {
149-
let diagnostics = rule.check(&tree, code.as_bytes());
149+
let diagnostics = rule.check(&tree, &code);
150150
for diagnostic in diagnostics {
151151
match cli.format {
152152
OutputFormat::Pretty => {

src/rules/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ pub trait Rule {
2727
/// - `tree`: [`Tree`] representing the file.
2828
/// - `code`: Text/code of the given file.
2929
#[must_use]
30-
fn check(&self, tree: &Tree, code: &[u8]) -> Vec<Diagnostic<()>>;
30+
fn check(&self, tree: &Tree, code: &str) -> Vec<Diagnostic<()>>;
3131
}

src/rules/rule01a.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const QUERY_STR: &str = indoc! { /* query */ r#"
6565
pub struct Rule01a {}
6666

6767
impl Rule for Rule01a {
68-
fn check(&self, tree: &Tree, code: &[u8]) -> Vec<Diagnostic<()>> {
68+
fn check(&self, tree: &Tree, code: &str) -> Vec<Diagnostic<()>> {
6969
let helper = QueryHelper::new(QUERY_STR, tree, code);
7070
let mut diagnostics = Vec::new();
7171
helper.for_each_capture(|_label, capture| {
@@ -86,9 +86,7 @@ impl Rule for Rule01a {
8686
)
8787
.with_label(Label::secondary((), capture.node.byte_range()).with_message(format!(
8888
"Perhaps you meant `{}'",
89-
guess_lower_snake_case(
90-
capture.node.utf8_text(code).expect("Code is not valid UTF-8")
91-
)
89+
guess_lower_snake_case(&code[capture.node.byte_range()])
9290
)));
9391
diagnostics.push(diagnostic);
9492
});

src/rules/rule01b.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use crate::rules::api::Rule;
4646
pub struct Rule01b {}
4747

4848
impl Rule for Rule01b {
49-
fn check(&self, _tree: &Tree, _code: &[u8]) -> Vec<Diagnostic<()>> {
49+
fn check(&self, _tree: &Tree, _code: &str) -> Vec<Diagnostic<()>> {
5050
Vec::with_capacity(0)
5151
}
5252
}

src/rules/rule01c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ const QUERY_STR: &str = indoc! { /* query */ r#"
6666
pub struct Rule01c {}
6767

6868
impl Rule for Rule01c {
69-
fn check(&self, tree: &Tree, code: &[u8]) -> Vec<Diagnostic<()>> {
69+
fn check(&self, tree: &Tree, code: &str) -> Vec<Diagnostic<()>> {
7070
let helper = QueryHelper::new(QUERY_STR, tree, code);
7171
let mut diagnostics = Vec::new();
7272
helper.for_each_capture(|name: &str, capture: QueryCapture| {
73-
let node_text = capture.node.utf8_text(code).expect("Code is not valid UTF-8");
73+
let node_text = &code[capture.node.byte_range()];
7474
let (message, label, fix) = match name {
7575
"constant.name.short" => (
7676
"Constant name must contain at least 2 characters",

src/rules/rule01d.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const QUERY_STR: &str = indoc! {
6464
pub struct Rule01d {}
6565

6666
impl Rule for Rule01d {
67-
fn check(&self, tree: &Tree, code: &[u8]) -> Vec<Diagnostic<()>> {
67+
fn check(&self, tree: &Tree, code: &str) -> Vec<Diagnostic<()>> {
6868
let helper = QueryHelper::new(QUERY_STR, tree, code);
6969
let mut first_function_position = None;
7070
let mut diagnostics = Vec::new();
@@ -90,10 +90,7 @@ impl Rule for Rule01d {
9090
.with_message("Variable declared here"),
9191
)
9292
.with_label(Label::secondary((), capture.node.byte_range()).with_message(
93-
format!(
94-
"Perhaps you meant `g_{}'",
95-
capture.node.utf8_text(code).expect("Code is not valid UTF-8")
96-
),
93+
format!("Perhaps you meant `g_{}'", &code[capture.node.byte_range()]),
9794
))
9895
}
9996
"declaration.top_level" => {

src/rules/rule02a.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,10 @@ const QUERY_STR: &str = indoc! { /* query */ r##"
106106
"## };
107107

108108
impl Rule for Rule02a {
109-
fn check(&self, tree: &Tree, code_bytes: &[u8]) -> Vec<Diagnostic<()>> {
109+
fn check(&self, tree: &Tree, code: &str) -> Vec<Diagnostic<()>> {
110110
let mut diagnostics = Vec::new();
111111

112112
// Check for lines >80 columns long
113-
let code = std::str::from_utf8(code_bytes).expect("Code is not valid UTF-8");
114113
for (line, index) in LinesWithPosition::from(code) {
115114
let width = line_width(line);
116115
if width > 80 {
@@ -122,7 +121,7 @@ impl Rule for Rule02a {
122121
}
123122
}
124123

125-
let helper = QueryHelper::new(QUERY_STR, tree, code_bytes);
124+
let helper = QueryHelper::new(QUERY_STR, tree, code);
126125
let splittable_capture_i = helper.expect_index_for_capture("splittable");
127126
let splittable_begin_capture_i = helper.expect_index_for_capture("splittable.begin");
128127
let splittable_end_capture_i = helper.expect_index_for_capture("splittable.end");
@@ -157,9 +156,7 @@ impl Rule for Rule02a {
157156
}
158157

159158
// Check indentation of wrapped lines and construct list of labels
160-
let mut code_lines = LinesWithPosition::from(
161-
std::str::from_utf8(code_bytes).expect("Code is not valid UTF-8"),
162-
)
159+
let mut code_lines = LinesWithPosition::from(code)
163160
.skip(range.start_point.row)
164161
.take(range.end_point.row + 1 - range.start_point.row);
165162
let (first_line, first_line_byte_pos) = code_lines.next().unwrap();
@@ -337,7 +334,7 @@ mod tests {
337334
code.push_str("}\n");
338335
dbg!(&code);
339336
let tree = parser.parse(code.as_bytes(), None).unwrap();
340-
let diagnostics = rule.check(&tree, code.as_bytes());
337+
let diagnostics = rule.check(&tree, &code);
341338
assert_eq!($ndiag, diagnostics.len());
342339
let nlabels_list: &[usize] = &$nlabels_list;
343340
assert_eq!(

src/rules/rule02b.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const QUERY_STR: &str = indoc! {
5353
pub struct Rule02b {}
5454

5555
impl Rule for Rule02b {
56-
fn check(&self, tree: &Tree, code: &[u8]) -> Vec<Diagnostic<()>> {
56+
fn check(&self, tree: &Tree, code: &str) -> Vec<Diagnostic<()>> {
5757
let helper = QueryHelper::new(QUERY_STR, tree, code);
5858
let mut diagnostics = Vec::new();
5959
helper.for_each_capture(|label: &str, capture: QueryCapture| match label {
@@ -110,7 +110,7 @@ mod tests {
110110
let tree = parser.parse(code.as_bytes(), None).unwrap();
111111
let rule02b = Rule02b {};
112112
assert_eq!(
113-
rule02b.check(&tree, code.as_bytes()),
113+
rule02b.check(&tree, &code),
114114
vec![Diagnostic::warning()
115115
.with_code("II:B")
116116
.with_message(format!(

0 commit comments

Comments
 (0)