Skip to content

Commit e66a4f8

Browse files
authored
feat: custom display and sort codelens items (#57)
1 parent 69f35cf commit e66a4f8

File tree

4 files changed

+125
-56
lines changed

4 files changed

+125
-56
lines changed

package.json

+28-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
"description": "%configuration.enableRenameSuggestion.description%",
5959
"order": 1
6060
},
61+
"autodev.customPromptDir": {
62+
"type": "string",
63+
"default": "prompts",
64+
"description": "%configuration.customPromptDir.description%",
65+
"order": 2
66+
},
6167
"autodev.codelensDisplayMode": {
6268
"type": "string",
6369
"enum": [
@@ -69,12 +75,29 @@
6975
"%configuration.codelensDisplayMode.item.collapse%"
7076
],
7177
"description": "%configuration.codelensDisplayMode.description%",
72-
"default": "expand"
78+
"default": "expand",
79+
"order": 3
7380
},
74-
"autodev.customPromptDir": {
75-
"type": "string",
76-
"default": "prompts",
77-
"description": "%configuration.customPromptDir.description%",
81+
"autodev.codelensDislayItems": {
82+
"type": "array",
83+
"items": {
84+
"type": "string",
85+
"enum": [
86+
"quickChat",
87+
"explainCode",
88+
"optimizeCode",
89+
"autoComment",
90+
"autoTest",
91+
"customAction"
92+
]
93+
},
94+
"uniqueItems": true,
95+
"default": [
96+
"quickChat",
97+
"autoTest",
98+
"autoComment"
99+
],
100+
"description": "%configuration.codelensDisplayItems.description%",
78101
"order": 4
79102
}
80103
},

package.nls.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,6 @@
104104

105105
"configuration.codelensDisplayMode.item.expand": "Expand",
106106
"configuration.codelensDisplayMode.item.collapse": "Collapse",
107-
"configuration.codelensDisplayMode.description": "Controls the display of CodeLens"
107+
"configuration.codelensDisplayMode.description": "Controls the display of CodeLens",
108+
"configuration.codelensDisplayItems.description": "Custom the display and sorting CodeLens Items"
108109
}

package.nls.zh-cn.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"configuration.enableRenameSuggestion.description": "启用重命名建议",
66
"configuration.customPromptDir.description": "自定义提示目录",
77

8+
"configuration.codelensDisplayMode.item.expand": "操作平铺",
9+
"configuration.codelensDisplayMode.item.collapse": "操作收起",
10+
"configuration.codelensDisplayMode.description": "控制行间按钮的显示方式",
11+
"configuration.codelensDisplayItems.description": "配置生成注释、生成测试、代码解释、代码优化等行间按钮的展示",
12+
813
"configuration.chat.title": "对话",
914
"configuration.chat.enable.description": "启用或禁用聊天功能",
1015
"configuration.chat.models.description": "可供聊天界面选择的模型列表",
@@ -75,8 +80,5 @@
7580
"command.genApiData.title": "生成 API 数据",
7681

7782
"command.codebase.createIndexes.title": "索引生成",
78-
"command.codebase.retrievalCode.title": "代码检索",
79-
80-
"configuration.codelensDisplayMode.item.expand": "操作平铺",
81-
"configuration.codelensDisplayMode.item.collapse": "操作收起"
83+
"command.codebase.retrievalCode.title": "代码检索"
8284
}

src/action/providers/AutoDevCodeLensProvider.ts

+89-46
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import {
1414
commands,
1515
Disposable,
1616
l10n,
17-
Range,
18-
Selection,
1917
TextDocument,
2018
window,
2119
WorkspaceEdit,
@@ -38,6 +36,8 @@ import { logger } from 'base/common/log/log';
3836

3937
import { type AutoDevExtension } from '../../AutoDevExtension';
4038

39+
type CodeLensItemType = 'quickChat' | 'explainCode' | 'optimizeCode' | 'autoComment' | 'autoTest' | 'customAction';
40+
4141
export class AutoDevCodeLensProvider implements CodeLensProvider {
4242
private config: ConfigurationService;
4343
private lsp: ILanguageServiceProvider;
@@ -134,22 +134,31 @@ export class AutoDevCodeLensProvider implements CodeLensProvider {
134134
return this.config.get<string>('codelensDisplayMode') === 'collapse';
135135
}
136136

137+
getDisplayCodelensItems() {
138+
return new Set(this.config.get<CodeLensItemType[]>('codelensDislayItems'));
139+
}
140+
137141
hasCustomPromps() {
138-
return this.autodev.teamPromptsBuilder.teamPrompts().length;
142+
return this.autodev.teamPromptsBuilder.teamPrompts().length > 0;
139143
}
140144

141145
async provideCodeLenses(document: TextDocument, token: CancellationToken) {
142146
if (isFileTooLarge(document) || !isSupportedLanguage(document.languageId)) {
143147
return [];
144148
}
145149

150+
const displayItems = this.getDisplayCodelensItems();
151+
if (displayItems.size === 0) {
152+
return [];
153+
}
154+
146155
const elements = await this.parseToNamedElements(document);
147156

148157
if (token.isCancellationRequested || elements.length === 0) {
149158
return [];
150159
}
151160

152-
const groups = this.buildCodeLensGroups(elements, document, token);
161+
const groups = this.buildCodeLensGroups(displayItems, elements, document, token);
153162
if (groups.length === 0) {
154163
return [];
155164
}
@@ -173,53 +182,87 @@ export class AutoDevCodeLensProvider implements CodeLensProvider {
173182
});
174183
}
175184

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+
) {
177191
const result: CodeLens[][] = [];
192+
const hasCustomPromps = this.hasCustomPromps();
178193

179194
for (const element of elements) {
180195
const codelenses: CodeLens[] = [];
181196

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+
}
223266
}
224267

225268
result.push(codelenses);

0 commit comments

Comments
 (0)