├── .gitignore ├── README.md ├── autoload └── tagquery.vim ├── install.sh └── plugin └── tagquery.vim /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-tagquery 2 | 3 | A vim plugin that enables improved querying of tags. 4 | 5 | Currently, this depends on [fzf.vim](https://github.com/junegunn/fzf.vim), but there are plans for a command that does not require it. 6 | 7 | ## Usage 8 | 9 | This finds all locations with the tags `foo` and `bar`, but not `bazz`. It will put the results in a filterable `fzf` buffer with a preview of the file: 10 | 11 | ``` 12 | :FzfTagQuery foo & bar & !bazz 13 | ``` 14 | 15 | An example binding: 16 | 17 | ```viml 18 | noremap :FzfTagQuery 19 | ``` 20 | 21 | ## Installation 22 | 23 | Using [`vim-plug`](https://github.com/junegunn/vim-plug): 24 | 25 | ```vim 26 | Plug 'matt-snider/vim-tagquery', { 'do': 'bash install.sh' } 27 | ``` 28 | 29 | ## Configuration 30 | 31 | Path to the vimwiki ctags file: 32 | 33 | `let g:tagquery_ctags_file = '~/vimwiki/.vimwiki_tags'` 34 | -------------------------------------------------------------------------------- /autoload/tagquery.vim: -------------------------------------------------------------------------------- 1 | " ============================================================================ 2 | " vim-tagquery - A vim plugin that enables improved querying of tags 3 | " 4 | " Maintainer: Matt Snider 5 | " ============================================================================ 6 | let s:root = expand(':p:h:h') 7 | let s:ctags_binary = 'ctags-query' 8 | 9 | if exists('g:tagquery_ctags_file') 10 | let s:local_ctags_file = g:tagquery_ctags_file 11 | else 12 | let s:local_ctags_file = getcwd() . '/.tags' 13 | endif 14 | 15 | 16 | " Returns the path to the `ctags-query` binary 17 | function! tagquery#binary_path() abort 18 | return s:root . '/bin/ctags-query' 19 | endfunction 20 | 21 | 22 | " Executes the query on the given file and returns 23 | " a list of lines output by `ctags-query`. 24 | function! tagquery#execute_cli(query, ctags_path) abort 25 | let cmd = tagquery#binary_path() . ' ' 26 | \ . '-f ' . a:ctags_path . ' ' 27 | \ . shellescape(a:query) 28 | let result = system(cmd) 29 | return split(result, '\n') 30 | endfunction 31 | 32 | 33 | " Filter results with FZF given specific options 34 | function! tagquery#fzf(query) 35 | let source = tagquery#execute_cli(a:query, s:local_ctags_file) 36 | call map(source, { _, val -> substitute(val, '\t', ':', 'g') }) 37 | 38 | call fzf#run(fzf#vim#with_preview({ 39 | \ 'source': source, 40 | \ 'options': '--delimiter : --with-nth="3.."', 41 | \ 'sink': function('s:fzf_sink'), 42 | \ 'down': 10, }), 43 | \ 'right:50%') 44 | endfunction 45 | 46 | 47 | " Handles opening a selected result 48 | function! s:fzf_sink(result) 49 | let parts = split(a:result, ':') 50 | let filepath = parts[0] 51 | let location = '+' . parts[1] 52 | execute 'silent' 'edit' location filepath 53 | endfunction 54 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | # 3 | # Download the `ctags-query` binary, falling back to building with `cargo` if that fails. 4 | # 5 | # Script modified from autozimu/LanguageClient-neovim 6 | # https://github.com/autozimu/LanguageClient-neovim/blob/next/LICENSE.txt 7 | # 8 | # Copyright (c) 2017 Junfeng Li and contributors. 9 | 10 | set -o nounset # error when referencing undefined variable 11 | set -o errexit # exit when command fails 12 | 13 | name=ctags-query 14 | url=https://github.com/matt-snider/$name 15 | version=0.1.1 16 | 17 | try_curl() { 18 | command -v curl > /dev/null && \ 19 | curl --fail --location "$1" --output bin/$name 20 | } 21 | 22 | try_wget() { 23 | command -v wget > /dev/null && \ 24 | wget --output-document=bin/$name "$1" 25 | } 26 | 27 | download() { 28 | echo "Downloading binary ${name}..." 29 | mkdir -p bin/ 30 | 31 | url=$url/releases/download/$version/${1} 32 | if (try_curl "$url" || try_wget "$url"); then 33 | chmod a+x bin/$name 34 | return 35 | else 36 | try_build || echo "Prebuilt binary not found, please try again shortly or report the issue\n\nhttps://github.com/matt-snider/vim-tagquery/issues" 37 | fi 38 | } 39 | 40 | try_build() { 41 | if command -v cargo > /dev/null; then 42 | echo "Building locally..." 43 | 44 | # Get/update repo 45 | cd $name/ \ 46 | || git clone $url \ 47 | && cd $name/ 48 | git pull 49 | cargo build --release 50 | 51 | # Create bin 52 | cd .. 53 | mkdir bin/ 54 | cp $name/target/release/$name bin/ 55 | chmod a+x bin/ctags-query 56 | else 57 | return 1 58 | fi 59 | } 60 | 61 | rm -f bin/$name 62 | 63 | arch=$(uname -sm) 64 | case "${arch}" in 65 | "Linux x86_64") download $name-$version-x86_64-unknown-linux-gnu ;; 66 | # "Linux i686") download $name-$version-i686-unknown-linux-musl ;; 67 | # "Linux aarch64") download $name-$version-aarch64-unknown-linux-gnu ;; 68 | # "Darwin x86_64") download $name-$version-x86_64-apple-darwin ;; 69 | # "FreeBSD amd64") download $name-$version-x86_64-unknown-freebsd ;; 70 | *) echo "No pre-built binary available for ${arch}."; try_build ;; 71 | esac 72 | 73 | -------------------------------------------------------------------------------- /plugin/tagquery.vim: -------------------------------------------------------------------------------- 1 | " ============================================================================ 2 | " vim-tagquery - A vim plugin that enables improved querying of tags 3 | " 4 | " Maintainer: Matt Snider 5 | " ============================================================================ 6 | 7 | if exists('g:loaded_tagquery') 8 | finish 9 | endif 10 | let g:loaded_tagquery = 1 11 | 12 | " Filter through tag query results in Ffz 13 | command! -narg=1 -complete=tag FzfTagQuery call tagquery#fzf() 14 | 15 | --------------------------------------------------------------------------------