@@ -2,17 +2,15 @@ import { TextDocument } from "vscode";
22import { PythonType as PythonType , DataTypeContainer , getDataTypeContainer } from "./python" ;
33import { TypeSearch , VariableSearchResult } from "./typeSearch" ;
44import { TypingHintProvider } from "./typingHintProvider" ;
5- import { WorkspaceSearcher } from "./workspaceSearcher" ;
65import { TypeHintSettings } from "./settings" ;
76
87/**
98 * Provides type hints.
109 */
1110export class TypeHintProvider {
1211
13- private doc : TextDocument ;
14- private settings : TypeHintSettings ;
15- private typingHintProvider : TypingHintProvider ;
12+ private _typingHintProvider : TypingHintProvider ;
13+
1614 private likelyTypes : PythonType [ ] = [
1715 PythonType . String ,
1816 PythonType . List ,
@@ -22,20 +20,13 @@ export class TypeHintProvider {
2220 PythonType . Tuple ,
2321 PythonType . Float
2422 ] ;
25- private providedTypeHints : string [ ] = [ ] ;
26- private typeContainer : DataTypeContainer = getDataTypeContainer ( ) ;
23+ private _providedTypeHints : string [ ] = [ ] ;
24+ private _typeContainer : DataTypeContainer ;
2725 private _typingImported = false ;
2826
29- /**
30- * Constructs a new TypeHintProvider.
31- *
32- * @param doc The active document.
33- * @param settings User settings.
34- */
35- constructor ( doc : TextDocument , settings : TypeHintSettings ) {
36- this . doc = doc ;
37- this . settings = settings ;
38- this . typingHintProvider = new TypingHintProvider ( this . typeContainer ) ;
27+ constructor ( typeContainer : DataTypeContainer ) {
28+ this . _typeContainer = typeContainer ;
29+ this . _typingHintProvider = new TypingHintProvider ( typeContainer ) ;
3930 }
4031
4132 public get typingImported ( ) {
@@ -47,47 +38,44 @@ export class TypeHintProvider {
4738 * The returned hints are ordered with the most likely type being first.
4839 *
4940 * @param param The parameter name.
41+ * @param documentText The text to search in order to estimate types.
5042 */
51- public async getTypeHints ( param : string ) : Promise < string [ ] > {
43+ public async estimateTypeHints ( param : string , documentText : string ) : Promise < string [ ] > {
5244 const typeHints : string [ ] = [ ] ;
53- const documentText = this . doc . getText ( ) ;
54-
55- const typingSearch = this . typingHintProvider . detectTypingImport ( documentText ) ;
5645
46+ const typingSearch = this . _typingHintProvider . detectTypingImport ( documentText ) ;
5747 const variableSearch = TypeSearch . variableWithSameName ( param , documentText ) ;
5848
59- const wsSearcher = new WorkspaceSearcher ( this . doc . uri , this . settings ) ;
60- const workspaceSearch = wsSearcher . findHintOfSimilarParam ( param , documentText ) ;
61-
62- this . tryAdd ( TypeSearch . classWithSameName ( param , documentText ) , typeHints ) ;
63- this . tryAdd ( TypeSearch . hintOfSimilarParam ( param , documentText ) , typeHints ) ;
64-
6549 let typeName = this . getTypeParamEndsWith ( param , "_" ) ;
50+ this . _typingImported = await typingSearch ;
6651 if ( typeName ) {
6752 this . add ( typeName , typeHints ) ;
68- this . tryAddTypingHint ( await typingSearch , typeName , typeHints ) ;
69- wsSearcher . cancel ( ) ;
70- await workspaceSearch ;
53+ this . tryAddTypingHint ( typeName , typeHints ) ;
54+ }
55+
56+ if (
57+ typeHints . length > 0
58+ || this . tryAdd ( TypeSearch . hintOfSimilarParam ( param , documentText ) , typeHints )
59+ || this . tryAdd ( TypeSearch . classWithSameName ( param , documentText ) , typeHints )
60+ ) {
7161 return typeHints ;
7262 }
7363
7464 const searchResult = await variableSearch ;
7565
7666 if ( searchResult !== null && ! TypeSearch . invalidTernaryOperator ( searchResult ) ) {
7767 this . add ( searchResult . typeName , typeHints ) ;
78- this . tryAddTypingHints ( await typingSearch , searchResult , typeHints ) ;
68+ this . tryAddTypingHints ( searchResult , typeHints ) ;
7969 this . tryAdd ( this . typeGuessFor ( param , typeHints ) , typeHints ) ;
8070 } else {
8171 typeName = this . getTypeParamEndsWith ( param , "" ) ;
8272 if ( typeName ) {
8373 this . add ( typeName , typeHints ) ;
84- this . tryAddTypingHint ( await typingSearch , typeName , typeHints ) ;
74+ this . tryAddTypingHint ( typeName , typeHints ) ;
8575 } else {
8676 this . tryAdd ( this . typeGuessFor ( param , typeHints ) , typeHints ) ;
8777 }
88- }
89-
90- this . tryAdd ( await workspaceSearch , typeHints ) ;
78+ }
9179 return typeHints ;
9280 }
9381
@@ -97,7 +85,7 @@ export class TypeHintProvider {
9785 public remainingTypeHints ( ) : PythonType [ ] {
9886 const result : PythonType [ ] = [ ] ;
9987
100- for ( const type of Object . values ( this . typeContainer ) ) {
88+ for ( const type of Object . values ( this . _typeContainer ) ) {
10189 if ( this . hintNotProvided ( type . name ) ) {
10290 result . push ( type . name ) ;
10391 }
@@ -110,7 +98,11 @@ export class TypeHintProvider {
11098 * Returns hints for the typing module that have not been provided yet.
11199 */
112100 public remainingTypingHints ( ) : string [ ] {
113- return this . typingHintProvider . getRemainingHints ( ) ;
101+ return this . _typingHintProvider . getRemainingHints ( ) ;
102+ }
103+
104+ public hintNotProvided ( typeHint : string ) : boolean {
105+ return ! this . _providedTypeHints . includes ( typeHint ) ;
114106 }
115107
116108 /**
@@ -153,27 +145,21 @@ export class TypeHintProvider {
153145 type = type . trim ( ) ;
154146 if ( this . hintNotProvided ( type ) ) {
155147 typeHints . push ( type ) ;
156- this . providedTypeHints . push ( type ) ;
148+ this . _providedTypeHints . push ( type ) ;
157149 }
158150 }
159-
160- private hintNotProvided ( type : string ) : boolean {
161- return ! this . providedTypeHints . includes ( type ) ;
162- }
163151
164- private tryAdd ( type : string | null , typeHints : string [ ] ) {
152+ private tryAdd ( type : string | null , typeHints : string [ ] ) : boolean {
165153 if ( type ) {
166154 this . add ( type , typeHints ) ;
155+ return true ;
167156 }
157+ return false ;
168158 }
169159
170- private tryAddTypingHints (
171- typingFound : boolean ,
172- searchResult : VariableSearchResult | null ,
173- typeHints : string [ ]
174- ) {
175- if ( typingFound ) {
176- const hints : string [ ] | null = this . typingHintProvider . getHints ( searchResult ) ;
160+ private tryAddTypingHints ( searchResult : VariableSearchResult | null , typeHints : string [ ] ) {
161+ if ( this . _typingImported ) {
162+ const hints : string [ ] | null = this . _typingHintProvider . getHints ( searchResult ) ;
177163 if ( hints ) {
178164 for ( const hint of hints ) {
179165 this . add ( hint , typeHints ) ;
@@ -182,10 +168,9 @@ export class TypeHintProvider {
182168 }
183169 }
184170
185- private tryAddTypingHint ( typingImported : boolean , typeName : string , typeHints : string [ ] ) {
186- this . _typingImported = typingImported ;
187- if ( typingImported ) {
188- const typingHint = this . typingHintProvider . getHint ( typeName ) ;
171+ private tryAddTypingHint ( typeName : string , typeHints : string [ ] ) {
172+ if ( this . _typingImported ) {
173+ const typingHint = this . _typingHintProvider . getHint ( typeName ) ;
189174 if ( typingHint ) {
190175 this . add ( typingHint , typeHints ) ;
191176 }
0 commit comments