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

[feature request] send within treesitter node #1

Open
jmbuhr opened this issue Jun 3, 2023 · 4 comments
Open

[feature request] send within treesitter node #1

jmbuhr opened this issue Jun 3, 2023 · 4 comments

Comments

@jmbuhr
Copy link

jmbuhr commented Jun 3, 2023

Very cool plugin! I would love to integrate this further with quarto-nvim such as using it for QuartoSendAbove etc. instead of the current vim-slime plugin.

I'm about to merge a cool addition to otter.nvim to use treesitter injections to query code chunks, meaning there won't be a need to define a custom query for every filetype (jmbuhr/otter.nvim#39).
What I found there might be usefull here as well. In injections.scm the captures that belong to file types with code chunks (such as markdown, quarto, orgmode) always have a caputure _lang that contains the code language and @content that contains the code of the chunk. That means getting the @content capture in which the cursor is will always get the code that should be send to the REPL and could be made even cooler if the type of REPL to send to would depend on _lang i.e. send R code to R REPL and python code to ipython.
I still have to do some cleaning up on the otter.nvim PR, but the code of this function might be helpful here:
https://github.com/jmbuhr/otter.nvim/blob/79632759085aa1f3165454811767f4717ab04114/lua/otter/keeper.lua#L26 as well as https://github.com/jmbuhr/otter.nvim/blob/79632759085aa1f3165454811767f4717ab04114/lua/otter/tools/functions.lua#L70

@milanglacier
Copy link
Owner

milanglacier commented Jun 3, 2023

This is a cool feature I would be excited to see!

However, instead of making it a dedicated feature such that it can only be used with yarepl, how about making it as a text object / motion such that every operator (not limited to yarepl) can get benefit from, such that we can easily copy, change, or do whatever to the whole code cell?

Example of using treesitter to get text objects: nvim-treesitter-textobjects

I also have a hard coded, non intelligent implementation for code cell (see code at here link).
I use an expression keymap to make keybindings to send a code cell to a REPL using less keystroke: link

I would be excited to see that we can use treesitter to create a syntax aware code cell text object!

@jmbuhr
Copy link
Author

jmbuhr commented Jun 3, 2023

Is there a function to directly give the text that should be send to yarepl via lua? In this case I can easily adapt the QuartoSend functions (https://github.com/quarto-dev/quarto-nvim/blob/9a1c79713397a7bdcf67ea687720e0b6961d90cc/lua/quarto/init.lua#L162-L206) to use different backends.

@milanglacier
Copy link
Owner

Could you please check if #2 works for you?

@milanglacier
Copy link
Owner

milanglacier commented Jun 11, 2023

I just made a change to the function signature _send_strings API in the latest commit in case you have already used the previous function signature to build the command, please make a slightly change to the call:

---@param id number the id of the repl,
---@param name string|nil the name of the closest repl that will try to find
---@param bufnr number|nil the buffer number from which to find the attached REPL.
---@param strings table[string] a list of strings
---@param use_formatter boolean|nil whether use formatter (e.g. bracketed_pasting)? Default: true
-- Send a list of strings to the repl specified by `id` and `name` and `bufnr`.
-- If `id` is 0, then will try to find the REPL that `bufnr` is attached to, if
-- not find, will use `id = 1`. If `name` is not nil or not an empty string,
-- then will try to find the REPL with `name` relative to `id`. If `bufnr` is
-- nil or `bufnr` = 0, will find the REPL that current buffer is attached to.
M._send_strings = function(id, name, bufnr, strings, use_formatter)

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

2 participants