Address #9: Add fzf integration

This commit is contained in:
Steven Arcangeli 2021-07-16 15:49:10 -07:00
parent 53e51cf2b7
commit 07c1c8c1f5
3 changed files with 73 additions and 2 deletions

View file

@ -230,7 +230,10 @@ Key | Command
## Fuzzy Finding
There is a telescope extension for fuzzy finding and jumping to symbols. It
### Telescope
If you have [telescope](https://github.com/nvim-telescope/telescope.nvim)
installed, there is an extension for fuzzy finding and jumping to symbols. It
functions similarly to the builtin `lsp_document_symbols` picker, the main
difference being that the aerial only includes the types of symbols in the
`filter_kind` configuration option. Load the extension with:
@ -241,6 +244,14 @@ require('telescope').load_extension('aerial')
You can then begin fuzzy finding with `:Telescope aerial`
### fzf
If you have [fzf](https://github.com/junegunn/fzf.vim) installed you can trigger
fuzzy finding with `:call aerial#fzf()`. To create a mapping:
```vim
nmap <silent> <leader>ds <cmd>call aerial#fzf()<cr>
```
## Highlight
There are highlight groups created for each `SymbolKind`. There will be one for

View file

@ -1,4 +1,24 @@
function! aerial#foldexpr() abort
return luaeval('require"aerial.fold".foldexpr(_A)', v:lnum)
endfunction
function! aerial#fzf() abort
let l:labels = luaeval("require('aerial.fzf').get_labels()")
if type(l:labels) == type(v:null) && l:labels == v:null
return
endif
call fzf#run({
\ 'source': l:labels,
\ 'sink': funcref('aerial#goto_symbol'),
\ 'options': '--prompt="Document symbols: "',
\ 'window': {'width': 0.5, 'height': 0.4},
\ })
endfunction
function! aerial#goto_symbol(symbol) abort
call luaeval("require('aerial.fzf').goto_symbol(_A)", a:symbol)
endfunction
function! s:mk_fzf_callback(callback)
return { item -> s:fzf_leave(a:callback, s:ChooserValueFromLabel(item)) }
endfunction

40
lua/aerial/fzf.lua Normal file
View file

@ -0,0 +1,40 @@
local backend = require("aerial.backend")
local data = require("aerial.data")
local navigation = require("aerial.navigation")
local M = {}
M.get_labels = function(opts)
opts = opts or {}
if not backend.is_supported() then
backend.log_support_err()
return nil
elseif not data:has_symbols(0) then
backend.fetch_symbols_sync(opts.timeout)
end
local results = {}
if data:has_symbols(0) then
data[0]:visit(function(item)
local label = string.format("%d:%s", item.lnum, item.name)
table.insert(results, label)
end)
end
return results
end
M.goto_symbol = function(symbol)
local colon = string.find(symbol, ":")
local lnum = tonumber(string.sub(symbol, 1, colon - 1))
local name = string.sub(symbol, colon + 1)
local idx = 1
data[0]:visit(function(item)
if lnum == item.lnum and name == item.name then
navigation.select({
index = idx,
})
return true
end
idx = idx + 1
end)
end
return M