From 2173ff86e0e6224a6f7819e56eb6d1268de5be8a Mon Sep 17 00:00:00 2001 From: long2ice Date: Mon, 12 May 2025 00:32:37 +0800 Subject: [PATCH] feat: update command.yml files and enhance workflow scripts --- community/github/command.yml | 4 +- community/github/commit/command.yml | 2 +- .../github/commit/{commit.py => main.py} | 0 community/github/commit/zh/command.yml | 8 --- community/github/main.py | 13 ++++ community/gitlab/command.yml | 4 +- .../gitlab/commit/{__init__py => __init__.py} | 0 community/gitlab/commit/command.yml | 2 +- .../gitlab/commit/{commit.py => main.py} | 0 community/gitlab/commit/zh/command.yml | 5 -- community/gitlab/main.py | 13 ++++ community/refactor/api/command.py | 21 +++--- lib/workflow/call.py | 21 ++++++ lib/workflow/decorators.py | 66 +++++++++++++++++++ merico/ask_issue/README.md | 2 +- merico/ask_issue/README.zh.md | 2 +- merico/ask_issue/command.yml | 2 +- merico/chatflow/ask/command.py | 19 ++---- merico/chatflow/gen/command.py | 7 +- merico/comments/main.py | 21 +----- merico/docstring/main.py | 36 ++-------- merico/explain/command.yml | 2 +- merico/explain/{explain.py => main.py} | 36 ++-------- merico/fix/main.py | 36 ++-------- merico/fix_issue/main.py | 22 ++----- merico/perf/command.yml | 2 +- merico/perf/{rewrite.py => main.py} | 57 +++------------- merico/perf/names/main.py | 6 +- 28 files changed, 179 insertions(+), 230 deletions(-) rename community/github/commit/{commit.py => main.py} (100%) delete mode 100644 community/github/commit/zh/command.yml create mode 100644 community/github/main.py rename community/gitlab/commit/{__init__py => __init__.py} (100%) rename community/gitlab/commit/{commit.py => main.py} (100%) delete mode 100644 community/gitlab/commit/zh/command.yml create mode 100644 community/gitlab/main.py create mode 100644 lib/workflow/decorators.py rename merico/explain/{explain.py => main.py} (57%) rename merico/perf/{rewrite.py => main.py} (89%) diff --git a/community/github/command.yml b/community/github/command.yml index f4af57b..f9ba188 100644 --- a/community/github/command.yml +++ b/community/github/command.yml @@ -1 +1,3 @@ -description: Root of github commands. \ No newline at end of file +description: Workflows for github. +steps: + - run: $devchat_python $command_path/main.py \ No newline at end of file diff --git a/community/github/commit/command.yml b/community/github/commit/command.yml index 3a015fb..dedc8df 100644 --- a/community/github/commit/command.yml +++ b/community/github/commit/command.yml @@ -5,4 +5,4 @@ help: en: README.md zh: README.zh.md steps: - - run: $devchat_python $command_path/commit.py "$input" "english" + - run: $devchat_python $command_path/main.py "$input" "english" diff --git a/community/github/commit/commit.py b/community/github/commit/main.py similarity index 100% rename from community/github/commit/commit.py rename to community/github/commit/main.py diff --git a/community/github/commit/zh/command.yml b/community/github/commit/zh/command.yml deleted file mode 100644 index b594578..0000000 --- a/community/github/commit/zh/command.yml +++ /dev/null @@ -1,8 +0,0 @@ -description: '为你选定的代码变更生成格式规范的提交信息,并通过 Git 提交。如需要可包含对应 issue 编号(例如,输入"/commit to close #12")' -hint: to close Issue #issue_number -input: optional -steps: - - run: $devchat_python $command_path/../commit.py "$input" "chinese" -help: - en: README.md - zh: README.zh.md diff --git a/community/github/main.py b/community/github/main.py new file mode 100644 index 0000000..79fea98 --- /dev/null +++ b/community/github/main.py @@ -0,0 +1,13 @@ +from lib.workflow.call import print_sub_workflows, workflow_call +from lib.workflow.decorators import check_config + + +@check_config(["github_token"]) +def main(github_token: bool): + print_sub_workflows() + if not github_token: + workflow_call("/github.config") + + +if __name__ == "__main__": + main() diff --git a/community/gitlab/command.yml b/community/gitlab/command.yml index b7d8e9c..86a6f95 100644 --- a/community/gitlab/command.yml +++ b/community/gitlab/command.yml @@ -1 +1,3 @@ -description: Root of gitlab commands. \ No newline at end of file +description: Workflow for gitlab. +steps: + - run: $devchat_python $command_path/main.py diff --git a/community/gitlab/commit/__init__py b/community/gitlab/commit/__init__.py similarity index 100% rename from community/gitlab/commit/__init__py rename to community/gitlab/commit/__init__.py diff --git a/community/gitlab/commit/command.yml b/community/gitlab/commit/command.yml index 3a015fb..dedc8df 100644 --- a/community/gitlab/commit/command.yml +++ b/community/gitlab/commit/command.yml @@ -5,4 +5,4 @@ help: en: README.md zh: README.zh.md steps: - - run: $devchat_python $command_path/commit.py "$input" "english" + - run: $devchat_python $command_path/main.py "$input" "english" diff --git a/community/gitlab/commit/commit.py b/community/gitlab/commit/main.py similarity index 100% rename from community/gitlab/commit/commit.py rename to community/gitlab/commit/main.py diff --git a/community/gitlab/commit/zh/command.yml b/community/gitlab/commit/zh/command.yml deleted file mode 100644 index 08a5d54..0000000 --- a/community/gitlab/commit/zh/command.yml +++ /dev/null @@ -1,5 +0,0 @@ -description: '为你选定的代码变更生成格式规范的提交信息,并通过 Git 提交。如需要可包含对应 issue 编号(例如,输入“/commit to close #12”)' -hint: to close Issue #issue_number -input: optional -steps: - - run: $devchat_python $command_path/../commit.py "$input" "chinese" \ No newline at end of file diff --git a/community/gitlab/main.py b/community/gitlab/main.py new file mode 100644 index 0000000..66de57c --- /dev/null +++ b/community/gitlab/main.py @@ -0,0 +1,13 @@ +from lib.workflow.call import print_sub_workflows, workflow_call +from lib.workflow.decorators import check_config + + +@check_config(["gitlab_token"]) +def main(gitlab_token: bool): + print_sub_workflows() + if not gitlab_token: + workflow_call("/gitlab.config") + + +if __name__ == "__main__": + main() diff --git a/community/refactor/api/command.py b/community/refactor/api/command.py index 3568113..4003711 100755 --- a/community/refactor/api/command.py +++ b/community/refactor/api/command.py @@ -7,8 +7,8 @@ from devchat.llm import chat_json from lib.chatmark import Button, Form, TextEditor -from lib.ide_service import IDEService from lib.workflow import workflow_call +from lib.workflow.decorators import check_select_code # 步骤3: 使用AI识别API路径和METHOD的提示词 API_ANALYSIS_PROMPT = """ @@ -37,28 +37,23 @@ def analyze_api(code: str) -> Dict[str, str]: pass -def main() -> None: +@check_select_code("Please select code to refactor.") +def main(code: dict) -> None: """API重构工作流主函数""" try: # 步骤1: 获取用户输入的重构目标 if len(sys.argv) < 2: - print("错误: 请提供重构目标") + print("请提供重构目标", file=sys.stderr) sys.exit(1) refactor_target = sys.argv[1] - # 步骤2: 获取用户选中的代码 - selected_code = IDEService().get_selected_range() - if not selected_code or not selected_code.text.strip(): - print("错误: 请先选择需要重构的代码") - sys.exit(1) - # 步骤3: 使用AI识别API路径和METHOD print("正在分析选中代码中的API信息...") - api_info = analyze_api(code=selected_code.text) + api_info = analyze_api(code=code["text"]) if not api_info or "api_path" not in api_info or "method" not in api_info: - print("错误: 无法识别API信息") + print("无法识别API信息", file=sys.stderr) sys.exit(1) api_path = api_info["api_path"] @@ -91,7 +86,7 @@ def main() -> None: refactor_result = workflow_call(f"/refactor {refactor_target}") if refactor_result != 0: - print("错误: API重构失败") + print("API重构失败") sys.exit(1) print("API重构成功!") @@ -124,7 +119,7 @@ def main() -> None: print("API重构工作流执行完毕!") except Exception as e: - print(f"错误: 执行过程中发生异常: {str(e)}") + print(f"执行过程中发生异常: {str(e)}", file=sys.stderr) sys.exit(1) diff --git a/lib/workflow/call.py b/lib/workflow/call.py index c7cb787..24f1b5a 100644 --- a/lib/workflow/call.py +++ b/lib/workflow/call.py @@ -5,6 +5,8 @@ import yaml +import __main__ + def find_workflow_script(command_name: str) -> str: """ @@ -147,3 +149,22 @@ def workflow_call(command: str) -> int: print(f"命令执行失败,返回码: {return_code}") return return_code + + +def print_sub_workflows(): + base_dir = os.path.dirname(os.path.abspath(__main__.__file__)) + root_dir = base_dir.split("/")[-1] + print(f"#### {root_dir.capitalize()} Workflows\n", flush=True) + + for root, dirs, files in os.walk(base_dir): + if "command.yml" in files: + if root == base_dir: + continue + rel_path = os.path.relpath(root, base_dir) + workflow_name = f"/{root_dir}.{rel_path.replace(os.path.sep, '.')}" + if workflow_name.endswith(".zh"): + continue + with open(os.path.join(root, "command.yml"), "r", encoding="utf-8") as f: + config = yaml.safe_load(f) + description = config.get("description", "No description available") + print(f"- **{workflow_name}**: {description}", flush=True) diff --git a/lib/workflow/decorators.py b/lib/workflow/decorators.py new file mode 100644 index 0000000..1d56ece --- /dev/null +++ b/lib/workflow/decorators.py @@ -0,0 +1,66 @@ +import functools +import json +import os +import sys + +from lib.ide_service import IDEService + + +def check_config(configs: list[str], is_global: bool = True): + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + config_file = ".workflow_config.json" + if is_global: + config_path = os.path.join(os.path.expanduser("~/.chat"), config_file) + else: + config_path = os.path.join(os.getcwd(), ".chat", config_file) + + if not os.path.exists(config_path): + return func(False, *args, **kwargs) + + with open(config_path, "r") as f: + config = json.load(f) + + for config_name in configs: + if config_name not in config: + return func(False, *args, **kwargs) + + return func(True, *args, **kwargs) + + return wrapper + + return decorator + + +def check_select_code(description: str): + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + selected_data = IDEService().get_selected_range().dict() + if ( + selected_data["range"]["start"]["line"] == -1 + or selected_data["range"]["start"] == selected_data["range"]["end"] + ): + print(description, file=sys.stderr) + sys.exit(1) + return func(selected_data, *args, **kwargs) + + return wrapper + + return decorator + + +def check_input(description: str): + def decorator(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + arg = sys.argv[1] + if len(arg) == 0: + print(description, file=sys.stderr) + sys.exit(1) + return func(arg, *args, **kwargs) + + return wrapper + + return decorator diff --git a/merico/ask_issue/README.md b/merico/ask_issue/README.md index 214bc77..eacec45 100644 --- a/merico/ask_issue/README.md +++ b/merico/ask_issue/README.md @@ -1,6 +1,6 @@ ### ask_issue -Automatically fix lint errors in code. +Ask issue for lint errors. #### Purpose diff --git a/merico/ask_issue/README.zh.md b/merico/ask_issue/README.zh.md index 1ebf618..21caafa 100644 --- a/merico/ask_issue/README.zh.md +++ b/merico/ask_issue/README.zh.md @@ -1,6 +1,6 @@ ### ask_issue -自动修复代码中的 lint 错误。 +询问代码中的 lint 错误。 #### 用途 diff --git a/merico/ask_issue/command.yml b/merico/ask_issue/command.yml index 59fe5a4..9318fd3 100644 --- a/merico/ask_issue/command.yml +++ b/merico/ask_issue/command.yml @@ -1,4 +1,4 @@ -description: Automatically fix lint errors. +description: Ask issue for lint errors. help: en: README.md zh: README.zh.md diff --git a/merico/chatflow/ask/command.py b/merico/chatflow/ask/command.py index 651f10e..8126055 100644 --- a/merico/chatflow/ask/command.py +++ b/merico/chatflow/ask/command.py @@ -4,7 +4,7 @@ from devchat.llm import chat -from lib.ide_service import IDEService +from lib.workflow.decorators import check_input, check_select_code ROOT_WORKFLOW_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(ROOT_WORKFLOW_DIR) @@ -28,20 +28,15 @@ def ask(question, selected_code, file_path): pass -def get_selected_code(): - """Retrieves the selected lines of code from the user's selection.""" - selected_data = IDEService().get_selected_range().dict() - return selected_data - - -def main(question): - selected_text = get_selected_code() - file_path = selected_text.get("abspath", "") - code_text = selected_text.get("text", "") +@check_input("请输入问题") +@check_select_code("请选中代码") +def main(code, question): + file_path = code.get("abspath", "") + code_text = code.get("text", "") ask(question=question, selected_code=code_text, file_path=file_path) sys.exit(0) if __name__ == "__main__": - main(sys.argv[1]) + main() diff --git a/merico/chatflow/gen/command.py b/merico/chatflow/gen/command.py index 9309190..fc93e15 100644 --- a/merico/chatflow/gen/command.py +++ b/merico/chatflow/gen/command.py @@ -15,6 +15,7 @@ from lib.chatmark import Form, Radio, Step, TextEditor from lib.ide_service import IDEService +from lib.workflow.decorators import check_input ROOT_WORKFLOW_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.append(ROOT_WORKFLOW_DIR) @@ -323,10 +324,8 @@ def create_workflow_files(command_dir, command_name, description, input_required f.write(readme_content) -def main(): - # 获取用户输入 - user_input = sys.argv[1] if len(sys.argv) > 1 else "" - +@check_input("请输入工作流描述") +def main(user_input): # 步骤1: 通过AI分析用户输入,提取必要信息 with Step("分析用户输入,提取工作流信息..."): workflow_info = extract_workflow_info(user_input=user_input, contexts=CONTEXTS) diff --git a/merico/comments/main.py b/merico/comments/main.py index 46227fb..9e0084a 100644 --- a/merico/comments/main.py +++ b/merico/comments/main.py @@ -1,4 +1,3 @@ -import os import re import sys @@ -6,6 +5,7 @@ from devchat.memory import FixSizeChatMemory from lib.ide_service import IDEService +from lib.workflow.decorators import check_select_code PROMPT = """ file: {file_path} @@ -194,21 +194,6 @@ ] -def get_selected_code(): - """Retrieves the selected lines of code from the user's selection.""" - selected_data = IDEService().get_selected_range().dict() - if selected_data["range"]["start"] == selected_data["range"]["end"]: - readme_path = os.path.join(os.path.dirname(__file__), "README.md") - if os.path.exists(readme_path): - with open(readme_path, "r", encoding="utf-8") as f: - readme_text = f.read() - print(readme_text) - sys.exit(0) - print("Please select some text.", file=sys.stderr, flush=True) - sys.exit(-1) - return selected_data - - memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_FEW_SHOT) @@ -253,8 +238,8 @@ def remove_unnecessary_escapes(code_a, code_b): return code_copy -def main(): - selected_text = get_selected_code() +@check_select_code("Please select code to add comments.") +def main(selected_text: dict): file_path = selected_text.get("abspath", "") code_text = selected_text.get("text", "") diff --git a/merico/docstring/main.py b/merico/docstring/main.py index 7171414..de69db8 100644 --- a/merico/docstring/main.py +++ b/merico/docstring/main.py @@ -1,4 +1,3 @@ -import os import re import sys @@ -6,6 +5,7 @@ from devchat.memory import FixSizeChatMemory from lib.ide_service import IDEService +from lib.workflow.decorators import check_select_code PROMPT = prompt = """ file: {file_path} @@ -145,35 +145,6 @@ def content_to_json(content): ] -def get_selected_code(): - """ - Retrieves the selected lines of code from the user's selection. - - This function extracts the text selected by the user in their IDE or text editor. - If no text has been selected, it prints an error message to stderr and exits the - program with a non-zero status indicating failure. - - Returns: - dict: A dictionary containing the key 'selectedText' with the selected text - as its value. If no text is selected, the program exits. - """ - selected_data = IDEService().get_selected_range().dict() - - miss_selected_error = "Please select some text." - if selected_data["range"]["start"] == selected_data["range"]["end"]: - readme_path = os.path.join(os.path.dirname(__file__), "README.md") - if os.path.exists(readme_path): - with open(readme_path, "r", encoding="utf-8") as f: - readme_text = f.read() - print(readme_text) - sys.exit(0) - - print(miss_selected_error, file=sys.stderr, flush=True) - sys.exit(-1) - - return selected_data - - memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_A) @@ -283,9 +254,10 @@ def merge_code(selected_text, docstring): return docstring + "\n" + selected_text -def main(): +@check_select_code("Please select code to add docstring.") +def main(code: dict): # Prepare code - selected_text = get_selected_code() + selected_text = code["text"] # Rewrite response = add_docstring( diff --git a/merico/explain/command.yml b/merico/explain/command.yml index b3f51d3..6c82583 100644 --- a/merico/explain/command.yml +++ b/merico/explain/command.yml @@ -3,4 +3,4 @@ help: en: README.md zh: README.zh.md steps: - - run: $devchat_python $command_path/explain.py + - run: $devchat_python $command_path/main.py diff --git a/merico/explain/explain.py b/merico/explain/main.py similarity index 57% rename from merico/explain/explain.py rename to merico/explain/main.py index 9d9dfa7..bb39680 100644 --- a/merico/explain/explain.py +++ b/merico/explain/main.py @@ -1,38 +1,9 @@ -import os import sys from devchat.llm import chat from lib.ide_service import IDEService - - -def get_selected_code(): - """ - Retrieves the selected lines of code from the user's selection. - - This function extracts the text selected by the user in their IDE or text editor. - If no text has been selected, it prints an error message to stderr and exits the - program with a non-zero status indicating failure. - - Returns: - dict: A dictionary containing the key 'selectedText' with the selected text - as its value. If no text is selected, the program exits. - """ - selected_data = IDEService().get_selected_range().dict() - - miss_selected_error = "Please select some text." - if selected_data["range"]["start"] == selected_data["range"]["end"]: - readme_path = os.path.join(os.path.dirname(__file__), "README.md") - if os.path.exists(readme_path): - with open(readme_path, "r", encoding="utf-8") as f: - readme_text = f.read() - print(readme_text) - sys.exit(0) - - print(miss_selected_error, file=sys.stderr, flush=True) - sys.exit(-1) - - return selected_data +from lib.workflow.decorators import check_select_code def get_visible_code(): @@ -82,8 +53,9 @@ def explain(selected_text, visible_text): pass # pylint: disable=unnecessary-pass -def main(): - result = explain(selected_text=get_selected_code(), visible_text=get_visible_code()) +@check_select_code("Please select code to explain.") +def main(code: dict): + result = explain(selected_text=code["text"], visible_text=get_visible_code()) sys.exit(0 if result else 1) diff --git a/merico/fix/main.py b/merico/fix/main.py index 16ad7e5..4dc0afc 100644 --- a/merico/fix/main.py +++ b/merico/fix/main.py @@ -1,39 +1,10 @@ -import os import re import sys from devchat.llm import chat from lib.ide_service import IDEService - - -def get_selected_code(): - """ - Retrieves the selected lines of code from the user's selection. - - This function extracts the text selected by the user in their IDE or text editor. - If no text has been selected, it prints an error message to stderr and exits the - program with a non-zero status indicating failure. - - Returns: - dict: A dictionary containing the key 'selectedText' with the selected text - as its value. If no text is selected, the program exits. - """ - selected_data = IDEService().get_selected_range().dict() - - miss_selected_error = "Please select some text." - if selected_data["range"]["start"] == selected_data["range"]["end"]: - readme_path = os.path.join(os.path.dirname(__file__), "README.md") - if os.path.exists(readme_path): - with open(readme_path, "r", encoding="utf-8") as f: - readme_text = f.read() - print(readme_text) - sys.exit(0) - - print(miss_selected_error, file=sys.stderr, flush=True) - sys.exit(-1) - - return selected_data +from lib.workflow.decorators import check_select_code def get_visible_code(): @@ -134,9 +105,10 @@ def extract_markdown_block(text): return None -def main(): +@check_select_code("Please select code to fix bugs.") +def main(code: dict): # prepare code - selected_text = get_selected_code() + selected_text = code["text"] visible_text = get_visible_code() # rewrite diff --git a/merico/fix_issue/main.py b/merico/fix_issue/main.py index dcb0523..f261249 100644 --- a/merico/fix_issue/main.py +++ b/merico/fix_issue/main.py @@ -8,6 +8,7 @@ from devchat.memory import FixSizeChatMemory from lib.ide_service import IDEService +from lib.workflow.decorators import check_select_code def extract_edits_block(text): @@ -53,20 +54,6 @@ def extract_markdown_block(text): return text -# step 1 : get selected code -def get_selected_code(): - selected_data = IDEService().get_selected_range().dict() - - if selected_data["range"]["start"] == -1: - return None, None, None - - if selected_data["range"]["start"]["line"] != selected_data["range"]["end"]["line"]: - print("Please select the line code of issue reported.\n\n", file=sys.stderr) - sys.exit(1) - - return selected_data["abspath"], selected_data["text"], selected_data["range"]["start"]["line"] - - # step 2 : input issue descriptions def input_issue_descriptions(file_path, issue_line_num): diagnostics = IDEService().get_diagnostics_in_range(file_path, issue_line_num, issue_line_num) @@ -415,14 +402,17 @@ def apply_changes(changes, file_path): os.remove(changes_file) -def main(): +@check_select_code("Please select code to fix issues.") +def main(code: dict): """ Main function to fix issues in the selected code. It retrieves the selected code, gets issue descriptions, generates fix solutions using LLM, and applies the changes. """ print("start fix issue ...\n\n", flush=True) - file_path, issue_line, issue_line_num = get_selected_code() + file_path = code["abspath"] + issue_line = code["text"] + issue_line_num = code["range"]["start"]["line"] if not file_path or not issue_line: print("No code selected. Please select the code line you want to fix.", file=sys.stderr) sys.exit(1) diff --git a/merico/perf/command.yml b/merico/perf/command.yml index d63c610..bd78556 100644 --- a/merico/perf/command.yml +++ b/merico/perf/command.yml @@ -5,4 +5,4 @@ help: en: README.md zh: README.zh.md steps: - - run: $devchat_python $command_path/rewrite.py "$input" + - run: $devchat_python $command_path/main.py "$input" diff --git a/merico/perf/rewrite.py b/merico/perf/main.py similarity index 89% rename from merico/perf/rewrite.py rename to merico/perf/main.py index 670f123..f38c349 100644 --- a/merico/perf/rewrite.py +++ b/merico/perf/main.py @@ -7,36 +7,7 @@ from lib.chatmark import Step from lib.ide_service import IDEService - - -def get_selected_code(): - """ - Retrieves the selected lines of code from the user's selection. - - This function extracts the text selected by the user in their IDE or text editor. - If no text has been selected, it prints an error message to stderr and exits the - program with a non-zero status indicating failure. - - Returns: - dict: A dictionary containing the key 'selectedText' with the selected text - as its value. If no text is selected, the program exits. - """ - selected_data = IDEService().get_selected_range().dict() - - miss_selected_error = "Please select some text." - if selected_data["range"]["start"] == selected_data["range"]["end"]: - readme_path = os.path.join(os.path.dirname(__file__), "README.md") - if os.path.exists(readme_path): - with open(readme_path, "r", encoding="utf-8") as f: - readme_text = f.read() - print(readme_text) - sys.exit(0) - - print(miss_selected_error, file=sys.stderr, flush=True) - sys.exit(-1) - - return selected_data - +from lib.workflow.decorators import check_select_code REWRITE_PROMPT = prompt = """ 你是一个代码重构专家,你的任务是根据用户的需求重写代码。你需要根据用户的需求,重写代码,并保证代码的语法正确性和逻辑正确性。 @@ -390,28 +361,20 @@ def find_project_root(file_path: str) -> str: return os.path.dirname(file_path) -def main(): +@check_select_code("Please select code to refactor.") +def main(code: dict): ide_service = IDEService() question = sys.argv[1] rafact_task = sys.argv[1] # prepare code - - # 步骤1: 获取用户选中的代码片段 - with Step("获取选中的代码片段..."): - selected_code = ide_service.get_selected_range() - - if not selected_code or not selected_code.text.strip(): - print("请先选择一段代码片段再执行此命令。") - return - # print(selected_code) - selected_text = selected_code.text - project_root_path = find_project_root(selected_code.abspath) + selected_text = code["text"] + project_root_path = find_project_root(code["abspath"]) print(f"项目根目录: {project_root_path}\n\n") # 步骤2: 分析代码片段中缺少定义的符号 with Step("分析代码中缺少定义的符号..."): try: - analysis_result = analyze_missing_symbols(code=selected_code.text, task=rafact_task) + analysis_result = analyze_missing_symbols(code=selected_text, task=rafact_task) missing_symbols = analysis_result # 直接获取返回的列表 if not missing_symbols: @@ -424,12 +387,12 @@ def main(): print(f"分析代码时出错: {str(e)}") return - base_line = selected_code.range.start.line + base_line = code["range"]["start"]["line"] # 步骤3: 将分析结果转换为可处理的结构 with Step("处理符号信息..."): symbols = [] - code_lines = selected_code.text.splitlines() + code_lines = selected_text.splitlines() for symbol_info in missing_symbols: symbol_name = symbol_info["symbol"] @@ -474,7 +437,7 @@ def main(): symbol_type = symbol.get("type", "unknown") definitions = get_symbol_definition( - selected_code.abspath, + code["abspath"], symbol_line, symbol_char, symbol_name, @@ -487,7 +450,7 @@ def main(): # 计算每个文件被引用次数 files_ref_counts = {} # 当前选中代码文件,默认计算100 - files_ref_counts[selected_code.abspath] = 100 + files_ref_counts[code["abspath"]] = 100 for symbol in symbol_definitions: for definition in symbol_definitions[symbol]: if definition[0] not in files_ref_counts: diff --git a/merico/perf/names/main.py b/merico/perf/names/main.py index bd7dadd..31c11dd 100644 --- a/merico/perf/names/main.py +++ b/merico/perf/names/main.py @@ -6,6 +6,7 @@ from devchat.memory import FixSizeChatMemory from lib.ide_service import IDEService +from lib.workflow.decorators import check_select_code PROMPT = prompt = """ file: {file_path} @@ -221,9 +222,10 @@ def remove_unnecessary_escapes(code_a, code_b): return code_copy -def main(): +@check_select_code("Please select code to refactor.") +def main(code: dict): # prepare code - selected_text = get_selected_code() + selected_text = code["text"] selected_code = selected_text.get("text", "") selected_file = selected_text.get("abspath", "")