SendCode icon indicating copy to clipboard operation
SendCode copied to clipboard

New Feature: Expand to cell

Open fredcallaway opened this issue 5 years ago • 16 comments

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.

fredcallaway avatar Jun 30 '20 11:06 fredcallaway

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!!

maegul avatar Sep 16 '21 06:09 maegul

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 avatar Sep 16 '21 11:09 fredcallaway

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

fredcallaway avatar Sep 16 '21 11:09 fredcallaway

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?

mrrezaie avatar Nov 30 '22 09:11 mrrezaie

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.

fredcallaway avatar Nov 30 '22 15:11 fredcallaway

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?

mrrezaie avatar Nov 30 '22 16:11 mrrezaie

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~"}

fredcallaway avatar Nov 30 '22 16:11 fredcallaway

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'

mrrezaie avatar Nov 30 '22 17:11 mrrezaie

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?

fredcallaway avatar Dec 01 '22 13:12 fredcallaway

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

mrrezaie avatar Dec 01 '22 15:12 mrrezaie

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

fredcallaway avatar Dec 01 '22 16:12 fredcallaway

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 avatar Dec 01 '22 17:12 mrrezaie

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

mrrezaie avatar Dec 01 '22 17:12 mrrezaie

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

mrrezaie avatar Dec 01 '22 17:12 mrrezaie

{ "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}},

fredcallaway avatar Dec 01 '22 18:12 fredcallaway

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

mrrezaie avatar Dec 01 '22 18:12 mrrezaie