@@ -5,12 +5,13 @@ use regex::{Regex, RegexBuilder};
55use serde_derive:: Deserialize ;
66use std:: collections:: HashMap ;
77use std:: io:: BufReader ;
8+ use std:: ops:: Range ;
89use std:: path:: { Path , PathBuf } ;
910use std:: process:: Command ;
1011use std:: sync:: Mutex ;
1112use std:: time:: SystemTime ;
1213use std:: { fs, mem} ;
13- use tree_sitter:: Language ;
14+ use tree_sitter:: { Language , QueryError } ;
1415use tree_sitter_highlight:: HighlightConfiguration ;
1516use tree_sitter_tags:: TagsConfiguration ;
1617
@@ -543,13 +544,32 @@ impl Loader {
543544
544545impl < ' a > LanguageConfiguration < ' a > {
545546 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+
546565 self . highlight_config
547566 . get_or_try_init ( || {
548- let highlights_query =
567+ let ( highlights_query, highlight_ranges ) =
549568 self . read_queries ( & self . highlights_filenames , "highlights.scm" ) ?;
550- let injections_query =
569+ let ( injections_query, injection_ranges ) =
551570 self . read_queries ( & self . injections_filenames , "injections.scm" ) ?;
552- let locals_query = self . read_queries ( & self . locals_filenames , "locals.scm" ) ?;
571+ let ( locals_query, locals_ranges) =
572+ self . read_queries ( & self . locals_filenames , "locals.scm" ) ?;
553573
554574 if highlights_query. is_empty ( ) {
555575 Ok ( None )
@@ -560,9 +580,25 @@ impl<'a> LanguageConfiguration<'a> {
560580 & injections_query,
561581 & locals_query,
562582 )
563- . map_err ( Error :: wrap ( || {
564- format ! ( "Failed to load queries in {:?}" , self . root_path)
565- } ) ) ?;
583+ . map_err ( |error| {
584+ if error. offset < injections_query. len ( ) {
585+ include_path_in_error ( error, & injection_ranges, & injections_query, 0 )
586+ } else if error. offset < injections_query. len ( ) + locals_query. len ( ) {
587+ include_path_in_error (
588+ error,
589+ & locals_ranges,
590+ & locals_query,
591+ injections_query. len ( ) ,
592+ )
593+ } else {
594+ include_path_in_error (
595+ error,
596+ & highlight_ranges,
597+ & highlights_query,
598+ injections_query. len ( ) + locals_query. len ( ) ,
599+ )
600+ }
601+ } ) ?;
566602 let mut all_highlight_names = self . highlight_names . lock ( ) . unwrap ( ) ;
567603 if self . use_all_highlight_names {
568604 for capture_name in result. query . capture_names ( ) {
@@ -581,8 +617,8 @@ impl<'a> LanguageConfiguration<'a> {
581617 pub fn tags_config ( & self , language : Language ) -> Result < Option < & TagsConfiguration > > {
582618 self . tags_config
583619 . get_or_try_init ( || {
584- let tags_query = self . read_queries ( & self . tags_filenames , "tags.scm" ) ?;
585- let locals_query = self . read_queries ( & self . locals_filenames , "locals.scm" ) ?;
620+ let ( tags_query, _ ) = self . read_queries ( & self . tags_filenames , "tags.scm" ) ?;
621+ let ( locals_query, _ ) = self . read_queries ( & self . locals_filenames , "locals.scm" ) ?;
586622 if tags_query. is_empty ( ) {
587623 Ok ( None )
588624 } else {
@@ -596,27 +632,34 @@ impl<'a> LanguageConfiguration<'a> {
596632 . map ( Option :: as_ref)
597633 }
598634
599- fn read_queries ( & self , paths : & Option < Vec < String > > , default_path : & str ) -> Result < String > {
635+ fn read_queries (
636+ & self ,
637+ paths : & Option < Vec < String > > ,
638+ default_path : & str ,
639+ ) -> Result < ( String , Vec < ( String , Range < usize > ) > ) > {
640+ let mut query = String :: new ( ) ;
641+ let mut path_ranges = Vec :: new ( ) ;
600642 if let Some ( paths) = paths. as_ref ( ) {
601- let mut query = String :: new ( ) ;
602643 for path in paths {
603- let path = self . root_path . join ( path) ;
604- query += & fs:: read_to_string ( & path) . map_err ( Error :: wrap ( || {
644+ let abs_path = self . root_path . join ( path) ;
645+ let prev_query_len = query. len ( ) ;
646+ query += & fs:: read_to_string ( & abs_path) . map_err ( Error :: wrap ( || {
605647 format ! ( "Failed to read query file {:?}" , path)
606648 } ) ) ?;
649+ path_ranges. push ( ( path. clone ( ) , prev_query_len..query. len ( ) ) ) ;
607650 }
608- Ok ( query)
609651 } else {
610652 let queries_path = self . root_path . join ( "queries" ) ;
611653 let path = queries_path. join ( default_path) ;
612654 if path. exists ( ) {
613- fs:: read_to_string ( & path) . map_err ( Error :: wrap ( || {
655+ query = fs:: read_to_string ( & path) . map_err ( Error :: wrap ( || {
614656 format ! ( "Failed to read query file {:?}" , path)
615- } ) )
616- } else {
617- Ok ( String :: new ( ) )
657+ } ) ) ?;
658+ path_ranges. push ( ( default_path. to_string ( ) , 0 ..query. len ( ) ) ) ;
618659 }
619660 }
661+
662+ Ok ( ( query, path_ranges) )
620663 }
621664}
622665
0 commit comments