From e66a4f889be02d4040d5c8962a03c7428c00af4e Mon Sep 17 00:00:00 2001 From: zhengxs2018 Date: Fri, 14 Jun 2024 21:15:54 +0800 Subject: [PATCH] feat: custom display and sort codelens items (#57) --- package.json | 33 ++++- package.nls.json | 3 +- package.nls.zh-cn.json | 10 +- .../providers/AutoDevCodeLensProvider.ts | 135 ++++++++++++------ 4 files changed, 125 insertions(+), 56 deletions(-) diff --git a/package.json b/package.json index 5479d389..58cdbc86 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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 } }, diff --git a/package.nls.json b/package.nls.json index cdeb0dcd..6398b859 100644 --- a/package.nls.json +++ b/package.nls.json @@ -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" } diff --git a/package.nls.zh-cn.json b/package.nls.zh-cn.json index 2ace676b..d7adc119 100644 --- a/package.nls.zh-cn.json +++ b/package.nls.zh-cn.json @@ -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": "可供聊天界面选择的模型列表", @@ -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": "代码检索" } diff --git a/src/action/providers/AutoDevCodeLensProvider.ts b/src/action/providers/AutoDevCodeLensProvider.ts index 3993eba3..949a18fe 100644 --- a/src/action/providers/AutoDevCodeLensProvider.ts +++ b/src/action/providers/AutoDevCodeLensProvider.ts @@ -14,8 +14,6 @@ import { commands, Disposable, l10n, - Range, - Selection, TextDocument, window, WorkspaceEdit, @@ -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; @@ -134,8 +134,12 @@ export class AutoDevCodeLensProvider implements CodeLensProvider { return this.config.get('codelensDisplayMode') === 'collapse'; } + getDisplayCodelensItems() { + return new Set(this.config.get('codelensDislayItems')); + } + hasCustomPromps() { - return this.autodev.teamPromptsBuilder.teamPrompts().length; + return this.autodev.teamPromptsBuilder.teamPrompts().length > 0; } async provideCodeLenses(document: TextDocument, token: CancellationToken) { @@ -143,13 +147,18 @@ export class AutoDevCodeLensProvider implements CodeLensProvider { 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 []; } @@ -173,53 +182,87 @@ export class AutoDevCodeLensProvider implements CodeLensProvider { }); } - private buildCodeLensGroups(elements: NamedElement[], document: TextDocument, token: CancellationToken) { + private buildCodeLensGroups( + displaySet: Set, + 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);