-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexus.vim
172 lines (140 loc) · 4.94 KB
/
nexus.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
" nexus.vim - Get vim and tmux to play nice together
" Author: Stefan Kanev
" URL: http://github.com/skanev/vim-nexus
" Initialize g:nexusVim {{{1
if !exists('g:nexusVim')
if has("gui_macvim")
let g:nexusVim = 'mvim'
elseif has("gui_running")
let g:nexusVim = 'gvim'
else
let g:nexusVim = 'vim'
end
endif
" Modes and targets {{{1
let s:runners_path = expand('<sfile>:h:h') . '/runners'
let s:modes = {}
let s:modes.cucumber = {}
let s:modes.cucumber.matcher = '\.feature$'
let s:modes.cucumber.run_test = '"command cucumber " . expand("%") . " --drb"'
let s:modes.cucumber.run_test_line = '"command cucumber " . expand("%") . ":" . line(".") . " --drb"'
let s:modes.rspec = {}
let s:modes.rspec.matcher = '_spec\.rb$'
let s:modes.rspec.run_test = '"command rspec --format nested " . expand("%") . " --drb --require " . s:runner("rspec")'
let s:modes.rspec.run_test_line = '"command rspec --format nested " . expand("%") . " --line " . line(".") . " --drb --require " . s:runner("rspec")'
let s:modes.rspec.errorformat = "%f:%l:%m"
let s:modes.rspec.test_runner = 'rspec.rb'
" Nexus utility functions {{{1
function! s:currentSessionName()
return fnamemodify(getcwd(), ':t')
endfunction
function! s:tmux(command)
if s:notRunning() | return s:errorNotRunning() | endif
let session = s:currentSessionName()
let escaped = shellescape(a:command)
let tmuxCall = "tmux send-keys -t " . session . ":nexus C-l C-u " . escaped . " C-m"
call system(tmuxCall)
endfunction
function! s:run(target)
let commands = {}
for [mode, definition] in items(s:modes)
if match(expand("%"), definition.matcher) != -1
let commands = definition
break
endif
endfor
if has_key(commands, a:target)
let command = eval(commands[a:target])
let g:nexusLastTest = command
call s:tmux(command)
elseif exists("g:nexusLastTest")
call s:tmux(g:nexusLastTest)
else
echohl ErrorMsg | echo "Nexus: cannot run current file (" . a:target . ")"| echohl None
return
end
endfunction
function! s:sendKeys(text)
if s:notRunning() | return s:errorNotRunning() | endif
let session = s:currentSessionName()
let lines = split(a:text, "\n")
for line in lines
let keys = shellescape(line)
let keys = substitute(keys, ';', "' '" . '\\;' . "' '", "g")
call system("tmux send-keys -t " . session . " " . keys . " C-m")
endfor
endfunction
function! s:sessionRunning()
let existingSessions = split(system("tmux list-sessions | cut -f 1 -d :"))
return index(existingSessions, s:currentSessionName()) >= 0
endfunction
function! s:notRunning()
return !s:sessionRunning()
endfunction
function! s:errorNotRunning()
if s:notRunning()
echohl WarningMsg | echo 'Cannot interact with tmux - session is not running' | echohl None
end
endfunction
function! s:runner(mode)
if !has_key(s:modes, a:mode)
echoerr "Undefined mode: " . a:mode | return
elseif !has_key(s:modes[a:mode], 'test_runner')
echoerr "The mode '" . a:mode . "' does not have a test runner" | return
else
let path = s:runners_path . '/' . s:modes[a:mode].test_runner
return fnamemodify(path, ':~:.')
endif
endfunction
" Session management {{{1
function! s:createSession()
let session = s:currentSessionName()
let existingSessions = split(system("tmux list-sessions | cut -f 1 -d :"))
if s:sessionRunning()
echohl WarningMsg | echo "tmux session already created. Run 'tmux attach -t " . session . "' to join it." | echohl None
return
end
call system('NEXUS_VIM=' . g:nexusVim . ' tmux new-session -d -s ' . session)
call system('tmux rename-window -t ' . session . ' nexus')
echohl MoreMsg | echo "tmux session created. Run 'tmux attach -t " . session . "' to join it." | echohl None
endfunction
" Sending text {{{1
function! s:sendSelection()
call s:sendKeys(s:selectedText())
endfunction
function! s:sendBuffer()
call s:sendKeys(s:currentBufferContents())
endfunction
" Reading text {{{1
function! s:readPane()
if s:notRunning() | return s:errorNotRunning() | endif
let session = s:currentSessionName()
call system('tmux capture-pane -t ' . session)
let contents = system('tmux save-buffer - | cat')
let contents = substitute(contents, '\v[\n ]+$', '', 'g')
let lines = split(contents, "\n")
call append(line('.'), lines)
endfunction
" Utility functions {{{1
function! s:selectedText()
try
let a_save = @a
normal! gv"ay
return @a
finally
let @a = a_save
endtry
endfunction
function! s:currentBufferContents()
return join(getline(0, '$'), "\n")
endfunction
" Commands {{{1
command! Nexus call s:createSession()
command! NexusSendBuffer call s:sendBuffer()
command! NexusReadPane call s:readPane()
" Mappings {{{1
noremap <expr> <Plug>NexusRunTestFile <SID>run('run_test')
noremap <expr> <Plug>NexusRunTestLine <SID>run('run_test_line')
noremap <expr> <Plug>NexusSendBuffer <SID>sendBuffer()
xnoremap <Plug>NexusSendSelection <ESC>:call <SID>sendSelection()<CR>
" vim:set et ft=vim foldmethod=marker ts=2 sts=2 sw=2