Skip to content

Commit

Permalink
feat: custom display and sort codelens items (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengxs2018 authored Jun 14, 2024
1 parent 69f35cf commit e66a4f8
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 56 deletions.
33 changes: 28 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
"description": "%configuration.enableRenameSuggestion.description%",
"order": 1
},
"autodev.customPromptDir": {
"type": "string",
"default": "prompts",
"description": "%configuration.customPromptDir.description%",
"order": 2
},
"autodev.codelensDisplayMode": {
"type": "string",
"enum": [
Expand All @@ -69,12 +75,29 @@
"%configuration.codelensDisplayMode.item.collapse%"
],
"description": "%configuration.codelensDisplayMode.description%",
"default": "expand"
"default": "expand",
"order": 3
},
"autodev.customPromptDir": {
"type": "string",
"default": "prompts",
"description": "%configuration.customPromptDir.description%",
"autodev.codelensDislayItems": {
"type": "array",
"items": {
"type": "string",
"enum": [
"quickChat",
"explainCode",
"optimizeCode",
"autoComment",
"autoTest",
"customAction"
]
},
"uniqueItems": true,
"default": [
"quickChat",
"autoTest",
"autoComment"
],
"description": "%configuration.codelensDisplayItems.description%",
"order": 4
}
},
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@

"configuration.codelensDisplayMode.item.expand": "Expand",
"configuration.codelensDisplayMode.item.collapse": "Collapse",
"configuration.codelensDisplayMode.description": "Controls the display of CodeLens"
"configuration.codelensDisplayMode.description": "Controls the display of CodeLens",
"configuration.codelensDisplayItems.description": "Custom the display and sorting CodeLens Items"
}
10 changes: 6 additions & 4 deletions package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"configuration.enableRenameSuggestion.description": "启用重命名建议",
"configuration.customPromptDir.description": "自定义提示目录",

"configuration.codelensDisplayMode.item.expand": "操作平铺",
"configuration.codelensDisplayMode.item.collapse": "操作收起",
"configuration.codelensDisplayMode.description": "控制行间按钮的显示方式",
"configuration.codelensDisplayItems.description": "配置生成注释、生成测试、代码解释、代码优化等行间按钮的展示",

"configuration.chat.title": "对话",
"configuration.chat.enable.description": "启用或禁用聊天功能",
"configuration.chat.models.description": "可供聊天界面选择的模型列表",
Expand Down Expand Up @@ -75,8 +80,5 @@
"command.genApiData.title": "生成 API 数据",

"command.codebase.createIndexes.title": "索引生成",
"command.codebase.retrievalCode.title": "代码检索",

"configuration.codelensDisplayMode.item.expand": "操作平铺",
"configuration.codelensDisplayMode.item.collapse": "操作收起"
"command.codebase.retrievalCode.title": "代码检索"
}
135 changes: 89 additions & 46 deletions src/action/providers/AutoDevCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import {
commands,
Disposable,
l10n,
Range,
Selection,
TextDocument,
window,
WorkspaceEdit,
Expand All @@ -38,6 +36,8 @@ import { logger } from 'base/common/log/log';

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

type CodeLensItemType = 'quickChat' | 'explainCode' | 'optimizeCode' | 'autoComment' | 'autoTest' | 'customAction';

export class AutoDevCodeLensProvider implements CodeLensProvider {
private config: ConfigurationService;
private lsp: ILanguageServiceProvider;
Expand Down Expand Up @@ -134,22 +134,31 @@ export class AutoDevCodeLensProvider implements CodeLensProvider {
return this.config.get<string>('codelensDisplayMode') === 'collapse';
}

getDisplayCodelensItems() {
return new Set(this.config.get<CodeLensItemType[]>('codelensDislayItems'));
}

hasCustomPromps() {
return this.autodev.teamPromptsBuilder.teamPrompts().length;
return this.autodev.teamPromptsBuilder.teamPrompts().length > 0;
}

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

const displayItems = this.getDisplayCodelensItems();
if (displayItems.size === 0) {
return [];
}

const elements = await this.parseToNamedElements(document);

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

const groups = this.buildCodeLensGroups(elements, document, token);
const groups = this.buildCodeLensGroups(displayItems, elements, document, token);
if (groups.length === 0) {
return [];
}
Expand All @@ -173,53 +182,87 @@ export class AutoDevCodeLensProvider implements CodeLensProvider {
});
}

private buildCodeLensGroups(elements: NamedElement[], document: TextDocument, token: CancellationToken) {
private buildCodeLensGroups(
displaySet: Set<CodeLensItemType>,
elements: NamedElement[],
document: TextDocument,
token: CancellationToken,
) {
const result: CodeLens[][] = [];
const hasCustomPromps = this.hasCustomPromps();

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

codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('Quick Chat'),
command: CMD_CODELENS_QUICK_CHAT,
arguments: [document, element],
}),
new CodeLens(element.identifierRange, {
title: l10n.t('Explain Code'),
command: CMD_CODELENS_EXPLAIN_CODE,
arguments: [document, element],
}),
new CodeLens(element.identifierRange, {
title: l10n.t('Optimize Code'),
command: CMD_CODELENS_OPTIMIZE_CODE,
arguments: [document, element],
}),
);

if (!element.isTestFile()) {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('AutoComment'),
command: CMD_CODELENS_GEN_DOCSTRING,
arguments: [document, element],
}),
new CodeLens(element.identifierRange, {
title: l10n.t('AutoTest'),
command: CMD_CODELENS_CREATE_UNIT_TEST,
arguments: [document, element, new WorkspaceEdit()],
}),
);
}

if (this.hasCustomPromps()) {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('Custom Action'),
command: CMD_CODELENS_SHOW_CUSTOM_ACTION,
arguments: [document, element],
}),
);
for (const type of displaySet) {
if (type === 'quickChat') {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('Quick Chat'),
command: CMD_CODELENS_QUICK_CHAT,
arguments: [document, element],
}),
);
continue;
}

if (type === 'explainCode') {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('Explain Code'),
command: CMD_CODELENS_EXPLAIN_CODE,
arguments: [document, element],
}),
);
continue;
}
if (type === 'optimizeCode') {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('Optimize Code'),
command: CMD_CODELENS_OPTIMIZE_CODE,
arguments: [document, element],
}),
);
continue;
}

if (type === 'autoComment') {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('AutoComment'),
command: CMD_CODELENS_GEN_DOCSTRING,
arguments: [document, element],
}),
);
continue;
}

if (type === 'autoTest') {
if (!element.isTestFile()) {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('AutoTest'),
command: CMD_CODELENS_CREATE_UNIT_TEST,
arguments: [document, element, new WorkspaceEdit()],
}),
);
}
continue;
}

if (type === 'customAction') {
if (hasCustomPromps) {
codelenses.push(
new CodeLens(element.identifierRange, {
title: l10n.t('Custom Action'),
command: CMD_CODELENS_SHOW_CUSTOM_ACTION,
arguments: [document, element],
}),
);
}
continue;
}
}

result.push(codelenses);
Expand Down

0 comments on commit e66a4f8

Please sign in to comment.