-
Notifications
You must be signed in to change notification settings - Fork 387
/
strategy.vim
334 lines (274 loc) · 9.86 KB
/
strategy.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function! test#strategy#vimscript(cmd) abort
execute a:cmd
endfunction
function! test#strategy#basic(cmd) abort
if has('nvim')
-tabnew
call termopen(a:cmd)
if !get(g:, 'test#basic#start_normal', 0)
startinsert
endif
else
if s:restorescreen()
execute '!'.s:pretty_command(a:cmd)
else
execute '!'.a:cmd
endif
end
endfunction
function! test#strategy#make(cmd) abort
call s:execute_with_compiler(a:cmd, 'make')
endfunction
function! test#strategy#make_bang(cmd) abort
call s:execute_with_compiler(a:cmd, 'make!')
endfunction
function! test#strategy#neomake(cmd) abort
call s:execute_with_compiler(a:cmd, 'NeomakeProject')
endfunction
function! test#strategy#makegreen(cmd) abort
call s:execute_with_compiler(a:cmd, 'MakeGreen')
endfunction
function! test#strategy#asyncrun(cmd) abort
execute 'AsyncRun '.a:cmd
endfunction
function! test#strategy#asyncrun_setup_unlet_global_autocmd() abort
if !exists('#asyncrun_background#User#AsynRunStop')
augroup asyncrun_background
autocmd!
autocmd User AsyncRunStop if exists('g:test#strategy#cmd') | unlet g:test#strategy#cmd | endif
augroup END
endif
endfunction
function! test#strategy#asyncrun_background(cmd) abort
let g:test#strategy#cmd = a:cmd
call test#strategy#asyncrun_setup_unlet_global_autocmd()
execute 'AsyncRun -mode=async -silent -post=echo\ eval("g:asyncrun_code\ ?\"Failure\":\"Success\"").":"'
\ .'\ substitute(g:test\#strategy\#cmd,\ "\\",\ "",\ "") '.a:cmd
endfunction
function! test#strategy#asyncrun_background_term(cmd) abort
let g:test#strategy#cmd = a:cmd
call test#strategy#asyncrun_setup_unlet_global_autocmd()
execute 'AsyncRun -mode=term -pos=tab -focus=0 -listed=0 -post=echo\ eval("g:asyncrun_code\ ?\"Failure\":\"Success\"").":"'
\ .'\ substitute(g:test\#strategy\#cmd,\ "\\",\ "",\ "") '.a:cmd
endfunction
function! test#strategy#dispatch(cmd) abort
execute 'Dispatch '.a:cmd
endfunction
function! test#strategy#dispatch_background(cmd) abort
execute 'Dispatch! '.a:cmd
endfunction
function! test#strategy#spawn(cmd) abort
execute 'Spawn '.a:cmd
endfunction
function! test#strategy#spawn_background(cmd) abort
execute 'Spawn! '.a:cmd
endfunction
function! test#strategy#vimproc(cmd) abort
execute 'VimProcBang '.a:cmd
endfunction
function! s:neovim_new_term(cmd) abort
let term_position = get(g:, 'test#neovim#term_position', 'botright')
execute term_position . ' new'
call termopen(a:cmd)
endfunction
function! s:neovim_reopen_term(bufnr) abort
let l:current_window = win_getid()
let term_position = get(g:, 'test#neovim#term_position', 'botright')
execute term_position . ' sbuffer ' . a:bufnr
let l:new_window = win_getid()
call win_gotoid(l:current_window)
return l:new_window
endfunction
function! test#strategy#neovim(cmd) abort
call s:neovim_new_term(a:cmd)
au BufDelete <buffer> wincmd p " switch back to last window
if !get(g:, 'test#neovim#start_normal', 0)
startinsert
endif
endfunction
function! test#strategy#neovim_sticky(cmd) abort
let l:cmd = [a:cmd, '']
let l:tag = '_test_vim_neovim_sticky'
let l:buffers = getbufinfo({ 'buflisted': 1 })
\ ->filter({i, v -> has_key(v.variables, l:tag)})
if len(l:buffers) == 0
let l:current_window = win_getid()
call s:neovim_new_term(&shell)
let b:[l:tag] = 1
let l:buffers = getbufinfo(bufnr())
call win_gotoid(l:current_window)
else
if !get(g:, 'test#preserve_screen', 1)
let l:cmd = [&shell == 'cmd.exe' ? 'cls': 'clear'] + l:cmd
endif
if get(g:, 'test#neovim_sticky#kill_previous', 0)
let l:cmd = [""] + l:cmd
endif
endif
let l:win = win_findbuf(l:buffers[0].bufnr)
if !len(l:win) && get(g:, 'test#neovim_sticky#reopen_window', 0)
let l:win = [s:neovim_reopen_term(l:buffers[0].bufnr)]
endif
call chansend(l:buffers[0].variables.terminal_job_id, l:cmd)
if len(l:win) > 0
call win_execute(l:win[0], 'normal G', 1)
endif
endfunction
function! test#strategy#vimterminal(cmd) abort
let term_position = get(g:, 'test#vim#term_position', 'botright')
execute term_position . ' new'
call term_start(!s:Windows() ? ['/bin/sh', '-c', a:cmd] : ['cmd.exe', '/c', a:cmd], {'curwin': 1, 'term_name': a:cmd})
au BufDelete <buffer> wincmd p " switch back to last window
nnoremap <buffer> <Enter> :bd<CR>
redraw
echo "Press <Enter> to exit test runner terminal (<Ctrl-C> first if command is still running)"
endfunction
function! test#strategy#neoterm(cmd) abort
call neoterm#do({ 'cmd': a:cmd})
endfunction
function! test#strategy#toggleterm(cmd) abort
execute "TermExec cmd='".substitute(a:cmd, "'", '"', "g")."'"
endfunction
function! test#strategy#floaterm(cmd) abort
execute 'FloatermNew --autoclose=0 '.a:cmd
endfunction
function! test#strategy#vtr(cmd) abort
call VtrSendCommand(s:pretty_command(a:cmd), 1)
endfunction
function! test#strategy#vimux(cmd) abort
if exists('g:test#preserve_screen') && !g:test#preserve_screen
call VimuxClearTerminalScreen()
call VimuxClearRunnerHistory()
call VimuxRunCommand(s:command(a:cmd))
else
call VimuxRunCommand(s:pretty_command(a:cmd))
endif
endfunction
function! test#strategy#tslime(cmd) abort
call Send_to_Tmux(s:pretty_command(a:cmd)."\n")
endfunction
function! test#strategy#slimux(cmd) abort
if exists('g:test#preserve_screen') && !g:test#preserve_screen
call SlimuxSendCommand(s:pretty_command(a:cmd))
else
call SlimuxSendCommand(s:command(a:cmd))
endif
endfunction
function! test#strategy#tmuxify(cmd) abort
call tmuxify#pane_send_raw('C-u', '!')
call tmuxify#pane_send_raw('q', '!')
call tmuxify#pane_send_raw('C-u', '!')
if exists('g:test#preserve_screen') && !g:test#preserve_screen
call tmuxify#pane_send_raw('C-u', '!')
call tmuxify#pane_send_raw('C-l', '!')
call tmuxify#pane_send_raw('C-u', '!')
endif
call tmuxify#pane_run('!', s:command(a:cmd))
endfunction
function! test#strategy#vimshell(cmd) abort
execute 'VimShellExecute '.a:cmd
endfunction
function! test#strategy#terminal(cmd) abort
let cmd = join(['cd ' . shellescape(getcwd()), s:pretty_command(a:cmd)], '; ')
call s:execute_script('osx_terminal', cmd)
endfunction
function! test#strategy#iterm(cmd) abort
let cmd = join(['cd ' . shellescape(getcwd()), s:pretty_command(a:cmd)], '; ')
call s:execute_script('osx_iterm', cmd)
endfunction
function! test#strategy#kitty(cmd) abort
let cmd = join(['cd ' . shellescape(getcwd()), s:pretty_command(a:cmd)], '; ')
call s:execute_script('kitty_runner', cmd)
endfunction
function! test#strategy#shtuff(cmd) abort
if !exists('g:shtuff_receiver')
echoerr 'You must define g:shtuff_receiver to use this strategy'
return
endif
if exists('g:test#preserve_screen') && !g:test#preserve_screen
call system("shtuff into " . shellescape(g:shtuff_receiver) . " " . shellescape("clear;" . a:cmd))
else
call system("shtuff into " . shellescape(g:shtuff_receiver) . " " . shellescape(a:cmd))
endif
endfunction
function! test#strategy#harpoon(cmd) abort
let g:cmd = a:cmd . "\n"
let l:harpoon_term = exists("g:test#harpoon_term") ? g:test#harpoon_term : 1
lua require("harpoon.term").sendCommand(vim.api.nvim_eval("l:harpoon_term"), vim.g.cmd)
let goToTerminal = exists("g:test#harpoon#gototerminal") ? g:test#harpoon#gototerminal : 1
if goToTerminal
lua require("harpoon.term").gotoTerminal(vim.api.nvim_eval("l:harpoon_term"))
endif
endfunction
function! test#strategy#wezterm(cmd) abort
let l:wezterm = get(g:, "test#wezterm#executable", "wezterm")
if !exists("g:test#wezterm#pane_id")
let l:output = systemlist([l:wezterm, "cli", "get-pane-direction", "next"])
" wezterm outputs the current pane ID if only one pane is open
if l:output[0] == $WEZTERM_PANE
let l:prev = $WEZTERM_PANE
let l:dir = get(g:, "test#wezterm#split_direction", "right")
let l:width = get(g:, "test#wezterm#split_percent", 50)
let l:output = systemlist([l:wezterm, "cli", "split-pane", "--percent", l:width, "--" . l:dir])
" return to original pane
call system([l:wezterm, "cli", "activate-pane", "--pane-id", l:prev])
endif
let g:test#wezterm#pane_id = l:output[0]
endif
call system([l:wezterm, "cli", "send-text", "--no-paste", "--pane-id", g:test#wezterm#pane_id, a:cmd . ""])
endfunction
function! s:execute_with_compiler(cmd, script) abort
try
let default_makeprg = &l:makeprg
let default_errorformat = &l:errorformat
let default_compiler = get(b:, 'current_compiler', '')
if exists(':Dispatch')
let compiler = dispatch#compiler_for_program(a:cmd)
if !empty(compiler)
execute 'compiler ' . compiler
endif
endif
let &l:makeprg = a:cmd
execute a:script
finally
let &l:makeprg = default_makeprg
let &l:errorformat = default_errorformat
if empty(default_compiler)
unlet! b:current_compiler
else
let b:current_compiler = default_compiler
endif
endtry
endfunction
function! s:execute_script(name, cmd) abort
let script_path = g:test#plugin_path . '/bin/' . a:name
let cmd = join([shellescape(script_path), shellescape(a:cmd)])
execute 'silent !'.cmd
endfunction
function! s:pretty_command(cmd) abort
let cmds = []
let separator = !s:Windows() ? '; ' : ' & '
if !get(g:, 'test#preserve_screen')
call add(l:cmds, !s:Windows() ? 'clear' : 'cls')
endif
if get(g:, 'test#echo_command', 1)
call add(l:cmds, !s:Windows() ? 'echo -e '.shellescape(a:cmd) : 'Echo '.shellescape(a:cmd))
endif
call add(l:cmds, a:cmd)
return join(l:cmds, l:separator)
endfunction
function! s:command(cmd) abort
let separator = !s:Windows() ? '; ' : ' & '
return join([a:cmd], l:separator)
endfunction
function! s:Windows() abort
return has('win32') && fnamemodify(&shell, ':t') ==? 'cmd.exe'
endfunction
function! s:restorescreen() abort
if s:Windows()
return &restorescreen
else
return !empty(&t_ti) || !empty(&t_te)
endif
endfunction