11import { TextDocument } from "vscode" ;
22import * as search from "./codeSearch" ;
3+ import { Type , returnHintTrigger } from "./syntax" ;
4+ import { ToTitleCase } from "./utils" ;
35
46export class TypeResolver {
57
8+ private likelyTypes : Type [ ] = [ Type . String , Type . List , Type . Dict , Type . Bool , Type . Int , Type . Tuple , Type . Float ] ;
9+
610 /**
7- * Estimates the type of a parameter.
11+ * Estimates the type of a parameter,
12+ * by searching the document for an initialized variable with the same name.
13+ *
814 * @param doc The document to search.
915 * @param param The parameter name.
1016 */
1117 public EstimateType ( doc : TextDocument , param : string ) : string | null {
1218 const documentText = doc . getText ( ) ;
13- let type : string | null = "" ;
14- const match = new RegExp ( `^[ \t]*${ param } *=.` , "m" ) . exec ( documentText ) ;
19+
20+ let type = this . GetTypeIfEndsWith ( param , "_" , ...this . likelyTypes ) ;
21+ if ( type ) {
22+ return type ;
23+ }
24+
25+ const variableMatch = new RegExp ( `^[ \t]*${ param } *=.` , "m" ) . exec ( documentText ) ;
1526
16- if ( match && match [ 0 ] ) {
17- const valueStartPosition = doc . positionAt ( match . index + match [ 0 ] . length ) ;
27+ if ( variableMatch && variableMatch [ 0 ] ) {
28+ const valueStartPosition = doc . positionAt ( variableMatch . index + variableMatch [ 0 ] . length ) ;
1829 const line = doc . lineAt ( valueStartPosition ) ;
1930
20- type = search . detectBasicType ( line . text ) ;
31+ let typeName = search . detectBasicType ( line . text ) ;
2132
22- if ( type === null ) {
23- type = search . detectNonBasicType ( line . text , documentText ) ;
33+ if ( typeName === null ) {
34+ typeName = search . detectNonBasicType ( line . text , documentText ) ;
2435 }
2536
26- if ( type !== null ) {
27- if ( ! search . invalidTernaryOperator ( type , line . text ) ) {
28- return type ;
37+ if ( typeName !== null ) {
38+ if ( ! search . invalidTernaryOperator ( typeName , line . text ) ) {
39+ return typeName ;
2940 }
3041 }
3142 }
43+ type = this . GuessType ( param ) ;
44+ return type ? type : this . GetTypeIfEndsWith ( param , "" , Type . List , Type . Dict ) ;
45+ }
46+
47+ private GetTypeIfEndsWith ( param : string , prefix : string , ...typesToCheckFor : Type [ ] ) : Type | null {
48+
49+ for ( const type of typesToCheckFor ) {
50+ if ( param . endsWith ( `${ prefix } ${ type } ` ) ) {
51+ return type ;
52+ }
53+ }
3254 return null ;
3355 }
56+
57+ private GuessType ( param : string ) : Type | null {
58+ if ( param in this . TypeGuesses ) {
59+ return this . TypeGuesses [ param ] ;
60+ }
61+ return null ;
62+ }
63+
64+ /**
65+ * Guesses used if a param with the same name is not found in the active document.
66+ */
67+ private TypeGuesses : { [ key : string ] : Type } = {
68+ "string" : Type . String ,
69+ "text" : Type . String ,
70+ "path" : Type . String ,
71+ "url" : Type . String ,
72+ "uri" : Type . String ,
73+ "fullpath" : Type . String ,
74+ "full_path" : Type . String ,
75+ "number" : Type . Int ,
76+ "num" : Type . Int
77+ } ;
3478}
0 commit comments