axieax / urlview.nvim

🔎 Neovim plugin for viewing all the URLs in a buffer
MIT License
232 stars 5 forks source link

[Feature Request] Add highlights for Urls #51

Closed utkarshgupta137 closed 1 year ago

utkarshgupta137 commented 1 year ago

It would be nice if urlview.nvim could add highlights for the URLs if it is possible. I would like to have URLs underlined (& maybe have blue fg) so that they're readily visible.

axieax commented 1 year ago

Hi, this is most likely out of scope for this plugin, since writing a URL parser would most likely be inefficient (especially given the current Lua string parsing design) unless nvim-treesitter is used (only checking URLs in strings or comments). I don't have much experience with writing treesitter queries, but please let me know if you have any thoughts on this :))

utkarshgupta137 commented 1 year ago

I came across a plugin that uses a cursed regex to do this, but I don't want to use that for performance reasons. I've not looked into the code of this plugin, but are the URLs of buffer pre-computer, or does it search for the next URL when the command is called? I was imagining that we could scan the buffer for URLs and then add highlights? I don't think it is in scope for treesitter, because it just parses comments as comments, and doesn't look into them.

axieax commented 1 year ago

Oh yes that regex parser indeed looks very scary. This plugin finds URL buffers when the user calls the command. Applying highlights to the entire buffer each time doesn't seem like a very efficient way of doing so, which is why I was considering using treesitter to limit the scope and only change affected nodes.

I don't think it is in scope for treesitter, because it just parses comments as comments, and doesn't look into them.

I thought it was possible to get the node content using ts query, especially using @spell defined by highlights.scm

axieax commented 1 year ago

Update: I've managed to apply highlights to URLs in comments and strings using the @spell annotation in #52. Will need to have a look later at attaching the parser to a buffer and using LanguageTree:register_cbs with on_changedtree to update changes more efficiently, like folke/todo-comments.nvim or p00f/nvim-ts-rainbow.

image

utkarshgupta137 commented 1 year ago

Amazing. I'll start testing this then. Also, you might want to refer to the fork of nvim-ts-rainbow: https://github.com/HiPhish/nvim-ts-rainbow2.

utkarshgupta137 commented 1 year ago

This is working perfectly for me:

return {
  "axieax/urlview.nvim",
  branch = "url-parser",
  init = function()
    vim.api.nvim_create_autocmd("BufReadPost", {
      desc = "HighlightUrls",
      callback = function() require("urlview.parser").parse() end,
    })
  end,
  event = "BufReadPost",
  opts = {},
}

Edit: Notice that I'm using BufReadPost so that the file is only parsed once - thus alleviating any performance concerns (at least for me).

axieax commented 1 year ago

Glad to hear that it's applying the right highlights! However, the main issue with only parsing once is that changes to nodes don't reapply the highlights, causing incorrect highlighting, as can be seen in the clip below:

https://github.com/axieax/urlview.nvim/assets/62098008/e8ed5606-da26-44f3-ab05-cfd532bd6575

utkarshgupta137 commented 1 year ago

Not able to see the view due to "No video with supported format and MIME type found". Try sharing it on asciinema or as a GIF? Also, I understand what you're saying. If I add new URLs, then they won't get highlighted which is fine. I just want existing URLs in the file to be visible, I'm unlikely to add any myself. AFAI can tell, existing highlights don't get messed up by edits. You could also add a parser_event option which will be used, which can also be set to nil to disable the parser.

axieax commented 1 year ago

AFAI can tell, existing highlights don't get messed up by edits

The video above demonstrates this exact behaviour :/ Hopefully a gif works this time:

urlview parser

utkarshgupta137 commented 1 year ago

Yeah, edge edits were an issue. But as I said, I rarely add/modify URLs, so I'm fine with the tradeoff. With a config option, the user can choose something like BufWritePost.

axieax commented 1 year ago

Cool, in that case, please continue using this branch for this feature. I'll merge the PR when the updating functionality is completed.

axieax commented 1 year ago

Seems like the treesitter parser for comments have recently added native support for highlighting uris as well. Would this be enough for your use case? If so, I will close the PR.

utkarshgupta137 commented 1 year ago

Seems like the treesitter parser for comments have recently added native support for highlighting uris as well. Would this be enough for your use case? If so, I will close the PR.

I'm not sure how to enable that.

axieax commented 1 year ago

Can you try running :TSInstall comment or :TSUpdate comment? You need the comment treesitter parser which automatically highlights all uris.

utkarshgupta137 commented 1 year ago

Can you try running :TSInstall comment or :TSUpdate comment? You need the comment treesitter parser which automatically highlights all uris.

Works. Thanks!