@@ -2,6 +2,7 @@ import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
22
33import { getPromptFileContent } from './promptContent' ;
44import type {
5+ CodeCompletionConfig ,
56 DiscardReason ,
67 EnrichedCompletion ,
78 ICodeCompletionAPI ,
@@ -10,27 +11,61 @@ import type {
1011 InternalSuggestion ,
1112} from './types' ;
1213
14+ const DEFAULT_CONFIG : Required < CodeCompletionConfig > = {
15+ debounceTime : 200 ,
16+ textLimits : {
17+ beforeCursor : 8000 ,
18+ afterCursor : 1000 ,
19+ } ,
20+ telemetry : {
21+ enabled : true ,
22+ } ,
23+ suggestionCache : {
24+ enabled : true ,
25+ } ,
26+ } ;
27+
1328export class CodeCompletionService implements ICodeCompletionService {
1429 private prevSuggestions : InternalSuggestion [ ] = [ ] ;
1530 private timer : number | null = null ;
1631 private readonly api : ICodeCompletionAPI ;
1732 private readonly telemetry : ITelemetryService ;
18- private readonly editor ?: monaco . editor . IStandaloneCodeEditor ;
33+ private readonly config : Required < CodeCompletionConfig > ;
1934
2035 constructor (
2136 api : ICodeCompletionAPI ,
2237 telemetry : ITelemetryService ,
23- editor ?: monaco . editor . IStandaloneCodeEditor ,
38+ userConfig ?: CodeCompletionConfig ,
2439 ) {
2540 this . api = api ;
2641 this . telemetry = telemetry ;
27- this . editor = editor ;
42+ // Merge user config with defaults, ensuring all properties exist
43+ this . config = {
44+ ...DEFAULT_CONFIG ,
45+ ...userConfig ,
46+ textLimits : {
47+ ...DEFAULT_CONFIG . textLimits ,
48+ ...( userConfig ?. textLimits || { } ) ,
49+ } ,
50+ telemetry : {
51+ ...DEFAULT_CONFIG . telemetry ,
52+ ...( userConfig ?. telemetry || { } ) ,
53+ } ,
54+ suggestionCache : {
55+ ...DEFAULT_CONFIG . suggestionCache ,
56+ ...( userConfig ?. suggestionCache || { } ) ,
57+ } ,
58+ } ;
2859 }
2960
3061 handleItemDidShow (
3162 _completions : monaco . languages . InlineCompletions < EnrichedCompletion > ,
3263 item : EnrichedCompletion ,
3364 ) {
65+ if ( ! this . config . suggestionCache . enabled ) {
66+ return ;
67+ }
68+
3469 for ( const suggests of this . prevSuggestions ) {
3570 for ( const completion of suggests . items ) {
3671 if ( completion . pristine === item . pristine ) {
@@ -47,10 +82,13 @@ export class CodeCompletionService implements ICodeCompletionService {
4782 _context : monaco . languages . InlineCompletionContext ,
4883 _token : monaco . CancellationToken ,
4984 ) {
50- const cachedCompletions = this . getCachedCompletion ( model , position ) ;
51- if ( cachedCompletions . length ) {
52- return { items : cachedCompletions } ;
85+ if ( this . config . suggestionCache . enabled ) {
86+ const cachedCompletions = this . getCachedCompletion ( model , position ) ;
87+ if ( cachedCompletions . length ) {
88+ return { items : cachedCompletions } ;
89+ }
5390 }
91+
5492 while ( this . prevSuggestions . length > 0 ) {
5593 this . dismissCompletion ( this . prevSuggestions . pop ( ) ) ;
5694 }
@@ -89,13 +127,14 @@ export class CodeCompletionService implements ICodeCompletionService {
89127 this . telemetry . sendAcceptTelemetry ( requestId , suggestionText ) ;
90128 }
91129
92- commandDiscard ( reason : DiscardReason = 'OnCancel' ) : void {
130+ commandDiscard (
131+ reason : DiscardReason = 'OnCancel' ,
132+ editor : monaco . editor . IStandaloneCodeEditor ,
133+ ) : void {
93134 while ( this . prevSuggestions . length > 0 ) {
94135 this . discardCompletion ( reason , this . prevSuggestions . pop ( ) ) ;
95136 }
96- if ( this . editor ) {
97- this . editor . trigger ( undefined , 'editor.action.inlineSuggest.hide' , undefined ) ;
98- }
137+ editor . trigger ( undefined , 'editor.action.inlineSuggest.hide' , undefined ) ;
99138 }
100139
101140 emptyCache ( ) {
@@ -165,12 +204,15 @@ export class CodeCompletionService implements ICodeCompletionService {
165204 window . clearTimeout ( this . timer ) ;
166205 }
167206 await new Promise ( ( r ) => {
168- this . timer = window . setTimeout ( r , 200 ) ;
207+ this . timer = window . setTimeout ( r , this . config . debounceTime ) ;
169208 } ) ;
170209 let suggestions : EnrichedCompletion [ ] = [ ] ;
171210 let requestId = '' ;
172211 try {
173- const data = getPromptFileContent ( model , position ) ;
212+ const data = getPromptFileContent ( model , position , {
213+ beforeCursor : this . config . textLimits . beforeCursor ,
214+ afterCursor : this . config . textLimits . afterCursor ,
215+ } ) ;
174216 if ( ! data ) {
175217 return { suggestions : [ ] } ;
176218 }
0 commit comments