Exemplo n.º 1
0
 def test_get_cursor(self):
     '''Test method `get_cursor`.'''
     api = Mock()
     api.eval.side_effect = \
         lambda x: [None, 3, 6, None] if x == 'getpos(".")' else None
     vim = VimSupport(api)
     self.assertEqual(vim.get_cursor(), Mark(3, 6))
Exemplo n.º 2
0
 def __init__(self):
     self._vim = VimSupport()
     self._sessions = {}
     self._session_views = {}
     self._tabpage_view = TabpageView(self._vim)
     self._worker = _ThreadExecutor()
     self._last_focused_bufnr = None
Exemplo n.º 3
0
 def test_del_match(self):
     '''Test method `del_match`.'''
     api = Mock()
     vim = VimSupport(api)
     vim.del_match([1, 2])
     self.assertListEqual(api.eval.call_args_list, [
         call('matchdelete(1)'),
         call('matchdelete(2)'),
     ])
Exemplo n.º 4
0
 def test_set_bufname_lines(self):
     '''Test method `set_bufname_lines`.'''
     buf1 = MagicMock()
     buf1.name = 'buf1'
     buf2 = MagicMock()
     buf2.name = 'buf2'
     api = Mock()
     api.buffers = [buf1, buf2]
     vim = VimSupport(api)
     vim.set_bufname_lines('buf2', ['a', 'b'])
     buf2.__setitem__.assert_called_once_with(
         slice(None, None, None), ['a', 'b'])
     buf1.__setitem__.assert_not_called()
Exemplo n.º 5
0
 def test_add_match(self):
     '''Test method `add_match`.'''
     def _eval(cmd):
         if cmd == 'matchaddpos("CoqStcSent", [[1, 5, 3], 2, [3, 1, 2]])':
             return 1
         return None
     api = Mock()
     api.eval.side_effect = _eval
     api.current.buffer = [
         'aaaa333',
         '32414',
         '33aaa']
     vim = VimSupport(api)
     self.assertEqual(vim.add_match(Mark(1, 5), Mark(3, 3), 'CoqStcSent'),
                      [1])
Exemplo n.º 6
0
    def test_in_winid(self):
        '''Test method `in_winid`.'''
        def _eval(cmd):
            if cmd == 'winsaveview()':
                return {'a': 1}
            elif cmd == 'winnr()':
                return '3'
            elif cmd == 'win_id2win(184)':
                return '1'
            elif cmd == 'winrestview({\'a\': 1})':
                return None
            raise AssertionError('bad cmd: {}'.format(cmd))

        api = Mock()
        api.eval.side_effect = _eval
        vim = VimSupport(api)
        done = False
        with vim.in_winid('184'):
            done = True
        self.assertTrue(done)
        self.assertListEqual(api.command.call_args_list, [
            call('1wincmd w'),
            call('3wincmd w'),
        ])
Exemplo n.º 7
0
 def test_get_sentence_after(self):
     '''Test method `get_sentence_after`.'''
     api = Mock()
     api.current.buffer = [
         'Proof. simpl.',
         '  reflexivity.',
         'Qed.']
     vim = VimSupport(api)
     sentence = vim.get_sentence_after(Mark(1, 1))
     self.assertEqual(sentence,
                      Sentence('Proof.', Mark(1, 1), Mark(1, 7)))
     sentence = vim.get_sentence_after(Mark(1, 7))
     self.assertEqual(sentence,
                      Sentence(' simpl.', Mark(1, 7), Mark(1, 14)))
     sentence = vim.get_sentence_after(Mark(1, 14))
     self.assertEqual(sentence,
                      Sentence('\n  reflexivity.', Mark(1, 14), Mark(2, 15)))
     sentence = vim.get_sentence_after(Mark(2, 15))
     self.assertEqual(sentence,
                      Sentence('\nQed.', Mark(2, 15), Mark(3, 5)))
Exemplo n.º 8
0
class Plugin:
    '''The plugin entry point.'''
    def __init__(self):
        self._vim = VimSupport()
        self._sessions = {}
        self._session_views = {}
        self._tabpage_view = TabpageView(self._vim)
        self._worker = _ThreadExecutor()
        self._last_focused_bufnr = None

    @_catch_exception
    @_draw_views
    def new_session(self):
        '''Create a new session on the current buffer.'''
        buf = self._vim.get_buffer()
        bufnr = buf.number
        if bufnr in self._sessions:
            print('Already in a Coq session')
            return
        logger.debug('Create session on buffer: %s [%s]', buf.name, bufnr)
        session_view = SessionView(bufnr, self._tabpage_view, self._vim)
        session = Session(session_view, self._vim, self._worker)
        self._sessions[bufnr] = session
        self._session_views[bufnr] = session_view

    @_catch_exception
    @_draw_views
    @_in_session
    def close_session(self, session, buf):
        '''Close the session on the current buffer.'''
        logger.debug('Close session on buffer: %s [%s]', buf.name, buf.number)
        session.close()
        del self._sessions[buf.number]
        del self._session_views[buf.number]

    @_catch_exception
    @_draw_views
    @_in_session
    @_not_busy
    def forward_one(self, session, buf):  # pylint: disable=R0201
        '''Forward one sentence.'''
        logger.debug('Session [%s]: forward one', buf.name)
        session.forward_one()

    @_catch_exception
    @_draw_views
    @_in_session
    @_not_busy
    def backward_one(self, session, buf):  # pylint: disable=R0201
        '''Backward one sentence.'''
        logger.debug('Session [%s]: backward one', buf.name)
        session.backward_one()

    @_catch_exception
    @_draw_views
    @_in_session
    @_not_busy
    def to_cursor(self, session, buf):  # pylint: disable=R0201
        '''Run to cursor.'''
        logger.debug('Session [%s]: to cursor', buf.name)
        session.to_cursor()

    @_catch_exception
    @_draw_views
    def redraw_goals(self):
        '''Redraw the goals to the goal window.'''
        logger.debug('Redraw goals')
        self._tabpage_view.redraw_goals()

    @_catch_exception
    @_draw_views
    def redraw_messages(self):
        '''Redraw the messages to the message window.'''
        logger.debug('Redraw messages')
        self._tabpage_view.redraw_messages()

    @_catch_exception
    @_draw_views
    @_not_busy
    def process_feedbacks(self):
        '''Process the feedbacks received by each session.'''
        for session in self._sessions.values():
            session.process_feedbacks()

    @_catch_exception
    @_draw_views
    @_in_session
    def focus(self, _, buf):
        '''Focus a session.'''
        if buf.number == self._last_focused_bufnr:
            return
        logger.debug('Session [%s]: focus', buf.name)
        if self._last_focused_bufnr is not None:
            last_view = self._session_views[self._last_focused_bufnr]
            last_view.unfocus()
        self._last_focused_bufnr = buf.number
        cur_view = self._session_views[self._last_focused_bufnr]
        cur_view.focus()

    @_catch_exception
    @_draw_views
    @_in_session
    def set_active(self, _, buf):  # pylint: disable=R0201
        '''Set the session active in a window.'''
        logger.debug('Session [%s]: set window active', buf.name)
        view = self._session_views[buf.number]
        view.set_active()

    @_catch_exception
    @_draw_views
    @_in_session
    def set_inactive(self, _, buf):  # pylint: disable=R0201
        '''Set the session inactive in a window.'''
        logger.debug('Session [%s]: set window inactive', buf.name)
        view = self._session_views[buf.number]
        view.set_inactive()

    @_catch_exception
    @_draw_views
    def clear_messages(self):
        '''Clear the messages in the message window.'''
        logger.debug('Clear messages')
        self._tabpage_view.clear_messages()

    def cleanup(self):
        '''Cleanup the plugin.'''
        logger.debug('Plugin clean up')
        for session in self._sessions.values():
            session.close()
        for view in self._session_views.values():
            view.destroy()
        self._sessions.clear()
        self._session_views.clear()
        self._worker.shutdown()

    def do_draw_views(self):
        '''Draw the changes of the views to Vim.'''
        self._tabpage_view.draw()
        for view in self._session_views.values():
            view.draw()
Exemplo n.º 9
0
 def test_get_buffer(self):
     '''Test method `get_buffer`.'''
     api = Mock()
     vim = VimSupport(api)
     self.assertEqual(vim.get_buffer(), api.current.buffer)