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

New Feature: Expand to cell #135

Open
fredcallaway opened this issue Jun 30, 2020 · 16 comments
Open

New Feature: Expand to cell #135

fredcallaway opened this issue Jun 30, 2020 · 16 comments

Comments

@fredcallaway
Copy link

Hey there! A while ago, I implemented a feature to expand to the full containing block regardless of where in the block you are in (as opposed to current behavior which requires that you are on the first line of the block (with # %%) to run the full cell. This matches the behavior of R studio and (to some extent) jupyter notebooks.

Would you consider a pull request? Unfortunately, I made the change on an old version and so it's not trivial to integrate into master, which is why I'm asking before I put in the time.

@maegul
Copy link

maegul commented Sep 16, 2021

Nice! I was thinking of doing the same thing!

I just had a quick look at the code ... and it seems relatively trivial to adapt the existing code base to do this.

The current process, as implemented in the block_expand method, is to search from the current line to the end of the file for the block ending comment (as defined in the settings, and by default presumed to be # %%).

To send code blocks while the cursor is somewhere within the block, rather than at the top, like you suggest, would simply require running this same search that is currently performed upward and downward when necessary.

If you've done something like this, I might be inclined to merge it in manually myself. It would definitely be a nice addition to the library!!

@fredcallaway
Copy link
Author

You can find my version here https://github.com/fredcallaway/SendCode but I noticed that master currently has a "block_start_pattern" preference and a corresponding block_expand method in getter... so i feel like this should already work. My hacked up version also has a bunch of extra stuff to emulate RStudio that probably won't be useful to you.

However, if you're into cells, you may find this little command useful: https://github.com/fredcallaway/Fredlime/blob/master/main.py#L94

    { "keys": ["alt+down"], "command": "jump_cell"},
    { "keys": ["alt+up"], "command": "jump_cell", "args": {"backward": true}},

@fredcallaway
Copy link
Author

I'm sorry, I just noticed that you're already aware of the block_expand method. I think it might actually be pretty easy to adapt block_expand to use my find_surround helper.

https://github.com/fredcallaway/SendCode/blob/master/code_getter/getter.py#L7

@mrrezaie
Copy link

mrrezaie commented Nov 30, 2022

Hi, I'm looking for this behavior.

To send code blocks while the cursor is somewhere within the block, rather than at the top

Could you please guide me how I can do that if this is already done?

@fredcallaway
Copy link
Author

I don't think this is possible with the main package. I have it in my fork, which you could install by removing the standard one and cloning directly into your Packages folder.

https://github.com/fredcallaway/SendCode

Then add { "keys": ["ctrl+enter"], "command": "send_code", "args": {"cell": true} in your sublime-keymap file.

But beware, I've made lots of arbitrary changes to the code to suit my needs; not sure you will like all of them. The basic block expansion logic is implemented in commit d63a2987c269.

@mrrezaie
Copy link

Thanks @fredcallaway

I removed the standard one and installed yours using Add Repository and Install Package. Now SendCode exists in this directory: Roaming\Sublime Text\Packages. Then, I updated the keymap file. But nothing happens. Any idea?

@fredcallaway
Copy link
Author

fredcallaway commented Nov 30, 2022

Can you show me the console output with sublime.log_commands(True)? (View > Show Console). For example, with the file

# %%
| # cursor location
x = 1
# %%

If I hit ctrl+enter, the console prints:

command: send_code {"cell": true}
command: terminus_send_string {"string": "\u001b[200~# %%\n\nx = 1\u001b[201~"}

@mrrezaie
Copy link

This is the error:

command: send_code {"cell": true}
Traceback (most recent call last):
File "F:\Sublime Text\Lib\python33\sublime_plugin.py", line 1488, in run_
return self.run(edit, **args)
File "C:\Users\mrr\AppData\Roaming\Sublime Text\Packages\SendCode\send_code.py", line 100, in run
sender = CodeSender.initialize(self.view, prog=prog, from_view=cmd is None)
File "C:\Users\mrr\AppData\Roaming\Sublime Text\Packages\SendCode\code_sender\sender.py", line 35, in initialize
syntax = Settings(view).syntax()
File "C:\Users\mrr\AppData\Roaming\Sublime Text\Packages\SendCode\settings.py", line 15, in init
def init(self, view):
AttributeError: 'NoneType' object has no attribute 'load_settings'

@fredcallaway
Copy link
Author

That line is self.s = sublime.load_settings("SendCode.sublime-settings"). I have no idea how sublime could be None. I assume you tried restarting Sublime Text?

@mrrezaie
Copy link

mrrezaie commented Dec 1, 2022

Yes, I did. But this error was before that one:

reloading python 3.3 plugin SendCode.send_code
Traceback (most recent call last):
File "F:\Sublime Text\Lib\python33\sublime_plugin.py", line 308, in reload_plugin
m = importlib.import_module(modulename)
File "./python3.3/importlib/init.py", line 90, in import_module
File "", line 1584, in _gcd_import
File "", line 1565, in _find_and_load
File "", line 1532, in _find_and_load_unlocked
File "", line 584, in _check_name_wrapper
File "", line 1022, in load_module
File "", line 1003, in load_module
File "", line 560, in module_for_loader_wrapper
File "", line 868, in _load_module
File "", line 313, in call_with_frames_removed
File "C:\Users\mrr\AppData\Roaming\Sublime Text\Packages\SendCode\send_code.py", line 7, in
from .code_sender import CodeSender
File "C:\Users\mrr\AppData\Roaming\Sublime Text\Packages\SendCode\code_sender_init
.py", line 1, in
from .sender import CodeSender
File "C:\Users\mrr\AppData\Roaming\Sublime Text\Packages\SendCode\code_sender\sender.py", line 17, in
from .terminalview import send_to_terminalview
ImportError: No module named 'SendCode.code_sender.terminalview'
reloading python 3.3 plugin SendCode.settings
plugins loaded

@fredcallaway
Copy link
Author

Ah weird, that folder somehow didn't make it into the git repo. I fixed it. Try pulling the updated repo?

@mrrezaie
Copy link

mrrezaie commented Dec 1, 2022

Thanks @fredcallaway.
It is loaded successfully and when I press ctrl+enter, the cursor moves to the next cell, but nothing moves to terminus. This error also appears in the console:

ConEmuC.exe not found. Specify the path to ConEmuC.exe in SendCode.sublime-settings.

@mrrezaie
Copy link

mrrezaie commented Dec 1, 2022

Oh Sorry, my bad. I didn't adjust the settings. This is working as expected and I deeply appreciate that.

@mrrezaie
Copy link

mrrezaie commented Dec 1, 2022

Is it possible to set the auto_advance in the args? So that users can create a keymap to "run" or "run and advance".

@fredcallaway
Copy link
Author

{ "keys": ["ctrl+shift+enter"], "command": "send_code", "args": {"cell": true, "advance": true}},

I also have commands to move up and down cells without running them here (you could rename the file and put it in the Packages/User/)

    { "keys": ["alt+down"], "command": "jump_cell"},
    { "keys": ["alt+up"], "command": "jump_cell", "args": {"backward": true}},

@mrrezaie
Copy link

mrrezaie commented Dec 1, 2022

Oh, thank you so much. That's really great.

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

No branches or pull requests

3 participants