Skip to content

Commit ba239ce

Browse files
committed
Make query error line numbers consistently display 1-indexed
1 parent 518916f commit ba239ce

5 files changed

Lines changed: 102 additions & 68 deletions

File tree

‎cli/src/loader.rs‎

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::time::SystemTime;
1313
use std::{fs, mem};
1414
use tree_sitter::{Language, QueryError};
1515
use tree_sitter_highlight::HighlightConfiguration;
16-
use tree_sitter_tags::TagsConfiguration;
16+
use tree_sitter_tags::{Error as TagsError, TagsConfiguration};
1717

1818
#[cfg(unix)]
1919
const DYLIB_EXTENSION: &'static str = "so";
@@ -544,25 +544,8 @@ impl Loader {
544544

545545
impl<'a> LanguageConfiguration<'a> {
546546
pub fn highlight_config(&self, language: Language) -> Result<Option<&HighlightConfiguration>> {
547-
fn include_path_in_error<'a>(
548-
mut error: QueryError,
549-
ranges: &'a Vec<(String, Range<usize>)>,
550-
source: &str,
551-
start_offset: usize,
552-
) -> (&'a str, QueryError) {
553-
let offset = error.offset - start_offset;
554-
let (path, range) = ranges
555-
.iter()
556-
.find(|(_, range)| range.contains(&offset))
557-
.unwrap();
558-
error.row = source[range.start..offset]
559-
.chars()
560-
.filter(|c| *c == '\n')
561-
.count();
562-
(path.as_ref(), error)
563-
}
564-
565-
self.highlight_config
547+
return self
548+
.highlight_config
566549
.get_or_try_init(|| {
567550
let (highlights_query, highlight_ranges) =
568551
self.read_queries(&self.highlights_filenames, "highlights.scm")?;
@@ -582,16 +565,21 @@ impl<'a> LanguageConfiguration<'a> {
582565
)
583566
.map_err(|error| {
584567
if error.offset < injections_query.len() {
585-
include_path_in_error(error, &injection_ranges, &injections_query, 0)
568+
Self::include_path_in_query_error(
569+
error,
570+
&injection_ranges,
571+
&injections_query,
572+
0,
573+
)
586574
} else if error.offset < injections_query.len() + locals_query.len() {
587-
include_path_in_error(
575+
Self::include_path_in_query_error(
588576
error,
589577
&locals_ranges,
590578
&locals_query,
591579
injections_query.len(),
592580
)
593581
} else {
594-
include_path_in_error(
582+
Self::include_path_in_query_error(
595583
error,
596584
&highlight_ranges,
597585
&highlights_query,
@@ -611,27 +599,67 @@ impl<'a> LanguageConfiguration<'a> {
611599
Ok(Some(result))
612600
}
613601
})
614-
.map(Option::as_ref)
602+
.map(Option::as_ref);
615603
}
616604

617605
pub fn tags_config(&self, language: Language) -> Result<Option<&TagsConfiguration>> {
618606
self.tags_config
619607
.get_or_try_init(|| {
620-
let (tags_query, _) = self.read_queries(&self.tags_filenames, "tags.scm")?;
621-
let (locals_query, _) = self.read_queries(&self.locals_filenames, "locals.scm")?;
608+
let (tags_query, tags_ranges) =
609+
self.read_queries(&self.tags_filenames, "tags.scm")?;
610+
let (locals_query, locals_ranges) =
611+
self.read_queries(&self.locals_filenames, "locals.scm")?;
622612
if tags_query.is_empty() {
623613
Ok(None)
624614
} else {
625615
TagsConfiguration::new(language, &tags_query, &locals_query)
626-
.map_err(Error::wrap(|| {
627-
format!("Failed to load queries in {:?}", self.root_path)
628-
}))
629-
.map(|config| Some(config))
616+
.map(Some)
617+
.map_err(|error| {
618+
if let TagsError::Query(error) = error {
619+
if error.offset < locals_query.len() {
620+
Self::include_path_in_query_error(
621+
error,
622+
&locals_ranges,
623+
&locals_query,
624+
0,
625+
)
626+
} else {
627+
Self::include_path_in_query_error(
628+
error,
629+
&tags_ranges,
630+
&tags_query,
631+
locals_query.len(),
632+
)
633+
}
634+
.into()
635+
} else {
636+
error.into()
637+
}
638+
})
630639
}
631640
})
632641
.map(Option::as_ref)
633642
}
634643

644+
fn include_path_in_query_error<'b>(
645+
mut error: QueryError,
646+
ranges: &'b Vec<(String, Range<usize>)>,
647+
source: &str,
648+
start_offset: usize,
649+
) -> (&'b str, QueryError) {
650+
let offset_within_section = error.offset - start_offset;
651+
let (path, range) = ranges
652+
.iter()
653+
.find(|(_, range)| range.contains(&offset_within_section))
654+
.unwrap();
655+
error.offset = offset_within_section - range.start;
656+
error.row = source[range.start..offset_within_section]
657+
.chars()
658+
.filter(|c| *c == '\n')
659+
.count();
660+
(path.as_ref(), error)
661+
}
662+
635663
fn read_queries(
636664
&self,
637665
paths: &Option<Vec<String>>,

‎cli/src/tags.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ pub fn generate_tags(
5353

5454
let source = fs::read(path)?;
5555
let t0 = Instant::now();
56-
for tag in context.generate_tags(tags_config, &source, Some(&cancellation_flag))?.0 {
56+
for tag in context
57+
.generate_tags(tags_config, &source, Some(&cancellation_flag))?
58+
.0
59+
{
5760
let tag = tag?;
5861
if !quiet {
5962
write!(
@@ -69,7 +72,7 @@ pub fn generate_tags(
6972
)?;
7073
if let Some(docs) = tag.docs {
7174
if docs.len() > 120 {
72-
write!(&mut stdout, "\t{:?}...", &docs[0..120])?;
75+
write!(&mut stdout, "\t{:?}...", docs.get(0..120).unwrap_or(""))?;
7376
} else {
7477
write!(&mut stdout, "\t{:?}", &docs)?;
7578
}

‎cli/src/tests/query_test.rs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn test_query_errors_on_invalid_symbols() {
130130
assert_eq!(
131131
Query::new(language, "(clas)").unwrap_err(),
132132
QueryError {
133-
row: 1,
133+
row: 0,
134134
offset: 1,
135135
column: 1,
136136
kind: QueryErrorKind::NodeType,
@@ -140,7 +140,7 @@ fn test_query_errors_on_invalid_symbols() {
140140
assert_eq!(
141141
Query::new(language, "(if_statement (arrayyyyy))").unwrap_err(),
142142
QueryError {
143-
row: 1,
143+
row: 0,
144144
offset: 15,
145145
column: 15,
146146
kind: QueryErrorKind::NodeType,
@@ -150,7 +150,7 @@ fn test_query_errors_on_invalid_symbols() {
150150
assert_eq!(
151151
Query::new(language, "(if_statement condition: (non_existent3))").unwrap_err(),
152152
QueryError {
153-
row: 1,
153+
row: 0,
154154
offset: 26,
155155
column: 26,
156156
kind: QueryErrorKind::NodeType,
@@ -160,7 +160,7 @@ fn test_query_errors_on_invalid_symbols() {
160160
assert_eq!(
161161
Query::new(language, "(if_statement condit: (identifier))").unwrap_err(),
162162
QueryError {
163-
row: 1,
163+
row: 0,
164164
offset: 14,
165165
column: 14,
166166
kind: QueryErrorKind::Field,
@@ -170,7 +170,7 @@ fn test_query_errors_on_invalid_symbols() {
170170
assert_eq!(
171171
Query::new(language, "(if_statement conditioning: (identifier))").unwrap_err(),
172172
QueryError {
173-
row: 1,
173+
row: 0,
174174
offset: 14,
175175
column: 14,
176176
kind: QueryErrorKind::Field,
@@ -189,7 +189,7 @@ fn test_query_errors_on_invalid_predicates() {
189189
Query::new(language, "((identifier) @id (@id))").unwrap_err(),
190190
QueryError {
191191
kind: QueryErrorKind::Syntax,
192-
row: 1,
192+
row: 0,
193193
column: 19,
194194
offset: 19,
195195
message: [
@@ -214,7 +214,7 @@ fn test_query_errors_on_invalid_predicates() {
214214
Query::new(language, "((identifier) @id (#eq? @id @ok))").unwrap_err(),
215215
QueryError {
216216
kind: QueryErrorKind::Capture,
217-
row: 1,
217+
row: 0,
218218
column: 29,
219219
offset: 29,
220220
message: "ok".to_string(),
@@ -236,7 +236,7 @@ fn test_query_errors_on_impossible_patterns() {
236236
),
237237
Err(QueryError {
238238
kind: QueryErrorKind::Structure,
239-
row: 1,
239+
row: 0,
240240
offset: 38,
241241
column: 38,
242242
message: [
@@ -256,7 +256,7 @@ fn test_query_errors_on_impossible_patterns() {
256256
Query::new(js_lang, "(function_declaration name: (statement_block))"),
257257
Err(QueryError {
258258
kind: QueryErrorKind::Structure,
259-
row: 1,
259+
row: 0,
260260
offset: 22,
261261
column: 22,
262262
message: [
@@ -272,7 +272,7 @@ fn test_query_errors_on_impossible_patterns() {
272272
Query::new(rb_lang, "(call receiver:(binary))"),
273273
Err(QueryError {
274274
kind: QueryErrorKind::Structure,
275-
row: 1,
275+
row: 0,
276276
offset: 6,
277277
column: 6,
278278
message: [
@@ -303,7 +303,7 @@ fn test_query_errors_on_impossible_patterns() {
303303
),
304304
Err(QueryError {
305305
kind: QueryErrorKind::Structure,
306-
row: 3,
306+
row: 2,
307307
offset: 88,
308308
column: 42,
309309
message: [
@@ -318,7 +318,7 @@ fn test_query_errors_on_impossible_patterns() {
318318
Query::new(js_lang, "(identifier (identifier))",),
319319
Err(QueryError {
320320
kind: QueryErrorKind::Structure,
321-
row: 1,
321+
row: 0,
322322
offset: 12,
323323
column: 12,
324324
message: [
@@ -332,7 +332,7 @@ fn test_query_errors_on_impossible_patterns() {
332332
Query::new(js_lang, "(true (true))",),
333333
Err(QueryError {
334334
kind: QueryErrorKind::Structure,
335-
row: 1,
335+
row: 0,
336336
offset: 6,
337337
column: 6,
338338
message: [
@@ -354,7 +354,7 @@ fn test_query_errors_on_impossible_patterns() {
354354
Query::new(js_lang, "(if_statement condition: (_expression))",),
355355
Err(QueryError {
356356
kind: QueryErrorKind::Structure,
357-
row: 1,
357+
row: 0,
358358
offset: 14,
359359
column: 14,
360360
message: [

‎lib/binding_rust/lib.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,16 +1174,16 @@ impl Query {
11741174
let offset = error_offset as usize;
11751175
let mut line_start = 0;
11761176
let mut row = 0;
1177-
let line_containing_error = source.split("\n").find_map(|line| {
1178-
row += 1;
1177+
let mut line_containing_error = None;
1178+
for line in source.split("\n") {
11791179
let line_end = line_start + line.len() + 1;
11801180
if line_end > offset {
1181-
Some(line)
1182-
} else {
1183-
line_start = line_end;
1184-
None
1181+
line_containing_error = Some(line);
1182+
break;
11851183
}
1186-
});
1184+
line_start = line_end;
1185+
row += 1;
1186+
}
11871187
let column = offset - line_start;
11881188

11891189
let kind;

‎tags/src/lib.rs‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,24 @@ impl TagsContext {
271271
.matches(&config.query, tree_ref.root_node(), move |node| {
272272
&source[node.byte_range()]
273273
});
274-
Ok((TagsIter {
275-
_tree: tree,
276-
matches,
277-
source,
278-
config,
279-
cancellation_flag,
280-
prev_line_info: None,
281-
tag_queue: Vec::new(),
282-
iter_count: 0,
283-
scopes: vec![LocalScope {
284-
range: 0..source.len(),
285-
inherits: false,
286-
local_defs: Vec::new(),
287-
}],
288-
}, tree_ref.root_node().has_error()))
274+
Ok((
275+
TagsIter {
276+
_tree: tree,
277+
matches,
278+
source,
279+
config,
280+
cancellation_flag,
281+
prev_line_info: None,
282+
tag_queue: Vec::new(),
283+
iter_count: 0,
284+
scopes: vec![LocalScope {
285+
range: 0..source.len(),
286+
inherits: false,
287+
local_defs: Vec::new(),
288+
}],
289+
},
290+
tree_ref.root_node().has_error(),
291+
))
289292
}
290293
}
291294

0 commit comments

Comments
 (0)