@@ -18,120 +18,136 @@ export class FauxpilotCompletionProvider implements InlineCompletionItemProvider
18
18
private statusBar : StatusBarItem ;
19
19
private extConfig : WorkspaceConfiguration ;
20
20
private userPressKeyCount = 0 ;
21
- private baseUrl : string ;
22
21
23
22
constructor ( statusBar : StatusBarItem , extConfig : WorkspaceConfiguration ) {
24
23
this . statusBar = statusBar ;
25
24
this . extConfig = extConfig ;
26
- this . baseUrl = fauxpilotClient . BaseUrl ;
27
- this . baseUrl = fauxpilotClient . BaseUrl ;
28
25
}
29
26
30
27
//@ts -ignore
31
28
// because ASYNC and PROMISE
32
29
public async provideInlineCompletionItems ( document : TextDocument , position : Position , context : InlineCompletionContext , token : CancellationToken ) : ProviderResult < InlineCompletionItem [ ] | InlineCompletionList > {
33
- if ( ! fauxpilotClient . isEnabled ) {
34
- fauxpilotClient . log ( "Extension not enabled, skipping." ) ;
35
- return Promise . resolve ( ( [ ] as InlineCompletionItem [ ] ) ) ;
36
- }
30
+ fauxpilotClient . log ( `call inline: ${ position . line } :${ position . character } ` ) ;
37
31
38
- var fileExt = document . fileName . split ( '.' ) . pop ( ) ;
39
- if ( fileExt && fauxpilotClient . ExcludeFileExts . includes ( fileExt ) ) {
40
- // check if fileExt in array excludeFileExts
41
- fauxpilotClient . log ( "Ignore file ext: " + fileExt ) ;
42
- return [ ] ;
43
- }
32
+ try {
33
+ if ( ! fauxpilotClient . isEnabled ) {
34
+ fauxpilotClient . log ( "Extension not enabled, skipping." ) ;
35
+ return ;
36
+ }
44
37
45
- const prompt = this . getPrompt ( document , position ) ;
46
- let suggestionDelay = fauxpilotClient . SuggestionDelay ;
47
- if ( suggestionDelay > 0 ) {
48
- let holdPressId = ++ this . userPressKeyCount ;
49
- fauxpilotClient . log ( `try await ${ suggestionDelay } , ${ holdPressId } ` ) ;
50
- await delay ( suggestionDelay ) ;
51
- if ( holdPressId != this . userPressKeyCount ) {
52
- return [ ] ;
53
- }
54
- fauxpilotClient . log ( `after await, ${ holdPressId } , ${ this . userPressKeyCount } ` ) ;
55
- if ( token . isCancellationRequested ) {
56
- fauxpilotClient . log ( 'request cancelled.' ) ;
57
- return [ ] ;
38
+ var fileExt = document . fileName . split ( '.' ) . pop ( ) ;
39
+ if ( fileExt && fauxpilotClient . ExcludeFileExts . includes ( fileExt ) ) {
40
+ // check if fileExt in array excludeFileExts
41
+ fauxpilotClient . log ( "Ignore file ext: " + fileExt ) ;
42
+ return ;
58
43
}
59
- }
60
44
61
- // fauxpilotClient.log(`Requesting completion for prompt: ${prompt}`);
62
- fauxpilotClient . log ( `Requesting completion for prompt, length: ${ prompt ?. length ?? 0 } ` ) ;
45
+ const prompt = this . getPrompt ( document , position ) ;
46
+ let suggestionDelay = fauxpilotClient . SuggestionDelay ;
47
+ if ( suggestionDelay > 0 ) {
48
+ let holdPressId = ++ this . userPressKeyCount ;
49
+ fauxpilotClient . log ( `try await ${ suggestionDelay } , ${ holdPressId } ` ) ;
50
+ await delay ( suggestionDelay ) ;
51
+ if ( holdPressId != this . userPressKeyCount ) {
52
+ return ;
53
+ }
54
+ fauxpilotClient . log ( `after await, ${ holdPressId } , ${ this . userPressKeyCount } ` ) ;
55
+ if ( token . isCancellationRequested ) {
56
+ fauxpilotClient . log ( 'request cancelled.' ) ;
57
+ return ;
58
+ }
59
+ }
63
60
64
- if ( this . isNil ( prompt ) ) {
65
- fauxpilotClient . log ( "Prompt is empty, skipping" ) ;
66
- return Promise . resolve ( ( [ ] as InlineCompletionItem [ ] ) ) ;
67
- }
61
+ // fauxpilotClient.log(`Requesting completion for prompt: ${prompt}`);
62
+ fauxpilotClient . log ( `Requesting completion for prompt, length: ${ prompt ?. length ?? 0 } ` ) ;
68
63
69
- const currentTimestamp = Date . now ( ) ;
70
- const currentId = nextId ( ) ;
71
- this . cachedPrompts . set ( currentId , currentTimestamp ) ;
72
-
73
- // check there is no newer request util this.request_status is done
74
- while ( this . requestStatus === "pending" ) {
75
- fauxpilotClient . log ( "pending, and Waiting for response..." ) ;
76
- await delay ( 200 ) ;
77
- fauxpilotClient . log ( "current id = " + currentId + " request status = " + this . requestStatus ) ;
78
- if ( this . newestTimestamp ( ) > currentTimestamp ) {
79
- fauxpilotClient . log ( "newest timestamp=" + this . newestTimestamp ( ) + "current timestamp=" + currentTimestamp ) ;
80
- fauxpilotClient . log ( "Newer request is pending, skipping" ) ;
81
- this . cachedPrompts . delete ( currentId ) ;
64
+ if ( this . isNil ( prompt ) ) {
65
+ fauxpilotClient . log ( "Prompt is empty, skipping" ) ;
82
66
return Promise . resolve ( ( [ ] as InlineCompletionItem [ ] ) ) ;
83
67
}
84
- }
85
68
86
- if ( token . isCancellationRequested ) {
87
- fauxpilotClient . log ( 'request cancelled.' ) ;
88
- return [ ] ;
89
- }
90
-
91
- fauxpilotClient . log ( "Calling OpenAi, prompt length: " + prompt ?. length ) ;
92
- const promptStr = prompt ?. toString ( ) ;
93
- if ( ! promptStr ) {
94
- return [ ] ;
95
- }
69
+ const currentTimestamp = Date . now ( ) ;
70
+ const currentId = nextId ( ) ;
71
+ this . cachedPrompts . set ( currentId , currentTimestamp ) ;
72
+
73
+ // check there is no newer request util this.request_status is done
74
+ while ( this . requestStatus === "pending" ) {
75
+ fauxpilotClient . log ( "pending, and Waiting for response..." ) ;
76
+ await delay ( 200 ) ;
77
+ fauxpilotClient . log ( "current id = " + currentId + " request status = " + this . requestStatus ) ;
78
+ if ( this . newestTimestamp ( ) > currentTimestamp ) {
79
+ fauxpilotClient . log ( "newest timestamp=" + this . newestTimestamp ( ) + "current timestamp=" + currentTimestamp ) ;
80
+ fauxpilotClient . log ( "Newer request is pending, skipping" ) ;
81
+ this . cachedPrompts . delete ( currentId ) ;
82
+ return Promise . resolve ( ( [ ] as InlineCompletionItem [ ] ) ) ;
83
+ }
84
+ }
96
85
97
- fauxpilotClient . log ( "current id = " + currentId + "set request status to pending" ) ;
98
- this . requestStatus = "pending" ;
99
- this . statusBar . tooltip = "Fauxpilot - Working" ;
100
- this . statusBar . text = "$(loading~spin)" ;
101
- return fetch ( promptStr ) . then ( ( response ) => {
102
- this . statusBar . text = "$(light-bulb)" ;
103
- // if (token.isCancellationRequested) {
104
- // fauxpilotClient.log('request cancelled.');
105
- // return [];
106
- // }
107
- var result = this . toInlineCompletions ( response , position ) ;
108
- fauxpilotClient . log ( "inline completions array length: " + result . length ) ;
109
- return result ;
110
- } ) . finally ( ( ) => {
111
- fauxpilotClient . log ( "current id = " + currentId + " set request status to done" ) ;
112
- this . requestStatus = "done" ;
113
- this . cachedPrompts . delete ( currentId ) ;
114
- } ) ;
86
+ if ( token . isCancellationRequested ) {
87
+ fauxpilotClient . log ( 'request cancelled.' ) ;
88
+ return ;
89
+ }
115
90
91
+ fauxpilotClient . log ( "Calling OpenAi, prompt length: " + prompt ?. length ) ;
92
+ const promptStr = prompt ?. toString ( ) ;
93
+ if ( ! promptStr ) {
94
+ return ;
95
+ }
96
+ // fauxpilotClient.log(promptStr);
97
+
98
+ fauxpilotClient . log ( "current id = " + currentId + " set request status to pending" ) ;
99
+ this . requestStatus = "pending" ;
100
+ this . statusBar . tooltip = "Fauxpilot - Working" ;
101
+ this . statusBar . text = "$(loading~spin)" ;
102
+ return fetch ( promptStr ) . then ( ( response ) => {
103
+ this . statusBar . text = "$(light-bulb)" ;
104
+ // if (token.isCancellationRequested) {
105
+ // fauxpilotClient.log('request cancelled.');
106
+ // return [];
107
+ // }
108
+ var result = this . toInlineCompletions ( response , position ) ;
109
+ fauxpilotClient . log ( "inline completions array length: " + result . length ) ;
110
+ return result ;
111
+ } ) . finally ( ( ) => {
112
+ fauxpilotClient . log ( "current id = " + currentId + " set request status to done" ) ;
113
+ this . requestStatus = "done" ;
114
+ this . cachedPrompts . delete ( currentId ) ;
115
+ } ) ;
116
+
117
+ } catch ( error ) {
118
+ console . log ( 'An error occurred: ' + error ) ;
119
+ if ( typeof error === 'string' ) {
120
+ fauxpilotClient . log ( "Catch an error: " + error ) ;
121
+ } else if ( error instanceof Error ) {
122
+ fauxpilotClient . log ( `Catch an error, ${ error . name } : ${ error . message } ` ) ;
123
+ fauxpilotClient . log ( `stack: ${ error . stack } ` ) ;
124
+ } else {
125
+ fauxpilotClient . log ( 'an unknown error!' ) ;
126
+ }
127
+ }
116
128
}
117
129
118
- private getPrompt ( document : TextDocument , position : Position ) : String | undefined {
119
- const promptLinesCount = this . extConfig . get ( "maxLines" ) as number ;
130
+ private getPrompt ( document : TextDocument , position : Position ) : string {
131
+ const promptLinesCount = fauxpilotClient . MaxLines ;
120
132
121
133
/*
122
134
Put entire file in prompt if it's small enough, otherwise only
123
135
take lines above the cursor and from the beginning of the file.
124
136
*/
125
- if ( document . lineCount <= promptLinesCount ) {
137
+
138
+ // Only determine the content before the cursor
139
+ const currentLine = position . line ; // document.lineCount
140
+ if ( currentLine <= promptLinesCount ) {
126
141
const range = new Range ( 0 , 0 , position . line , position . character ) ;
127
142
return document . getText ( range ) ;
128
143
} else {
129
144
const leadingLinesCount = Math . floor ( LEADING_LINES_PROP * promptLinesCount ) ;
130
145
const prefixLinesCount = promptLinesCount - leadingLinesCount ;
131
- const firstPrefixLine = position . line - prefixLinesCount ;
146
+ const firstPrefixLine = Math . max ( position . line - prefixLinesCount , 0 ) ;
147
+
148
+ const leading = document . getText ( new Range ( 0 , 0 , leadingLinesCount , 200 ) ) ;
132
149
const prefix = document . getText ( new Range ( firstPrefixLine , 0 , position . line , position . character ) ) ;
133
- const leading = document . getText ( new Range ( 0 , 0 , leadingLinesCount , 0 ) ) ;
134
- return leading + prefix ;
150
+ return `${ leading } \n${ prefix } ` ;
135
151
}
136
152
}
137
153
@@ -155,7 +171,7 @@ export class FauxpilotCompletionProvider implements InlineCompletionItemProvider
155
171
}
156
172
157
173
fauxpilotClient . log ( 'Get choice text: ' + choice1Text ) ;
158
- fauxpilotClient . log ( '---------END-OF-CHOICE-TEXT-----------' ) ;
174
+ // fauxpilotClient.log('---------END-OF-CHOICE-TEXT-----------');
159
175
if ( choice1Text . trim ( ) . length <= 0 ) {
160
176
return [ ] ;
161
177
}
0 commit comments