feat: Display README when no code is selected

- Add feature to show README.md when no text is selected
- Modify all command and main modules to support this feature
- Remove 'input: required' from command.yml files for flexibility
This commit is contained in:
bobo 2024-04-01 16:41:25 +08:00
parent ceb280012d
commit 852706d25d
9 changed files with 41 additions and 4 deletions

View File

@ -1,4 +1,3 @@
description: Automatically add doc comments. Select some code and execute this command to generate comments.
input: required
steps:
- run: $devchat_python $command_path/main.py

View File

@ -1,4 +1,5 @@
import re
import os
import sys
from devchat.ide.service import IDEService
@ -6,6 +7,7 @@ from devchat.ide.vscode_services import selected_lines, visible_lines
from devchat.llm import chat
def get_selected_code():
"""
Retrieves the selected lines of code from the user's selection.
@ -22,6 +24,13 @@ def get_selected_code():
miss_selected_error = "Please select some text."
if selected_data["text"] == "":
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)

View File

@ -1,4 +1,3 @@
description: Automatically add docstrings. Select a function or method and execute this command to generate docstring.
input: required
steps:
- run: $devchat_python $command_path/main.py

View File

@ -1,4 +1,5 @@
import re
import os
import sys
from devchat.ide.service import IDEService
@ -22,6 +23,13 @@ def get_selected_code():
miss_selected_error = "Please select some text."
if selected_data["text"] == "":
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)

View File

@ -1,4 +1,3 @@
description: Explain selected code.
input: required
steps:
- run: $devchat_python $command_path/explain.py

View File

@ -1,4 +1,5 @@
import sys
import os
from devchat.ide.service import IDEService
from devchat.llm import chat
@ -20,6 +21,13 @@ def get_selected_code():
miss_selected_error = "Please select some text."
if selected_data["text"] == "":
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)

View File

@ -1,4 +1,3 @@
description: Try to find out potential bugs in the selected code and try fixing these bugs automaticly.
input: required
steps:
- run: $devchat_python $command_path/main.py

View File

@ -1,4 +1,5 @@
import re
import os
import sys
from devchat.ide.service import IDEService
@ -22,6 +23,13 @@ def get_selected_code():
miss_selected_error = "Please select some text."
if selected_data["text"] == "":
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)

View File

@ -1,4 +1,5 @@
import re
import os
import sys
from devchat.ide.service import IDEService
@ -22,6 +23,13 @@ def get_selected_code():
miss_selected_error = "Please select some text."
if selected_data["text"] == "":
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)