@@ -315,21 +315,25 @@ export class Reference {
315
315
}
316
316
}
317
317
318
- // inside a trait/contract, when we write `foo`, we want to automatically complete it
319
- // with `self.foo` if there are any methods/fields/constants with the same name
320
- const ownerNode = parentOfType ( this . element . node , "contract_body" , "trait_body" )
321
- if ( ownerNode !== null && state . get ( "completion" ) ) {
322
- const constructor = ownerNode . type === "contract_body" ? Contract : Trait
323
- const parent = ownerNode . parent
324
- if ( ! parent ) return true
325
-
326
- const owner = new constructor ( parent , this . element . file )
327
- const typeConstructor = ownerNode . type === "contract_body" ? ContractTy : TraitTy
328
- const ownerTy = new typeConstructor ( owner . name ( ) , owner )
329
- const expr = new Expression ( this . element . node , this . element . file )
318
+ if ( state . get ( "completion" ) ) {
319
+ const ownerNode = parentOfType (
320
+ this . element . node ,
321
+ "contract_body" ,
322
+ "trait_body" ,
323
+ "global_function" ,
324
+ )
325
+
326
+ // inside a trait/contract, when we write `foo`, we want to automatically complete it
327
+ // with `self.foo` if there are any methods/fields/constants with the same name
328
+ if ( ownerNode ?. type === "contract_body" || ownerNode ?. type === "trait_body" ) {
329
+ if ( ! this . processContractTraitSelfCompletion ( ownerNode , state , proc ) ) return false
330
+ }
330
331
331
- const newState = state . withValue ( "prefix" , "self." )
332
- if ( ! this . processType ( expr , ownerTy , proc , newState ) ) return false
332
+ // inside extends function, when we write `foo`, we want to automatically complete it
333
+ // with `self.foo` if there are any methods/fields/constants with the same name
334
+ if ( ownerNode ?. type === "global_function" ) {
335
+ if ( ! this . processExtendsMethodSelfCompletion ( ownerNode , state , proc ) ) return false
336
+ }
333
337
}
334
338
335
339
if ( this . element . node . type === "tvm_instruction" ) {
@@ -369,6 +373,41 @@ export class Reference {
369
373
return this . processAllEntities ( proc , state )
370
374
}
371
375
376
+ private processContractTraitSelfCompletion (
377
+ ownerNode : SyntaxNode ,
378
+ state : ResolveState ,
379
+ proc : ScopeProcessor ,
380
+ ) : boolean {
381
+ const constructor = ownerNode . type === "contract_body" ? Contract : Trait
382
+ const parent = ownerNode . parent
383
+ if ( ! parent ) return true
384
+
385
+ const owner = new constructor ( parent , this . element . file )
386
+ const typeConstructor = ownerNode . type === "contract_body" ? ContractTy : TraitTy
387
+ const ownerTy = new typeConstructor ( owner . name ( ) , owner )
388
+ const expr = new Expression ( this . element . node , this . element . file )
389
+
390
+ const newState = state . withValue ( "prefix" , "self." )
391
+ return this . processType ( expr , ownerTy , proc , newState )
392
+ }
393
+
394
+ private processExtendsMethodSelfCompletion (
395
+ ownerNode : SyntaxNode ,
396
+ state : ResolveState ,
397
+ proc : ScopeProcessor ,
398
+ ) : boolean {
399
+ const func = new Fun ( ownerNode , this . element . file )
400
+ const selfParam = func . selfParam ( )
401
+ if ( selfParam === null ) return true // skip if we are not in extends function
402
+
403
+ const selfTy = TypeInferer . inferType ( selfParam )
404
+ if ( ! selfTy ) return true
405
+ const expr = new Expression ( this . element . node , this . element . file )
406
+
407
+ const newState = state . withValue ( "prefix" , "self." )
408
+ return this . processType ( expr , selfTy , proc , newState )
409
+ }
410
+
372
411
private resolveInstanceInitField (
373
412
parent : SyntaxNode ,
374
413
proc : ScopeProcessor ,
0 commit comments