Permalink
Browse files

Refactor UI class to not call vim module directly

  • Loading branch information...
1 parent 341c205 commit 940d8d4a9a349137784826f6db5727cda4e4b3a9 @joonty committed Apr 6, 2013
Showing with 139 additions and 37 deletions.
  1. +49 −4 plugin/python/vdebug/ui/vimui/api.py
  2. +8 −29 plugin/python/vdebug/ui/vimui/ui.py
  3. +82 −4 tests/test_ui_vimui_api.py
@@ -25,22 +25,67 @@ def current_cursor_row(cls):
def current_line(cls):
return cls.line_at_row(cls.current_cursor_row())
+ @classmethod
+ def is_buffer_modified(cls):
+ return int(cls.vim.eval('&mod'))
+
""" Other getter methods """
@classmethod
def line_at_row(cls, row):
return cls.vim.eval("getline(%s)" % str(row))
+ @classmethod
+ def signs_of_type(cls, type):
+ sign_lines = cls.command('sign place').split("\n")
+ positions = {}
+ try:
+ for line in sign_lines:
+ if "name="+type in line:
+ attributes = line.strip().split()
+ lineinfo = attributes[0].split('=')
+ idinfo = attributes[1].split('=')
+ positions[idinfo[1]] = lineinfo[1]
+ except:
+ pass
+ return positions
+
+ """ Buffer/tab modification """
+
+ @classmethod
+ def close_tab(cls, tab_number):
+ cls.vim.command('silent! %stabc!' % str(tab_number))
+
+ @classmethod
+ def change_tab(cls, tab_number):
+ cls.vim.command('tabn %s' % str(tab_number))
- """ Setter/creation methods """
+ """ Command methods """
+
+ @classmethod
+ def error(cls, err):
+ cls.vim.command('echohl Error | echo "%s" | echohl None'\
+ % str(err).replace('"','\\"'))
+
+ @classmethod
+ def command(cls, cmd):
+ cls.vim.command('redir => _vdebug')
+ cls.vim.command(cmd)
+ cls.vim.command('redir END')
+ return cls.vim.eval('_vdebug')
@classmethod
def create_tab(cls, buffer_name):
cls.vim.command("silent tabnew %s" % buffer_name)
return cls
@classmethod
- def place_breakpoint_sign(cls, sign_id, file, line):
- cls.vim.command("sign place %s name=breakpt line=%s file=%s"\
- %(str(sign_id), str(line), file))
+ def place_sign(cls, sign_id, type, file, line):
+ cls.vim.command("sign place %s name=%s line=%s file=%s"\
+ %(str(sign_id), type, str(line), file))
+ return cls
+
+ @classmethod
+ def remove_sign(cls, sign_id):
+ cls.vim.command("sign unplace %s" % str(sign_id))
return cls
@@ -20,11 +20,7 @@ def __init__(self,breakpoints):
self.tabnr = None
def is_modified(self):
- modified = int(vim.eval('&mod'))v
- if modified:
- return True
- else:
- return False
+ Vim.is_buffer_modified()
def open(self):
if self.is_open:
@@ -118,37 +114,24 @@ def place_breakpoint(self,sign_id,file,line):
def remove_breakpoint(self,breakpoint):
id = breakpoint.id
- vim.command('sign unplace %i' % id)
+ Vim.remove_breakpoint_sign(id)
if self.breakpointwin.is_open:
self.breakpointwin.remove_breakpoint(id)
def get_breakpoint_sign_positions(self):
- sign_lines = self.command('sign place').split("\n")
- positions = {}
- for line in sign_lines:
- if "name=breakpt" in line:
- attributes = line.strip().split()
- lineinfo = attributes[0].split('=')
- idinfo = attributes[1].split('=')
- positions[idinfo[1]] = lineinfo[1]
- return positions
+ return Vim.signs_of_type('breakpt')
# Execute a vim command and return the output.
def command(self,cmd):
- vim.command('redir => _tmp')
- vim.command('silent %s' % cmd)
- vim.command('redir END')
- return vim.eval('_tmp')
+ return Vim.command(cmd)
def say(self,string):
""" Vim picks up Python prints, so just print """
print str(string)
vdebug.log.Log(string,vdebug.log.Logger.INFO)
def error(self,string):
- vim.command('echohl Error | echo "'+\
- str(string).replace('"','\\"')+\
- '" | echohl None')
+ Vim.error(string)
vdebug.log.Log(string,vdebug.log.Logger.ERROR)
def close(self):
@@ -165,17 +148,17 @@ def close(self):
vdebug.log.Log.remove_logger('WindowLogger')
if self.tabnr:
- vim.command('silent! '+self.tabnr+'tabc!')
+ Vim.close_tab(self.tabnr)
if self.current_tab:
- vim.command('tabn '+self.current_tab)
+ Vim.change_tab(self.current_tab)
self.watchwin = None
self.stackwin = None
self.statuswin = None
def __get_srcwin_name(self):
- return vim.current.buffer.name
+ return Vim.current_buffer_name()
def __get_srcwinno_by_name(self,name):
i = 1
@@ -192,7 +175,3 @@ def __get_srcwinno_by_name(self,name):
vdebug.log.Log("Returning window number %d" % i,\
vdebug.log.Logger.INFO)
return i
-
- def __get_buf_list(self):
- return vim.eval("range(1, bufnr('$'))")
-
View
@@ -72,12 +72,90 @@ def test_line_at_row_eval(self):
Vim.line_at_row(10)
self.module.eval.assert_called_with("getline(10)")
- def test_place_breakpoint_sign_command(self):
- Vim.place_breakpoint_sign(110, 'the_file', 30)
+ def test_place_sign_command(self):
+ Vim.place_sign(110, 'breakpt', 'the_file', 30)
self.module.command.assert_called_with(\
'sign place 110 name=breakpt line=30 file=the_file')
- def test_place_breakpoint_sign_returns_class(self):
- retval = Vim.place_breakpoint_sign(110, 'the_file', 30)
+ def test_place_sign_returns_class(self):
+ retval = Vim.place_sign(110, 'breakpt', 'the_file', 30)
self.assertIs(Vim, retval)
+
+ def test_remove_sign_command(self):
+ Vim.remove_sign(110)
+
+ self.module.command.assert_called_with('sign unplace 110')
+
+ def test_remove_sign_command_with_string_arg(self):
+ Vim.remove_sign("150")
+
+ self.module.command.assert_called_with('sign unplace 150')
+
+ def test_signs_of_type_breakpt(self):
+ self.module.eval.return_value = """ --- Signs ---
+Signs for plugin/python/vdebug/ui/vimui/ui.py:
+ line=23 id=5009 name=SyntasticError
+Signs for plugin/python/vdebug/ui/vimui/api.py:
+ line=30 id=11000 name=breakpt
+ line=38 id=11001 name=breakpt
+Signs for tests/test_ui_vimui_api.py:
+ line=96 id=5010 name=SyntasticError"""
+
+ signs = Vim.signs_of_type('breakpt')
+ self.assertEqual('30', signs['11000'])
+ self.assertEqual('38', signs['11001'])
+
+ def test_signs_of_type_syntastic(self):
+ self.module.eval.return_value = """ --- Signs ---
+Signs for plugin/python/vdebug/ui/vimui/ui.py:
+ line=23 id=5009 name=SyntasticError
+Signs for plugin/python/vdebug/ui/vimui/api.py:
+ line=30 id=11000 name=breakpt
+ line=38 id=11001 name=breakpt
+Signs for tests/test_ui_vimui_api.py:
+ line=96 id=5010 name=SyntasticError"""
+
+ signs = Vim.signs_of_type('SyntasticError')
+ self.assertEqual('23', signs['5009'])
+ self.assertEqual('96', signs['5010'])
+
+ def test_signs_of_type_command(self):
+ Vim.signs_of_type('blargh')
+ self.module.command.assert_any_call('sign place')
+ self.module.eval.assert_called_with('_vdebug')
+
+ def test_command_calls_commands(self):
+ Vim.command('blargh')
+ self.module.command.assert_any_call('redir => _vdebug')
+ self.module.command.assert_any_call('blargh')
+ self.module.command.assert_any_call('redir END')
+ self.module.eval.assert_called_with('_vdebug')
+
+ def test_error(self):
+ Vim.error('this is an error')
+ self.module.command.assert_called_with(\
+ 'echohl Error | echo "this is an error" | echohl None')
+
+ def test_error_handles_quotes(self):
+ Vim.error('this is an "error"')
+ self.module.command.assert_called_with(\
+ 'echohl Error | echo "this is an \\"error\\"" | echohl None')
+
+ def test_close_tab(self):
+ Vim.close_tab(3)
+ self.module.command.assert_called_with('silent! 3tabc!')
+
+ def test_change_tab(self):
+ Vim.change_tab(2)
+ self.module.command.assert_called_with('tabn 2')
+
+ def test_is_buffer_modified_return(self):
+ self.module.eval.return_value = "1"
+ self.assertEqual(True, Vim.is_buffer_modified())
+
+ def test_is_buffer_modified_eval(self):
+ self.module.eval.return_value = 0
+ ret = Vim.is_buffer_modified()
+ self.module.eval.assert_called_with('&mod')
+ self.assertEqual(False, ret)

0 comments on commit 940d8d4

Please sign in to comment.