2014-05-17 15:43:18 -04:00
|
|
|
"Read and return all new lines from a Vebugger pipe object.
|
2013-12-23 16:37:18 -05:00
|
|
|
function! s:readNewLinesFromPipe(pipeObject)
|
|
|
|
"read
|
|
|
|
let l:text=a:pipeObject.pipe.read(1000,0)
|
|
|
|
while 0<len(l:text)
|
|
|
|
let a:pipeObject.buffer.=l:text
|
|
|
|
let l:text=a:pipeObject.pipe.read(1000,0)
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
"parse
|
|
|
|
let l:lastNewline=strridx(a:pipeObject.buffer,"\n")
|
|
|
|
if 0<=l:lastNewline
|
2014-05-16 10:42:22 -04:00
|
|
|
let l:outLines=split(strpart(a:pipeObject.buffer,0,l:lastNewline),'\r\n\|\n\|\r')
|
2013-12-23 16:37:18 -05:00
|
|
|
let a:pipeObject.buffer=strpart(a:pipeObject.buffer,l:lastNewline+1)
|
|
|
|
return l:outLines
|
|
|
|
endif
|
|
|
|
|
|
|
|
return []
|
|
|
|
endfunction
|
|
|
|
|
2013-12-25 13:33:12 -05:00
|
|
|
let s:f_debugger={}
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Terminate the debugger
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:f_debugger.kill() dict
|
2014-05-16 10:42:22 -04:00
|
|
|
if self.shell.is_valid
|
|
|
|
call self.addLineToTerminal('','== DEBUGGER TERMINATED ==')
|
|
|
|
endif
|
2013-12-25 13:33:12 -05:00
|
|
|
let &updatetime=self.prevUpdateTime
|
|
|
|
call self.shell.kill(15)
|
2014-05-16 10:42:22 -04:00
|
|
|
if exists('s:debugger')
|
|
|
|
for l:closeHandler in s:debugger.closeHandlers
|
|
|
|
call l:closeHandler.handle(self)
|
|
|
|
endfor
|
|
|
|
endif
|
2013-12-25 13:33:12 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Write a line to the debugger's interactive shell
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:f_debugger.writeLine(line) dict
|
|
|
|
call self.shell.stdin.write(a:line."\n")
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Check for new lines from the debugger's interactive shell and handle them
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:f_debugger.invokeReading() dict
|
|
|
|
let l:newLines={}
|
|
|
|
for l:k in keys(self.pipes)
|
|
|
|
let l:nl=s:readNewLinesFromPipe(self.pipes[l:k])
|
|
|
|
if 0<len(l:nl)
|
|
|
|
let l:newLines[l:k]=l:nl
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
for l:k in keys(l:newLines)
|
|
|
|
for l:line in l:newLines[l:k]
|
|
|
|
call self.handleLine(l:k,l:line)
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
|
|
|
|
let l:checkpid=self.shell.checkpid()
|
|
|
|
if 'exit'==l:checkpid[0]
|
|
|
|
\|| 'error'==l:checkpid[0]
|
|
|
|
call self.kill()
|
|
|
|
endif
|
2014-05-15 22:00:26 -04:00
|
|
|
call feedkeys("f\e") " Make sure the CursorHold event is refired even if the user does nothing
|
2013-12-25 13:33:12 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Handle a single line from the debugger's interactive shell
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:f_debugger.handleLine(pipeName,line) dict
|
2014-05-14 14:52:55 -04:00
|
|
|
call self.addLineToTerminal(a:pipeName,a:line)
|
2013-12-25 13:33:12 -05:00
|
|
|
|
|
|
|
let l:readResult=deepcopy(self.readResultTemplate,1)
|
|
|
|
|
|
|
|
for l:readHandler in self.readHandlers
|
|
|
|
call l:readHandler.handle(a:pipeName,a:line,l:readResult,self)
|
|
|
|
endfor
|
|
|
|
|
|
|
|
for l:thinkHandler in self.thinkHandlers
|
2013-12-27 15:54:11 -05:00
|
|
|
call l:thinkHandler.handle(l:readResult,self)
|
2013-12-25 13:33:12 -05:00
|
|
|
endfor
|
2013-12-27 16:52:57 -05:00
|
|
|
|
|
|
|
call self.performWriteActions()
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Perform all write actions
|
2013-12-27 16:52:57 -05:00
|
|
|
function! s:f_debugger.performWriteActions() dict
|
|
|
|
for l:namespace in keys(self.writeActions)
|
|
|
|
let l:handlers=get(self.writeHandlers,l:namespace)
|
|
|
|
if !empty(l:handlers)
|
|
|
|
for l:writeAction in items(self.writeActions[l:namespace])
|
|
|
|
if !empty(l:writeAction[1])
|
|
|
|
if has_key(l:handlers,l:writeAction[0])
|
|
|
|
call l:handlers[l:writeAction[0]].handle(l:writeAction[1],self)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
call self.generateWriteActionsFromTemplate()
|
2013-12-25 13:33:12 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Show the terminal buffer that gets it's content from the debugger's
|
|
|
|
"interactive shell
|
2014-05-14 14:52:55 -04:00
|
|
|
function! s:f_debugger.showTerminalBuffer() dict
|
|
|
|
if has_key(self,'terminalBuffer')
|
|
|
|
if -1<bufwinnr(self.terminalBuffer)
|
2013-12-25 13:33:12 -05:00
|
|
|
return
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
new
|
|
|
|
setlocal buftype=nofile
|
|
|
|
setlocal bufhidden=wipe
|
2014-05-14 14:52:55 -04:00
|
|
|
let self.terminalBuffer=bufnr('')
|
2014-05-16 10:42:22 -04:00
|
|
|
silent file Vebugger:Ternimal
|
2013-12-25 13:33:12 -05:00
|
|
|
wincmd p
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Close the terminal buffer
|
2014-05-14 14:52:55 -04:00
|
|
|
function! s:f_debugger.closeTerminalBuffer() dict
|
|
|
|
if has_key(self,'terminalBuffer')
|
|
|
|
if -1<bufwinnr(self.terminalBuffer)
|
|
|
|
let l:bufwin=bufwinnr(self.terminalBuffer)
|
2013-12-25 13:33:12 -05:00
|
|
|
exe l:bufwin.'wincmd w'
|
|
|
|
wincmd c
|
|
|
|
wincmd p
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Check if the terminal buffer associated with this debugger is currently open
|
2014-05-14 14:52:55 -04:00
|
|
|
function! s:f_debugger.isTerminalBufferOpen() dict
|
|
|
|
if has_key(self,'terminalBuffer')
|
|
|
|
if -1<bufwinnr(self.terminalBuffer)
|
2013-12-27 17:08:42 -05:00
|
|
|
return 1
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
return 0
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Turn on and off the terminal buffer associated with this debugger
|
2014-05-14 14:52:55 -04:00
|
|
|
function! s:f_debugger.toggleTerminalBuffer() dict
|
|
|
|
if self.isTerminalBufferOpen()
|
|
|
|
call self.closeTerminalBuffer()
|
2013-12-27 17:08:42 -05:00
|
|
|
else
|
2014-05-14 14:52:55 -04:00
|
|
|
call self.showTerminalBuffer()
|
2013-12-27 17:08:42 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Write a line to the terminal buffer. This function does not process the line
|
2014-05-14 14:52:55 -04:00
|
|
|
function! s:f_debugger.addLineToTerminal(pipeName,line) dict
|
|
|
|
if has_key(self,'terminalBuffer')
|
|
|
|
let l:bufwin=bufwinnr(self.terminalBuffer)
|
2013-12-25 13:33:12 -05:00
|
|
|
if -1<l:bufwin
|
|
|
|
exe l:bufwin.'wincmd w'
|
2014-05-16 10:42:22 -04:00
|
|
|
if has_key(self,'pipes')
|
|
|
|
\&&has_key(self.pipes,a:pipeName)
|
|
|
|
\&&has_key(self.pipes[a:pipeName],'annotation')
|
|
|
|
call append (line('$'),(self.pipes[a:pipeName].annotation).(a:line))
|
2013-12-25 13:33:12 -05:00
|
|
|
else
|
2014-05-16 10:42:22 -04:00
|
|
|
call append (line('$'),a:line)
|
2013-12-25 13:33:12 -05:00
|
|
|
endif
|
|
|
|
normal G
|
|
|
|
wincmd p
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Add an handler to a handler list
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:addHandler(list,handler)
|
|
|
|
if type(a:handler) == type({})
|
|
|
|
call add(a:list,a:handler)
|
|
|
|
elseif type(a:handler) == type(function('tr'))
|
|
|
|
call add(a:list,{'handle':a:handler})
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Set a named handler in a handler dictionary
|
2013-12-27 16:52:57 -05:00
|
|
|
function! s:setHandler(dict,namespace,name,handler)
|
|
|
|
if !has_key(a:dict,a:namespace)
|
|
|
|
let a:dict[a:namespace]={}
|
|
|
|
endif
|
|
|
|
if type(a:handler) == type({})
|
|
|
|
let a:dict[a:namespace][a:name]=a:handler
|
|
|
|
elseif type(a:handler) == type(function('tr'))
|
|
|
|
let a:dict[a:namespace][a:name]={'handle':a:handler}
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Add a read handler. Read handlers process output from the debugger's
|
|
|
|
"interactive shell and modify read result objects with structured information
|
|
|
|
"parsed from those lines
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:f_debugger.addReadHandler(handler) dict
|
|
|
|
call s:addHandler(self.readHandlers,a:handler)
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Add a think handler. Think handlers are debugger agnostic - they look at
|
|
|
|
"read result objects and decide what to do with them.
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:f_debugger.addThinkHandler(handler) dict
|
|
|
|
call s:addHandler(self.thinkHandlers,a:handler)
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Set a write handler. Write handlers get write action objects and convert them
|
|
|
|
"to debugger specific commands. A write action can only handle a write action
|
|
|
|
"of the namespace and name it is registered for, to prevent the same write
|
|
|
|
"action handled by multiple write handlers.
|
2013-12-27 16:52:57 -05:00
|
|
|
function! s:f_debugger.setWriteHandler(namespace,name,handler) dict
|
|
|
|
call s:setHandler(self.writeHandlers,a:namespace,a:name,a:handler)
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Add a close handler. Close handlers are called when the debugger is closed to
|
|
|
|
"tidy things up.
|
2013-12-25 13:33:12 -05:00
|
|
|
function! s:f_debugger.addCloseHandler(handler) dict
|
|
|
|
call s:addHandler(self.closeHandlers,a:handler)
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Create an empty write action that follows the write actions template. That
|
|
|
|
"action will later be filled by think handlers or from outside.
|
2013-12-27 16:52:57 -05:00
|
|
|
function! s:f_debugger.generateWriteActionsFromTemplate() dict
|
|
|
|
let self.writeActions=deepcopy(self.writeActionsTemplate)
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Set a write action of a specific namespace and name, for write actions that
|
|
|
|
"do not support a list
|
2013-12-27 16:52:57 -05:00
|
|
|
function! s:f_debugger.setWriteAction(namespace,name,value) dict
|
|
|
|
let self.writeActions[a:namespace][a:name]=a:value
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Add a write action of a specific namespace and name, for write actions that supports a list
|
2013-12-28 15:23:33 -05:00
|
|
|
function! s:f_debugger.addWriteAction(namespace,name,value) dict
|
|
|
|
call add(self.writeActions[a:namespace][a:name],a:value)
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Create a bare debugger object from a raw shell line
|
2013-12-25 13:33:12 -05:00
|
|
|
function! vebugger#createDebugger(command)
|
|
|
|
|
|
|
|
let l:debugger=deepcopy(s:f_debugger)
|
|
|
|
|
2014-05-14 06:14:34 -04:00
|
|
|
let l:debugger.shell=vimproc#ptyopen(a:command,3)
|
2014-05-16 10:42:22 -04:00
|
|
|
|
2013-12-23 16:37:18 -05:00
|
|
|
let l:debugger.outBuffer=''
|
|
|
|
let l:debugger.errBuffer=''
|
|
|
|
|
|
|
|
let l:debugger.pipes={
|
|
|
|
\'out':{'pipe':(l:debugger.shell.stdout),'buffer':''},
|
2014-05-16 10:42:22 -04:00
|
|
|
\'err':{'pipe':(l:debugger.shell.stderr),'buffer':'','annotation':"err:\t\t"}}
|
2013-12-23 16:37:18 -05:00
|
|
|
|
2013-12-25 13:33:12 -05:00
|
|
|
let l:debugger.readResultTemplate={}
|
|
|
|
let l:debugger.state={}
|
2013-12-27 16:52:57 -05:00
|
|
|
let l:debugger.writeActionsTemplate={}
|
2013-12-25 13:33:12 -05:00
|
|
|
|
2013-12-24 18:21:41 -05:00
|
|
|
let l:debugger.readHandlers=[]
|
|
|
|
let l:debugger.thinkHandlers=[]
|
|
|
|
let l:debugger.writeHandlers={}
|
2013-12-25 13:33:12 -05:00
|
|
|
let l:debugger.closeHandlers=[]
|
2013-12-24 18:21:41 -05:00
|
|
|
|
|
|
|
let l:debugger.prevUpdateTime=&updatetime
|
|
|
|
|
|
|
|
set updatetime=500
|
2013-12-23 16:37:18 -05:00
|
|
|
return l:debugger
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Create a debugger and set it as the currently active debugger
|
2013-12-23 16:37:18 -05:00
|
|
|
function! vebugger#startDebugger(command)
|
|
|
|
call vebugger#killDebugger()
|
|
|
|
|
2013-12-25 13:33:12 -05:00
|
|
|
let s:debugger=vebugger#createDebugger(a:command)
|
2013-12-23 16:37:18 -05:00
|
|
|
|
2013-12-25 13:33:12 -05:00
|
|
|
augroup vebugger_shell
|
2013-12-23 16:37:18 -05:00
|
|
|
autocmd!
|
|
|
|
autocmd CursorHold * call s:debugger.invokeReading()
|
|
|
|
augroup END
|
2013-12-25 13:33:12 -05:00
|
|
|
|
|
|
|
return s:debugger
|
2013-12-23 16:37:18 -05:00
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Terminate the currently active debugger
|
2013-12-23 16:37:18 -05:00
|
|
|
function! vebugger#killDebugger()
|
2013-12-25 13:33:12 -05:00
|
|
|
augroup vebugger_shell
|
2013-12-24 18:21:41 -05:00
|
|
|
autocmd!
|
|
|
|
augroup END
|
2013-12-23 16:37:18 -05:00
|
|
|
if exists('s:debugger')
|
2014-05-14 14:52:55 -04:00
|
|
|
call s:debugger.closeTerminalBuffer()
|
2013-12-25 13:33:12 -05:00
|
|
|
call s:debugger.kill()
|
2013-12-23 16:37:18 -05:00
|
|
|
unlet s:debugger
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Write a line to the currently active debugger
|
2013-12-23 16:37:18 -05:00
|
|
|
function! vebugger#writeLine(line)
|
|
|
|
if exists('s:debugger')
|
|
|
|
call s:debugger.writeLine(a:line)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Invoke reading for the currently active debugger
|
2013-12-23 16:37:18 -05:00
|
|
|
function! vebugger#invokeReading()
|
|
|
|
if exists('s:debugger')
|
|
|
|
call s:debugger.invokeReading()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Toggle the terminal buffer for the currently active debugger
|
2014-05-14 14:52:55 -04:00
|
|
|
function! vebugger#toggleTerminalBuffer()
|
2013-12-24 18:21:41 -05:00
|
|
|
if exists('s:debugger')
|
2014-05-14 14:52:55 -04:00
|
|
|
call s:debugger.toggleTerminalBuffer()
|
2013-12-24 18:21:41 -05:00
|
|
|
endif
|
2013-12-23 16:37:18 -05:00
|
|
|
endfunction
|
2013-12-27 16:09:58 -05:00
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Fetch the currently active debugger object
|
2013-12-27 16:09:58 -05:00
|
|
|
function! vebugger#getActiveDebugger()
|
2013-12-28 15:23:33 -05:00
|
|
|
if exists('s:debugger')
|
|
|
|
return s:debugger
|
|
|
|
else
|
|
|
|
return {}
|
|
|
|
endif
|
2013-12-27 16:09:58 -05:00
|
|
|
endfunction
|
2013-12-27 16:52:57 -05:00
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Set a write action for the currently active debugger
|
2013-12-27 16:52:57 -05:00
|
|
|
function! vebugger#setWriteAction(namespace,name,value)
|
|
|
|
if exists('s:debugger')
|
|
|
|
call s:debugger.setWriteAction(a:namespace,a:name,a:value)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Add a write action to the currently active debugger
|
2013-12-28 15:23:33 -05:00
|
|
|
function! vebugger#addWriteAction(namespace,name,value)
|
|
|
|
if exists('s:debugger')
|
|
|
|
call s:debugger.addWriteAction(a:namespace,a:name,a:value)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Force performing all the write of the currently active debugger
|
2013-12-27 16:52:57 -05:00
|
|
|
function! vebugger#performWriteActions()
|
|
|
|
if exists('s:debugger')
|
|
|
|
call s:debugger.performWriteActions()
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Set a write action for the currently active debugger and perform it
|
2013-12-27 16:52:57 -05:00
|
|
|
function! vebugger#setWriteActionAndPerform(namespace,name,value)
|
|
|
|
call vebugger#setWriteAction(a:namespace,a:name,a:value)
|
|
|
|
call vebugger#performWriteActions()
|
|
|
|
endfunction
|
2013-12-28 15:23:33 -05:00
|
|
|
|
2014-05-17 15:43:18 -04:00
|
|
|
"Add a write action to the currently active debugger and perform it
|
2013-12-28 15:23:33 -05:00
|
|
|
function! vebugger#addWriteActionAndPerform(namespace,name,value)
|
|
|
|
call vebugger#addWriteAction(a:namespace,a:name,a:value)
|
|
|
|
call vebugger#performWriteActions()
|
|
|
|
endfunction
|