Seperated full and substring matches for glob and regex matching #2328
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.
The current implementations of the
searchmethod/filtermethodoptions is slightly inconsistent.textandregexboth require a substring match of a filename, whileglobrequires a full match of the entire filename. (See also https://pkg.go.dev/path/filepath#Match and https://pkg.go.dev/regexp#MatchString) Neither behaviour is documented in lf so far.This PR attempts to improve on this inconsistency by allowing the user to explicitly specify wether they want full or just substring matches. For this I added the two options
glob-fullandregex-full, that require a full filename match. The original optionstext,globandregexnow all require just a partial or substring match.(I haven't implemented a
text-fulloption, as this can at most only ever match a single file and is functionally equivalent to just using theselectcommand with the desired filename.)In my opinion this provides more consistent and expected behaviour, especially when switching the
searchmethod/filtermethodoptions. In particular, any regular string (without special characters, so likefoobar) now provides identical behaviour for thetext,globandregexoptions.And arguably only providing a partial match is more useful and intuitive for the user. If I am searching or filtering files, it's likely that I don't yet know the exact filename I'm looking for, making specifications of full filename matches more cumbersome.
This change technically doesn't add any new features to lf, as glob patterns can easily be made into substring matches, by surrounding them with
*. Similarly, regex patterns can easily be made full matches by using^and$. Both can be easily implemented with push mappings. But I still think the consistent behaviour provided by this PR is worth it's inclusion.Unfortunately neither Go Package provides functions already implementing this behaviour, so I had to rely on changing the patterns as described above. However, Go's implementation of glob patterns is quite simple and does not provide any conditionals (beside arguably character classes), so surrounding the patterns with
*suffices and should never result in any unwanted or unexpected behaviour. Regexs can simply be surrounded by brackets to avoid any issues with conditionals.I should also mention that while
regexandregex-fullare equally powerful (as the user can simply create regexs like^.*PATTER.*$to restore substring matches even withregex-full),globis actually weaker thanglob-fullas there is no way to force a full match after surrounding a pattern with*.As this PR changes the behaviour of the
globoption, this is a breaking change. But as the old behaviour can be easily restored by replacingglobwithglob-full, I doubt this will cause any real trouble.As I mentioned above, I personally find the full filename matches of the options
glob-fullandregex-fulla bit useless. So we might even consider removing them and only changing the behaviour of thegloboption. But this is technically a regressions as thegloboption is weakened through this PR. (See above.) But I don't have any strong opinions here either way.I haven't yet added anything to the documentation, in case anyone would like any further changes.