@@ -10,12 +10,12 @@ import {
1010 TextDocument
1111} from "vscode" ;
1212import { TypeHintProvider , TypeHint } from "./typeHintProvider" ;
13- import { paramHintTrigger , returnHintTrigger , TypeName } from "./python" ;
13+ import { paramHintTrigger , returnHintTrigger , PythonType } from "./python" ;
1414
1515
1616abstract class CompletionProvider {
1717
18- protected pushTypeNamesToItems ( typeNames : TypeName [ ] , completionItems : CompletionItem [ ] ) {
18+ protected pushTypesToItems ( typeNames : PythonType [ ] , completionItems : CompletionItem [ ] ) {
1919 for ( const typeName of typeNames ) {
2020 const item = new CompletionItem ( typeName , CompletionItemKind . TypeParameter ) ;
2121
@@ -38,19 +38,25 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
3838 token : CancellationToken ,
3939 context : CompletionContext
4040 ) : Promise < CompletionList | null > {
41- if ( context . triggerCharacter !== paramHintTrigger ) {
42- return Promise . resolve ( null ) ;
43- }
44- const items : CompletionItem [ ] = [ ] ;
45- const line = doc . lineAt ( pos ) ;
46- const param = this . findParam ( line , pos ) ;
47- const provider = new TypeHintProvider ( doc ) ;
48-
49- if ( param && param . length > 0 ) {
50- this . pushEstimationsToItems ( await provider . getTypeHints ( param ) , items ) ;
41+ if ( context . triggerCharacter === paramHintTrigger ) {
42+ const items : CompletionItem [ ] = [ ] ;
43+ const line = doc . lineAt ( pos ) ;
44+
45+ if ( this . shouldProvideItems ( line , pos ) ) {
46+ const param = this . findParam ( line , pos ) ;
47+ const provider = new TypeHintProvider ( doc ) ;
48+
49+ if ( param && param . length > 0 ) {
50+ try {
51+ this . pushEstimationsToItems ( await provider . getTypeHints ( param ) , items ) ;
52+ } catch ( error ) {
53+ }
54+ }
55+ this . pushTypesToItems ( provider . getRemainingTypes ( ) , items ) ;
56+ return Promise . resolve ( new CompletionList ( items , false ) ) ;
57+ }
5158 }
52- this . pushTypeNamesToItems ( provider . getRemainingTypes ( ) , items ) ;
53- return Promise . resolve ( new CompletionList ( items , false ) ) ;
59+ return Promise . resolve ( null ) ;
5460 }
5561
5662 /**
@@ -69,9 +75,9 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
6975 return param ;
7076 }
7177
72- protected pushEstimationsToItems ( typeHints : TypeHint [ ] , items : CompletionItem [ ] ) {
78+ private pushEstimationsToItems ( typeHints : TypeHint [ ] , items : CompletionItem [ ] ) {
7379
74- if ( typeHints ) {
80+ if ( typeHints . length > 0 ) {
7581 let typeHint = typeHints [ 0 ] . type ;
7682 let item = new CompletionItem ( typeHint , CompletionItemKind . TypeParameter ) ;
7783 item . sortText = `0${ typeHint } ` ;
@@ -89,6 +95,14 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
8995
9096 }
9197 }
98+
99+ private shouldProvideItems ( line : TextLine , pos : Position ) : boolean {
100+
101+ if ( pos . character > 0 ) {
102+ return new RegExp ( "^[ \t]*def" , "m" ) . test ( line . text ) ;
103+ }
104+ return false ;
105+ }
92106}
93107
94108/**
@@ -108,17 +122,17 @@ export class ReturnHintCompletionProvider extends CompletionProvider implements
108122 const items : CompletionItem [ ] = [ ] ;
109123 const line = doc . lineAt ( pos ) ;
110124
111- if ( this . shouldProvideReturnHint ( line , pos ) ) {
112- this . pushTypeNamesToItems ( Object . values ( TypeName ) , items ) ;
125+ if ( this . shouldProvideItems ( line , pos ) ) {
126+ this . pushTypesToItems ( Object . values ( PythonType ) , items ) ;
113127 }
114128 return Promise . resolve ( new CompletionList ( items , false ) ) ;
115129 }
116130
117- private shouldProvideReturnHint ( line : TextLine , pos : Position ) : boolean {
131+ private shouldProvideItems ( line : TextLine , pos : Position ) : boolean {
118132
119133 if ( pos . character > 0 && line . text . substr ( pos . character - 2 , 2 ) === "->" ) {
120134
121- return new RegExp ( "^[* \t]*def.*\\) *->[: ]*$" , "m" ) . test ( line . text ) ;
135+ return new RegExp ( "^[ \t]*def.*\\) *->[: ]*$" , "m" ) . test ( line . text ) ;
122136 }
123137 return false ;
124138 }
0 commit comments