Skip to content
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

Added the Ability to edit the code before running it. #935

Closed
wants to merge 3 commits into from

Conversation

Eliaskhal
Copy link

@Eliaskhal Eliaskhal commented Jan 17, 2024

Now the user can edit the code before running it.
OI does not keep track of previous code (before it was edited).

What I did exactly:

  • Instead of just asking the user if they want to run the code or not, I added a while loop that keeps on asking if they want to run the code or edit it. The purpose of using the loop is to give the user the ability to re-edit the code until they are satisfied.
  • When the user chooses to edit the code, the code gets saved in a temp file that is opened in "VS Code". After the user is done editing, they can type any character into the terminal and the "code" variable will be changed.

Possible Improvements:

  • The code before it's edited is not saved in the message history.
  • It would be better if all previous versions of the code were saved so the user can go back to each one if they want to.

NOTE:

I haven't been able to test the code since I had problems running it even though I have followed the documentation.

Check List:

  • I have included relevant documentation updates (stored in /docs)
  • I have read docs/CONTRIBUTING.md
  • I have read docs/ROADMAP.md

Tests:

  • Tested on Windows
  • Tested on MacOS
  • Tested on Linux

Now the user can edit the code before running it. OI does not keep track of previous code (before it was edited).
@Eliaskhal
Copy link
Author

I also think it would be a good idea to give the user the ability to choose their favorite editor by asking them once the first time they want to edit a code.

temp_file.write(code)
temp_filename = temp_file.name

subprocess.run(['code', temp_filename], check=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use something like this to open it in default editor. Check language and add extention to the tmp file. python = .py etc..

import os
import subprocess
import platform

def open_file_default_editor(file_path):
    try:
        if platform.system() == 'Windows':
            os.startfile(file_path)
        elif platform.system() == 'Darwin':  # macOS
            subprocess.call(('open', file_path))
        elif platform.system() == 'Linux':
            subprocess.call(('xdg-open', file_path))
        else:
            print(f"Unsupported OS: {platform.system()}")
    except Exception as e:
        print(f"Error opening file: {e}")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback!
For checking the language, does OI already know what language the code he wrote is in?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but if language is None it defaults to python

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please give me the name of the variable that holds that value?

One more thing, I noticed that whatever changes I made to the code, when I run interpreter in the terminal, it just runs the first version. Why is that?

Copy link
Collaborator

@Notnaton Notnaton Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at contributors.md
I added a comment, to show where you can get the language

@Notnaton
Copy link
Collaborator

#830
Here is a similar PR, but outdated

@KillianLucas
Copy link
Collaborator

Hi @Eliaskhal!

This is going to be such a useful feature. I think this is close, but I want to approach it in a slightly different place.

My main edit: I think it's too much to ask the user if they want to edit every code block, especially to insert it into a familiar (y/n) ask. I think edit is much more similar to the other %magic commands (%shell is very similar to edit, in that it lets you type and run code) than to the central OI concept of running code (which putting it below every code block, as this does, would imply).

I'm happy to merge if it can be converted to a magic command with the flow outlined here, otherwise I'll merge once this feature becomes my priority, then I'll convert it into a magic command.

Also it's worth noting that we do store the file extension for the code, so if you interpreter.computer.terminal.get(language).file_extension you can retrieve the proper file extension, allowing you to open it in the user's default editor.

Great work on building the feature. I'm personally very excited to use this, and your PR works incredibly well.

@Eliaskhal
Copy link
Author

Hey @KillianLucas, I did not notice your comment and I ended up adding what @Notnaton suggested. I would be more than happy to use your idea. My only problem is that I am still having issues understanding how everything is architected in the project.

I understand OI has a message history and magic commands. If you don't mind, could you please explain a bit about how those messages are stored? and where does the user type the magic command exactly?

@Eliaskhal
Copy link
Author

I think I was able to figure it out. I added the magic command and it works now. I will close this request and start a new one. Thank you!

@Eliaskhal Eliaskhal closed this Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants