Add some new features to tests/vim.py
This commit is contained in:
parent
ebd122d4ac
commit
f10729f637
60
tests/vim.py
60
tests/vim.py
|
@ -1,10 +1,10 @@
|
||||||
# vim:fileencoding=utf-8:noet
|
# vim:fileencoding=utf-8:noet
|
||||||
_log = []
|
_log = []
|
||||||
_g = {}
|
vars = {}
|
||||||
_window = 0
|
_window = 0
|
||||||
_mode = 'n'
|
_mode = 'n'
|
||||||
_buf_purge_events = set()
|
_buf_purge_events = set()
|
||||||
_options = {
|
options = {
|
||||||
'paste': 0,
|
'paste': 0,
|
||||||
'ambiwidth': 'single',
|
'ambiwidth': 'single',
|
||||||
}
|
}
|
||||||
|
@ -151,7 +151,7 @@ def command(cmd):
|
||||||
if cmd.startswith('let g:'):
|
if cmd.startswith('let g:'):
|
||||||
import re
|
import re
|
||||||
varname, value = re.compile(r'^let g:(\w+)\s*=\s*(.*)').match(cmd).groups()
|
varname, value = re.compile(r'^let g:(\w+)\s*=\s*(.*)').match(cmd).groups()
|
||||||
_g[varname] = value
|
vars[varname] = value
|
||||||
elif cmd.startswith('hi '):
|
elif cmd.startswith('hi '):
|
||||||
sp = cmd.split()
|
sp = cmd.split()
|
||||||
_highlights[sp[1]] = sp[2:]
|
_highlights[sp[1]] = sp[2:]
|
||||||
|
@ -162,9 +162,9 @@ def command(cmd):
|
||||||
@_vim
|
@_vim
|
||||||
def eval(expr):
|
def eval(expr):
|
||||||
if expr.startswith('g:'):
|
if expr.startswith('g:'):
|
||||||
return _g[expr[2:]]
|
return vars[expr[2:]]
|
||||||
elif expr.startswith('&'):
|
elif expr.startswith('&'):
|
||||||
return _options[expr[1:]]
|
return options[expr[1:]]
|
||||||
elif expr.startswith('PowerlineRegisterCachePurgerEvent'):
|
elif expr.startswith('PowerlineRegisterCachePurgerEvent'):
|
||||||
_buf_purge_events.add(expr[expr.find('"') + 1:expr.rfind('"') - 1])
|
_buf_purge_events.add(expr[expr.find('"') + 1:expr.rfind('"') - 1])
|
||||||
return "0"
|
return "0"
|
||||||
|
@ -174,7 +174,7 @@ def eval(expr):
|
||||||
@_vim
|
@_vim
|
||||||
def bindeval(expr):
|
def bindeval(expr):
|
||||||
if expr == 'g:':
|
if expr == 'g:':
|
||||||
return _g
|
return vars
|
||||||
import re
|
import re
|
||||||
match = re.compile(r'^function\("([^"\\]+)"\)$').match(expr)
|
match = re.compile(r'^function\("([^"\\]+)"\)$').match(expr)
|
||||||
if match:
|
if match:
|
||||||
|
@ -201,10 +201,10 @@ def _emul_getbufvar(bufnr, varname):
|
||||||
if bufnr not in buffers:
|
if bufnr not in buffers:
|
||||||
return ''
|
return ''
|
||||||
try:
|
try:
|
||||||
return _buf_options[bufnr][varname[1:]]
|
return buffers[bufnr].options[varname[1:]]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
try:
|
try:
|
||||||
return _options[varname[1:]]
|
return options[varname[1:]]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return ''
|
return ''
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -213,12 +213,12 @@ def _emul_getbufvar(bufnr, varname):
|
||||||
@_vim
|
@_vim
|
||||||
@_str_func
|
@_str_func
|
||||||
def _emul_getwinvar(winnr, varname):
|
def _emul_getwinvar(winnr, varname):
|
||||||
return _win_scopes[winnr][varname]
|
return windows[winnr].vars[varname]
|
||||||
|
|
||||||
|
|
||||||
@_vim
|
@_vim
|
||||||
def _emul_setwinvar(winnr, varname, value):
|
def _emul_setwinvar(winnr, varname, value):
|
||||||
_win_scopes[winnr][varname] = value
|
windows[winnr].vars[varname] = value
|
||||||
|
|
||||||
|
|
||||||
@_vim
|
@_vim
|
||||||
|
@ -262,7 +262,7 @@ def _emul_bufnr(expr):
|
||||||
@_vim
|
@_vim
|
||||||
def _emul_exists(varname):
|
def _emul_exists(varname):
|
||||||
if varname.startswith('g:'):
|
if varname.startswith('g:'):
|
||||||
return varname[2:] in _g
|
return varname[2:] in vars
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
@ -273,10 +273,9 @@ def _emul_line2byte(line):
|
||||||
return sum((len(s) for s in buflines)) + 1
|
return sum((len(s) for s in buflines)) + 1
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
_window_ids = [None]
|
_window_ids = [None]
|
||||||
_window_id = 0
|
_window_id = 0
|
||||||
_win_scopes = [None]
|
|
||||||
_win_options = [None]
|
|
||||||
|
|
||||||
|
|
||||||
class _Window(object):
|
class _Window(object):
|
||||||
|
@ -295,15 +294,13 @@ class _Window(object):
|
||||||
windows.append(self)
|
windows.append(self)
|
||||||
_window_id += 1
|
_window_id += 1
|
||||||
_window_ids.append(_window_id)
|
_window_ids.append(_window_id)
|
||||||
_win_scopes.append({})
|
self.options = {}
|
||||||
_win_options.append({})
|
self.vars = {}
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<window ' + str(windows.index(self)) + '>'
|
return '<window ' + str(windows.index(self)) + '>'
|
||||||
|
|
||||||
|
|
||||||
_buf_scopes = {}
|
|
||||||
_buf_options = {}
|
|
||||||
_buf_lines = {}
|
_buf_lines = {}
|
||||||
_undostate = {}
|
_undostate = {}
|
||||||
_undo_written = {}
|
_undo_written = {}
|
||||||
|
@ -317,8 +314,8 @@ class _Buffer(object):
|
||||||
bufnr = _last_bufnr
|
bufnr = _last_bufnr
|
||||||
self.number = bufnr
|
self.number = bufnr
|
||||||
self.name = os.path.abspath(name) if name else None
|
self.name = os.path.abspath(name) if name else None
|
||||||
_buf_scopes[bufnr] = {}
|
self.vars = {}
|
||||||
_buf_options[bufnr] = {
|
self.options = {
|
||||||
'modified': 0,
|
'modified': 0,
|
||||||
'readonly': 0,
|
'readonly': 0,
|
||||||
'fileformat': 'unix',
|
'fileformat': 'unix',
|
||||||
|
@ -337,13 +334,13 @@ class _Buffer(object):
|
||||||
return _buf_lines[self.number][line]
|
return _buf_lines[self.number][line]
|
||||||
|
|
||||||
def __setitem__(self, line, value):
|
def __setitem__(self, line, value):
|
||||||
_buf_options[self.number]['modified'] = 1
|
self.options['modified'] = 1
|
||||||
_buf_lines[self.number][line] = value
|
_buf_lines[self.number][line] = value
|
||||||
from copy import copy
|
from copy import copy
|
||||||
_undostate[self.number].append(copy(_buf_lines[self.number]))
|
_undostate[self.number].append(copy(_buf_lines[self.number]))
|
||||||
|
|
||||||
def __setslice__(self, *args):
|
def __setslice__(self, *args):
|
||||||
_buf_options[self.number]['modified'] = 1
|
self.options['modified'] = 1
|
||||||
_buf_lines[self.number].__setslice__(*args)
|
_buf_lines[self.number].__setslice__(*args)
|
||||||
from copy import copy
|
from copy import copy
|
||||||
_undostate[self.number].append(copy(_buf_lines[self.number]))
|
_undostate[self.number].append(copy(_buf_lines[self.number]))
|
||||||
|
@ -359,12 +356,10 @@ class _Buffer(object):
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
bufnr = self.number
|
bufnr = self.number
|
||||||
if _buf_options:
|
if _buf_lines:
|
||||||
_buf_options.pop(bufnr)
|
|
||||||
_buf_lines.pop(bufnr)
|
_buf_lines.pop(bufnr)
|
||||||
_undostate.pop(bufnr)
|
_undostate.pop(bufnr)
|
||||||
_undo_written.pop(bufnr)
|
_undo_written.pop(bufnr)
|
||||||
_buf_scopes.pop(bufnr)
|
|
||||||
|
|
||||||
|
|
||||||
class _Current(object):
|
class _Current(object):
|
||||||
|
@ -436,8 +431,9 @@ def _undo():
|
||||||
return
|
return
|
||||||
_undostate[_buffer()].pop(-1)
|
_undostate[_buffer()].pop(-1)
|
||||||
_buf_lines[_buffer()] = _undostate[_buffer()][-1]
|
_buf_lines[_buffer()] = _undostate[_buffer()][-1]
|
||||||
|
buf = current.buffer
|
||||||
if _undo_written[_buffer()] == len(_undostate[_buffer()]):
|
if _undo_written[_buffer()] == len(_undostate[_buffer()]):
|
||||||
_buf_options[_buffer()]['modified'] = 0
|
buf.options['modified'] = 0
|
||||||
|
|
||||||
|
|
||||||
@_vim
|
@_vim
|
||||||
|
@ -468,8 +464,6 @@ def _split():
|
||||||
@_vim
|
@_vim
|
||||||
def _del_window(winnr):
|
def _del_window(winnr):
|
||||||
win = windows.pop(winnr - 1)
|
win = windows.pop(winnr - 1)
|
||||||
_win_scopes.pop(winnr)
|
|
||||||
_win_options.pop(winnr)
|
|
||||||
_window_ids.pop(winnr)
|
_window_ids.pop(winnr)
|
||||||
return win
|
return win
|
||||||
|
|
||||||
|
@ -525,7 +519,7 @@ def _get_buffer():
|
||||||
|
|
||||||
@_vim
|
@_vim
|
||||||
def _set_bufoption(option, value, bufnr=None):
|
def _set_bufoption(option, value, bufnr=None):
|
||||||
_buf_options[bufnr or _buffer()][option] = value
|
buffers[bufnr or _buffer()].options[option] = value
|
||||||
if option == 'filetype':
|
if option == 'filetype':
|
||||||
_launch_event('FileType')
|
_launch_event('FileType')
|
||||||
|
|
||||||
|
@ -565,11 +559,11 @@ class _WithBufOption(object):
|
||||||
self.new = new
|
self.new = new
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.bufnr = _buffer()
|
self.buffer = buffers[_buffer()]
|
||||||
self.old = _set_dict(_buf_options[self.bufnr], self.new, _set_bufoption)[0]
|
self.old = _set_dict(self.buffer.options, self.new, _set_bufoption)[0]
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
_buf_options[self.bufnr].update(self.old)
|
self.buffer.options.update(self.old)
|
||||||
|
|
||||||
|
|
||||||
class _WithMode(object):
|
class _WithMode(object):
|
||||||
|
@ -632,8 +626,8 @@ def _with(key, *args, **kwargs):
|
||||||
elif key == 'bufoptions':
|
elif key == 'bufoptions':
|
||||||
return _WithBufOption(**kwargs)
|
return _WithBufOption(**kwargs)
|
||||||
elif key == 'options':
|
elif key == 'options':
|
||||||
return _WithDict(_options, **kwargs)
|
return _WithDict(options, **kwargs)
|
||||||
elif key == 'globals':
|
elif key == 'globals':
|
||||||
return _WithDict(_g, **kwargs)
|
return _WithDict(vars, **kwargs)
|
||||||
elif key == 'split':
|
elif key == 'split':
|
||||||
return _WithSplit()
|
return _WithSplit()
|
||||||
|
|
Loading…
Reference in New Issue