changeset 35504:deb5a5c92cbd

runtime(termdebug): Refactored StartDebug_term and EndDebug functions Commit: https://github.com/vim/vim/commit/225d4d9212edb27cc4bd473db565517594aaeede Author: Ubaldo Tiberi <ubaldo.tiberi@gmail.com> Date: Sun Jun 23 17:25:05 2024 +0200 runtime(termdebug): Refactored StartDebug_term and EndDebug functions - Functions are way too long. Readability and maintainability should be slightly improved. - Some variables are re-assigned to their initial value at teardown. This should not be needed since all internal variables are re-initialized at startup of every Termdebug session. closes: #15086 Signed-off-by: Ubaldo Tiberi <ubaldo.tiberi@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Sun, 23 Jun 2024 17:30:05 +0200
parents ff0250ab0d8a
children ce6c9348573d
files runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
diffstat 1 files changed, 61 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
+++ b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim
@@ -86,7 +86,7 @@ var varbufnr: number
 var varbufname: string
 var asmbufnr: number
 var asmbufname: string
-var promptbuf: number
+var promptbufnr: number
 # This is for the "debugged-program" thing
 var ptybufnr: number
 var ptybufname: string
@@ -167,7 +167,7 @@ def InitScriptVariables()
   varbufname = 'Termdebug-variables-listing'
   asmbufnr = 0
   asmbufname = 'Termdebug-asm-listing'
-  promptbuf = 0
+  promptbufnr = 0
   # This is for the "debugged-program" thing
   ptybufname = "debugged-program"
   ptybufnr = 0
@@ -265,7 +265,7 @@ enddef
 
 # Define the default highlighting, using the current 'background' value.
 def InitHighlight()
-  Highlight(1, '', &background)
+  Highlight(true, '', &background)
   hi default debugBreakpoint term=reverse ctermbg=red guibg=red
   hi default debugBreakpointDisabled term=reverse ctermbg=gray guibg=gray
 enddef
@@ -309,9 +309,6 @@ def StartDebug_internal(dict: dict<any>)
     return
   endif
 
-  # Uncomment this line to write logging in "debuglog".
-  # call ch_logfile('debuglog', 'w')
-
   InitScriptVariables()
   if !SanityCheck()
     return
@@ -321,6 +318,9 @@ def StartDebug_internal(dict: dict<any>)
     doauto <nomodeline> User TermdebugStartPre
   endif
 
+  # Uncomment this line to write logging in "debuglog".
+  # call ch_logfile('debuglog', 'w')
+
   # Assume current window is the source code window
   sourcewin = win_getid()
   var wide = 0
@@ -368,38 +368,29 @@ enddef
 
 # Use when debugger didn't start or ended.
 def CloseBuffers()
-  var buf_numbers = [ptybufnr, commbufnr, asmbufnr, varbufnr]
+  var buf_numbers = [promptbufnr, ptybufnr, commbufnr, asmbufnr, varbufnr]
   for buf_nr in buf_numbers
     if buf_nr > 0 && bufexists(buf_nr)
       exe $'bwipe! {buf_nr}'
     endif
   endfor
-
-  running = false
-  gdbwin = 0
 enddef
 
 def IsGdbStarted(): bool
   var gdbproc_status = job_status(term_getjob(gdbbufnr))
   if gdbproc_status !=# 'run'
-    var cmd_name = string(GetCommand()[0])
-    Echoerr($'{cmd_name} exited unexpectedly')
-    CloseBuffers()
     return false
   endif
   return true
 enddef
 
-# Open a terminal window without a job, to run the debugged program in.
-def StartDebug_term(dict: dict<any>)
+def CreateProgramPty(): string
   ptybufnr = term_start('NONE', {
     term_name: ptybufname,
     vertical: vvertical})
   if ptybufnr == 0
-    Echoerr('Failed to open the program terminal window')
-    return
+    return null_string
   endif
-  var pty = job_info(term_getjob(ptybufnr))['tty_out']
   ptywin = win_getid()
 
   if vvertical
@@ -412,6 +403,10 @@ def StartDebug_term(dict: dict<any>)
     endif
   endif
 
+  return job_info(term_getjob(ptybufnr))['tty_out']
+enddef
+
+def CreateCommunicationPty(): string
   # Create a hidden terminal window to communicate with gdb
   commbufnr = term_start('NONE', {
     term_name: commbufname,
@@ -419,12 +414,12 @@ def StartDebug_term(dict: dict<any>)
     hidden: 1
   })
   if commbufnr == 0
-    Echoerr('Failed to open the communication terminal window')
-    exe $'bwipe! {ptybufnr}'
-    return
+    return null_string
   endif
-  var commpty = job_info(term_getjob(commbufnr))['tty_out']
+  return job_info(term_getjob(commbufnr))['tty_out']
+enddef
 
+def CreateGdbConsole(dict: dict<any>, pty: string, commpty: string): string
   # Start the gdb buffer
   var gdb_args = get(dict, 'gdb_args', [])
   var proc_args = get(dict, 'proc_args', [])
@@ -464,9 +459,7 @@ def StartDebug_term(dict: dict<any>)
         term_finish: 'close',
         })
   if gdbbufnr == 0
-    Echoerr('Failed to open the gdb terminal window')
-    CloseBuffers()
-    return
+    return 'Failed to open the gdb terminal window'
   endif
   gdbwin = win_getid()
 
@@ -480,8 +473,7 @@ def StartDebug_term(dict: dict<any>)
   var success = false
   while !success && counter < counter_max
     if !IsGdbStarted()
-      CloseBuffers()
-      return
+      return $'{gdbbufname} exited unexpectedly'
     endif
 
     for lnum in range(1, 200)
@@ -496,9 +488,7 @@ def StartDebug_term(dict: dict<any>)
   endwhile
 
   if !success
-    Echoerr('Failed to startup the gdb program.')
-    CloseBuffers()
-    return
+    return 'Failed to startup the gdb program.'
   endif
 
   # ---- gdb started. Next, let's set the MI interface. ---
@@ -518,7 +508,7 @@ def StartDebug_term(dict: dict<any>)
   success = false
   while !success && counter < counter_max
     if !IsGdbStarted()
-      return
+      return $'{gdbbufname} exited unexpectedly'
     endif
 
     var response = ''
@@ -529,10 +519,8 @@ def StartDebug_term(dict: dict<any>)
         # response can be in the same line or the next line
         response = $"{line1}{line2}"
         if response =~ 'Undefined command'
-          Echoerr('Sorry, your gdb is too old, gdb 7.12 is required')
           # CHECKME: possibly send a "server show version" here
-          CloseBuffers()
-          return
+          return 'Sorry, your gdb is too old, gdb 7.12 is required'
         endif
         if response =~ 'New UI allocated'
           # Success!
@@ -551,11 +539,37 @@ def StartDebug_term(dict: dict<any>)
   endwhile
 
   if !success
-    Echoerr('Cannot check if your gdb works, continuing anyway')
+    return 'Cannot check if your gdb works, continuing anyway'
+  endif
+  return ''
+enddef
+
+
+# Open a terminal window without a job, to run the debugged program in.
+def StartDebug_term(dict: dict<any>)
+
+  var programpty = CreateProgramPty()
+  if programpty is null_string
+    Echoerr('Failed to open the program terminal window')
+    CloseBuffers()
     return
   endif
 
-  job_setoptions(term_getjob(gdbbufnr), {exit_cb: function('EndTermDebug')})
+  var commpty = CreateCommunicationPty()
+  if commpty is null_string
+    Echoerr('Failed to open the communication terminal window')
+    CloseBuffers()
+    return
+  endif
+
+  var err_message = CreateGdbConsole(dict, programpty, commpty)
+  if !empty(err_message)
+    Echoerr(err_message)
+    CloseBuffers()
+    return
+  endif
+
+  job_setoptions(term_getjob(gdbbufnr), {exit_cb: function('EndDebug')})
 
   # Set the filetype, this can be used to add mappings.
   set filetype=termdebug
@@ -574,13 +588,13 @@ def StartDebug_prompt(dict: dict<any>)
     new
   endif
   gdbwin = win_getid()
-  promptbuf = bufnr('')
-  prompt_setprompt(promptbuf, 'gdb> ')
+  promptbufnr = bufnr('')
+  prompt_setprompt(promptbufnr, 'gdb> ')
   set buftype=prompt
   exe $"file {gdbbufname}"
 
-  prompt_setcallback(promptbuf, function('PromptCallback'))
-  prompt_setinterrupt(promptbuf, function('PromptInterrupt'))
+  prompt_setcallback(promptbufnr, function('PromptCallback'))
+  prompt_setinterrupt(promptbufnr, function('PromptInterrupt'))
 
   if vvertical
     # Assuming the source code window will get a signcolumn, use two more
@@ -607,15 +621,15 @@ def StartDebug_prompt(dict: dict<any>)
 
   ch_log($'executing "{join(gdb_cmd)}"')
   gdbjob = job_start(gdb_cmd, {
-    exit_cb: function('EndPromptDebug'),
+    exit_cb: function('EndDebug'),
     out_cb: function('GdbOutCallback'),
   })
   if job_status(gdbjob) != "run"
     Echoerr('Failed to start gdb')
-    exe $'bwipe! {promptbuf}'
+    exe $'bwipe! {promptbufnr}'
     return
   endif
-  exe $'au BufUnload <buffer={promptbuf}> ++once ' ..
+  exe $'au BufUnload <buffer={promptbufnr}> ++once ' ..
        'call job_stop(gdbjob, ''kill'')'
   # Mark the buffer modified so that it's not easy to close.
   set modified
@@ -852,8 +866,6 @@ def DecodeMessage(quotedText: string, li
         #\ Note: GDB docs also mention hex encodings - the translations below work
         #\       but we keep them out for performance-reasons until we actually see
         #\       those in mi-returns
-        #\ \ ->substitute('\\0x\(\x\x\)', {-> eval('"\x' .. submatch(1) .. '"')}, 'g')
-        #\ \ ->substitute('\\0x00', NullRepl, 'g')
         ->substitute('\\\\', '\', 'g')
         ->substitute(NullRepl, '\\000', 'g')
   if !literal
@@ -891,28 +903,17 @@ def GetAsmAddr(msg: string): string
   return addr
 enddef
 
-
-def EndTermDebug(job: any, status: any)
+def EndDebug(job: any, status: any)
   if exists('#User#TermdebugStopPre')
     doauto <nomodeline> User TermdebugStopPre
   endif
 
-  if commbufnr > 0 && bufexists(commbufnr)
-    exe $'bwipe! {commbufnr}'
+  if way is Way.Prompt
+    ch_log("Returning from EndDebug()")
   endif
-  gdbwin = 0
-  EndDebugCommon()
-enddef
 
-def EndDebugCommon()
   var curwinid = win_getid()
-  var buf_numbers = [ptybufnr, asmbufnr, varbufnr]
-  for buf_nr in buf_numbers
-    if buf_nr > 0 && bufexists(buf_nr)
-      exe $'bwipe! {buf_nr}'
-    endif
-  endfor
-  running = false
+  CloseBuffers()
 
   # Restore 'signcolumn' in all buffers for which it was set.
   win_gotoid(sourcewin)
@@ -954,21 +955,6 @@ def EndDebugCommon()
   g:termdebug_is_running = false
 enddef
 
-def EndPromptDebug(job: any, status: any)
-  if exists('#User#TermdebugStopPre')
-    doauto <nomodeline> User TermdebugStopPre
-  endif
-
-  if bufexists(promptbuf)
-    exe $'bwipe! {promptbuf}'
-  endif
-
-  EndDebugCommon()
-  gdbwin = 0
-  ch_log("Returning from EndPromptDebug()")
-enddef
-
-
 # Disassembly window - added by Michael Sartain
 #
 # - CommOutput: &"disassemble $pc\n"