@@ -43,6 +43,7 @@ impl Backend {
4343 self . update_ast ( text, uri, rope, parser) ?;
4444 Ok ( ( ) )
4545 }
46+ const BYTE_WINDOW : usize = 200 ;
4647 pub fn python_completions (
4748 & self ,
4849 params : CompletionParams ,
@@ -64,14 +65,15 @@ impl Backend {
6465 cursor. set_match_limit ( 256 ) ;
6566 let bytes = rope. bytes ( ) . collect :: < Vec < _ > > ( ) ;
6667 // TODO: Very inexact, is there a better way?
67- let range = offset. saturating_sub ( 50 ) ..bytes. len ( ) . min ( offset + 200 ) ;
68+ let range = offset. saturating_sub ( Self :: BYTE_WINDOW ) ..bytes. len ( ) . min ( offset + Self :: BYTE_WINDOW ) ;
6869 let query = py_completions ( ) ;
6970 cursor. set_byte_range ( range. clone ( ) ) ;
7071 let mut items = vec ! [ ] ;
7172 ' match_: for match_ in cursor. matches ( query, ast. root_node ( ) , & bytes[ ..] ) {
72- match match_. captures {
73- [ _, _, xml_id] => {
74- let range = xml_id. node . byte_range ( ) ;
73+ for capture in match_. captures {
74+ if capture. index == 2 {
75+ // @xml_id
76+ let range = capture. node . byte_range ( ) ;
7577 if range. contains ( & offset) {
7678 let Some ( slice) = rope. get_byte_slice ( range. clone ( ) ) else {
7779 dbg ! ( & range) ;
@@ -87,9 +89,9 @@ impl Backend {
8789 items,
8890 } ) ) ) ;
8991 }
90- }
91- [ _ , model] => {
92- let range = model . node . byte_range ( ) ;
92+ } else if capture . index == 3 {
93+ // @ model
94+ let range = capture . node . byte_range ( ) ;
9395 if range. contains ( & offset) {
9496 let Some ( slice) = rope. get_byte_slice ( range. clone ( ) ) else {
9597 dbg ! ( & range) ;
@@ -105,32 +107,31 @@ impl Backend {
105107 } ) ) ) ;
106108 }
107109 }
108- unk => Err ( diagnostic ! ( "Unknown pattern {unk:?}" ) ) ?,
109110 }
110111 }
111112 Ok ( None )
112113 }
113114 pub fn python_jump_def ( & self , params : GotoDefinitionParams , rope : Rope ) -> miette:: Result < Option < Location > > {
114- let Some ( ast) = self
115+ let uri = & params. text_document_position_params . text_document . uri ;
116+ let ast = self
115117 . ast_map
116- . get ( params. text_document_position_params . text_document . uri . path ( ) )
117- else {
118- return Ok ( None ) ;
119- } ;
118+ . get ( uri. path ( ) )
119+ . ok_or_else ( || diagnostic ! ( "Did not build AST for {}" , uri. path( ) ) ) ?;
120120 let Some ( ByteOffset ( offset) ) = position_to_offset ( params. text_document_position_params . position , rope. clone ( ) )
121121 else {
122- return Ok ( None ) ;
122+ Err ( diagnostic ! ( "could not find offset for {}" , uri . path ( ) ) ) ?
123123 } ;
124124 let query = py_completions ( ) ;
125125 let bytes = rope. bytes ( ) . collect :: < Vec < _ > > ( ) ;
126- let range = offset. saturating_sub ( 50 ) ..bytes. len ( ) . min ( offset + 200 ) ;
126+ let range = offset. saturating_sub ( Self :: BYTE_WINDOW ) ..bytes. len ( ) . min ( offset + Self :: BYTE_WINDOW ) ;
127127 let mut cursor = tree_sitter:: QueryCursor :: new ( ) ;
128128 cursor. set_match_limit ( 256 ) ;
129- cursor. set_byte_range ( range. clone ( ) ) ;
129+ cursor. set_byte_range ( range) ;
130130 ' match_: for match_ in cursor. matches ( query, ast. root_node ( ) , & bytes[ ..] ) {
131- match match_. captures {
132- [ _, _, xml_id] => {
133- let range = xml_id. node . byte_range ( ) ;
131+ for capture in match_. captures {
132+ if capture. index == 2 {
133+ // @xml_id
134+ let range = capture. node . byte_range ( ) ;
134135 if range. contains ( & offset) {
135136 let range = range. contract ( 1 ) ;
136137 let Some ( slice) = rope. get_byte_slice ( range. clone ( ) ) else {
@@ -141,9 +142,9 @@ impl Backend {
141142 return self
142143 . jump_def_inherit_id ( & slice, & params. text_document_position_params . text_document . uri ) ;
143144 }
144- }
145- [ _ , model] => {
146- let range = model . node . byte_range ( ) ;
145+ } else if capture . index == 3 {
146+ // @ model
147+ let range = capture . node . byte_range ( ) ;
147148 if range. contains ( & offset) {
148149 let range = range. contract ( 1 ) ;
149150 let Some ( slice) = rope. get_byte_slice ( range. clone ( ) ) else {
@@ -154,7 +155,6 @@ impl Backend {
154155 return self . jump_def_model ( & slice) ;
155156 }
156157 }
157- unk => Err ( diagnostic ! ( "Unknown pattern {unk:?}" ) ) ?,
158158 }
159159 }
160160 Ok ( None )
@@ -170,25 +170,22 @@ impl Backend {
170170 } ;
171171 let query = py_references ( ) ;
172172 let bytes = rope. bytes ( ) . collect :: < Vec < _ > > ( ) ;
173- let range = offset. saturating_sub ( 50 ) ..bytes. len ( ) . min ( offset + 200 ) ;
173+ let range = offset. saturating_sub ( Self :: BYTE_WINDOW ) ..bytes. len ( ) . min ( offset + Self :: BYTE_WINDOW ) ;
174174 let mut cursor = tree_sitter:: QueryCursor :: new ( ) ;
175175 cursor. set_match_limit ( 256 ) ;
176- cursor. set_byte_range ( range. clone ( ) ) ;
176+ cursor. set_byte_range ( range) ;
177177 ' match_: for match_ in cursor. matches ( query, ast. root_node ( ) , & bytes[ ..] ) {
178- match match_. captures {
179- [ _, model] => {
180- let range = model. node . byte_range ( ) ;
181- if range. contains ( & offset) {
182- let range = range. contract ( 1 ) ;
183- let Some ( slice) = rope. get_byte_slice ( range. clone ( ) ) else {
184- dbg ! ( & range) ;
185- break ' match_;
186- } ;
187- let slice = Cow :: from ( slice) ;
188- return self . model_references ( & slice) ;
189- }
178+ for model in match_. nodes_for_capture_index ( 1 ) {
179+ let range = dbg ! ( & model) . byte_range ( ) ;
180+ if range. contains ( & offset) {
181+ let range = range. contract ( 1 ) ;
182+ let Some ( slice) = rope. get_byte_slice ( range. clone ( ) ) else {
183+ dbg ! ( & range) ;
184+ break ' match_;
185+ } ;
186+ let slice = Cow :: from ( slice) ;
187+ return self . model_references ( & slice) ;
190188 }
191- unk => Err ( diagnostic ! ( "Unknown pattern {unk:?}" ) ) ?,
192189 }
193190 }
194191 Ok ( None )
0 commit comments