@@ -10,6 +10,9 @@ import { TypeHintSettings } from "./settings";
1010 */
1111export class TypeHintProvider {
1212
13+ private doc : TextDocument ;
14+ private settings : TypeHintSettings ;
15+ private typingHintProvider : TypingHintProvider ;
1316 private likelyTypes : PythonType [ ] = [
1417 PythonType . String ,
1518 PythonType . List ,
@@ -19,10 +22,9 @@ export class TypeHintProvider {
1922 PythonType . Tuple ,
2023 PythonType . Float
2124 ] ;
25+ private providedTypeHints : string [ ] = [ ] ;
2226 private typeContainer : DataTypeContainer = getDataTypeContainer ( ) ;
23- private typeNamesIncludedInResult : string [ ] = [ ] ;
24- private doc : TextDocument ;
25- private settings : TypeHintSettings ;
27+ private _typingImported = false ;
2628
2729 /**
2830 * Constructs a new TypeHintProvider.
@@ -33,20 +35,24 @@ export class TypeHintProvider {
3335 constructor ( doc : TextDocument , settings : TypeHintSettings ) {
3436 this . doc = doc ;
3537 this . settings = settings ;
38+ this . typingHintProvider = new TypingHintProvider ( this . typeContainer ) ;
39+ }
40+
41+ public get typingImported ( ) {
42+ return this . _typingImported ;
3643 }
3744
3845 /**
39- * Provides type hints for a parameter.
46+ * Estimates a parameter's type and returns type hints for it.
47+ * The returned hints are ordered with the most likely type being first.
4048 *
4149 * @param param The parameter name.
42- * @returns An array of type hints, ordered by estimation accuracy.
4350 */
4451 public async getTypeHints ( param : string ) : Promise < string [ ] > {
4552 const typeHints : string [ ] = [ ] ;
4653 const documentText = this . doc . getText ( ) ;
4754
48- const typingHintProvider = new TypingHintProvider ( documentText , this . typeContainer ) ;
49- const typingSearch = typingHintProvider . containsTyping ( ) ;
55+ const typingSearch = this . typingHintProvider . detectTypingImport ( documentText ) ;
5056
5157 const variableSearch = TypeSearch . variableWithSameName ( param , documentText ) ;
5258
@@ -59,7 +65,7 @@ export class TypeHintProvider {
5965 let typeName = this . getTypeParamEndsWith ( param , "_" ) ;
6066 if ( typeName ) {
6167 this . add ( typeName , typeHints ) ;
62- this . tryAddTypingHint ( await typingSearch , typeName , typingHintProvider , typeHints ) ;
68+ this . tryAddTypingHint ( await typingSearch , typeName , typeHints ) ;
6369 wsSearcher . cancel ( ) ;
6470 await workspaceSearch ;
6571 return typeHints ;
@@ -69,35 +75,44 @@ export class TypeHintProvider {
6975
7076 if ( searchResult !== null && ! TypeSearch . invalidTernaryOperator ( searchResult ) ) {
7177 this . add ( searchResult . typeName , typeHints ) ;
72- this . tryAddTypingHints ( await typingSearch , searchResult , typingHintProvider , typeHints ) ;
78+ this . tryAddTypingHints ( await typingSearch , searchResult , typeHints ) ;
7379 this . tryAdd ( this . typeGuessFor ( param , typeHints ) , typeHints ) ;
74- } else if ( typeName = this . getTypeParamEndsWith ( param , "" ) ) {
75- this . add ( typeName , typeHints ) ;
76- this . tryAddTypingHint ( await typingSearch , typeName , typingHintProvider , typeHints ) ;
7780 } else {
78- this . tryAdd ( this . typeGuessFor ( param , typeHints ) , typeHints ) ;
79- }
81+ typeName = this . getTypeParamEndsWith ( param , "" ) ;
82+ if ( typeName ) {
83+ this . add ( typeName , typeHints ) ;
84+ this . tryAddTypingHint ( await typingSearch , typeName , typeHints ) ;
85+ } else {
86+ this . tryAdd ( this . typeGuessFor ( param , typeHints ) , typeHints ) ;
87+ }
88+ }
8089
8190 this . tryAdd ( await workspaceSearch , typeHints ) ;
8291 return typeHints ;
8392 }
8493
8594 /**
86- * Gets types that have not been provided as hints.
87- *
88- * @returns An array of types.
95+ * Returns hints for types that have not been provided yet.
8996 */
90- public getRemainingTypes ( ) : PythonType [ ] {
97+ public remainingTypeHints ( ) : PythonType [ ] {
9198 const result : PythonType [ ] = [ ] ;
9299
93100 for ( const type of Object . values ( this . typeContainer ) ) {
94- if ( this . typeNotIncluded ( type . name ) ) {
101+ if ( this . hintNotProvided ( type . name ) ) {
95102 result . push ( type . name ) ;
96103 }
97104 }
105+
98106 return result ;
99107 }
100108
109+ /**
110+ * Returns hints for the typing module that have not been provided yet.
111+ */
112+ public remainingTypingHints ( ) : string [ ] {
113+ return this . typingHintProvider . getRemainingHints ( ) ;
114+ }
115+
101116 /**
102117 * If the param ends with a type name, the type is returned.
103118 * @param param The parameter name.
@@ -134,16 +149,16 @@ export class TypeHintProvider {
134149 }
135150
136151
137- private add ( typeName : string , typeHints : string [ ] ) {
138- typeName = typeName . trim ( ) ;
139- if ( this . typeNotIncluded ( typeName ) ) {
140- typeHints . push ( typeName ) ;
141- this . typeNamesIncludedInResult . push ( typeName ) ;
152+ private add ( type : string , typeHints : string [ ] ) {
153+ type = type . trim ( ) ;
154+ if ( this . hintNotProvided ( type ) ) {
155+ typeHints . push ( type ) ;
156+ this . providedTypeHints . push ( type ) ;
142157 }
143158 }
144159
145- private typeNotIncluded ( type : string ) : boolean {
146- return ! this . typeNamesIncludedInResult . includes ( type ) ;
160+ private hintNotProvided ( type : string ) : boolean {
161+ return ! this . providedTypeHints . includes ( type ) ;
147162 }
148163
149164 private tryAdd ( type : string | null , typeHints : string [ ] ) {
@@ -155,11 +170,10 @@ export class TypeHintProvider {
155170 private tryAddTypingHints (
156171 typingFound : boolean ,
157172 searchResult : VariableSearchResult | null ,
158- typingHintProvider : TypingHintProvider ,
159173 typeHints : string [ ]
160174 ) {
161175 if ( typingFound ) {
162- const hints : string [ ] | null = typingHintProvider . getTypingHints ( searchResult ) ;
176+ const hints : string [ ] | null = this . typingHintProvider . getHints ( searchResult ) ;
163177 if ( hints ) {
164178 for ( const hint of hints ) {
165179 this . add ( hint , typeHints ) ;
@@ -168,14 +182,10 @@ export class TypeHintProvider {
168182 }
169183 }
170184
171- private tryAddTypingHint (
172- typingFound : boolean ,
173- typeName : string ,
174- typingHintProvider : TypingHintProvider ,
175- typeHints : string [ ]
176- ) {
177- if ( typingFound ) {
178- const typingHint = typingHintProvider . getTypingHint ( typeName ) ;
185+ private tryAddTypingHint ( typingImported : boolean , typeName : string , typeHints : string [ ] ) {
186+ this . _typingImported = typingImported ;
187+ if ( typingImported ) {
188+ const typingHint = this . typingHintProvider . getHint ( typeName ) ;
179189 if ( typingHint ) {
180190 this . add ( typingHint , typeHints ) ;
181191 }
0 commit comments