micangl / cmp-vimtex

Vimtex source for nvim-cmp.
MIT License
78 stars 5 forks source link

Autocommand not triggered #1

Closed lervag closed 10 months ago

lervag commented 10 months ago

The autocommand defined here is not triggered if cmp-vimtex is loaded lazily by e.g. lazy.nvim. I think this will be a quite common situation.

A simple, but possibly good solution is to add a check if the current file is already a .tex file, and to then start the parser immediately.

I would also advice to put all code inside the setup function. That is, requireing a script should not have a side effect in itself.

micangl commented 10 months ago

@lervag I may have fixed it on branch test_autocmd.

lervag commented 10 months ago

No, I don't think so. The FileType autocommand will also have triggered before the .setup() function is called in my particular case.

micangl commented 10 months ago

A simple, but possibly good solution is to add a check if the current file is already a .tex file, and to then start the parser immediately.

I misunderstood this remark. What did you mean, exactly?

lervag commented 10 months ago

I can propose a PR, it might be easier to understand if I show what I mean.

micangl commented 10 months ago

I'd appreciate it!

lervag commented 10 months ago

See #2, specifically this commit.

micangl commented 10 months ago

Wow, great job!

I have a doubt, though: wouldn't the parser be triggered two times if setup wasn't called by lazy.nivm, i.e. inside the init.lua or automatically? Since, in this case, both the autocmd and

if vim.opt_local.filetype:get() == "tex" then
    _start_parser()
end

would launch parser.

lervag commented 10 months ago

Wow, great job!

Thanks :)

I have a doubt, though: wouldn't the parser be triggered two times if setup wasn't called by lazy.nivm, i.e. inside the init.lua or automatically? Since, in this case, both the autocmd and

if vim.opt_local.filetype:get() == "tex" then
    _start_parser()
end

would launch parser.

No, I don't think so - and this is the problem I'm trying to solve. If you are already inside a .tex file in which the filetype option is set to "tex" when you are running the .setup function, then the FileType trigger was already executed. Thus the autocommand does not have an effect on the current buffer, and so we want to trigger the parser manually.

micangl commented 10 months ago

No, I don't think so - and this is the problem I'm trying to solve. If you are already inside a .tex file in which the filetype option is set to "tex" when you are running the .setup function, then the FileType trigger was already executed. Thus the autocommand does not have an effect on the current buffer, and so we want to trigger the parser manually.

I understood this; what I meant was: suppose that we use a regular plugin manager, which loads plugins on startup. When setup is executed, it sets the autocommand and executes _start_parser. Since we are at startup the FileType event is triggered, which triggers the parser, again, with the autocommand.

lervag commented 10 months ago

Ah, but that's where if vim.opt_local.filetype:get() == "tex" comes in. With a regular plugin manager that loads plugins on startup, the filetype will not be "tex".

micangl commented 10 months ago

Very interesting. I'll test and push as soon as I can.