azabiong / vim-highlighter

Highlight words and expressions
MIT License
226 stars 9 forks source link

Feature Request: Send highlights to quickfix/loclist #11

Closed Kamholtz closed 2 years ago

Kamholtz commented 2 years ago

I think it would be useful to provide a command that sends all highlights to the quickfix/loclist so that it's possible to see all of the highlights in once place and benefit from cdo and quickfix plugins.

Even better would be providing highlights in the quickfix/loclist too!

azabiong commented 2 years ago

Hi Carl, I think that's a good idea! and thanks for your opinion! 👍 I think it can be useful when quickly finding a highlighted item. Regarding using the quickfix/location window, I have a slightly different idea considering that it's unique resource per session or windows. I want to make it available to users for more important tasks. Instead, I think we can add a vertical narrow tool window, not sure whether it should be a popup. And yes, highlighting list items will be useful to easily find each item.

azabiong commented 2 years ago

Is there specific commands you would like to run if items are listed in the quickfix/location window? Each window can have different highlight items. So, how about listing the items in a popup window and then providing some key functions, for example, p to position the currently selected item, and enter to jump and close popup?

Kamholtz commented 2 years ago

I am rather fond of the cdo command to do something like cdo s/pattern/text/g | update to perform substitutions on each line or cdo norm @q | update to run macros. Plus sending to the quickfix list makes it possible to benefit from quickfix plugins like "kevinhwang91/nvim-bqf" or "romainl/vim-qf" (fuzzy filtering with fzf, open in split or tab). I see it as a way to benefit from some powerful, existing vim functionality and other plugins.

Might it be possible to provide a command that only sends highlights from the current buffer?

azabiong commented 2 years ago

Thank you. It looks powerful. The highlight pattern is somewhat different. There is no position information such as line and column to jump. I think you can try saving and loading some highlight patterns using the command:

 :Hi save ./filename

and then, open it using :edit ./filename or load it directly into the quickfix window, using:

 :caddfile ./filename  |  copen

Since there is no position information, there doesn't seem to be much that can be done with that alone.

Kamholtz commented 2 years ago

Oh I see, perhaps a different approach could be a command that returns an array of items that represent the current highlights for the current context. This could be used by the caller to create a regex that could populate the quickfix.

As an example, if I make a highlight on the word foo and make a highlight on the visual selection bar. When calling the "GetHighlights" function, it could return something like:

[{text: "foo", mode: "normal"}, {text: "bar", mode: "visual"}]

From that, the function caller could produce a command such as:

:vimgrep/\v(<foo>|bar)/gj %

Which would populate the quick fix list with matches for foo with word boundaries and bar.

image

I am not familiar with vimscript and have only had a quick look through function s:SetHighlight(cmd, mode, num) for ideas, so I am not sure if the above vimgrep regex covers all the possible highlights that vim-highlighter creates (for example, when mode is "="), but I would like to hear your thoughts! I figure if you just have a command that returns some structured representation of the highlights it would allow users to support all sorts of search tools which may have different regex patterns.

azabiong commented 2 years ago

I think it's a great idea to bind the highlight list, pass it to the search tool, and finally produce different types of output with position. 👍 And in your example the vimgrep regex above can cover all modes. Then, I think we can provide a function which returns a list of highlight patterns, or a list of dictionaries with a color field. Which list do you prefer? And what about the function name HiList() ?

Kamholtz commented 2 years ago

A list of dictionaries sounds best. I suppose text, color and mode would be everything needed to create a search pattern that can match all possible highlights and possibly colour the matches appropriately (maybe in a telescope picker or something custom). HiList() is a good name

azabiong commented 2 years ago

Okay! then I will proceed. The mode field should be merged into the pattern because the search command doesn't use it. I will prepare HiList() function, and test and update you when it's ready. Thanks

azabiong commented 2 years ago

Now it's ready! Please see :h HiList().  I hope you like it.

Kamholtz commented 2 years ago

This is fantastic, thank you for taking the time to implement it!