neorg icon indicating copy to clipboard operation
neorg copied to clipboard

Neorg highlighting only works in specific situations when opening .norg files.

Open linguini1 opened this issue 1 year ago • 5 comments

Prerequisites

  • [X] I am using the latest stable release of Neovim
  • [X] I am using the latest version of the plugin

Neovim Version

NVIM v0.10.0-dev-1031+gc431d820e

Neorg setup

return {
    "nvim-neorg/neorg",
    enabled = not vim.g.started_by_firenvim,
    build = ":Neorg sync-parsers",
    ft = "norg",
    cmd = "Neorg",
    priority = 30,
    dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-treesitter/nvim-treesitter",
        { "nvim-neorg/neorg-telescope", dependencies = { "nvim-telescope/telescope.nvim" } },
        "folke/zen-mode.nvim",
        "jbyuki/nabla.nvim",
    },
    config = function()
        require("neorg").setup({
            load = {
                ["core.defaults"] = {},
                ["core.qol.toc"] = { config = { close_after_use = true } },
                ["core.completion"] = { config = { engine = "nvim-cmp" } },
                ["core.summary"] = {},
                ["core.concealer"] = { config = { icons = { code_block = { conceal = true, width = 120 } } } },
                ["core.export"] = {},
                ["core.looking-glass"] = {},
                ["core.presenter"] = { config = { zen_mode = "zen-mode" } },
                ["core.integrations.telescope"] = {},
                ["core.integrations.treesitter"] = {},
                ["core.ui"] = {},
                ["core.tempus"] = {},
                ["core.ui.calendar"] = {},
                ["core.export.markdown"] = {},
                ["core.journal"] = {
                    config = {
                        journal_folder = "journo",
                    },
                },
                ["core.dirman"] = {
                    config = {
                        workspaces = {
                            junk = "~/notes/junk",
                            school = "~/notes/school",
                            projects = "~/notes/projects",
                            cuinspace = "~/notes/cuinspace",
                        },
                        default_workspace = "junk",
                    },
                },
                ["core.keybinds"] = {
                    config = {
                        hook = function(keybinds)
                            keybinds.remap_event("norg", "n", "<LocalLeader>z", "core.looking-glass.magnify-code-block")
                            keybinds.map("norg", "n", "<LocalLeader>p", "<Cmd>Neorg presenter start<CR>")
                            keybinds.map("norg", "n", "<LocalLeader>v", require("nabla").popup)
                            keybinds.map("norg", "n", "<leader>fnh", "<Cmd>Telescope neorg search_headers<CR>")
                            keybinds.map("norg", "n", "<leader>fnl", "<Cmd>Telescope neorg find_linkable<CR>")
                            keybinds.map("norg", "n", "<LocalLeader>il", "<Cmd>Telescope neorg insert_file_link<CR>")
                        end,
                    },
                },
            },
        })

        vim.api.nvim_create_autocmd("FileType", {
            pattern = "norg",
            callback = function() vim.opt_local.conceallevel = 3 end,
            desc = "Set conceal level to 3 on Neorg files so that links are properly concealed.",
        })
    end,
}

Actual behavior

When I open a .norg file from my file-tree or using the :Neorg command, syntax highlighting is added as expected. However, when I open a .norg file using Telescope, although Neorg is then loaded, syntax highlighting does not appear as expected. If I then try to open a .norg file in any other method, syntax highlighting remains broken.

https://github.com/nvim-neorg/neorg/assets/62850091/29cc1e87-2131-46ab-aa4d-2618868c20dc

Expected behavior

Since lazy.nvim can load Neorg on filetypes of norg, I'd expect since the Telescope buffer preview shares that filetype, it would load the parser in time to see the preview. I would also expect that since the norg and norg_meta parsers are already installed, treesitter parsing should work without Neorg having to first be loaded. Finally, I would also assume that once Neorg has been loaded (by opening a .norg file from Telescope's find_files picker), treesitter parsing would then be loaded and work for subsequently opened files.

Steps to reproduce

See the video in "Actual behaviour".

You will need to have the same lazy-loaded Neorg setup as shown above.

  1. Use Telescope find_files to open the UI with a .norg file in the display/previewer
  2. Hit <CR> to select that buffer and open it
  3. Observe the lack of highlighting
  4. Continue to open other .norg files using any method
  5. Continue to observe the lack of syntax highlighting

Alternatively, to see situations where the syntax highlighting works:

  1. Open Neovim and before using Telescope find_files:
  • Open a .norg file using :e or
  • Open a .norg file using neo-tree or
  • Open a .norg file using :Neorg workspace ...
  1. Now use Telescope find_files, and observe that syntax highlighting works as expected everywhere

Potentially conflicting plugins

I think this is caused by filtype not being properly set when Telescope opens a buffer in the preview window.

I also think it's plausible that this is simply caused by the way I am lazy-loading Neorg on ft=norg, but that does seem to be supported in the docs. I wish there was a way for the tree-sitter parsing to work in the preview window of telescope without the hacky autocommand workaround.

Other information

  • Syntax highlighting also fails if I run :checkhealth nvim-treesitter before loading Neorg.
  • I have tried :TSEnable highlights neorg when syntax highlighting fails with no luck
  • The norg parser shows up as having no capabilities until Neorg is loaded.
  • norg_meta also doesn't show up in :checkhealth nvim-treesitter until Neorg is loaded.

Checkhealth before Neorg is loaded

before_neorg_loaded

Checkhealth after Neorg is loaded

after_neorg_loaded

Help

Yes, but I don't know how to start. I would need guidance (check question below)

Implementation help

I had a look at the codebase to see if I could find somewhere where the norg treesitter parser is enabled, since I assume it's only being enabled when Neorg is loaded or something similar. I couldn't find anywhere where that happens, and I am not sure where else to look to try to solve this issue.

linguini1 avatar Sep 10 '23 23:09 linguini1

I haven't read through your whole description yet I just saw that it's about neorg not being loaded when previewing files in telescope This might be because iirc we use a BufEnter *.norg autocommand or something like that which propably isn't called when previewing files with telescope I'm not sure how recent your neovim version is but couldn't this be fixed with the filetype detection for norg now being in core?

max397574 avatar Sep 11 '23 05:09 max397574

I haven't read through your whole description yet I just saw that it's about neorg not being loaded when previewing files in telescope This might be because iirc we use a BufEnter *.norg autocommand or something like that which propably isn't called when previewing files with telescope I'm not sure how recent your neovim version is but couldn't this be fixed with the filetype detection for norg now being in core?

That's correct. I'm skeptical it's just an issue with the auto command though because even once the buffer is opened, highlighting still breaks. It's also broken if I :checkhealth nvim-treesitter before opening a .norg file.

My Neovim version is NVIM v0.10.0-dev-1031+gc431d820e, so I'm not sure if the filetype issue you mentioned applies.

linguini1 avatar Sep 11 '23 12:09 linguini1

well perhaps just try updating to latest nightly and see if the issue is still there

max397574 avatar Sep 11 '23 13:09 max397574

well perhaps just try updating to latest nightly and see if the issue is still there

I am using the latest nightly

linguini1 avatar Sep 11 '23 13:09 linguini1

then it should have the filetype thing so that's also not fixing it https://github.com/neovim/neovim/pull/24948

max397574 avatar Sep 11 '23 13:09 max397574