radian
radian copied to clipboard
Preserve existing terminal input when sending code to radian
As described here: Ikuyadeu/vscode-R/issues/400 it is a bit of a hassle that code send to radian via ctrl+enter is just added onto the existing input which causes issues when there is already some incomplete command there. Rstudio handles this perfectly but vscode-r has no access to radian so it can not.
I looked into bracketed paste mode as a solution to this (to distinguish typed/hand pasted from send code) and I think the solution is relatively easy? But I do not know how to run/test radian from source :-/
I think this would emulate rstudio (or maybe it does not work like that at all 😹 )? https://github.com/assignUser/radian/blob/723b15a6e2f4c442e03139aeb4fef64b1f97cba5/radian/key_bindings.py#L291
I found out that installation does not require conda etc., so I could test a bit. I have it almost:
The previous input is saved and insert back in but to early so it will be displayed with the rest of the input but wont be evaluated by R.
I am not sure how to proceed my knowledge of python and radians internals is not enough...
I thought that prompttools save_to_undo_stack might be exactly what is needed but undo
does not work for some reason (maybe its deleted when .reset()
)?
The result above was achieved with:
if should_eval and prase_text_complete(data):
data = data.rstrip("\n")
old_doc = event.current_buffer.document.text
event.current_buffer.reset()
event.current_buffer.insert_text(data)
event.current_buffer.validate_and_handle()
event.current_buffer.insert_text(old_doc)
else:
This causes weird issues with executing multiline code send from vsc.
Thank you for taking the time to investigate a solution. I think your solution is in the right direction but I won't reset the old_doc
after validate_and_handle
. Perhaps we will need to put some of the logics at https://github.com/randy3k/lineedit/blob/rewrite/lineedit/buffer.py
I try with my meager python skills 🤣 You mean adding something like buffer.execute_bracketed_insert()
or something specifically for this situation? I will have a look...
We might want to modify the behavior of validate_and_handle
instead of the current hacky solution.
@randy3k as far as I can see currently you use prompt_toolkit's buffer and expand it to ModalBuffer
but in the linked rewrite
branch it looks like you no longer use prompt_toolkit?
How is the timeline on the rewrite? Should I try it there or should I work in master for now?
The link is wrong. Sorry about it. Please use the master branch.
It would be great if we could solve this. What's the best way to help?
I think the solution is to patch https://github.com/randy3k/lineedit/blob/4262e9852aa50b09ff3f0569e1840b003ed00584/lineedit/buffer.py#L7 which is derived from https://github.com/randy3k/lineedit/blob/4262e9852aa50b09ff3f0569e1840b003ed00584/lineedit/deps/prompt_toolkit/buffer.py#L137
To be specific, patch around https://github.com/randy3k/lineedit/blob/4262e9852aa50b09ff3f0569e1840b003ed00584/lineedit/deps/prompt_toolkit/buffer.py#L1614