R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
155 stars 16 forks source link

Unable to use \d or \l to run lines of code that have been newly added to a .R file. #102

Closed matchbook-o closed 6 months ago

matchbook-o commented 6 months ago

When opening an R file and starting R.nvim, I can run all lines of code in my file using \d or \l with no issues.

When I add new lines to my code, \d and \l stop working. However, I can still run lines using \ss.

Using \d or \l causes the following error:

E5108: Error executing lua ~/.vim/plugged/R.nvim/lua/r/send.lua:95: attempt to index local 'n' (a nil value)                                                                                                              
stack traceback:
      ~/.vim/plugged/R.nvim/lua/r/send.lua:95: in function 'is_root'
      ~/.vim/plugged/R.nvim/lua/r/send.lua:99: in function 'get_code_to_send'
      ~/.vim/plugged/R.nvim/lua/r/send.lua:566: in function 'line'
      [string ":lua"]:1: in main chunk

I need to save the file and restart neovim to get \d and \l to work again.

PMassicotte commented 6 months ago

What do you mean with \d or \l?

jalvesaq commented 6 months ago

@PMassicotte, <LocalLeader>d is the default key binding to send the current line and go to the next line, and <LocalLeader>l is the default key binding to send the current line and stay put.

I can't replicate the reported issue. I can send new lines with no need to save the file. However, looking at line 95 of lua/r/send.lua it's clear that the bug is related with treesitter. I even get a warning that "need check nil":

image

@matchbook-o, do you have nvim-treesitter installed?

PMassicotte commented 6 months ago

Maybe we could use this:

https://github.com/R-nvim/R.nvim/blob/4d7a966bea00c070d2c8bf6467475216a7491ea9/lua/r/send.lua#L694-L700

PMassicotte commented 6 months ago

But I wonder why it works before adding new lines

matchbook-o commented 6 months ago

@jalvesaq : I do have treesitter installed.

For more info: I didn't have this issue until I had to reinstall Fedora 39 on my machine (but I am unsure this is related)

Edit: if it helps, I can post my init file. Let me know!

matchbook-o commented 6 months ago

This is my init file:

(EDIT: I still get this issue with a bare-bones init file)

" General Settings
set number
set clipboard=unnamedplus
set autoindent
syntax enable

" Plugin Management
call plug#begin('~/.vim/plugged')
Plug 'R-nvim/R.nvim'
Plug 'R-nvim/cmp-r'
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
Plug 'nvim-treesitter/nvim-treesitter'
" For vsnip users.
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'
call plug#end()

" CMP Setup
lua <<EOF
  -- Set up nvim-cmp.
  local cmp = require'cmp'

  cmp.setup({
    snippet = {
      -- REQUIRED - you must specify a snippet engine
      expand = function(args)
        vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
        -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
        -- require('snippy').expand_snippet(args.body) -- For `snippy` users.
        -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
        -- vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)
      end,
    },
    window = {
      -- completion = cmp.config.window.bordered(),
      -- documentation = cmp.config.window.bordered(),
    },
    mapping = cmp.mapping.preset.insert({
      ['<C-b>'] = cmp.mapping.scroll_docs(-4),
      ['<C-f>'] = cmp.mapping.scroll_docs(4),
      ['<C-Space>'] = cmp.mapping.complete(),
      ['<C-e>'] = cmp.mapping.abort(),
      ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
    }),
    sources = cmp.config.sources({
      { name = 'nvim_lsp' },
      { name = 'vsnip' }, -- For vsnip users.
      -- { name = 'luasnip' }, -- For luasnip users.
      -- { name = 'ultisnips' }, -- For ultisnips users.
      -- { name = 'snippy' }, -- For snippy users.
    }, {
      { name = 'buffer' },
    })
  })

  -- Set configuration for specific filetype.
  cmp.setup.filetype('gitcommit', {
    sources = cmp.config.sources({
      { name = 'git' }, -- You can specify the `git` source if [you were installed it](https://github.com/petertriho/cmp-git).
    }, {
      { name = 'buffer' },
    })
  })

  -- Use buffer source for `/` and `?` (if you enabled `native_menu`, this won't work anymore).
  cmp.setup.cmdline({ '/', '?' }, {
    mapping = cmp.mapping.preset.cmdline(),
    sources = {
      { name = 'buffer' }
    }
  })

  -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
  cmp.setup.cmdline(':', {
    mapping = cmp.mapping.preset.cmdline(),
    sources = cmp.config.sources({
      { name = 'path' }
    }, {
      { name = 'cmdline' }
    }),
    matching = { disallow_symbol_nonprefix_matching = false }
  })

  -- Set up lspconfig.
  local capabilities = require('cmp_nvim_lsp').default_capabilities()
  -- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
  require('lspconfig')['r_language_server'].setup {
    capabilities = capabilities
  }
EOF

" LaTeX Compilation
"au FileType tex nmap <leader>ll <Plug>Tex_Compile
"au FileType tex nmap <leader>lv <Plug>Tex_View

" Text Wrapping "
:set linebreak

" Appearance
set background=dark 

" Line Numbers & Indentation
set backspace=indent,eol,start
set ma
set expandtab
set smarttab
set autoindent
set si
set wrap

" Search
set ignorecase
set smartcase
set hlsearch
set incsearch
set magic

" Brackets
set showmatch
set mat=2

" Errors
set noerrorbells

" Color & Fonts
set encoding=utf8
if $COLORTERM == 'gnome-terminal'
    set t_Co=256
endif
matchbook-o commented 6 months ago

@jalvesaq : I have managed to fix this issue by using your NvimR plug in (the old one) instead of this new one. Everything there works fine with the init file above.

Feel free to close this ticket, although I hope the bug report has some value.

jalvesaq commented 6 months ago

Thanks for the feedback, @matchbook-o! I will let @PMassicotte close the issue if he doesn't want to investigate it further.

matchbook-o commented 6 months ago

@PMassicotte : I have figured out the issue.

In order for treesitter to work, I had to install the nodejs dependency. Once I had that I had to run :TSInstall r.

Now everything works using R.nvim!

I will close the issue, thanks for taking a look.