Skip to content

Commit

Permalink
Consistent Tags/BTags with query
Browse files Browse the repository at this point in the history
The `BTags` command is declared with optional `[QUERY]` while the `Tags`
command is declared with optional `[PREFIX]`.  The query behavior in
`BTags` source generator is less restrictive compared to `Tags` whose
source is generated using the `readtags` external command, where tags
are only listed if `PREFIX` is an exact match.  This commit relaxes the
exact prefix match when `readtags` is used to allow for a fuzzy prefix
similar to the source generator of `BTags`.  The `fzf` fuzzy score
ranking in the `s:tags_sink` will still rank the best fuzzy match on
top, same as if the match was exact like in the current implementation.

The changes in this commit are useful for tags that contain some scope
prefixes like the functions `s:tags_sink` or `fzf#vim#tags` in
vim-script code, in combination with a key mapping of the form

```vim
nnoremap <silent> <leader>l :execute "Tags '" . expand('<cword>')<CR>
nnoremap <silent> <leader>bl :execute "BTags '" . expand('<cword>')<CR>
```

If the cursor is placed on the word "tags" in this example
`fzf#vim#tags`, the relaxed `Tags` query call will list the tags entry
if `<cword>` expands to "tags", while the current implementation will
not.  Inconsistently, the `BTags` call will list the tags entry with its
current implementation.
  • Loading branch information
Fabian Wermelinger committed Sep 28, 2024
1 parent c5ce790 commit 84c9597
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ so you can omit it if you use a plugin manager that doesn't support hooks.
- `Ag` requires [The Silver Searcher (ag)][ag]
- `Rg` requires [ripgrep (rg)][rg]
- `Tags` and `Helptags` require Perl
- `Tags PREFIX` requires `readtags` command from [Universal Ctags](https://ctags.io/)
- `Tags QUERY` requires `readtags` command from [Universal Ctags](https://ctags.io/)

```sh
# Installing dependencies using Homebrew
Expand All @@ -93,7 +93,7 @@ Commands
| `:RG [PATTERN]` | [rg][rg] search result; relaunch ripgrep on every keystroke |
| `:Lines [QUERY]` | Lines in loaded buffers |
| `:BLines [QUERY]` | Lines in the current buffer |
| `:Tags [PREFIX]` | Tags in the project (`ctags -R`) |
| `:Tags [QUERY]` | Tags in the project (`ctags -R`) |
| `:BTags [QUERY]` | Tags in the current buffer |
| `:Changes` | Changelist across all open buffers |
| `:Marks` | Marks |
Expand Down
2 changes: 1 addition & 1 deletion autoload/fzf/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ function! fzf#vim#tags(query, ...)
return s:warn('Tags command requires perl')
endif
if len(a:query) && !executable('readtags')
return s:warn('readtags from universal-ctags is required to pre-filter tags with a prefix')
return s:warn('readtags from universal-ctags is required to pre-filter tags with a query')
endif

if empty(tagfiles())
Expand Down
6 changes: 3 additions & 3 deletions bin/tags.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

use strict;

my $prefix = shift @ARGV;
my $query = shift @ARGV;

foreach my $file (@ARGV) {
my $lines;
if ($prefix eq "") {
if ($query eq "") {
open $lines, $file;
} else {
# https://perldoc.perl.org/perlopentut#Expressing-the-command-as-a-list
open $lines, '-|', 'readtags', '-t', $file, '-e', '-p', '-', $prefix;
open $lines, '-|', 'readtags', '-t', $file, '-e', '-p', '-Q', "(#/^[^[:space:]]*$query\$/ \$name)", '-l';
}
while (<$lines>) {
unless (/^\!/) {
Expand Down

0 comments on commit 84c9597

Please sign in to comment.