Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

Known problem: accessing variables via meta accessors #69

Closed
matu3ba opened this issue Jun 25, 2021 · 15 comments
Closed

Known problem: accessing variables via meta accessors #69

matu3ba opened this issue Jun 25, 2021 · 15 comments

Comments

@matu3ba
Copy link
Contributor

matu3ba commented Jun 25, 2021

:help lua-vim-options shows

                                                             *lua-vim-options*
                                                                 *lua-vim-opt*
                                                                 *lua-vim-set*
                                                            *lua-vim-optlocal*
                                                            *lua-vim-setlocal*

and they are undocumented (dead links).

At least for me I have this issue that the filetype is set after everything is loaded, which makes vim.bo.filetype unusable to conditionally set options. see this issue.

Using the metaaccessor filetype does not work, but it works with vimscript:

  if ( vim.bo.filetype == "cpp" ) --first branch never gets executed
    vim.bo.shiftwidth       = 4; --visual mode >,<-key: number of spaces for indendation
  else
    vim.bo.shiftwidth       = 2;
  end
  if ( vim.bo.filetype == "cpp" ) then --first branch never gets executed
    vim.bo.tabstop          = 4; --Tab key: number of spaces for indendation
  else
    vim.bo.tabstop          = 2;
  end

But

if expand('%:e') == 'tex'
  "command! Buildauto :call Latexmklualatex()
  command! Buildauto :call Latexmkpdflatex()
  command! Pdf :call jobstart(["okular", "build/" . expand("%:t:r") . ".pdf"])
endif

just works. I did not find yet lua methods to replace expand.

@matu3ba matu3ba changed the title Known problems: accessing via Known problems: accessing via meta accessors Jun 25, 2021
@matu3ba matu3ba changed the title Known problems: accessing via meta accessors Known problem: accessing variables via meta accessors Jun 25, 2021
@matu3ba
Copy link
Contributor Author

matu3ba commented Jun 25, 2021

Interestingly also using the other 2 methods ie with :lua print(vim.inspect(filetype)) and :lua print(vim.inspect(vim.api.nvim_buf_get_option(filetype`) dont return a result.

Maybe I am doing stuff completely wrong, but I also tried without vim.inspect and was not successful. Do you have any idea what is wrong?

@nanotee
Copy link
Owner

nanotee commented Jun 25, 2021

and they are undocumented (dead links).

If I understand correctly, I think there's a slight misunderstanding here: these are not links but different tags that point to the same section

Using the metaaccessor filetype does not work, but it works with vimscript:

I'm not sure the problem is related to Lua, it's hard to tell what's happening since I don't know where this code is being called. Is this from an autocommand?

And there's a meaningful difference between the two snippets: expand('%:e') gets the file extension while vim.bo.filetype gets the filetype option (which, in my understanding, is not available in all contexts)

@matu3ba
Copy link
Contributor Author

matu3ba commented Jun 26, 2021

Is this written in the neovim manual or in this guide that certain options are not available in all contexts?

So there is neovim lua equivalent of expand('%:e') and one needs to use regex?

@matu3ba
Copy link
Contributor Author

matu3ba commented Jun 26, 2021

I'm not sure the problem is related to Lua, it's hard to tell what's happening since I don't know where this code is being called. Is this from an autocommand?

No, I wanted to conditionally set options depending on the filetype/file extension in lua.

@nanotee
Copy link
Owner

nanotee commented Jun 26, 2021

Is this written in the neovim manual or in this guide that certain options are not available in all contexts?

I don't remember if it's documented in the manual off the top of my head, this is something I inferred from runtime/filetype.vim: the filetype for a buffer is set when the BufRead or BufNewFile events are fired. If you try to get the filetype before the autocommand is triggered, you get an empty string instead.

No, I wanted to conditionally set options depending on the filetype/file extension in lua.

I think for your particular use case it's better to create an ftplugin/cpp.lua file in your config folder and put this code in it:

vim.opt_local.shiftwidth = 4
vim.opt_local.tabstop = 4

@matu3ba
Copy link
Contributor Author

matu3ba commented Jun 28, 2021

I think for your particular use case it's better to create an ftplugin/cpp.lua file in your config folder and put this code in it:

[user@pc nvim]$ cat ftplugin/cpp.lua
vim.opt_local.shiftwidth = 4
vim.opt_local.tabstop = 4

did not work. Changing to
vim.bo.shiftwidth and vim.bo.tabstop int ftplugin did neither work.

When gets the folder ftplugin executed?

@nanotee
Copy link
Owner

nanotee commented Jun 28, 2021

When gets the folder ftplugin executed?

When you set the filetype for a buffer IIRC. So opening a *.cpp file or running :setfiletype cpp should work.
Note that to do this in Lua, you have to be using the latest nightly as support for Lua runtime files was merged recently

@nanotee
Copy link
Owner

nanotee commented Jul 17, 2021

@matu3ba Have you found a solution to your problem? I created a ~/.config/nvim/ftplugin/cpp.lua file with various options and they're all set correctly when I open a *.cpp file.

@matu3ba
Copy link
Contributor Author

matu3ba commented Jul 19, 2021

I forgot to set symlinks for my dotfiles. -.-.
Where would it make sense to document this behavior?
Near the folders

    colors/
    compiler/
    ftplugin/
    ftdetect/
    indent/
    plugin/
    syntax/

and how they work with the order of how neovim sets up things (load order of stuff)?
Or would this stuff belong into the advanced guide?

@nanotee
Copy link
Owner

nanotee commented Jul 20, 2021

I forgot to set symlinks for my dotfiles. -.-.

Honest mistake :P

Where would it make sense to document this behavior?

Hmm, I'm not sure I understand, what behavior specifically? If you mean runtime files, they're already well documented under :help 'runtimepath'

@matu3ba
Copy link
Contributor Author

matu3ba commented Jul 20, 2021

Hmm, I'm not sure I understand, what behavior specifically?

when vim.bo.filetype is set or available in neovim with comparison if the same holds for vimscript filetype.

@nanotee
Copy link
Owner

nanotee commented Jul 20, 2021

This is the closest Vimscript equivalent to the Lua snippet you posted I could come up with:

if &l:filetype ==# 'cpp'
    let &l:shiftwidth = 4
else
    let &l:shiftwidth = 2
endif

if &l:filetype ==# 'cpp'
    let &l:tabstop = 4
else
    let &l:tabstop = 2
endif

This should behave the same as your original Lua code so I'm not sure the behavior you describe is Lua-specific (unless I misunderstood)

You're setting these options in your init.vim/init.lua, right? The 'filetype' option is set after loading your init file, which is why you get an empty string.

The usual mechanism for per-filetype configuration is either an autocommand (autocmd FileType cpp setlocal shiftwidth=4) or an ftplugin (ftplugin/cpp.lua)

@matu3ba
Copy link
Contributor Author

matu3ba commented Jul 21, 2021

The usual mechanism for per-filetype configuration is either an autocommand (autocmd FileType cpp setlocal shiftwidth=4) or an ftplugin (ftplugin/cpp.lua)

Ok, I understand. Since this is not lua specific, but for per-project configurations a shortcoming: Closing.

@matu3ba matu3ba closed this as completed Jul 21, 2021
@matu3ba
Copy link
Contributor Author

matu3ba commented Jul 21, 2021

You're setting these options in your init.vim/init.lua, right? The 'filetype' option is set after loading your init file, which is why you get an empty string.

:h filetype does not document this behavior. Or where can I read this in the manual?

The usual mechanism for per-filetype configuration is either an autocommand (autocmd FileType cpp setlocal shiftwidth=4) or an ftplugin (ftplugin/cpp.lua)

Is there a collection of "rough edges in neovim" that collects these kind of things?

@nanotee
Copy link
Owner

nanotee commented Jul 21, 2021

:h filetype does not document this behavior. Or where can I read this in the manual?

:help startup documents the order of all the different startup scripts. Section 5 mentions filetype and indent plugins, which are sourced after the init file mentioned in section 4.

The user-manual contains a lot of useful information as well. :help usr_43.txt deals with filetype plugins

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants