Giter Club home page Giter Club logo

Comments (15)

matu3ba avatar matu3ba commented on September 21, 2024

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?

from nvim-lua-guide.

nanotee avatar nanotee commented on September 21, 2024

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)

from nvim-lua-guide.

matu3ba avatar matu3ba commented on September 21, 2024

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?

from nvim-lua-guide.

matu3ba avatar matu3ba commented on September 21, 2024

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.

from nvim-lua-guide.

nanotee avatar nanotee commented on September 21, 2024

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

from nvim-lua-guide.

matu3ba avatar matu3ba commented on September 21, 2024

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?

from nvim-lua-guide.

nanotee avatar nanotee commented on September 21, 2024

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

from nvim-lua-guide.

nanotee avatar nanotee commented on September 21, 2024

@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.

from nvim-lua-guide.

matu3ba avatar matu3ba commented on September 21, 2024

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?

from nvim-lua-guide.

nanotee avatar nanotee commented on September 21, 2024

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'

from nvim-lua-guide.

matu3ba avatar matu3ba commented on September 21, 2024

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.

from nvim-lua-guide.

nanotee avatar nanotee commented on September 21, 2024

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)

from nvim-lua-guide.

matu3ba avatar matu3ba commented on September 21, 2024

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.

from nvim-lua-guide.

matu3ba avatar matu3ba commented on September 21, 2024

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?

from nvim-lua-guide.

nanotee avatar nanotee commented on September 21, 2024

: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

from nvim-lua-guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.