-
-
Notifications
You must be signed in to change notification settings - Fork 154
Fzf references #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fzf references #832
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fb7d587
Adds a function to get the relative path from an absolute path
camilledejoye 35a0587
Uses fzf to open refernces if available
camilledejoye 4bca1a4
Only search within the filename (exclude line and column numbers)
camilledejoye 63b46b5
Makes some optimization
camilledejoye 9e939f2
Jumps to the position of the reference when opening a file from fzf
camilledejoye f54c159
Updates the doc to talk about fzf&bat
1acff76
Extract the quickfix building threw zfz in a separate file
7c4089d
Adds an option to disable the use of fzf to build the quickfix
68afafb
Removes trailing spaces in phpactor.vim
02a148c
Adds an header in the fzf windows to tell about CTRL-Q
eced57c
Removes the header `fzf &fzf.vim` in the doc
5f3b02a
Rework the quickfix feature to include strategies
1618d76
Updates the doc after the feedbacks
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| function! phpactor#quickfix#strategy() abort | ||
| if !has_key(g:, 'PhpactorQuickfixStrategy') | ||
| return s:auto_detect_strategy() | ||
| endif | ||
|
|
||
| let Strategy = g:PhpactorQuickfixStrategy | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not blocking: should be lowercase S ?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or this is a naming convention for functions? |
||
|
|
||
| if type(function('tr')) == type(Strategy) | ||
| return Strategy | ||
| endif | ||
|
|
||
| if type('') != type(Strategy) || 0 == count(['fzf', 'vim'], Strategy) | ||
| echoerr 'Invalid strategy provided, check the value of "g:PhpactorQuickfixStrategy"' | ||
|
|
||
| return s:auto_detect_strategy() | ||
| endif | ||
|
|
||
| return function('phpactor#quickfix#'. Strategy) | ||
| endfunction | ||
|
|
||
| function! s:auto_detect_strategy() | ||
| let strategy = 'vim' | ||
|
|
||
| if exists("*fzf#complete") | ||
| let strategy = 'fzf' | ||
| endif | ||
|
|
||
| return function('phpactor#quickfix#'. strategy) | ||
| endfunction | ||
|
|
||
| function! phpactor#quickfix#vim(entries) abort | ||
| call setqflist(a:entries) | ||
| cw | ||
| endfunction | ||
|
|
||
| function! phpactor#quickfix#build(entries) abort | ||
| try | ||
| let Strategy = phpactor#quickfix#strategy() | ||
| call call(Strategy, [a:entries]) | ||
| catch /E117/ | ||
| redraw! | ||
| echo 'The strategy "'. string(Strategy) .'" is unknown, check the value of "g:PhpactorQuickfixStrategy".' | ||
| endtry | ||
| endfunction | ||
|
|
||
| function! phpactor#quickfix#fzf(entries) abort | ||
| let entries = {} | ||
| for entry in a:entries | ||
| let key = s:relative_path(entry['filename']) | ||
| \ .':'. entry['lnum'] | ||
| \ .':'. (entry['col']) | ||
| \ .':'. entry['text'] | ||
|
|
||
| let entries[key] = entry | ||
| endfor | ||
|
|
||
| let formated = s:align_pairs(keys(entries), '^\(.\{-}:\d\+:\d\+:\)\s*\(.*\)\s*$', 100) | ||
|
|
||
| let tmp = copy(entries) | ||
| let results = {} | ||
| for key in keys(tmp) | ||
| let newKey = formated[key] | ||
| let results[newKey] = tmp[key] | ||
| endfor | ||
| unlet tmp | ||
|
|
||
| let actions = { | ||
| \ 'ctrl-t': 'tab split', | ||
| \ 'ctrl-x': 'split', | ||
| \ 'ctrl-v': 'vsplit', | ||
| \ 'ctrl-q': function('phpactor#quickfix#vim') | ||
| \ } | ||
|
|
||
| call fzf#run(fzf#wrap('find_references', fzf#vim#with_preview({ | ||
| \ 'source': keys(results), | ||
| \ 'down': '60%', | ||
| \ '_action': actions, | ||
| \ 'sink*': function('<SID>quickfix_sink', [results, actions]), | ||
| \ 'options': [ | ||
| \ '--expect='. join(keys(actions), ','), | ||
| \ '--multi', | ||
| \ '--bind=ctrl-a:select-all,ctrl-d:deselect-all', | ||
| \ '--inline-info', | ||
| \ '--header', ":: Press \x1b[35mCTRL-Q\x1b[m to open the quickfix with your selection", | ||
| \ '--delimiter=:', '--nth=1,4' | ||
| \ ]}, 'up', '?'), 1)) | ||
| endfunction | ||
|
|
||
| function! s:quickfix_sink(results, actions, lines) abort | ||
| if 2 > len(a:lines) | ||
| return " Don't know how to handle this, should not append | ||
| endif | ||
|
|
||
| let actionKey = remove(a:lines, 0) | ||
| let Action = get(a:actions, actionKey, 'e') | ||
| let items = map(copy(a:lines), {key, value -> a:results[value]}) | ||
|
|
||
| if type(function('call')) == type(Action) | ||
| return Action(items) | ||
| endif | ||
|
|
||
| if len(a:lines) > 1 | ||
| augroup fzf_swap | ||
| autocmd SwapExists * let v:swapchoice='o' | echohl WarningMsg | ||
| \| echom 'fzf: E325: swap file exists: '. expand('<afile>') | ||
| \| echohl None | ||
| augroup END | ||
| endif | ||
|
|
||
| try | ||
| let empty = empty(expand('%')) && 1 == line('$') && empty(getline(1)) && !&modified | ||
| let autochdir = &autochdir | ||
| set noautochdir | ||
|
|
||
| for item in items | ||
| let filename = fnameescape(item.filename) | ||
| let Action = empty ? 'e' : Action " Use the current buffer if empty | ||
|
|
||
| execute Action '+'.item.lnum filename | ||
| execute 'normal!' item.col .'|' | ||
| normal! zz | ||
|
|
||
| if empty | ||
| let empty = v:false | ||
| endif | ||
|
|
||
| if !has('patch-8.0.0177') && !has('nvim-0.2') && exists('#BufEnter') | ||
| \ && isdirectory(item.filename) | ||
| doautocmd BufEnter | ||
| endif | ||
| endfor | ||
| catch /^Vim:Interrupt$/ | ||
| finally | ||
| let &autochdir = autochdir | ||
| silent! autocmd! fzf_swap | ||
| endtry | ||
| endfunction | ||
|
|
||
| function! s:align_pairs(list, regexp, ...) abort | ||
| let maxlen = 0 | ||
| let pairs = {} | ||
| for elem in a:list | ||
| let match = matchlist(elem, a:regexp) | ||
| let [filename, text] = match[1:2] | ||
| let maxlen = max([maxlen, len(filename)]) | ||
| let pairs[elem] = [filename, text] | ||
| endfor | ||
|
|
||
| let args = copy(a:000) | ||
| let max = 60 | ||
| if 0 < len(args) && type(v:t_number) == type(args[0]) | ||
| let max = remove(args, 0) | ||
| endif | ||
|
|
||
| let maxlen = min([maxlen, max]) | ||
|
|
||
| return map(pairs, "printf('%-'.maxlen.'s', v:val[0]).' '.v:val[1]") | ||
| endfunction | ||
|
|
||
| function! s:relative_path(absolute_path) | ||
| let l:cwd = getcwd() | ||
|
|
||
| return substitute(a:absolute_path, l:cwd .'/', '', '') | ||
| endfunction | ||
dantleech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| " vim: et ts=4 sw=4 fdm=marker | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would still prefer to define all available keys in the same place, which is here currently. Note also that options are
camelCase.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this can then safely assume that
g:phpactorQuickfixStrategyexists.Also still not sure
quickfixis the right term. We usequickfixto solve a problem, the problem is navigating and showing code references. So it's really a strategy for that? e.g.phpactorReferenceNavigation? For me it might make sense to sayphpactorReferenceNavigation = "quickfix"Saying that, I won't block on this, also hapy to leave it as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't use camel case with Funcref, it must be pascal case, otherwise vim raise an error.
From the vim doctumentation:
The quickfix is just the way vim show positions in file in a way that the user can jump to those positions.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the way
vimshows positions - Phpactor could usequickfixor not, it's an implementation for showing and jumping to positions. We could use FZF without using quickfix at all, but it's fine, it makes sense :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely, I just think we don't need a new word since we are naming a configuration variable for the vim plugin it's easier for the users to have the same language than vim. :)