-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimview.py
363 lines (287 loc) · 9.5 KB
/
vimview.py
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# Copyright (c) 2016, Michal Szymaniak
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import gdb
import subprocess
import os.path
### Helper functions ###
def _gdbBooleanToStr(val):
if val == True:
return 'on'
elif val == False:
return 'off'
elif val == None:
return 'auto'
else:
return str(val)
def _getVimServerNameVariable():
return os.environ['VIMSERVER']
def _isVimServerNameVariableSet():
try:
_getVimServerNameVariable()
return True
except KeyError:
return False
### Remote communication with vim ###
class VimView:
serverName = None
cmd = None
cmdFileArg = None
debug = False
curFile = None
curLine = None
nullPipe = None
def __init__(self):
self.nullPipe = open(os.devnull, 'w')
self.setServerName('gdb')
self.setUseTabs(False)
def dbgPrint(self, info, msg):
if self.debug and msg:
gdb.write('vimview ' + info + ': ' + msg + '\n')
def setServerName(self, name):
if name:
self.serverName = name
# self.cmd = ['vim', '+q', '--servername', name]
self.cmd = ['nvr']
def setUseTabs(self, useTabs):
if useTabs:
self.cmdFileArg = '--remote-tab'
else:
self.cmdFileArg = '--remote'
def execCmd(self, command):
cmd = self.cmd + ['--remote-expr', command]
self.dbgPrint('cmd', str(cmd))
new_env = os.environ.copy()
vim = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=new_env)
out, err = vim.communicate()
if out:
out = out.decode('utf-8')
self.dbgPrint('out', out)
if err:
err = err.decode('utf-8')
self.dbgPrint('err', err)
return out, err
def openFile(self, fileName, lineNo=None, existingOnly=True, reopen=False):
if not reopen and (fileName == self.curFile and lineNo == self.curLine):
return False
if existingOnly and not os.path.isfile(fileName):
return False
if lineNo:
cmd = self.cmd + [self.cmdFileArg, '+' + str(lineNo), fileName]
else:
cmd = self.cmd + [self.cmdFileArg, fileName]
self.dbgPrint('cmd', str(cmd))
self.curFile = fileName
self.curLine = lineNo
vim = subprocess.Popen(cmd, stdout=self.nullPipe, stderr=self.nullPipe)
vim.communicate()
return True
def openCurrentFile(self, showError=True, existingOnly=True, reopen=False):
try:
frameSym = gdb.selected_frame().find_sal()
if not frameSym.is_valid():
if showError:
gdb.write('this is not a valid frame\n')
return
except gdb.error:
if showError:
gdb.write('no frame is selected\n')
return
if not frameSym.symtab:
if showError:
gdb.write('can\'t read symbol for current frame\n')
return
if not frameSym.symtab.filename:
if showError:
gdb.write('cant read filename for current frame\n')
return
file = frameSym.symtab.fullname()
self.openFile(file, frameSym.line, existingOnly, reopen)
### Command: view current file in vim ###
class CmdView(gdb.Command):
"""Show current stack frame in vim.
This is part of the VimView plugin."""
def __init__(self, cmd):
super (CmdView, self).__init__(cmd, gdb.COMMAND_USER)
def invoke(self, arg, from_tty):
global vimView
vimView.openCurrentFile(showError=True, reopen=True)
### Command: set breakpoint under vim cursor ###
class CmdBreak(gdb.Command):
"""Set breakpoint under current vim cursor position.
If run with breakpoint number as argument, will show breakpoint position in vim.
This is part of the VimView plugin."""
def __init__(self, cmd):
super(CmdBreak, self).__init__(cmd, gdb.COMMAND_USER)
def invoke(self, arg, from_tty):
if arg:
try:
self.showBreak(int(arg))
except ValueError:
gdb.write('expected breakpoint number, got "' + arg + '"\n')
else:
self.putBreak()
def putBreak(self):
global vimView
out, err = vimView.execCmd('expand("%:p") . ":" . line(".")')
fileName = out.rstrip()
if not err:
try:
gdb.Breakpoint(fileName, gdb.BP_BREAKPOINT)
except RuntimeError as ex:
gdb.write(str(ex) + '\n')
else:
gdb.write('error: ' + err)
def showBreak(self, no):
# TODO: handle non-file name locations
global vimView
try:
br = next(x for x in gdb.breakpoints() if x.number==no)
try:
fileName, lineNo = br.location.rsplit(':', 1)
lineNo = int(lineNo)
except ValueError:
fileName = br.location
lineNo = None
if not vimView.openFile(fileName, lineNo, reopen=True):
gdb.write('cannot open file "' + fileName + '"\n')
except StopIteration:
gdb.write('no breakpoint number ' + str(no) + '\n')
### Convenience variable: word under vim cursor ###
class VarCursorWord(gdb.Function):
cmd = None
def __init__ (self, name, cmd):
self.name = name
self.cmd = cmd
super(VarCursorWord, self).__init__ (name)
def invoke(self, *args):
global vimView
out, err = vimView.execCmd(self.cmd)
if not err:
return out.rstrip()
else:
gdb.write('error: ' + err)
return ''
### Event handlers ###
def eventStop(ev):
global vimView
vimView.openCurrentFile(showError=False, reopen=True)
### Prompt hook ###
def prompt(pr):
global vimView
vimView.openCurrentFile(showError=False)
return None
### Parameter: vimview stop hook ###
class ParamVimViewOnStop(gdb.Parameter):
"""This is part of the VimView plugin."""
isHooked = False
def __init__(self, cmd):
self.value = False
self.set_doc = 'VimView: following frame on stop.'
self.show_doc = self.set_doc
super(ParamVimViewOnStop, self).__init__(cmd, gdb.COMMAND_SUPPORT, gdb.PARAM_AUTO_BOOLEAN)
def get_set_string(self):
if self.value == None: # auto
self.value = _isVimServerNameVariableSet()
if self.value:
if not self.isHooked:
gdb.events.stop.connect(eventStop)
self.isHooked = True
else:
if self.isHooked:
gdb.events.stop.disconnect(eventStop)
self.isHooked = False
return self.get_show_string(self.value)
def get_show_string(self, svalue):
return 'Vim follows frame on stop: ' + _gdbBooleanToStr(svalue)
### Parameter: vimview prompt hook ###
class ParamVimViewOnPrompt(gdb.Parameter):
"""This is part of the VimView plugin."""
prevHook = gdb.prompt_hook
def __init__(self, cmd):
self.value = False
self.set_doc = 'VimView: following frame on prompt show.'
self.show_doc = self.set_doc
super(ParamVimViewOnPrompt, self).__init__(cmd, gdb.COMMAND_SUPPORT, gdb.PARAM_AUTO_BOOLEAN)
def get_set_string(self):
if self.value == None: # auto
self.value = _isVimServerNameVariableSet()
if self.value:
if gdb.prompt_hook != prompt:
self.prevHook = gdb.prompt_hook
gdb.prompt_hook = prompt
else:
gdb.prompt_hook = self.prevHook
return self.get_show_string(self.value)
def get_show_string(self, svalue):
return 'Vim follows frame on prompt: ' + _gdbBooleanToStr(svalue)
### Parameter: vim server name ###
class ParamServerName(gdb.Parameter):
"""This is part of the VimView plugin."""
def __init__(self, cmd):
self.set_doc = 'VimView: remote vim server name.'
self.show_doc = self.set_doc
super(ParamServerName, self).__init__(cmd, gdb.COMMAND_SUPPORT, gdb.PARAM_STRING)
global vimView
self.value = vimView.serverName
def get_set_string(self):
global vimView
vimView.setServerName(self.value)
return self.get_show_string(self.value)
def get_show_string(self, svalue):
return 'Vim server name: "' + svalue + '"'
### Parameter: use tabs in vim ###
class ParamUseTabs(gdb.Parameter):
"""This is part of the VimView plugin."""
def __init__(self, cmd):
self.value = False
self.set_doc = 'VimView: open files in tabs.'
self.show_doc = self.set_doc
super(ParamUseTabs, self).__init__(cmd, gdb.COMMAND_SUPPORT, gdb.PARAM_BOOLEAN)
def get_set_string(self):
global vimView
vimView.setUseTabs(self.value)
return self.get_show_string(self.value)
def get_show_string(self, svalue):
return 'Open files in tabs: ' + _gdbBooleanToStr(svalue)
if __name__ == "__main__":
if 'vimView' not in globals():
vimView = VimView()
try:
serverName = _getVimServerNameVariable()
vimView.setServerName(serverName)
gdb.write('Vim server name: "' + serverName + '"\n')
except KeyError:
serverName = None
CmdView('vim')
CmdView('v')
CmdBreak('vbreak')
VarCursorWord('vw', 'expand("<cword>")')
VarCursorWord('ve', 'expand("<cWORD>")')
VarCursorWord('vf', 'expand("%:p")')
VarCursorWord('vl', 'line(".")')
VarCursorWord('vfl', 'expand("%:p") . ":" . line(".")')
ParamVimViewOnStop('vimview-onstop')
ParamVimViewOnPrompt('vimview-onprompt')
ParamServerName('vimview-server')
ParamUseTabs('vimview-tabs')