lucc / nvimpager

Use nvim as a pager to view manpages, diffs, etc with nvim's syntax highlighting
Other
361 stars 19 forks source link

Treesitter support #63

Open fhill2 opened 2 years ago

fhill2 commented 2 years ago

Can nvimpager show treesitter syntax highlighting?

From my tests below, nvimpager.lua isn't parsing treesitter syntax highlighting.

Here is what I am using to test:

mkdir -p ~/.local/share/nvimpager/site/pack/packer/opt
ln -s ~/.local/share/nvim/site/pack/packer/opt/nvim-treesitter ~/.local/share/nvimpager/site/pack/packer/opt/nvim-treesitter

~/.config/nvimpager/init.vim

packadd nvim-treesitter

lua << EOF
require'nvim-treesitter.configs'.setup {
ensure_installed = {
  "lua"
  },
highlight = {
enable = true
},
}
EOF

using nvimpager on a lua file to see if treesitter works:

nvimpager ~/lua.lua # shows nvim syntax highlighting
nvimpager -c ~/lua.lua # shows nvim syntax highlighting
nvimpager -p ~/lua.lua # shows treesitter highlighted text
lucc commented 2 years ago

Sorry, somehow I missed this issue. I just checked and I can reproduce it.

MatthiasGrandl commented 1 year ago

Can also confirm. Also tabstop settings are ignored in cat mode, but work fine in pager mode.

lucc commented 9 months ago

Today I found out that synID() which was used in cat mode does not see all highlights added to a position. I will have to refactor cat mode to use vim.inspect_pos()

Ref https://vi.stackexchange.com/a/41586/18062

night0721 commented 3 months ago

Would this be fixed any time soon?

lucc commented 3 months ago

You are welcome to work on it. I currently do not have the time to fix it / I can not give an ETA.

As noted above it would be necessary to refactor the code to use vim.inspect_pos() instead of synID(). I previously had a short look at the output of that function and it seems it yields all different extmarks and treesitter tokens like this:

{
  buffer = 14,
  col = 3,
  extmarks = { {
      col = 2,
      end_col = 10,
      end_row = 112,
      id = 25,
      ns = "illuminate.highlight",
      ns_id = 20,
      opts = {
        end_col = 10,
        end_right_gravity = false,
        end_row = 112,
        hl_eol = false,
        hl_group = "IlluminatedWordText",
        hl_group_link = "IlluminatedWordText",
        ns_id = 20,
        priority = 199,
        right_gravity = true
      },
      row = 112
    } },
  row = 112,
  semantic_tokens = {},
  syntax = {},
  treesitter = { {
      capture = "string",
      hl_group = "@string.yaml",
      hl_group_link = "Constant",
      lang = "yaml",
      metadata = {}
    }, {
      capture = "property",
      hl_group = "@property.yaml",
      hl_group_link = "Identifier",
      lang = "yaml",
      metadata = {}
    } }
}

The main question I had (and did not have the time to figure out) was in which order these different highlight groups should be stacked/merged to produce the final display attributes.

night0721 commented 3 months ago

Thanks for reply, I would have a look and learn lua first.

Night

On Wed, 20 Mar 2024 at 14:18, Lucas Hoffmann @.***> wrote:

You are welcome to work on it. I currently do not have the time to fix it / I can not give an ETA.

As noted above it would be necessary to refactor the code to use vim.inspect_pos() instead of synID(). I previously had a short look at the output of that function and it seems it yields all different extmarks and treesitter tokens like this:

{ buffer = 14, col = 3, extmarks = { { col = 2, end_col = 10, end_row = 112, id = 25, ns = "illuminate.highlight", ns_id = 20, opts = { end_col = 10, end_right_gravity = false, end_row = 112, hl_eol = false, hl_group = "IlluminatedWordText", hl_group_link = "IlluminatedWordText", ns_id = 20, priority = 199, right_gravity = true }, row = 112 } }, row = 112, semantic_tokens = {}, syntax = {}, treesitter = { { capture = "string", hl_group = @.", hl_group_link = "Constant", lang = "yaml", metadata = {} }, { capture = "property", hl_group = @.", hl_group_link = "Identifier", lang = "yaml", metadata = {} } } }

The main question I had (and did not have the time to figure out) was in which order these different highlight groups should be stacked/merged to produce the final display attributes.

— Reply to this email directly, view it on GitHub https://github.com/lucc/nvimpager/issues/63#issuecomment-2009684985, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASPPZ4JQVJ2XVSGXHGWRAZTYZGLEVAVCNFSM5Q25PRWKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBQHE3DQNBZHA2Q . You are receiving this because you commented.Message ID: @.***>

adeg commented 3 months ago

Hey folks — just to let to whoever is interested to know — I have managed to add treesitter syntax highlighting support. The only piece that's missing is a way to determine if the treesitter resolver function that I wrote should be used instead of built-in synID, so that compatibility with regular syntax highlighting is preserved. As soon as this is done followed by some testing — I will submit a pull request.