11use std:: { borrow:: Borrow , collections:: HashMap , sync:: OnceLock } ;
22
3- use async_recursion :: async_recursion ;
3+ use futures :: executor :: block_on ;
44use log:: debug;
55use tree_sitter:: { Node , Query , QueryCursor } ;
66
@@ -82,7 +82,7 @@ fn field_completion() -> &'static Query {
8282}
8383
8484impl Backend {
85- pub async fn model_of_range (
85+ pub fn model_of_range (
8686 & self ,
8787 node : Node < ' _ > ,
8888 range : ByteRange ,
@@ -149,15 +149,15 @@ impl Backend {
149149 let rhs = child. named_child ( 1 ) ?;
150150 if lhs. kind ( ) == "identifier" {
151151 let lhs = String :: from_utf8_lossy ( & contents[ lhs. byte_range ( ) ] ) ;
152- let type_ = self . type_of ( rhs, & scope, contents) . await ?;
152+ let type_ = self . type_of ( rhs, & scope, contents) ?;
153153 scope. insert ( lhs. into_owned ( ) , type_) ;
154154 }
155155 }
156156 "for_statement" => {
157157 let iteratee = child. named_child ( 0 ) ?;
158158 let src = child. named_child ( 1 ) ?;
159159 if iteratee. kind ( ) == "identifier" {
160- let type_ = self . type_of ( src, & scope, contents) . await ?;
160+ let type_ = self . type_of ( src, & scope, contents) ?;
161161 if let Some ( next) = children. peek ( ) . cloned ( ) {
162162 stack. push ( next) ;
163163 }
@@ -186,7 +186,7 @@ impl Backend {
186186 // Phase 3: Determine type of cursor expression using recursive ascent
187187 let mut descendant = Some ( target?. descendant_for_byte_range ( range. start . 0 , range. end . 0 ) ?) ;
188188 while let Some ( descendant_) = descendant {
189- match self . type_of ( descendant_, & scope, contents) . await {
189+ match self . type_of ( descendant_, & scope, contents) {
190190 Some ( Type :: Model ( model) ) => {
191191 return interner ( ) . get ( model) . map ( Into :: into) ;
192192 }
@@ -204,9 +204,8 @@ impl Backend {
204204 }
205205 None
206206 }
207- #[ async_recursion( ?Send ) ]
208- async fn type_of ( & self , mut node : Node < ' async_recursion > , scope : & Scope , contents : & [ u8 ] ) -> Option < Type > {
209- debug ! ( "type_of {}" , String :: from_utf8_lossy( & contents[ node. byte_range( ) ] ) ) ;
207+ fn type_of ( & self , mut node : Node , scope : & Scope , contents : & [ u8 ] ) -> Option < Type > {
208+ // debug!("type_of {}", String::from_utf8_lossy(&contents[node.byte_range()]));
210209 // What creates local scope, but doesn't contribute to method scope?
211210 // 1. For-statements: only within the block
212211 // 2. List comprehension: only within the object
@@ -230,23 +229,22 @@ impl Backend {
230229 // 1. foo.bar;
231230 // foo: Model('t) => bar: Model('t).field('bar')
232231 let interner = interner ( ) ;
233- match normalize ( & mut node) . kind ( ) {
232+ let kind = normalize ( & mut node) . kind ( ) ;
233+ match kind {
234234 "subscript" => {
235235 let rhs = node. named_child ( 1 ) ?;
236+ let rhs_range = rhs. byte_range ( ) . contract ( 1 ) ;
236237 if rhs. kind ( ) != "string" {
237238 return None ;
238239 }
239- let obj_ty = self . type_of ( node. child ( 0 ) ?, scope, contents) . await ?;
240- matches ! ( obj_ty, Type :: Env ) . then ( || {
241- Type :: Model (
242- String :: from_utf8_lossy ( & contents[ rhs. byte_range ( ) . contract ( 1 ) ] )
243- . as_ref ( )
244- . into ( ) ,
245- )
246- } )
240+ let lhs = node. named_child ( 0 ) ?;
241+ let obj_ty = self . type_of ( lhs, scope, contents) ?;
242+ matches ! ( obj_ty, Type :: Env )
243+ . then ( || Type :: Model ( String :: from_utf8_lossy ( & contents[ rhs_range] ) . as_ref ( ) . into ( ) ) )
247244 }
248245 "attribute" => {
249- let lhs = self . type_of ( node. named_child ( 0 ) ?, scope, contents) . await ?;
246+ let lhs = node. named_child ( 0 ) ?;
247+ let lhs = self . type_of ( lhs, scope, contents) ?;
250248 let rhs = node. named_child ( 1 ) ?;
251249 match & contents[ rhs. byte_range ( ) ] {
252250 b"env" if matches ! ( lhs, Type :: Model ( ..) | Type :: Record ( ..) ) => Some ( Type :: Env ) ,
@@ -276,7 +274,7 @@ impl Backend {
276274 let ident = String :: from_utf8_lossy ( ident) ;
277275 let ident = interner. get_or_intern ( ident. as_ref ( ) ) ;
278276 let mut entry = self . index . models . get_mut ( & model. into ( ) ) ?;
279- let fields = self . populate_field_names ( & mut entry) . await ;
277+ let fields = block_on ( self . populate_field_names ( & mut entry) ) ;
280278 let field = fields. ok ( ) ?. get ( & ident. into ( ) ) ?;
281279 match field. kind {
282280 FieldKind :: Relational ( model) => Some ( Type :: Model ( interner. resolve ( & model) . into ( ) ) ) ,
@@ -292,11 +290,11 @@ impl Backend {
292290 }
293291 "assignment" => {
294292 let rhs = node. named_child ( 1 ) ?;
295- self . type_of ( rhs, scope, contents) . await
293+ self . type_of ( rhs, scope, contents)
296294 }
297295 "call" => {
298296 let func = node. named_child ( 0 ) ?;
299- let func = self . type_of ( func, scope, contents) . await ?;
297+ let func = self . type_of ( func, scope, contents) ?;
300298 match func {
301299 Type :: RefFn => {
302300 // (call (_) @func (argument_list . (string) @xml_id))
0 commit comments