@@ -14,20 +14,19 @@ noremap <leader>e :call ruby_debugger#load_debugger() <bar> call g:RubyDebugger
1414noremap <leader> d :call ruby_debugger#load_debugger() <bar> call g:RubyDebugger.remove_breakpoints()<CR>
1515
1616command ! -nargs =? -complete =file Rdebugger call ruby_debugger#load_debugger () | call g: RubyDebugger .start (<q-args> )
17- command ! -nargs =0 RdbStop call g: RubyDebugger .stop ()
18- command ! -nargs =1 RdbCommand call g: RubyDebugger .send_command (<q-args> )
19- command ! -nargs =0 RdbTest call g: RubyDebugger .run_test ()
20- command ! -nargs =1 RdbEval call g: RubyDebugger .eval (<q-args> )
21- command ! -nargs =1 RdbCond call g: RubyDebugger .conditional_breakpoint (<q-args> )
22- command ! -nargs =1 RdbCatch call g: RubyDebugger .catch_exception (<q-args> )
17+ command ! -nargs =0 RdbStop call ruby_debugger#load_debugger () | call g: RubyDebugger .stop ()
18+ command ! -nargs =1 RdbCommand call ruby_debugger#load_debugger () | call g: RubyDebugger .send_command (<q-args> )
19+ command ! -nargs =0 RdbTest call ruby_debugger#load_debugger () | call g: RubyDebugger .run_test ()
20+ command ! -nargs =1 RdbEval call ruby_debugger#load_debugger () | call g: RubyDebugger .eval (<q-args> )
21+ command ! -nargs =1 RdbCond call ruby_debugger#load_debugger () | call g: RubyDebugger .conditional_breakpoint (<q-args> )
22+ command ! -nargs =1 RdbCatch call ruby_debugger#load_debugger () | call g: RubyDebugger .catch_exception (<q-args> )
2323
2424let g: ruby_debugger_loaded = 1
2525
2626
2727" Init section - set default values, highlight colors
2828
2929let s: rdebug_port = 39767
30- let s: debugger_port = 39768
3130" hostname() returns something strange in Windows (E98BD9A419BB41D), so set hostname explicitly
3231let s: hostname = ' localhost' " hostname()
3332" ~/.vim for Linux, vimfiles for Windows
@@ -40,6 +39,7 @@ let s:server_output_file = s:runtime_dir . '/tmp/ruby_debugger_output'
4039let s: current_line_sign_id = 120
4140let s: separator = " ++vim-ruby-debugger separator++"
4241let s: sign_id = 0
42+ let s: default_script = ' script/server webrick'
4343
4444" Create tmp directory if it doesn't exist
4545if ! isdirectory (s: runtime_dir . ' /tmp' )
@@ -181,13 +181,11 @@ endfunction
181181" Send message to debugger. This function should never be used explicitly,
182182" only through g:RubyDebugger.send_command function
183183function ! s: send_message_to_debugger (message)
184-
185184ruby << RUBY
186-
187- VIM.evaluate (' a:message' ).split (VIM.evaluate (' s:separator' )).each { |msg| @r debug_ide.puts (msg) }
188-
185+ if @r debug_ide && @f orked_pid
186+ VIM.evaluate (' a:message' ).split (VIM.evaluate (' s:separator' )).each { |msg| @r debug_ide.puts (msg) }
187+ end
189188RUBY
190-
191189endfunction
192190
193191
@@ -358,8 +356,8 @@ let g:RubyDebugger.queue = s:Queue.new()
358356" Run debugger server. It takes one optional argument with path to debugged
359357" ruby script ('script/server webrick' by default)
360358function ! RubyDebugger.start (... ) dict
361- let g: RubyDebugger .server = s: Server .new (s: hostname , s: rdebug_port , s: debugger_port , s: runtime_dir , s: tmp_file , s: server_output_file )
362- let script = a: 0 && ! empty (a: 1 ) ? a: 1 : ' script/server webrick '
359+ let g: RubyDebugger .server = s: Server .new (s: hostname , s: rdebug_port , s: server_output_file , s: tmp_file , v: servername , g: ruby_debugger_progname , s: separator )
360+ let script = a: 0 && ! empty (a: 1 ) ? a: 1 : s: default_script
363361 echo " Loading debugger..."
364362 call g: RubyDebugger .server.start (script )
365363
@@ -1777,148 +1775,146 @@ let s:Server = {}
17771775" ** Public methods
17781776
17791777" Constructor of new server. Just inits it, not runs
1780- function ! s: Server .new (hostname , rdebug_port, debugger_port, runtime_dir, tmp_file, output_file ) dict
1778+ function ! s: Server .new (host, port, output_file, tmp_file, servername, progname, separator ) dict
17811779 let var = copy (self )
1782- let var .hostname = a: hostname
1783- let var .rdebug_port = a: rdebug_port
1784- let var .runtime_dir = a: runtime_dir
1780+ let var .host = a: host
1781+ let var .port = a: port
17851782 let var .output_file = a: output_file
1783+ let var .tmp_file = a: tmp_file
1784+ let var .servername = a: servername
1785+ let var .progname = a: progname
1786+ let var .separator = a: separator
17861787 return var
17871788endfunction
17881789
17891790
17901791" Start the server. It will kill any listeners on given ports before.
17911792function ! s: Server .start (script ) dict
1792- call self ._stop_server (self .rdebug_port)
1793+ if self ._check_already_running_server (self .port)
1794+ echo " Another Vim instance has running rdebug-ide. Please stop it there (or kill 'zombie' rdebug-ide process if there is no running VIM)"
1795+ return 0
1796+ endif
1797+ call self ._stop_server ()
17931798 " Remove leading and trailing quotes
17941799 let script_name = substitute (a: script , " \\ (^['\" ]\\ |['\" ]$\\ )" , ' ' , ' g' )
1795- let rdebug = ' rdebug-ide -p ' . self .rdebug_port . ' -- ' . script_name
1800+ let rdebug_ide = ' rdebug-ide -p ' . self .port . ' -- ' . script_name
17961801
17971802 " Start in background
17981803 if has (" win32" ) || has (" win64" )
1799- silent exe ' ! start ' . rdebug
1800- let debugger = ' ruby "' . expand (self .runtime_dir . " /bin/ruby_debugger.rb" ) . ' "' . debugger_parameters
1801- silent exe ' ! start ' . debugger
1804+ " silent exe '! start ' . rdebug
1805+ " let debugger = 'ruby "' . expand(self.runtime_dir . "/bin/ruby_debugger.rb") . '"' . debugger_parameters
1806+ " silent exe '! start ' . debugger
18021807 else
1803- call system (rdebug . ' > ' . self .output_file . ' 2>&1 &' )
1804- call self .start_debugger (self .hostname , self .rdebug_port )
1808+ call system (rdebug_ide . ' > ' . self .output_file . ' 2>&1 &' )
1809+ call self .start_debugger (self .host , self .port, self .tmp_file, self .servername, self .progname, self .separator )
18051810 endif
1811+ autocmd VimLeavePre * :call g: RubyDebugger .stop ()
18061812
18071813 " Set PIDs of processes
1808- let self .rdebug_pid = self ._get_pid (self .rdebug_port , 1 )
1814+ let self .rdebug_pid = self ._get_pid (self .port , 1 )
18091815 call g: RubyDebugger .logger.put (" Start debugger" )
18101816endfunction
18111817
18121818
18131819" Kill servers and empty PIDs
18141820function ! s: Server .stop () dict
1815- call self ._kill_process ( self .rdebug_pid )
1821+ call self ._stop_server ( )
18161822 let self .rdebug_pid = " "
18171823endfunction
18181824
18191825
1820- function ! s: Server .start_debugger (hostname , rdebug_port ) dict
1826+ function ! s: Server .start_debugger (host, port, tmp_file, servername, progname, separator ) dict
18211827ruby << RUBY
18221828
1823- require ' socket'
1824-
1825- host = VIM.evaluate (' a:hostname ' )
1826- port = VIM.evaluate (' a:rdebug_port ' )
1827- @s erver_name = VIM.evaluate (' v :servername' )
1828- @p rogname = VIM.evaluate (' g:ruby_debugger_progname ' )
1829- @t mp_file = VIM.evaluate (' s:tmp_file ' )
1830- @s eparator = VIM. evaluate ( ' s:separator ' )
1831-
1832- def wait_for_opened_socket (host, port)
1833- attempts = 0
1834- begin
1835- TCPSocket. open (host, port)
1836- rescue Errno::ECONNREFUSED = > msg
1837- attempts += 1
1838- # If socket wasn' t be opened for 20 seconds, exit
1839- if attempts < 400
1840- sleep 0.05
1841- retry
1842- else
1843- raise Errno::ECONNREFUSED, " #{host}:#{port} wasn't be opened "
1829+ require ' socket'
1830+ host = VIM. evaluate ( ' a:host ' )
1831+ port = VIM.evaluate (' a:port ' )
1832+ @t mp_file = VIM.evaluate (' a:tmp_file ' )
1833+ @s erver_name = VIM.evaluate (' a :servername' )
1834+ @p rogname = VIM.evaluate (' a:progname ' )
1835+ @s eparator = VIM.evaluate (' a:separator ' )
1836+
1837+ def wait_for_opened_socket (host, port)
1838+ attempts = 0
1839+ begin
1840+ TCPSocket. open (host, port)
1841+ rescue Errno::ECONNREFUSED = > msg
1842+ attempts += 1
1843+ # If socket wasn' t be opened for 20 seconds, exit
1844+ if attempts < 400
1845+ sleep 0.05
1846+ retry
1847+ else
1848+ raise Errno::ECONNREFUSED, " #{host}:#{port} wasn't be opened "
1849+ end
18441850 end
18451851 end
1846- end
1847- @r debug_ide = wait_for_opened_socket (host, port)
1852+ @r debug_ide = wait_for_opened_socket (host, port)
18481853
1849- @f orked_pid = Process.fork do
1854+ @f orked_pid = Process.fork do
18501855
1851- Signal.trap (" TERM" ) { exit }
1856+ Signal.trap (" TERM" ) { exit }
18521857
1853- def read_rdebug_socket (response, rdebug, output = " " )
1854- if response && response[0 ] && response[0 ][0 ]
1855- output += response[0 ][0 ].recv (10000 )
1856- if have_unclosed_tag?(output)
1857- # If rdebug- ide doesn't send full message, we should wait for rest parts too.
1858- # We can understand that this is just part of message by matching unclosed tags
1859- another_response = select ([rdebug], nil, nil)
1860- else
1861- # Sometimes by some reason rdebug- ide sends blank strings just after main message.
1862- # We need to remove these strings by receiving them
1863- another_response = select ([rdebug], nil, nil, 0.01 )
1864- end
1865- if another_response && another_response[0 ] && another_response[0 ][0 ]
1866- output = read_rdebug_socket (another_response, rdebug, output)
1858+ def read_rdebug_socket (response, rdebug, output = " " )
1859+ if response && response[0 ] && response[0 ][0 ]
1860+ output += response[0 ][0 ].recv (10000 )
1861+ if have_unclosed_tag?(output)
1862+ # If rdebug- ide doesn't send full message, we should wait for rest parts too.
1863+ # We can understand that this is just part of message by matching unclosed tags
1864+ another_response = select ([rdebug], nil, nil)
1865+ else
1866+ # Sometimes by some reason rdebug- ide sends blank strings just after main message.
1867+ # We need to remove these strings by receiving them
1868+ another_response = select ([rdebug], nil, nil, 0.01 )
1869+ end
1870+ if another_response && another_response[0 ] && another_response[0 ][0 ]
1871+ output = read_rdebug_socket (another_response, rdebug, output)
1872+ end
18671873 end
1874+ output
18681875 end
1869- output
1870- end
18711876
1872- def have_unclosed_tag?(output)
1873- start_match = output.match (/^<([a-zA-Z0-9\-_]+)>/ )
1874- if start_match
1875- end_match = output.match (/<\/#{start_match[1]}>$/ )
1876- return end_match ? false : true
1877- else
1878- return false
1877+ def have_unclosed_tag?(output)
1878+ start_match = output.match (/^<([a-zA-Z0-9\-_]+)>/ )
1879+ if start_match
1880+ end_match = output.match (/<\/#{start_match[1]}>$/ )
1881+ return end_match ? false : true
1882+ else
1883+ return false
1884+ end
18791885 end
1880- end
18811886
1882- loop do
1883- response = select ([@r debug_ide], nil, nil)
1884- result = []
1885- result << read_rdebug_socket (response, @r debug_ide)
1886- # If we stop at breakpoint, add taking of local variables into queue
1887- stop_commands = [ ' <breakpoint ' , ' <suspended ' , ' <exception ' ]
1888- if stop_commands.any? { |c | result.first .include ?(c ) }
1889- @r debug_ide.puts (" var local" )
1887+ loop do
18901888 response = select ([@r debug_ide], nil, nil)
1889+ result = []
18911890 result << read_rdebug_socket (response, @r debug_ide)
1892- @r debug_ide.puts (" where" )
1893- response = select ([@r debug_ide], nil, nil)
1894- result << read_rdebug_socket (response, @r debug_ide)
1895- end
1896- message = result.join (@s eparator)
1897- if message && ! message.empty ?
1898- File.open (@t mp_file, ' w' ) { |f | f .puts (message) }
1899- command = " :call RubyDebugger.receive_command()"
1900- starter = " <C-\\\\ >"
1901- system (" #{@progname} --servername #{@server_name} -u NONE -U NONE --remote-send \" #{starter}<C-N>#{command}<CR>\" " )
1891+ # If we stop at breakpoint, add taking of local variables into queue
1892+ stop_commands = [ ' <breakpoint ' , ' <suspended ' , ' <exception ' ]
1893+ if stop_commands.any? { |c | result.first .include ?(c ) }
1894+ @r debug_ide.puts (" var local" )
1895+ response = select ([@r debug_ide], nil, nil)
1896+ result << read_rdebug_socket (response, @r debug_ide)
1897+ @r debug_ide.puts (" where" )
1898+ response = select ([@r debug_ide], nil, nil)
1899+ result << read_rdebug_socket (response, @r debug_ide)
1900+ end
1901+ message = result.join (@s eparator)
1902+ if message && ! message.empty ?
1903+ File.open (@t mp_file, ' w' ) { |f | f .puts (message) }
1904+ command = " :call RubyDebugger.receive_command()"
1905+ starter = " <C-\\\\ >"
1906+ system (" #{@progname} --servername #{@server_name} -u NONE -U NONE --remote-send \" #{starter}<C-N>#{command}<CR>\" " )
1907+ end
19021908 end
19031909 end
1904- end
19051910
1906- RUBY
1907-
1908- autocmd VimLeavePre * :call StopForkedRubyProcess ()
1909-
1910- endfunction
1911-
1912- function ! StopForkedRubyProcess ()
1913- ruby << RUBY
1914- Process.kill (" TERM" , @f orked_pid) if @f orked_pid
19151911RUBY
19161912endfunction
19171913
19181914
19191915" Return 1 if processes with set PID exist.
19201916function ! s: Server .is_running () dict
1921- return (self ._get_pid (self .rdebug_port , 0 ) = ~ ' ^\d\+$' )
1917+ return (self ._get_pid (self .port , 0 ) = ~ ' ^\d\+$' )
19221918endfunction
19231919
19241920
@@ -1956,8 +1952,40 @@ function! s:Server._get_pid_attempt(port)
19561952endfunction
19571953
19581954
1955+ function ! s: Server ._check_already_running_server (port)
1956+ let pid = self ._get_pid (a: port , 0 )
1957+ ruby << RUBY
1958+ if VIM.evaluate (' l:pid' ).to_i != 0 && ! @r debug_ide
1959+ VIM.command (' let result = 1' )
1960+ else
1961+ VIM.command (' let result = 0' )
1962+ end
1963+ RUBY
1964+ return result
1965+ endfunction
1966+
1967+
19591968" Kill listener of given host/port
1960- function ! s: Server ._stop_server (port) dict
1969+ function ! s: Server ._stop_server () dict
1970+ ruby << RUBY
1971+ if @f orked_pid
1972+ Process.kill (" TERM" , @f orked_pid)
1973+ VIM.command (" echo 'Forked process is stopped'" )
1974+ @f orked_pid = nil
1975+ end
1976+ if @r debug_ide
1977+ @r debug_ide.puts (" exit" )
1978+ @r debug_ide.flush
1979+ @r debug_ide.close
1980+ VIM.command (" echo 'Rdebug-ide is stopped'" )
1981+ @r debug_ide = nil
1982+ end
1983+ RUBY
1984+ endfunction
1985+
1986+
1987+ " Kill listener of given host/port
1988+ function ! s: Server ._kill_server (port) dict
19611989 let pid = self ._get_pid (a: port , 0 )
19621990 if pid = ~ ' ^\d\+$'
19631991 call self ._kill_process (pid)
@@ -1968,7 +1996,7 @@ endfunction
19681996" Kill process with given PID
19691997function ! s: Server ._kill_process (pid) dict
19701998 echo " Killing server with pid " . a: pid
1971- call system ( " ruby -e ' Process.kill(9, " . a: pid . " )' " )
1999+ ruby " Process.kill('KILL', VIM.evaluate(' a:pid')) "
19722000 sleep 100 m
19732001 call self ._log (" Killed server with pid: " . a: pid )
19742002endfunction
@@ -1986,9 +2014,6 @@ endfunction
19862014" *** Creating instances (start)
19872015
19882016" Initialize RUbyDebugger settings
1989- if ! exists (" g:ruby_debugger_fast_sender" )
1990- let g: ruby_debugger_fast_sender = 0
1991- endif
19922017if ! exists (" g:ruby_debugger_spec_path" )
19932018 let g: ruby_debugger_spec_path = ' /usr/bin/spec'
19942019endif
0 commit comments