1
1
import { TextDocument } from "vscode" ;
2
2
import * as search from "./codeSearch" ;
3
+ import { Type , returnHintTrigger } from "./syntax" ;
4
+ import { ToTitleCase } from "./utils" ;
3
5
4
6
export class TypeResolver {
5
7
8
+ private likelyTypes : Type [ ] = [ Type . String , Type . List , Type . Dict , Type . Bool , Type . Int , Type . Tuple , Type . Float ] ;
9
+
6
10
/**
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
+ *
8
14
* @param doc The document to search.
9
15
* @param param The parameter name.
10
16
*/
11
17
public EstimateType ( doc : TextDocument , param : string ) : string | null {
12
18
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 ) ;
15
26
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 ) ;
18
29
const line = doc . lineAt ( valueStartPosition ) ;
19
30
20
- type = search . detectBasicType ( line . text ) ;
31
+ let typeName = search . detectBasicType ( line . text ) ;
21
32
22
- if ( type === null ) {
23
- type = search . detectNonBasicType ( line . text , documentText ) ;
33
+ if ( typeName === null ) {
34
+ typeName = search . detectNonBasicType ( line . text , documentText ) ;
24
35
}
25
36
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 ;
29
40
}
30
41
}
31
42
}
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
+ }
32
54
return null ;
33
55
}
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
+ } ;
34
78
}
0 commit comments