@@ -19,20 +19,37 @@ import { TypingHintProvider } from "./typingHintProvider";
19
19
20
20
export abstract class CompletionProvider {
21
21
22
- protected bottomOfListSortPrefix : number = 999 ;
22
+ protected itemSortPrefix : number = 90 ;
23
23
24
24
/**
25
25
* Push type hints to the end of an array of completion items.
26
26
*/
27
- protected pushHintsToItems ( typeHints : string [ ] , completionItems : CompletionItem [ ] ) {
28
- const sortTextPrefix = this . bottomOfListSortPrefix . toString ( ) ;
29
- for ( const hint of typeHints ) {
30
- const item = new CompletionItem ( this . labelFor ( hint ) , CompletionItemKind . TypeParameter ) ;
31
- item . sortText = sortTextPrefix + hint ;
32
- completionItems . push ( item ) ;
27
+ protected pushHintsToItems ( typeHints : string [ ] , completionItems : CompletionItem [ ] , firstItemSelected : boolean ) {
28
+ const sortTextPrefix = this . itemSortPrefix . toString ( ) ;
29
+ completionItems . push (
30
+ firstItemSelected
31
+ ? this . selectedCompletionItem ( typeHints [ 0 ] )
32
+ : this . newCompletionitem ( typeHints [ 0 ] , sortTextPrefix )
33
+ ) ;
34
+
35
+ for ( let i = 1 ; i < typeHints . length ; i ++ ) {
36
+ completionItems . push ( this . newCompletionitem ( typeHints [ i ] , sortTextPrefix ) ) ;
33
37
}
34
38
}
35
39
40
+ private newCompletionitem = ( hint : string , sortTextPrefix : string ) : CompletionItem => {
41
+ const item = new CompletionItem ( this . labelFor ( hint ) , CompletionItemKind . TypeParameter ) ;
42
+ item . sortText = sortTextPrefix + hint ;
43
+ return item ;
44
+ } ;
45
+
46
+ protected selectedCompletionItem ( typeHint : string , sortTextPrefix : string = "0b" ) : CompletionItem {
47
+ let item = new CompletionItem ( this . labelFor ( typeHint ) , CompletionItemKind . TypeParameter ) ;
48
+ item . sortText = `${ sortTextPrefix } ${ typeHint } ` ;
49
+ item . preselect = true ;
50
+ return item ;
51
+ }
52
+
36
53
protected labelFor ( typeHint : string ) : string {
37
54
return " " + typeHint ;
38
55
}
@@ -74,33 +91,28 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
74
91
const typeContainer = getDataTypeContainer ( ) ;
75
92
const provider = new TypeHintProvider ( typeContainer ) ;
76
93
const wsSearcher = new WorkspaceSearcher ( doc . uri , this . settings , typeContainer ) ;
77
-
94
+ let estimations : string [ ] = [ ] ;
95
+
78
96
if ( param ) {
79
97
const workspaceHintSearch = this . settings . workspaceSearchEnabled
80
98
? this . workspaceHintSearch ( param , wsSearcher , documentText )
81
99
: null ;
82
100
try {
83
- const estimations = await provider . estimateTypeHints ( param , documentText ) ;
101
+ estimations = await provider . estimateTypeHints ( param , documentText ) ;
84
102
if ( estimations . length > 0 ) {
85
103
this . pushEstimationsToItems ( estimations , items ) ;
86
104
wsSearcher . cancel ( ) ;
87
105
}
88
106
} catch {
89
107
}
90
108
91
- const sortTextPrefix = ( this . bottomOfListSortPrefix - 1 ) . toString ( ) ;
92
- for ( const hint of provider . remainingTypeHints ( ) ) {
93
- let item = new CompletionItem ( this . labelFor ( hint ) , CompletionItemKind . TypeParameter ) ;
94
- item . sortText = sortTextPrefix + hint ;
95
- items . push ( item ) ;
96
- }
97
-
98
- if ( provider . typingImported ) {
99
- this . pushHintsToItems ( provider . remainingTypingHints ( ) , items ) ;
100
- }
109
+ this . pushHintsToItems ( provider . remainingTypeHints ( ) , items , estimations . length === 0 ) ;
110
+ this . itemSortPrefix ++ ;
111
+ this . pushHintsToItems ( provider . remainingTypingHints ( ) , items , false ) ;
112
+
101
113
const hint = await workspaceHintSearch ;
102
114
if ( hint && provider . hintNotProvided ( hint ) ) {
103
- items . unshift ( this . toSelectedCompletionItem ( hint ) ) ;
115
+ items . unshift ( this . selectedCompletionItem ( hint , "0a" ) ) ;
104
116
}
105
117
return Promise . resolve ( new CompletionList ( items , false ) ) ;
106
118
}
@@ -131,7 +143,7 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
131
143
private pushEstimationsToItems ( typeHints : string [ ] , items : CompletionItem [ ] ) {
132
144
133
145
if ( typeHints . length > 0 ) {
134
- items . push ( this . toSelectedCompletionItem ( typeHints [ 0 ] ) ) ;
146
+ items . push ( this . selectedCompletionItem ( typeHints [ 0 ] ) ) ;
135
147
136
148
for ( let i = 1 ; i < typeHints . length ; i ++ ) {
137
149
let item = new CompletionItem ( this . labelFor ( typeHints [ i ] ) , CompletionItemKind . TypeParameter ) ;
@@ -141,13 +153,6 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
141
153
}
142
154
}
143
155
144
- private toSelectedCompletionItem ( typeHint : string ) : CompletionItem {
145
- let item = new CompletionItem ( this . labelFor ( typeHint ) , CompletionItemKind . TypeParameter ) ;
146
- item . sortText = `0${ typeHint } ` ;
147
- item . preselect = true ;
148
- return item ;
149
- }
150
-
151
156
private shouldProvideItems ( precedingText : string , activePos : Position , doc : TextDocument ) : boolean {
152
157
153
158
if ( activePos . character > 0 && ! / # / . test ( precedingText ) ) {
@@ -185,11 +190,13 @@ export class ReturnHintCompletionProvider extends CompletionProvider implements
185
190
186
191
if ( this . shouldProvideItems ( line , pos ) ) {
187
192
const provider = new TypingHintProvider ( getDataTypeContainer ( ) ) ;
188
- await provider . detectTypingImport ( doc . getText ( ) ) ;
189
- this . pushHintsToItems ( provider . getRemainingHints ( ) , items ) ;
190
- this . bottomOfListSortPrefix -- ;
193
+ const detectTypingImport = provider . detectTypingImport ( doc . getText ( ) ) ;
194
+
195
+ this . pushHintsToItems ( Object . values ( PythonType ) , items , true ) ;
196
+ this . itemSortPrefix ++ ;
191
197
192
- this . pushHintsToItems ( Object . values ( PythonType ) , items ) ;
198
+ await detectTypingImport ;
199
+ this . pushHintsToItems ( provider . getRemainingHints ( ) , items , false ) ;
193
200
}
194
201
return Promise . resolve ( new CompletionList ( items , false ) ) ;
195
202
}
0 commit comments