Templatized global search as a typable command #5390
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds two new typable commands: one for global search and the other for a templatized global search using the current doc selection. These are made typable for enabling key remappings to further ease the pain of globally searching with complex regex surrounding the symbols that are currently selected.
Motivation
During visual debugging, whenever the LS is unable to go to definition, especially if the definition lies in a separate file, or if it is in a separate language like with protobuf, I often find myself repeating the same boilerplate regex to get to the definitions of these symbols. This can become painful when forced to repeat this hundreds of times each week. I believe that having a typable global search that can accept a regex template as an argument, which can be filled by the current doc selection will greatly improve the search experience.
For example, today if a Python class cannot be found by the LS I'd have to go to
global_search
mode first and then type something likeclass\s+XYZ
with no way to remap the prefixclass\s+
to a key. While this does work, it is much more convenient if we have a templatized search method in typable command mode that can then be remapped.Use
The templatized global search (
:tgs
) takes in up to two arguments: the first argument is a templatized regex, with empty{}
placeholders like with the rustformat!
macro, the optional second argument is for a delimiter, and on execution, the current document selection is split with the delimiter if it is present, and the results are used as parameters to format the template into a regex expression to search in the current workspace.The global search
:gs
is pretty much the same as theglobal_search
command but it is typable, allowing for key remaps to supply a constant regex argument to use. This does not use the current document selection in any way.Examples
There could be more advanced use cases for more complex pattern matching that requires multiple inputs to be filled in. Right now this implementation uses a delimiter to split the current selection to pass in as arguments to fill the regex template. Say we have a comma-separated list of test class names that we want to search for:
TestABC, TestDEF, TestXYZ
, we can search for all these classes at once with:tgs class\s+\({}|{}|{}\) ,
. Granted, this extended use case appears more esoteric and specialized, but implementing a generalized template model with support for an arbitrary number of arguments made more sense than just having one parameter for just the current selection.By adding something like the following to my config file, I can now search for classes, structs, or protobuf message definitions in a couple of keystrokes:
Caveats
\s
to represent whitespace pattern.?
register for auto-complete and the filled-in regexes are added to the/
register as it does not make sense for the regular search functions to have the incomplete template with{}
placeholders (which is invalid regex anyway) and further clutter the search history. As far as I am aware, the?
register is used for auto-complete only by the template global search command right now.