-
Notifications
You must be signed in to change notification settings - Fork 80
/
rg.vim
69 lines (65 loc) · 2.15 KB
/
rg.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
" vim:sw=2:
" ============================================================================
" FileName: rg.vim
" Author: voldikss <dyzplus@gmail.com>
" GitHub: https://github.com/voldikss
" ============================================================================
if executable('bat')
let s:viewer = 'bat --style=numbers --color=always --highlight-line {2}'
elseif executable('batcat')
let s:viewer = 'batcat --style=numbers --color=always --highlight-line {2}'
else
let s:viewer = 'cat -n'
endif
function! floaterm#wrapper#rg#(cmd, jobopts, config) abort
let FZF_DEFAULT_COMMAND = join([
\ "rg",
\ "--column",
\ "--line-number",
\ "--no-heading",
\ "--color=always",
\ "--smart-case",
\ join(split(a:cmd)[1:])
\ ])
let s:rg_tmpfile = tempname()
let prog = 'fzf'
let arglist = [
\ '--ansi',
\ '--multi',
\ '--no-height',
\ '--delimiter :',
\ '--bind ctrl-/:toggle-preview' ,
\ '--bind alt-a:select-all,alt-d:deselect-all',
\ '--preview-window +{2}-/2 --preview-window right',
\ printf('--preview "%s {1}"', s:viewer)
\ ]
let cmd = printf('%s %s > %s', prog, join(arglist), s:rg_tmpfile)
let cmd = [&shell, &shellcmdflag, cmd]
let jobopts = {
\ 'on_exit': funcref('s:rg_callback'),
\ 'env': {'FZF_DEFAULT_COMMAND': FZF_DEFAULT_COMMAND}
\ }
call floaterm#util#deep_extend(a:jobopts, jobopts)
return [v:false, cmd]
endfunction
function! s:rg_callback(job, data, event, opener) abort
if filereadable(s:rg_tmpfile)
let filenames = readfile(s:rg_tmpfile)
if !empty(filenames)
if has('nvim')
call floaterm#window#hide(bufnr('%'))
endif
let locations = []
for filename in filenames
let parts = matchlist(filename, '\(.\{-}\)\s*:\s*\(\d\+\)\%(\s*:\s*\(\d\+\)\)\?\%(\s*:\(.*\)\)\?')
let dict = {
\ 'filename': fnamemodify(parts[1], ':p'),
\ 'lnum': parts[2],
\ 'text': parts[4]
\ }
call add(locations, dict)
endfor
call floaterm#util#open(locations, a:opener)
endif
endif
endfunction