@@ -14,8 +14,6 @@ import {
14
14
commands ,
15
15
Disposable ,
16
16
l10n ,
17
- Range ,
18
- Selection ,
19
17
TextDocument ,
20
18
window ,
21
19
WorkspaceEdit ,
@@ -38,6 +36,8 @@ import { logger } from 'base/common/log/log';
38
36
39
37
import { type AutoDevExtension } from '../../AutoDevExtension' ;
40
38
39
+ type CodeLensItemType = 'quickChat' | 'explainCode' | 'optimizeCode' | 'autoComment' | 'autoTest' | 'customAction' ;
40
+
41
41
export class AutoDevCodeLensProvider implements CodeLensProvider {
42
42
private config : ConfigurationService ;
43
43
private lsp : ILanguageServiceProvider ;
@@ -134,22 +134,31 @@ export class AutoDevCodeLensProvider implements CodeLensProvider {
134
134
return this . config . get < string > ( 'codelensDisplayMode' ) === 'collapse' ;
135
135
}
136
136
137
+ getDisplayCodelensItems ( ) {
138
+ return new Set ( this . config . get < CodeLensItemType [ ] > ( 'codelensDislayItems' ) ) ;
139
+ }
140
+
137
141
hasCustomPromps ( ) {
138
- return this . autodev . teamPromptsBuilder . teamPrompts ( ) . length ;
142
+ return this . autodev . teamPromptsBuilder . teamPrompts ( ) . length > 0 ;
139
143
}
140
144
141
145
async provideCodeLenses ( document : TextDocument , token : CancellationToken ) {
142
146
if ( isFileTooLarge ( document ) || ! isSupportedLanguage ( document . languageId ) ) {
143
147
return [ ] ;
144
148
}
145
149
150
+ const displayItems = this . getDisplayCodelensItems ( ) ;
151
+ if ( displayItems . size === 0 ) {
152
+ return [ ] ;
153
+ }
154
+
146
155
const elements = await this . parseToNamedElements ( document ) ;
147
156
148
157
if ( token . isCancellationRequested || elements . length === 0 ) {
149
158
return [ ] ;
150
159
}
151
160
152
- const groups = this . buildCodeLensGroups ( elements , document , token ) ;
161
+ const groups = this . buildCodeLensGroups ( displayItems , elements , document , token ) ;
153
162
if ( groups . length === 0 ) {
154
163
return [ ] ;
155
164
}
@@ -173,53 +182,87 @@ export class AutoDevCodeLensProvider implements CodeLensProvider {
173
182
} ) ;
174
183
}
175
184
176
- private buildCodeLensGroups ( elements : NamedElement [ ] , document : TextDocument , token : CancellationToken ) {
185
+ private buildCodeLensGroups (
186
+ displaySet : Set < CodeLensItemType > ,
187
+ elements : NamedElement [ ] ,
188
+ document : TextDocument ,
189
+ token : CancellationToken ,
190
+ ) {
177
191
const result : CodeLens [ ] [ ] = [ ] ;
192
+ const hasCustomPromps = this . hasCustomPromps ( ) ;
178
193
179
194
for ( const element of elements ) {
180
195
const codelenses : CodeLens [ ] = [ ] ;
181
196
182
- codelenses . push (
183
- new CodeLens ( element . identifierRange , {
184
- title : l10n . t ( 'Quick Chat' ) ,
185
- command : CMD_CODELENS_QUICK_CHAT ,
186
- arguments : [ document , element ] ,
187
- } ) ,
188
- new CodeLens ( element . identifierRange , {
189
- title : l10n . t ( 'Explain Code' ) ,
190
- command : CMD_CODELENS_EXPLAIN_CODE ,
191
- arguments : [ document , element ] ,
192
- } ) ,
193
- new CodeLens ( element . identifierRange , {
194
- title : l10n . t ( 'Optimize Code' ) ,
195
- command : CMD_CODELENS_OPTIMIZE_CODE ,
196
- arguments : [ document , element ] ,
197
- } ) ,
198
- ) ;
199
-
200
- if ( ! element . isTestFile ( ) ) {
201
- codelenses . push (
202
- new CodeLens ( element . identifierRange , {
203
- title : l10n . t ( 'AutoComment' ) ,
204
- command : CMD_CODELENS_GEN_DOCSTRING ,
205
- arguments : [ document , element ] ,
206
- } ) ,
207
- new CodeLens ( element . identifierRange , {
208
- title : l10n . t ( 'AutoTest' ) ,
209
- command : CMD_CODELENS_CREATE_UNIT_TEST ,
210
- arguments : [ document , element , new WorkspaceEdit ( ) ] ,
211
- } ) ,
212
- ) ;
213
- }
214
-
215
- if ( this . hasCustomPromps ( ) ) {
216
- codelenses . push (
217
- new CodeLens ( element . identifierRange , {
218
- title : l10n . t ( 'Custom Action' ) ,
219
- command : CMD_CODELENS_SHOW_CUSTOM_ACTION ,
220
- arguments : [ document , element ] ,
221
- } ) ,
222
- ) ;
197
+ for ( const type of displaySet ) {
198
+ if ( type === 'quickChat' ) {
199
+ codelenses . push (
200
+ new CodeLens ( element . identifierRange , {
201
+ title : l10n . t ( 'Quick Chat' ) ,
202
+ command : CMD_CODELENS_QUICK_CHAT ,
203
+ arguments : [ document , element ] ,
204
+ } ) ,
205
+ ) ;
206
+ continue ;
207
+ }
208
+
209
+ if ( type === 'explainCode' ) {
210
+ codelenses . push (
211
+ new CodeLens ( element . identifierRange , {
212
+ title : l10n . t ( 'Explain Code' ) ,
213
+ command : CMD_CODELENS_EXPLAIN_CODE ,
214
+ arguments : [ document , element ] ,
215
+ } ) ,
216
+ ) ;
217
+ continue ;
218
+ }
219
+ if ( type === 'optimizeCode' ) {
220
+ codelenses . push (
221
+ new CodeLens ( element . identifierRange , {
222
+ title : l10n . t ( 'Optimize Code' ) ,
223
+ command : CMD_CODELENS_OPTIMIZE_CODE ,
224
+ arguments : [ document , element ] ,
225
+ } ) ,
226
+ ) ;
227
+ continue ;
228
+ }
229
+
230
+ if ( type === 'autoComment' ) {
231
+ codelenses . push (
232
+ new CodeLens ( element . identifierRange , {
233
+ title : l10n . t ( 'AutoComment' ) ,
234
+ command : CMD_CODELENS_GEN_DOCSTRING ,
235
+ arguments : [ document , element ] ,
236
+ } ) ,
237
+ ) ;
238
+ continue ;
239
+ }
240
+
241
+ if ( type === 'autoTest' ) {
242
+ if ( ! element . isTestFile ( ) ) {
243
+ codelenses . push (
244
+ new CodeLens ( element . identifierRange , {
245
+ title : l10n . t ( 'AutoTest' ) ,
246
+ command : CMD_CODELENS_CREATE_UNIT_TEST ,
247
+ arguments : [ document , element , new WorkspaceEdit ( ) ] ,
248
+ } ) ,
249
+ ) ;
250
+ }
251
+ continue ;
252
+ }
253
+
254
+ if ( type === 'customAction' ) {
255
+ if ( hasCustomPromps ) {
256
+ codelenses . push (
257
+ new CodeLens ( element . identifierRange , {
258
+ title : l10n . t ( 'Custom Action' ) ,
259
+ command : CMD_CODELENS_SHOW_CUSTOM_ACTION ,
260
+ arguments : [ document , element ] ,
261
+ } ) ,
262
+ ) ;
263
+ }
264
+ continue ;
265
+ }
223
266
}
224
267
225
268
result . push ( codelenses ) ;
0 commit comments