stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
4k stars 119 forks source link

bug: Highlights not working when opening a file #200

Open BitInByte opened 1 year ago

BitInByte commented 1 year ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

0.9.4

Operating system/version

MacOS Sonoma 14.0

Describe the bug

Hello, first of all, Thank you so much @stevearc, this file explorer was the plugin that I really needed to manage all of my files and directories in a project.

I'm just experiencing a strange bug. Sometimes, when I open a file, the highlights are just broke.

Screenshot 2023-10-18 at 08 49 52

I can't really get a pattern out of here, it's seems to be random on the files that that happens.

I also found out that, sometimes, when I open the file with some other plugin, or even manually with :e for example and the highlights work well, if I open oil and I hit <C-c> to close oil, it breaks the highlights again.

I use treesitter to manage the highlights.

Using neotree or telescope it works and always worked correctly but somehow, oil is not on this aspect and it's maybe the last piece of the cake to use this plugin on 90% of the times.

Gonna also be talking about this plugin on an upcoming YouTube video as well for all of the amazing work you did :D

Steps To Reproduce

  1. Open a file with oil.nvim

Expected Behavior

Screenshot 2023-10-18 at 09 22 16

This is how it shows the file whenever it's opened with either neotree or telescope.

Directory structure

No response

Repro

local lazypath = vim.fn.stdpath("data") .. "./.repro/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

local opts = {
    -- Use this colorscheme for the Lazy popup
    install = {
        colorscheme = { "nightfly" },
    },
    -- Automatic checks for plugin updates and notify
    checker = {
        enabled = true,
        notify = false,
    },
    -- detection of any file change on the modules module
    change_detection = {
        notify = false,
    },
}

require("lazy").setup({
    { "bluz71/vim-nightfly-colors", name = "nightfly", lazy = true, priority = 1000 },
    {
        "nvim-neo-tree/neo-tree.nvim",
        -- version = "v3.x",
        dependencies = {
            "nvim-lua/plenary.nvim",
            "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
            "MunifTanjim/nui.nvim",
        },
        config = function()
            -- Unless you are still migrating, remove the deprecated commands from v1.x
            vim.cmd([[ let g:neo_tree_remove_legacy_commands = 1 ]])
            require("neo-tree").setup({
                close_if_last_window = true,
                -- filesystem = {
                --  follow_current_file = {
                --      enabled = true,
                --      leave_dirs_open = true,
                --  },
                -- },
                -- sources = {
                --  "document_symbols",
                -- },
            })
        end,
    },
    {
        "stevearc/oil.nvim",
        -- opts = {},
        -- Optional dependencies
        dependencies = { "nvim-tree/nvim-web-devicons" },
        config = function()
            local oil = require("oil")
            -- require("oil").setup()
            oil.setup()
            vim.keymap.set("n", "-", "<CMD>Oil<CR>", { desc = "Open parent directory" })
        end,
    },
        {
            -- {
            "nvim-treesitter/nvim-treesitter",
            event = { "BufReadPre", "BufNewFile" },
            dependencies = {
                "nvim-treesitter/nvim-tree-docs",
                "nvim-treesitter/nvim-treesitter-context",
                "windwp/nvim-ts-autotag",
            },
            build = ":TSUpdate",
            config = function()
                require("nvim-treesitter.configs").setup({
                    -- A list of parser names, or "all"
                    ensure_installed = { "lua", "vim", "sql" },
                    -- Install parsers synchronously (only applied to `ensure_installed`)
                    sync_install = false,
                    -- Automatically install missing parsers when entering buffer
                    -- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally
                    auto_install = true,
                    -- List of parsers to ignore installing (for "all")
                    -- ignore_install = { "javascript" },

                    ---- If you need to change the installation directory of the parsers (see -> Advanced Setup)
                    -- parser_install_dir = "/some/path/to/store/parsers", -- Remember to run vim.opt.runtimepath:append("/some/path/to/store/parsers")!

                    highlight = {
                        -- `false` will disable the whole extension
                        enable = true,
                        -- NOTE: these are the names of the parsers and not the filetype. (for example if you want to
                        -- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is
                        -- the name of the parser)
                        -- list of language that will be disabled
                        --disable = { "c", "rust" },
                        -- Or use a function for more flexibility, e.g. to disable slow treesitter highlight for large files
                        --disable = function(lang, buf)
                        --    local max_filesize = 100 * 1024 -- 100 KB
                        --    local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
                        --    if ok and stats and stats.size > max_filesize then
                        --        return true
                        --    end
                        --end,

                        -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
                        -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
                        -- Using this option may slow down your editor, and you may see some duplicate highlights.
                        -- Instead of true it can also be a list of languages
                        additional_vim_regex_highlighting = false,
                    },
                    autotag = {
                        enable = true,
                        enable_close_on_slash = false,
                    },
                    tree_docs = { enable = true, spec_config = { jsdoc = {} } },
                    context_commentstring = {
                        enable = true,
                        enable_autocmd = false,
                    },
                    incremental_selection = {
                        enable = true,
                        keymaps = {
                            init_selection = "gnn", -- set to `false` to disable one of the mappings
                            node_incremental = "grn",
                            scope_incremental = "grc",
                            node_decremental = "grm",
                        },
                    },
                    indent = {
                        enable = true,
                    },
                })

                require("treesitter-context").setup({
                    enable = true, -- Enable this plugin (Can be enabled/disabled later via commands)
                    max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit.
                    min_window_height = 0, -- Minimum editor window height to enable context. Values <= 0 mean no limit.
                    line_numbers = true,
                    multiline_threshold = 20, -- Maximum number of lines to collapse for a single context line
                    trim_scope = "outer", -- Which context lines to discard if `max_lines` is exceeded. Choices: 'inner', 'outer'
                    mode = "cursor", -- Line used to calculate context. Choices: 'cursor', 'topline'
                    -- Separator between context and content. Should be a single character string, like '-'.
                    -- When separator is set, the context will only show up when there are at least 2 lines above cursorline.
                    separator = nil,
                    zindex = 20, -- The Z-index of the context window
                    on_attach = nil, -- (fun(buf: integer): boolean) return false to disable attaching
                })
            end,
            -- },
        },
}, opts)
vim.g.nightflyVirtualTextColor = true
vim.cmd([[colorscheme nightfly]])

Did you check the bug with a clean config?

stevearc commented 1 year ago

Hmmm...I've seen this issue before, but it was fixed a while ago. The problem before was related to when the filetype got set; if it was set during another autocmd then the FileType autocmd wouldn't be processed the same and syntax highlighting wouldn't kick in. The fact that it breaks sometimes when you <C-c> out of oil is extremely weird because at that point we're not even entering a new buffer, we're just switching back to a previously visible buffer.

I tried out the config you provided, but I haven't been able to get the issue to reproduce. Do you have any ideas what specific situations might cause it to happen?

BitInByte commented 1 year ago

Have been testing and debugging as well but it’s a bit strange. It seems to be random files but the files that previously failed the highlight have a big chance to not show the highlight anymore on following neovim sessions. It’s really strange

stevearc commented 1 year ago

It looks like you are using treesitter for syntax highlighting, correct? I've tried looking around for other clues on what could cause the syntax highlighting to be cleared, and I found this thread which looks similar but seems like the issue may have been a timeout with the typescript regex syntax highlighting. Do you see anything in :messages when this happens?

I read up more on treesitter highlighting and I don't see any obvious way for this to happen. If you manage to get a more solid repro let me know, otherwise I don't think there's much I can do.

BitInByte commented 1 year ago

There's nothing on :messages unfortunately. Do you think that this typescript timeout can also be triggering on lua highlighting? Because, the image that I used on the example is lua.

It's so strange this because, I really love this plugin but it get's me really annoyed since I don't have highlighting in there :(

BitInByte commented 1 year ago

Update: Now, in another file that doesn't have the correct highlight, I have found this error which is from Comment.nvim but seems that is caused by oil:

Screenshot 2023-10-19 at 08 43 16
BitInByte commented 1 year ago

Update: Looks like I may have found the reason. Inside of my plugin, at directory, after/plugin/functions.lua:

Screenshot 2023-10-19 at 08 48 27

I have a mkview as you can see:

Screenshot 2023-10-19 at 08 49 55

If I comment out this autocmd it works just fine.

stevearc commented 1 year ago

Interesting. I still don't know how that could cause the highlighting issue, and I still wasn't able to repro the issue. Some things you could try:

You could try to get a minimal repro using those autocmds, winsaveview, winrestview, and buffer switching. If you can get that minimal repro, it's probably something we can file as an issue upstream

Jaxx497 commented 1 year ago

I've run into this issue specifically when working with norg files. A simple :e fixes the problem.

I was curious, so I commented out the ft = "norg" line in my neorg config (using Lazy as my package manager) and now the highlighting works as expected upon opening a file. I'm not smart enough to further explain this, but hopefully it provides some understanding to those who are.

BitInByte commented 1 year ago

For me, the reason was really the event to read and store the folds that I have (views) that was breaking it. I had problems in the past with this thing but in the last year, I haven't any problem. I love to always keep the foldings and cursor position but to use oil.nvim, I can't have this event so I just disabled.

I wanted to investigate further but, haven't had time to do it yet but most probably, if there's an error on an event, since some are silence, oil.nvim just breaks highlight for some reason, that's my opinion as far as I investigated it.

stevearc commented 12 months ago

If anyone can get a repro that would be super helpful, but in the meantime you could also try out the change in #214. That PR is intending to fix behavior on Windows, but it also changes how oil opens files in general so there's a chance that it will have some effect on this bug.

BitInByte commented 12 months ago

@stevearc don't know if will be useful or not but, I was trying to find out why sometimes loadview breaks the highlight and I found this issue on telescope: https://github.com/nvim-telescope/telescope.nvim/issues/559 seems to be a bit similar with what happens with oil but they still have the issue open.

Most of the people just use :e as a quick workaround but seems to be a more complex issue.