jalvesaq / Nvim-R

Vim plugin to work with R
GNU General Public License v2.0
968 stars 125 forks source link

indentation of one-line conditional/loop blocks? #719

Closed dankelley closed 1 year ago

dankelley commented 1 year ago

I am using neovim quite a lot lately, and liking it a lot. One thing I've noticed, though is a change from what macvim does for me. With macvim if I have e.g.

if (TRUE)
    message("truth")

and indent it, it stays as you can see. However, if I indent this with neovim, the second line is lined up with the first, e.g.

if (TRUE)
message("truth")

I am wondering whether there is a variable setting for this. I have to admit, though, that I have become a bit lost on how to update things in either macvim or neovim, and so I am not even really sure where my settings files are with macvim. With neovim, I have a ~/.config/nvim/init.lua that I copied from someone a few months ago (via this friendly webpage!!!). Rather than paste the whole thing nearly 2000 lines of that, I am putting what I think the relevant bits, below (click Details to see).

The embarrassing thing is that I don't actually even know "who" is doing the indentation. I don't understand neovim well enough to know whether the indentation is being done by Nvim-R or by something else (the LSP?)

Jakson, I am very sorry this issue is so lacking in detail and precision. Please feel free to delete it, if you judge it to be a waste of time!! And, as always, thanks a million for all the work you've done on the R vim-mode over the years. I could not imagine how I would have any hair left, at all, if I had to use R.app or RStudio to work with R.

Dan.

```lua -- R { "jalvesaq/R-Vim-runtime", ft = { "r", "rmd", "rnoweb", "rout", "rhelp" } }, { "jalvesaq/Nvim-R", event = "VeryLazy", dependencies = { "jalvesaq/R-Vim-runtime" }, ft = { "r", "rmd", "rnoweb", "rout", "rhelp" }, config = function() -- do not update $HOME on Windows since I set it manually if vim.fn.has("win32") == 1 then vim.g.R_set_home_env = 0 end -- disable debugging support, vim.g.R_debug = 1 -- do not align function arguments vim.g.r_indent_align_args = 0 -- use two backticks to trigger the Rmarkdown chunk completion vim.g.R_rmdchunk = "``" -- use - to insert assignment vim.g.R_assign_map = "" -- show hidden objects in object browser vim.g.R_objbr_allnames = 1 -- show comments when sourced vim.g.R_commented_lines = 1 -- use the same working directory as Vim vim.g.R_nvim_wd = 1 -- highlight chunk header as R code vim.g.rmd_syn_hl_chunk = 1 -- only highlight functions when followed by a parenthesis vim.g.r_syntax_fun_pattern = 1 -- set encoding to UTF-8 when sourcing code vim.g.R_source_args = 'echo = TRUE, spaced = TRUE, encoding = "UTF-8"' -- number of columns to be offset when calculating R terminal width vim.g.R_setwidth = -7 -- manually set the R path since scoop did not write registry entries about R if string.lower(jit.os) == "windows" then local scoop_r = require("plenary.path").new(vim.loop.os_homedir(), "scoop", "apps", "r") if scoop_r:exists() then vim.g.R_path = scoop_r:joinpath("current", "bin").filename:gsub("\\", "/") end end -- auto quit R when close Vim vim.cmd[[ autocmd VimLeave * if exists("g:SendCmdToR") && string(g:SendCmdToR) != "function('SendCmdToR_fake')" | call RQuit("nosave") | endif ]] -- helper function to detect if current buffer is an R terminal -- created by Nvim-R local IsRTerm = function(buffer) local buf = buffer == nil and 0 or buffer if vim.api.nvim_buf_get_option(buf, "buftype") == "terminal" then local bufname = vim.api.nvim_buf_get_name(buf) -- this is a R termial created by NVim-R if string.find(bufname, "^term://.+//%d+:R%s?$") then return true else return false end else return false end end -- assign and pipe local r_set_keymap_pipe = function (buffer) local buf = buffer == nil and 0 or buffer vim.keymap.set("i", "", "<-", { buffer = buf }) vim.keymap.set("i", "", "%>%", { buffer = buf }) vim.keymap.set("i", "", "|>", { buffer = buf }) vim.keymap.set("i", "", ":=", { buffer = buf }) end -- {targets} local r_set_keymap_targets = function (buffer) local buf = buffer == nil and 0 or buffer vim.keymap.set("n", "tm", "RSend targets::tar_make()", { buffer = buf }) vim.keymap.set("n", "tM", "RSend targets::tar_make(callr_function = NULL)", { buffer = buf }) vim.keymap.set("n", "tf", "RSend targets::tar_make_future(workers = parallelly::availableCores() - 1L)", { buffer = buf }) end -- debug local r_set_keymap_debug = function (buffer) local buf = buffer == nil and 0 or buffer vim.keymap.set("n", "tb", "RSend traceback()", { buffer = buf }) vim.keymap.set("n", "sq", "RSend Q", { buffer = buf }) vim.keymap.set("n", "sc", "RSend c", { buffer = buf }) vim.keymap.set("n", "sn", "RSend n", { buffer = buf }) end -- add keymap for quit R if current window is an R terminal vim.api.nvim_create_autocmd( { "BufEnter", "BufWinEnter" }, { group = vim.api.nvim_create_augroup("RTermSetup", {}), pattern = "*", callback = function(args) -- if current buffer is an R terminal if IsRTerm(args.buf) then -- set keymap to quit R vim.keymap.set("n", "rq", "call RQuit('nosave')", { buffer = args.buf }) -- set other keymap r_set_keymap_targets(args.buf) r_set_keymap_debug(args.buf) end end } ) vim.api.nvim_create_autocmd( { "BufEnter", "BufWinEnter" }, { group = vim.api.nvim_create_augroup("RCommonSetup", {}), pattern = { "*.r", "*.R", "*.rmd", "*.Rmd", "*.qmd" }, callback = function() -- set roxygen comment string vim.opt_local.comments:append("b:#'") -- insert current comment leader vim.opt_local.formatoptions:append("r") -- nvim-lspconfig set formatexpr to use lsp formatting, -- which breaks gq for comments vim.opt_local.formatexpr = nil end } ) -- keymaps for inserting pipes and debugging vim.api.nvim_create_autocmd( { "BufEnter", "BufWinEnter" }, { group = vim.api.nvim_create_augroup("RKeymapSetup", {}), pattern = { "*.r", "*.R", "*.rmd", "*.Rmd", "*.qmd" }, callback = function() vim.wo.colorcolumn = "150" r_set_keymap_pipe() r_set_keymap_targets() r_set_keymap_debug() end } ) vim.api.nvim_create_autocmd( { "BufEnter", "BufWinEnter" }, { group = vim.api.nvim_create_augroup("RMarkdownSetup", {}), pattern = { "*.rmd", "*.Rmd" }, callback = function() -- wrap long lines vim.wo.wrap = true function RToggleRmdEnv() -- get current value local env = vim.g.R_rmd_environment if env == ".GlobalEnv" then env = "new.env()" else env = ".GlobalEnv" end vim.g.R_rmd_environment = env print("Rmd will be rendered in an empty environment.") end vim.keymap.set("n", "re", RToggleRmdEnv, { buffer = 0 }) end } ) end }, ```
jalvesaq commented 1 year ago

I can't replicate the issue. Perhaps, it's something related to Treesitter or LSP...

dankelley commented 1 year ago

Thanks. That was my guess. I will close this now, with my sincere thanks. If I ever discover the problem, I will add a comment explaining it, so that other folks might see it in future.

Many thanks, as always. Dan.