Last active
January 7, 2021 13:19
-
-
Save strboul/9503bd238c46bed9638665f8df8c213c to your computer and use it in GitHub Desktop.
FZF 'smart finder' buffers + files + gitwhatchanged etc. in one pane
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Plug 'https://github.com/vijaymarupudi/nvim-fzf' | |
-- nnoremap <leader>k <cmd>lua require('fzf_smart_finder').sf()<CR> | |
-- | |
-- Important note: run all `nvim_fzf` functions in coroutine | |
local fzf = require 'fzf'.fzf | |
local action = require 'fzf.actions'.action | |
coroutine.wrap(function() | |
-- items is a table of selected or hovered fzf items | |
local preview_shell = action(function(items) | |
-- only one item will be hovered at any time, so get the selection | |
-- out and convert it to a number | |
local buf = tonumber(items[1]['id']) | |
-- you can return either a string or a table to show in the preview | |
-- window | |
return vim.api.nvim_buf_get_lines(buf, 0, -1, false) | |
end) | |
local buffers = vim.api.nvim_list_bufs() | |
local loaded_buffers = {} | |
for _, buf_id in pairs(buffers) do | |
if vim.api.nvim_buf_is_loaded(buf_id) then | |
local buf_name = vim.api.nvim_buf_get_name(buf_id) | |
table.insert(loaded_buffers, {id = buf_id, name = buf_name}) | |
end | |
end | |
print(vim.inspect(loaded_buffers[1])) | |
-- print(table.concat(loaded_buffers, "|")) | |
local result = fzf(loaded_buffers, "--preview " .. preview_shell) | |
if result then | |
vim.cmd("buffer" .. result[1]) | |
end | |
end)() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" FIXME: work in progress | |
" | |
" Buffers start with [B] and files start with [F] (different colors for each | |
" if possible) | |
" | |
" - change commands to smaller cases | |
" - it may be better to use fzf internals directly (if possible) | |
" command! -bang SmartFinder | |
" \ call fzf#vim#files(<q-args>, fzf#vim#with_preview(), <bang>0) | |
" Sink ------------------------------ | |
function! s:bufopen(elem) | |
execute 'buffer' matchstr(a:elem, '^[ 0-9]*') | |
endfunction | |
function! s:fileopen(elem) | |
execute 'edit' a:elem | |
endfunction | |
function! s:smart_finder_sink(elem) | |
call s:bufopen(a:elem) | |
endfunction | |
" Source ------------------------------ | |
function s:smart_finder_source() | |
let l:buflist=BufList() | |
let l:git_files=ListGitFiles() | |
return l:buflist + l:git_files | |
endfunction | |
function! ListGitFiles() | |
let l:git_ls = split(system('git ls-files'), '\n') | |
return l:git_ls | |
endfunction | |
function! ListGitWhatChanged() | |
let l:gitwhatchanged = split(system('gitwhatchanged'), '\n') | |
return l:gitwhatchanged | |
endfunction | |
function! BufList() | |
redir => ls | |
silent ls | |
redir END | |
return split(ls, '\n') | |
endfunction | |
" Command ------------------------------ | |
command! FzfSmartFinder | |
\ call fzf#run(fzf#wrap({ | |
\ 'source': s:smart_finder_source(), | |
\ 'sink': function('s:smart_finder_sink'), | |
\ 'options': '-m -x +s' | |
\ })) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment