@@ -10,12 +10,12 @@ import {
10
10
TextDocument
11
11
} from "vscode" ;
12
12
import { TypeHintProvider , TypeHint } from "./typeHintProvider" ;
13
- import { paramHintTrigger , returnHintTrigger , TypeName } from "./python" ;
13
+ import { paramHintTrigger , returnHintTrigger , PythonType } from "./python" ;
14
14
15
15
16
16
abstract class CompletionProvider {
17
17
18
- protected pushTypeNamesToItems ( typeNames : TypeName [ ] , completionItems : CompletionItem [ ] ) {
18
+ protected pushTypesToItems ( typeNames : PythonType [ ] , completionItems : CompletionItem [ ] ) {
19
19
for ( const typeName of typeNames ) {
20
20
const item = new CompletionItem ( typeName , CompletionItemKind . TypeParameter ) ;
21
21
@@ -38,19 +38,25 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
38
38
token : CancellationToken ,
39
39
context : CompletionContext
40
40
) : 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
+ }
51
58
}
52
- this . pushTypeNamesToItems ( provider . getRemainingTypes ( ) , items ) ;
53
- return Promise . resolve ( new CompletionList ( items , false ) ) ;
59
+ return Promise . resolve ( null ) ;
54
60
}
55
61
56
62
/**
@@ -69,9 +75,9 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
69
75
return param ;
70
76
}
71
77
72
- protected pushEstimationsToItems ( typeHints : TypeHint [ ] , items : CompletionItem [ ] ) {
78
+ private pushEstimationsToItems ( typeHints : TypeHint [ ] , items : CompletionItem [ ] ) {
73
79
74
- if ( typeHints ) {
80
+ if ( typeHints . length > 0 ) {
75
81
let typeHint = typeHints [ 0 ] . type ;
76
82
let item = new CompletionItem ( typeHint , CompletionItemKind . TypeParameter ) ;
77
83
item . sortText = `0${ typeHint } ` ;
@@ -89,6 +95,14 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
89
95
90
96
}
91
97
}
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
+ }
92
106
}
93
107
94
108
/**
@@ -108,17 +122,17 @@ export class ReturnHintCompletionProvider extends CompletionProvider implements
108
122
const items : CompletionItem [ ] = [ ] ;
109
123
const line = doc . lineAt ( pos ) ;
110
124
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 ) ;
113
127
}
114
128
return Promise . resolve ( new CompletionList ( items , false ) ) ;
115
129
}
116
130
117
- private shouldProvideReturnHint ( line : TextLine , pos : Position ) : boolean {
131
+ private shouldProvideItems ( line : TextLine , pos : Position ) : boolean {
118
132
119
133
if ( pos . character > 0 && line . text . substr ( pos . character - 2 , 2 ) === "->" ) {
120
134
121
- return new RegExp ( "^[* \t]*def.*\\) *->[: ]*$" , "m" ) . test ( line . text ) ;
135
+ return new RegExp ( "^[ \t]*def.*\\) *->[: ]*$" , "m" ) . test ( line . text ) ;
122
136
}
123
137
return false ;
124
138
}
0 commit comments