-
Notifications
You must be signed in to change notification settings - Fork 21
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
Restore debugging feature #165
Conversation
Allow line highlighting in source buffer during browser and debug calls of functions.
Apologies for the delay. Full time work was prioritised until this weekend. I removed some code paths that looked like it was unsupported tmux. |
Thanks for the quick feedback. Admittedly, I have never tried to debug a function sourced inline into the global environment. I have been doing it solely from a loaded package for package development. e.g. via It works fine when functions are loaded (see video). debug1080.movI can reproduce the same behaviour you have that the functions sourced inline in the console will not trigger the visual highlighting debug/browser. |
Might be related to #147. Perhaps |
Just saw the CI checks failing. I'll see if I can fix those |
doc/R.nvim.txt
Outdated
Another limitation of R-Nvim is that it might be unable to jump to the line in | ||
the source code that is being evaluated if either you have sent the function | ||
directly to the R Console or the function has been sent by the `knitr` | ||
package, without calling the function `source()`. In either case, R-Nvim will | ||
try to find the pattern `funcname <- function(` in the buffer being edited. If | ||
the pattern is found, the cursor will jump to its position. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like it was already a known implementation? (I didn't write the documentation in this PR, I am merely restoring it from the previous state).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried sourcing the function in a file using the source
function and I get the same behaviour (no highlighting). So perhaps the documentation could be updated to reflect that loading functions into the environment via devtools
will work but not using source
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, debugging will only work when sourcing a package with devtools
.
Maybe because I am using an external terminal? |
Some notes:
|
lua/r/debug.lua
Outdated
local rlines | ||
local can_debug = type(config.external_term) == "boolean" and not config.external_term | ||
if not can_debug then return end | ||
|
||
s.func_offset = -1 -- Not found | ||
local sbopt = vim.o.switchbuf | ||
vim.o.switchbuf = "useopen,usetab" | ||
local curtab = vim.fn.tabpagenr() | ||
local isnormal = vim.fn.mode() == "n" | ||
local curwin = vim.fn.winnr() | ||
r_bufnr = vim.api.nvim_get_current_buf() | ||
vim.cmd("sb " .. r_bufnr) | ||
vim.fn.sleep(30) -- Time to fill the buffer lines | ||
rlines = vim.fn.getline(1, "$") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR, the debug code only finds rlines
(The R source lines I believe) if there is no external_term
in the config.
There was vimscript code that defined rlines
when debugging with tmux and I could look to convert it. That code is here.
Lines 97 to 102 in 5b55186
elseif string(g:SendCmdToR) == "function('SendCmdToR_Term')" | |
let tout = system('tmux -L NvimR capture-pane -p -t ' . g:rplugin.tmuxsname) | |
let rlines = split(tout, "\n") | |
elseif string(g:SendCmdToR) == "function('SendCmdToR_TmuxSplit')" | |
let tout = system('tmux capture-pane -p -t ' . g:rplugin.rconsole_pane) | |
let rlines = split(tout, "\n") |
I searched the commit history last week but couldn't determine what the proper way to do this in the lua plugin. As such I removed it. I mentioned it in the initial comment that I removed some code paths that looked like unsupported tmux.
Be happy to be advised on how to fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point, it's better to no longer look at Nvim-R
code. What has to be done is relatively simple:
- When beginning the debugging, load the file where the code is or jump to it if it's already open (this is already done in the specific case that I fixed) and store the buffer number (not done yet).
- From the second jump onward, move the cursor without switching to its buffer (this will be different from
Nvim-R
).
I'm trying to fix the pull request... |
…uns in a external terminal emulator - Replace R-Nvim with `R.nvim` in the documentation. - Rename `M.stop_r_debugging` as `M.stop`. - Rename `M.find_debug_func` as `local find_func`. - Rename `M.r_debug_jump` as `M.jump`. - Fix bugs.
It's fixed on my side if R is running in an external terminal emulator and the function being debugged is in another script which was read with the Uninstall echo 'remove.packages("nvimcom")' | R Then, follow the instructions from the documentation: For example, if you have a file named add.one <- function(x) {
y <- x + 1
print(y)
return(invisible(NULL))
} You could debug the source("path/to/my_function.R")
debug(add.one)
add.one(7) |
I removed "r" and "debug" from the names of Lua functions because the information that the function is related to debugging R functions is already present in the module name, "r.debug". |
Thanks for the quick update. I can confirm that it works for I took the liberty to update the documentation to show the debug_buf_switch.mov |
I can look at this on the next weekend. I think the solution will be to implement steps 1 and 2 from my previous comment. |
Try an R script with these lines: source("debug.R")
debug(addone)
addone(1) In its current buggy state, it only works if the function is in another script which still has to be open. |
It only works if R is running in an external terminal emulator or a Tmux split pane because the code to open and switch buffers is yet to be fixed. |
@jrwishart, please, see #166 as an alternative to this pull request... |
- Update README (remove reference to no implementation of the debugging feature). - Fix spelling of `debug_jump` option in `doc/R.nvim.txt`. - Fix jumps form a buffer to another. - Use `R_GetSrcFilename()` to simplify `nvimcom.c` code.
I found some time today and fixed the bugs I knew existed. Of course, instead of merging this pull request, it would be better to have a real DAP plugin. |
Thanks for the speedy reply/fix. I agree that having dap would be preferable but it doesn't exist in a working state at the moment. (Happy to be proved otherwise) |
The last commit fixed the cursor position but seems to have introduced a new bug where it doesn't find the correct line if debug or browser is called from another function in a different file. E.g. in the video, jump_480.mov |
It works on my side if I do |
Thanks for catching this! It should work now. |
When we So, the debugging will not work if you send a function directly to the R Console and switch to another buffer before starting to debug the function. |
There is still one possible improvement. When debugging a function from an installed package or a function sent directly to the R Console from another R script, the source code isn't available, but the whole function is printed in the R Console, and we could copy it from there and paste it in a new temporary R script. However, before investing time in doing this, let's see if the DAP works... |
Great this is working if I create a script like this: addone <- function(x) {
x + 1
}
browser()
addone(10) and then |
Hi all, I've only been somewhat following this thread because I very rarely use R's debugger. But just want to throw this out there in case it's relevant - the guys at Posit are working on a DAP server/LSP server for R called Ark. It's not yet usable by other software than their new Positron IDE, but the README says it will be soon. |
Thanks for the news, @wurli! This is another reason for not merging this pull request, but we can keep it open until either |
Ark is still usable only by Positron. https://codeberg.org/dgkf/debugadapter still says "Currently, nothing will happen" when "debugging". Do we have other options? Should we keep waiting or merge this pull request? |
Have you been able to test it? Could be nice to have it rather than nothing. Maybe switch later when something else is ready. |
I'm resolving the conflicts... |
I fixed the conflicts in Github's online editor. |
Ok I will test it. |
Using this as testing code: addone <- function(x) {
browser()
x + 1L
z <- x / 42L
}
addone(10L)
for (x in 1L:3L) {
print(x)
} If I go in the console and type However, I use
|
Fixed: |
lua/r/debug.lua
Outdated
"-t", | ||
require("r.external_term").get_tmuxsname(), | ||
} | ||
local resp = require("r.utils").system(run_cmd, { text = true }):wait() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the faulty line when trying to source a file that includes a browser()
call.
Thanks, fixed small error on keymaping and added a sentence. |
Co-authored-by: Philippe Massicotte <[email protected]>
Co-authored-by: Philippe Massicotte <[email protected]>
Thank you and thanks to @jrwishart for this work. |
* Restore debugging feature * Update documentation to mention devtools approach Co-authored-by: Jakson Alves de Aquino <[email protected]> Co-authored-by: Philippe Massicotte <[email protected]>
Allow line highlighting in source buffer during browser and debug calls
of functions.