lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.4k stars 388 forks source link

weird jump movement after vimtex compiled #2661

Closed lee-shun closed 1 year ago

lee-shun commented 1 year ago

Description

Hello!

Every time vimtex finishes compiling, if at that moment I am navigating using "h", there will be a weird movement that takes me to the next line.

NOTE: I tried without noice and notify, the bug disappears.

For example, in the following gif, I am pressing "h" only, but once the "message vimtex compile failed!" there will be a movement takes me to the next line...

Peek 2023-03-04 22-27

Steps to reproduce

I use lazy.nvim.

{
    "lervag/vimtex",
    ft = "tex",
    config = function()
        vim.g.vimtex_view_method = "zathura"
        vim.g.vimtex_quickfix_mode = 0
        vim.g.vimtex_mappings_enabled = 0
        vim.g.vimtex_imaps_enabled = 0
        vim.g.vimtex_text_obj_enabled = 0
        vim.g.vimtex_fold_enabled = 0
        vim.g.tex_conceal = "abdmg"
        vim.g.vimtex_syntax_conceal_disable = 1
        vim.g.vimtex_format_enabled = 0
        vim.g.vimtex_syntax_enabled = 0
    end,
}

noice.nvim

{
    "folke/noice.nvim",
    event = "VeryLazy",
    dependencies = { "MunifTanjim/nui.nvim" },
    opts = {
        lsp = {
            override = {
                ["vim.lsp.util.convert_input_to_markdown_lines"] = true,
                ["vim.lsp.util.stylize_markdown"] = true,
                ["cmp.entry.get_documentation"] = true,
            },
        },
        presets = {
            bottom_search = false,
            command_palette = false,
            long_message_to_split = false,
            inc_rename = true,
            lsp_doc_border = true,
        },
    },
  -- stylua: ignore
  keys = {
    { "<S-Enter>", function() require("noice").redirect(vim.fn.getcmdline()) end, mode = "c", desc = "Redirect Cmdline" },
    { "<leader>nl", function() require("noice").cmd("last") end, desc = "Noice Last Message" },
    { "<leader>nh", function() require("noice").cmd("history") end, desc = "Noice History" },
    { "<leader>na", function() require("noice").cmd("all") end, desc = "Noice All" },
    { "<c-f>", function() if not require("noice.lsp").scroll(4) then return "<c-f>" end end, silent = true, expr = true, desc = "Scroll forward", mode = {"i", "n", "s"} },
    { "<c-b>", function() if not require("noice.lsp").scroll(-4) then return "<c-b>" end end, silent = true, expr = true, desc = "Scroll backward", mode = {"i", "n", "s"}},
  },
    config = function(_, opts)
        require("noice").setup(opts)
        require("which-key").register({
            n = { name = "Noice" },
        }, {
            mode = "n",
            prefix = "<leader>",
            buffer = nil,
            silent = true,
            noremap = true,
            nowait = false,
        })
    end,
}

notify.nvim

{
    "rcarriga/nvim-notify",
    keys = {
        {
            "<leader>nu",
            function()
                require("notify").dismiss({ silent = true, pending = true })
            end,
            desc = "Delete all Notifications",
        },
    },
    opts = {
        timeout = 3000,
        max_height = function()
            return math.floor(vim.o.lines * 0.75)
        end,
        max_width = function()
            return math.floor(vim.o.columns * 0.75)
        end,
    },
    config = function(_, opts)
        require("notify").setup(opts)
        require("telescope").load_extension("notify")

        local wk = require("which-key")
        local telescope_map = {
            f = {
                name = "Find",
                n = { "<cmd>Telescope notify<cr>", "Find notify" },
            },
        }
        local telescope_map_opt = {
            mode = "n",
            prefix = "<leader>",
            buffer = nil,
            silent = true,
            noremap = true,
            nowait = false,
        }
        wk.register(telescope_map, telescope_map_opt)
    end,
}

Expected behavior

No response

Actual behavior

weird movement

Do you use a latexmkrc file?

NO

VimtexInfo

System info:
  OS: Ubuntu 20.04.5 LTS
  Vim version: NVIM v0.9.0-dev
  Has clientserver: true
  Servername: /run/user/1000/nvim.2135.0

VimTeX project: main
  base: main.tex
  root: /home/ls/forest_fire_thesis/CU_thesis
  tex: /home/ls/forest_fire_thesis/CU_thesis/main.tex
  main parser: recursive search
  document class: report
  packages: ConcordiaU algorithm algorithmicx algpseudocode amsbsy amsfonts amsgen amsmath amsopn amstext appendix atbegshi atbegshi-ltx atveryend atveryend-ltx auxhook bigintcalc bitset cite color enumitem epstopdf-base etexcmds float geometry gettitlestring graphics graphicx hycolor hyperref iftex ifthen ifvtex indentfirst infwarerr intcalc keyval kvdefinekeys kvoptions kvsetkeys letltxmacro ltxcmds multirow nameref pdfescape pdftexcmds refcount rerunfilecheck setspace setup/ConcordiaU subfigure times trig uniquecounter url
  source files:
    main.tex
    configs/Constants.tex
    supplements/Abstract.tex
    supplements/Acknowledgements.tex
    chapters/Introduction.tex
    chapters/Related_Works.tex
    chapters/System_Platform.tex
    chapters/Alg_detection.tex
    chapters/Alg_geolocation.tex
    chapters/Alg_reconstruction.tex
    chapters/Experiment.tex
    chapters/Conclusion.tex
    supplements/Appendix_A.tex
  compiler: latexmk
    engine: -pdf
    options:
      -verbose
      -file-line-error
      -synctex=1
      -interaction=nonstopmode
    callback: 1
    continuous: 1
    executable: latexmk
  viewer: Zathura
    xwin id: 0
  qf method: LaTeX logfile
lervag commented 1 year ago

Every time vimtex finishes compiling, if at that moment I am navigating using "h", there will be a weird movement that takes me to the next line.

For example, in the following gif, …

It looks like this is related to nvim-notify.

NOTE: I tried without noice and notify, the bug disappears.

Then it seems this is a bug with either noice or notify. Can you make a minimal example? E.g. something like this:

% test.tex
\documentclass{minimal}
\begin{document}
Hello world
\end{document}
" test.vim
set runtimepath^=~/path/to/vimtex
set runtimepath^=~/path/to/noice.nvim
set runtimepath^=~/path/to/nui.nvim
set runtimepath^=~/path/to/nvim-notify
set runtimepath+=~/path/to/vimtex/after
filetype plugin indent on
syntax enable

lua <<EOF

vim.g.vimtex_view_method = "zathura"
vim.g.vimtex_quickfix_mode = 0
vim.g.vimtex_mappings_enabled = 0
vim.g.vimtex_imaps_enabled = 0
vim.g.vimtex_text_obj_enabled = 0
vim.g.vimtex_fold_enabled = 0
vim.g.vimtex_syntax_conceal_disable = 1
vim.g.vimtex_format_enabled = 0
vim.g.vimtex_syntax_enabled = 0

-- options for noice.nvim, etc, goes here

EOF

Now run nvim --clean -u test.vim test.tex in some test folder and try and reproduce the error.

By the way, regarding your configuration:

lervag commented 1 year ago

It is probably related to both nvim-notify and noice. If you can make a minimal example like I've tried to help you start with, then it will be much easier to debug it. So I suggest you start there.

lee-shun commented 1 year ago

Thank you for your reply! Yes, because when I disable the noice and notify, this bug disappers. I will make a minimal example soon.

lee-shun commented 1 year ago

Hmmmm, interestingly I use the minimal "test.tex" with my current vim config, but this bug seems to have disappeared...

lee-shun commented 1 year ago

like this: Peek 2023-03-05 16-09

lee-shun commented 1 year ago

Hello! After hundreds of tries, I found that the "callback" option matters. After I put the following setting in my config, this bug disappeared.

let g:vimtex_compiler_latexmk = {
        \ 'build_dir' : '',
        \ 'callback' : 0,  " just disable this one
        \ 'continuous' : 1,
        \ 'executable' : 'latexmk',
        \ 'hooks' : [],
        \ 'options' : [
        \   '-verbose',
        \   '-file-line-error',
        \   '-synctex=1',
        \   '-interaction=nonstopmode',
        \ ],
        \}

But would you be so kind to explain to me what exactly the callback does? And why it will generate this weird movement with noice.nvim? @lervag

lervag commented 1 year ago

Yes: the callback option (which you should probably keep enabled!) allows the latexmk process that VimTeX starts that is responsible for compiling your project to communicate the status ("success", "failed", "new compilation triggered") to VimTeX. This in turn allows for the notifications you see, e.g. "VimTeX: compilation completed" or "VimTeX: compilation failed".

I don't believe there is an error or bug here, and I'm quite convinced that the problem is related to the combination of noice and notify. For debugging this, I would do this:

  1. First create a minimal LaTeX file on which you can reproduce the problem. Start with the one in your original post and simplify it until you can't make it smaller without removing the problem.
  2. Now, similarly, create a minimal .vimrc, init.vim or init.lua file with which you can reproduce the problem (similar to how I explained earlier).

If you are able to provide both a minimal LaTeX file, a minimal configuration, and a simple set of instructions with which I or anyone else can use to reproduce this problem, then the chance of it being fixed is MUCH higher!

lee-shun commented 1 year ago

@lervag Thank you for your answer. I just got time to reproduce the issue with the following minimal config:

set termguicolors
call plug#begin('~/.config/nvim/plugged')
Plug 'folke/noice.nvim'
Plug 'MunifTanjim/nui.nvim'
Plug 'rcarriga/nvim-notify'

Plug 'Pocco81/auto-save.nvim'
Plug 'lervag/vimtex'
call plug#end()

lua << EOF
 -- notice and notify
require("notify").setup(
{
    timeout = 3000,
    max_height = function()
    return math.floor(vim.o.lines * 0.75)
    end,
    max_width = function()
    return math.floor(vim.o.columns * 0.75)
    end,
})
require("noice").setup({
lsp = {
    -- override markdown rendering so that **cmp** and other plugins use **Treesitter**
    override = {
        ["vim.lsp.util.convert_input_to_markdown_lines"] = true,
        ["vim.lsp.util.stylize_markdown"] = true,
        ["cmp.entry.get_documentation"] = true,
        },
    },
    -- you can enable a preset for easier configuration
    presets = {
        bottom_search = false, -- use a classic bottom cmdline for search
        command_palette = false, -- position the cmdline and popupmenu together
        long_message_to_split = true, -- long messages will be sent to a split
        inc_rename = false, -- enables an input dialog for inc-rename.nvim
        lsp_doc_border = false, -- add a border to hover docs and signature help
        },
})

require("auto-save").setup {}

-- for vimtex

vim.g.vimtex_view_method = "zathura"
vim.g.vimtex_quickfix_mode = 0
vim.g.vimtex_mappings_enabled = 0
vim.g.vimtex_imaps_enabled = 0
vim.g.vimtex_text_obj_enabled = 0
vim.g.vimtex_fold_enabled = 0
vim.g.tex_conceal = "abdmg"
vim.g.vimtex_syntax_conceal_disable = 1
vim.g.vimtex_format_enabled = 0
vim.g.vimtex_syntax_enabled = 0

EOF

Compile the following template with the "VimtexCompile" command, and at the same time, move your cursor left or right continuously; after the notify message, you will find your cursor jumping to the next line! Concordia University Thesis Template_debug.zip

lervag commented 1 year ago

I can reproduce this with a much more minimal setup. The steps:

  1. Prepare:

    mkdir test
    cd test
    git clone --depth=1 https://github.com/lervag/vimtex
    git clone --depth=1 https://github.com/folke/noice.nvim
    git clone --depth=1 https://github.com/MunifTanjim/nui.nvim
  2. Create minimal init.vim with following contents:

    " init.vim
    set runtimepath^=vimtex
    set runtimepath^=nui.nvim
    set runtimepath^=noice.nvim
    filetype plugin on
    
    let g:vimtex_quickfix_mode = 0
    
    lua require("noice").setup {}
    
    silent edit test.tex
    
    silent! VimtexClean
    normal! 8G
    sleep 250m
    VimtexCompile
  3. Create minimal test.tex

    \documentclass{article}
    \usepackage[utf8]{inputenc}
    \usepackage{graphicx}
    
    \begin{document}
    
    Hello \xxx world
    
    \end{document}
  4. Now do nvim --clean -u init.vim

  5. When the file has loaded, press and hold h

  6. Observe that the cursor moves to the next line when the VimTeX callback is finished.

The line of code in VimTeX that is called when this thing happens is here:

https://github.com/lervag/vimtex/blob/4c942d475bbc4eecb319c0c45c81a09ab05ca22d/autoload/vimtex/qf/u.vim#L14

The stack trace:

FILE                                          LINE  FUNCTION CALL
vimtex/autoload/vimtex/qf/u.vim               15
vimtex/autoload/vimtex/qf/latexlog.vim              call vimtex#qf#u#caddfile(self, fnameescape(a:log))
vimtex/autoload/vimtex/qf.vim                 127   call b:vimtex.qf.addqflist(l:tex, l:log)
vimtex/autoload/vimtex/qf.vim                 46    call vimtex#qf#setqflist()
vimtex/autoload/vimtex/compiler.vim           95    call vimtex#qf#open(0)
vimtex/autoload/vimtex/compiler/_template.vim 500   call vimtex#compiler#callback(l:status)
vimtex/autoload/vimtex/compiler/_template.vim 439   call s:check_callback()

Brief description of this stack trace: The call stack starts from a callback from a jobstart that is started here:

https://github.com/lervag/vimtex/blob/4c942d475bbc4eecb319c0c45c81a09ab05ca22d/autoload/vimtex/compiler/_template.vim#L392

This callback is used to check for the status of latexmk and to act accordingly. An error is reported, so VimTeX starts to parse the log file to build the quickfix entries.

As far as I can understand, there is no bug on the VimTeX side. I have no idea why noautocmd caddfile ... would cause any such problems. I'm hoping that the minimal example can be helpful for others that may be able to help. I strongly believe that, if there is a bug, it is either 1) with noice.nvim or nui.nvim or 2) with neovim itself.

I tried to simplify this entire business further to avoid VimTeX as a dependency at all, but that was hard.

lervag commented 1 year ago

Notice, though, that this only happens when there is an error in the latex log. Also, notice that it only happens "sometimes"; if I try to reproduce three times, it may only be reproduced two of the three times.

lervag commented 1 year ago

Perhaps @folke would be able to help here with his expertice on both lua and noice.nvim?

folke commented 1 year ago

I also have no idea what could cause. This is the only issue that reports this kind of behavior. Since I'm not a vimtex user, and the issue is not really breaking, I'm not planning to spend time investigating this further.

If someone would figure it out and it turns out to be a noice issue, then feel free to create a PR

lervag commented 1 year ago

I also have no idea what could cause. This is the only issue that reports this kind of behavior. Since I'm not a vimtex user, and the issue is not really breaking, I'm not planning to spend time investigating this further.

That's fair. And it's the same for me; I've tried to figure it out, but I failed.

If someone would figure it out and it turns out to be a noice issue, then feel free to create a PR

Same for me; if someone should understand something I don't that explains why this is a VimTeX issue, then I would be happy to hear it and/or see a PR.

lervag commented 1 year ago

I'm closing this with "won't fix", as I don't plan to spend more time on it.

lee-shun commented 1 year ago

Thank you!

lervag commented 1 year ago

Sorry that I couldn't be of more help. :\

lee-shun commented 1 year ago

Sorry that I couldn't be of more help. :\

No worry! Thank you and @folke for creating these great plugins! Nothing is perfect, Cheers! :+1:

lervag commented 1 year ago

Thanks! :) <3

a-torgovitsky commented 1 week ago

@lee-shun Did you ever find a workaround for this problem?