Skip to content

Commit f926f8b

Browse files
authored
Add support for fzf_action in s:FZF (#1195)
1 parent a20a720 commit f926f8b

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

autoload/LanguageClient.vim

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ function! s:selectionUI_funcref(source, sink) abort
271271
endif
272272
endfunction
273273

274+
function! s:GetFZFAction() abort
275+
let l:default_fzf_action = {
276+
\ 'ctrl-t': 'tab split',
277+
\ 'ctrl-x': 'split',
278+
\ 'ctrl-v': 'vsplit'
279+
\ }
280+
return get(g:, 'fzf_action', l:default_fzf_action)
281+
endfunction
282+
274283
function! s:FZF(source, sink) abort
275284
if !get(g:, 'loaded_fzf')
276285
call s:Echoerr('FZF not loaded!')
@@ -285,9 +294,11 @@ function! s:FZF(source, sink) abort
285294
let l:options = []
286295
endif
287296
endif
297+
call add(l:options, '--expect='.join(keys(s:GetFZFAction()), ','))
298+
288299
call fzf#run(fzf#wrap({
289300
\ 'source': a:source,
290-
\ 'sink': function(a:sink),
301+
\ 'sink*': function(a:sink),
291302
\ 'options': l:options,
292303
\ }))
293304
if has('nvim') && !has('nvim-0.4')

doc/LanguageClient.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,11 @@ Valid options: "fzf" | "quickfix" | "location-list" | |Funcref|
228228
If you use a |Funcref|, the referenced function should have two arguments
229229
(source, sink). The "source" argument can be either a command string or a
230230
list. The sink argument can be a command string or a |Funcref| that takes the
231-
selected line as its first argument.
231+
selected line as its argument.
232232

233-
An example implementation is "s:FZF()" in autoload/LanguageClient.vim. For
234-
details, see the source and sink arguments of fzf#run():
233+
An example implementation is "s:FZF()" in autoload/LanguageClient.vim which
234+
supports opening selected file in different ways (configurable via
235+
|g:fzf_action|). For details, see the source and sink arguments of fzf#run():
235236
https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzfrun
236237

237238
Another example, this time using vim-clap: >

src/language_server_protocol.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,13 +3271,19 @@ impl LanguageClient {
32713271
}
32723272
};
32733273

3274-
let lines = <Vec<String>>::deserialize(&params)?;
3274+
let lines = <Vec<String>>::deserialize(&params[0])?;
32753275
if lines.is_empty() {
32763276
anyhow!("No selection!");
32773277
}
32783278

3279+
let fzf_action: HashMap<String, String> = self.vim()?.eval("s:GetFZFAction()")?;
3280+
let goto_cmd = match lines.get(0) {
3281+
Some(action) if fzf_action.contains_key(action) => fzf_action.get(action).cloned(),
3282+
_ => Some("edit".to_string()),
3283+
};
3284+
32793285
let location = lines
3280-
.get(0)
3286+
.get(1)
32813287
.ok_or_else(|| anyhow!("Failed to get line! lines: {:?}", lines))?
32823288
.split('\t')
32833289
.next()
@@ -3304,7 +3310,7 @@ impl LanguageClient {
33043310
.parse::<u32>()?
33053311
- 1;
33063312

3307-
self.edit(&None, &filename)?;
3313+
self.edit(&goto_cmd, &filename)?;
33083314
self.vim()?.cursor(line + 1, character + 1)?;
33093315

33103316
Ok(())

0 commit comments

Comments
 (0)