-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/edit code #830
Open
sbendary25
wants to merge
2
commits into
OpenInterpreter:main
Choose a base branch
from
sbendary25:feature/edit-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/edit code #830
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,8 @@ | ||||||
import argparse | ||||||
import subprocess | ||||||
import os | ||||||
import platform | ||||||
import pkg_resources | ||||||
import appdirs | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
from ..utils.display_markdown_message import display_markdown_message | ||||||
from ..utils.open_file import open_file | ||||||
from ..terminal_interface.conversation_navigator import conversation_navigator | ||||||
|
||||||
arguments = [ | ||||||
|
@@ -114,16 +112,9 @@ def cli(interpreter): | |||||
config_dir = appdirs.user_config_dir("Open Interpreter") | ||||||
config_path = os.path.join(config_dir, 'config.yaml') | ||||||
print(f"Opening `{config_path}`...") | ||||||
# Use the default system editor to open the file | ||||||
if platform.system() == 'Windows': | ||||||
os.startfile(config_path) # This will open the file with the default application, e.g., Notepad | ||||||
else: | ||||||
try: | ||||||
# Try using xdg-open on non-Windows platforms | ||||||
subprocess.call(['xdg-open', config_path]) | ||||||
except FileNotFoundError: | ||||||
# Fallback to using 'open' on macOS if 'xdg-open' is not available | ||||||
subprocess.call(['open', config_path]) | ||||||
|
||||||
open_file(file_path=config_path) | ||||||
|
||||||
return | ||||||
|
||||||
# TODO Implement model explorer | ||||||
|
Empty file.
25 changes: 25 additions & 0 deletions
25
interpreter/code_interpreters/languages/utils/language_tools.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from ...language_map import language_map | ||
|
||
|
||
def get_language_file_extension(language_name): | ||
""" | ||
Get the file extension for a given language | ||
""" | ||
language = language_map[language_name.lower()] | ||
|
||
if language.file_extension: | ||
return language.file_extension | ||
else: | ||
return language | ||
|
||
|
||
def get_language_proper_name(language_name): | ||
""" | ||
Get the proper name for a given language | ||
""" | ||
language = language_map[language_name.lower()] | ||
|
||
if language.proper_name: | ||
return language.proper_name | ||
else: | ||
return language |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,14 @@ def respond(interpreter): | |
print("Running code:", interpreter.messages[-1]) | ||
|
||
try: | ||
# Yield a message, such that the user can stop code execution if they want to | ||
try: | ||
yield {"executing": {"code": interpreter.messages[-1]["code"], "language": interpreter.messages[-1]["language"]}} | ||
except GeneratorExit: | ||
# The user might exit here. | ||
# We need to tell python what we (the generator) should do if they exit | ||
break | ||
|
||
# What code do you want to run? | ||
code = interpreter.messages[-1]["code"] | ||
|
||
|
@@ -105,13 +113,6 @@ def respond(interpreter): | |
interpreter._code_interpreters[language] = create_code_interpreter(language) | ||
code_interpreter = interpreter._code_interpreters[language] | ||
|
||
# Yield a message, such that the user can stop code execution if they want to | ||
try: | ||
yield {"executing": {"code": code, "language": language}} | ||
except GeneratorExit: | ||
# The user might exit here. | ||
# We need to tell python what we (the generator) should do if they exit | ||
break | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
# Yield each line, also append it to last messages' output | ||
interpreter.messages[-1]["output"] = "" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
from yaspin import yaspin | ||
|
||
from .temporary_file import create_temporary_file, cleanup_temporary_file | ||
from .open_file import open_file | ||
from ..code_interpreters.languages.utils.language_tools import get_language_file_extension, get_language_proper_name | ||
|
||
|
||
def edit_code(code, language, interpreter): | ||
""" | ||
Edit the code and listen for changes with watchdog | ||
""" | ||
|
||
temp_code = code | ||
|
||
temp_file = create_temporary_file( | ||
temp_code, get_language_file_extension(language), verbose=interpreter.debug_mode | ||
) | ||
|
||
language_name = get_language_proper_name(language) | ||
|
||
file_name = os.path.basename(temp_file) | ||
|
||
if interpreter.debug_mode: | ||
print(f"Editing {language_name} code in {file_name}") | ||
print("---") | ||
|
||
# Run semgrep | ||
try: | ||
print(" Press `ENTER` after you've saved your edits.") | ||
|
||
open_file(temp_file) | ||
|
||
with yaspin(text=f" Editing {language_name} code...").green.right.dots as loading: | ||
# HACK: we're just listening for the user to come back and hit Enter | ||
# but we aren't actually doing anything with it and since we're inside | ||
# of a yaspin handler, the input prompt doesn't actually render | ||
done = input(" Press `ENTER` when you're ready to continue:") | ||
|
||
loading.stop() | ||
loading.hide() | ||
|
||
if done == "": | ||
print(f" {language_name} code updated.") | ||
print("") # <- Aesthetic choice | ||
|
||
temp_code = open(temp_file).read() | ||
|
||
if interpreter.debug_mode: | ||
print(f"Getting updated {language_name} code from {file_name}") | ||
print("---") | ||
|
||
cleanup_temporary_file(temp_file, verbose=interpreter.debug_mode) | ||
|
||
if interpreter.debug_mode: | ||
print(f"Deleting {file_name}") | ||
print("---") | ||
|
||
except Exception as e: | ||
print(f"Could not edit {language} code.") | ||
print(e) | ||
print("") # <- Aesthetic choice | ||
|
||
return temp_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import os | ||
import platform | ||
import subprocess | ||
|
||
def open_file(file_path): | ||
if platform.system() == 'Windows': | ||
os.startfile(file_path) # This will open the file with the default application, e.g., Notepad | ||
else: | ||
try: | ||
# Try using xdg-open on non-Windows platforms | ||
subprocess.call(['xdg-open', file_path]) | ||
except FileNotFoundError: | ||
# Fallback to using 'open' on macOS if 'xdg-open' is not available | ||
subprocess.call(['open', file_path]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interpreter/cli/cli.py[](url)