1
|
|
|
"""Utility module.""" |
2
|
|
|
import re |
3
|
|
|
from typing import AnyStr, Union |
4
|
|
|
from neovim import Nvim |
5
|
|
|
|
6
|
|
|
_cached_encoding = None # type: str |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def get_encoding(nvim: Nvim) -> str: |
10
|
|
|
"""Return a Vim's internal encoding. |
11
|
|
|
|
12
|
|
|
The retrieve encoding is cached to the function instance while encoding |
13
|
|
|
options should not be changed in Vim's live session (see :h encoding) to |
14
|
|
|
enhance performance. |
15
|
|
|
|
16
|
|
|
Args: |
17
|
|
|
nvim (neovim.Nvim): A ``neovim.Nvim`` instance. |
18
|
|
|
|
19
|
|
|
Returns: |
20
|
|
|
str: A Vim's internal encoding. |
21
|
|
|
""" |
22
|
|
|
global _cached_encoding |
23
|
|
|
if _cached_encoding is None: |
24
|
|
|
_cached_encoding = nvim.options['encoding'] |
25
|
|
|
return _cached_encoding |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def ensure_bytes(nvim: Nvim, seed: AnyStr) -> bytes: |
29
|
|
|
"""Encode `str` to `bytes` if necessary and return. |
30
|
|
|
|
31
|
|
|
Args: |
32
|
|
|
nvim (neovim.Nvim): A ``neovim.Nvim`` instance. |
33
|
|
|
seed (AnyStr): A str or bytes instance. |
34
|
|
|
|
35
|
|
|
Example: |
36
|
|
|
>>> from unittest.mock import MagicMock |
37
|
|
|
>>> nvim = MagicMock() |
38
|
|
|
>>> nvim.options = {'encoding': 'utf-8'} |
39
|
|
|
>>> ensure_bytes(nvim, b'a') |
40
|
|
|
b'a' |
41
|
|
|
>>> ensure_bytes(nvim, 'a') |
42
|
|
|
b'a' |
43
|
|
|
|
44
|
|
|
Returns: |
45
|
|
|
bytes: A bytes represantation of ``seed``. |
46
|
|
|
""" |
47
|
|
|
if isinstance(seed, str): |
48
|
|
|
encoding = get_encoding(nvim) |
49
|
|
|
return seed.encode(encoding, 'surrogateescape') |
50
|
|
|
return seed |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def ensure_str(nvim: Nvim, seed: AnyStr) -> str: |
54
|
|
|
"""Decode `bytes` to `str` if necessary and return. |
55
|
|
|
|
56
|
|
|
Args: |
57
|
|
|
nvim (neovim.Nvim): A ``neovim.Nvim`` instance. |
58
|
|
|
seed (AnyStr): A str or bytes instance. |
59
|
|
|
|
60
|
|
|
Example: |
61
|
|
|
>>> from unittest.mock import MagicMock |
62
|
|
|
>>> nvim = MagicMock() |
63
|
|
|
>>> nvim.options = {'encoding': 'utf-8'} |
64
|
|
|
>>> ensure_str(nvim, b'a') |
65
|
|
|
'a' |
66
|
|
|
>>> ensure_str(nvim, 'a') |
67
|
|
|
'a' |
68
|
|
|
|
69
|
|
|
Returns: |
70
|
|
|
str: A str represantation of ``seed``. |
71
|
|
|
""" |
72
|
|
|
if isinstance(seed, bytes): |
73
|
|
|
encoding = get_encoding(nvim) |
74
|
|
|
return seed.decode(encoding, 'surrogateescape') |
75
|
|
|
return seed |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def int2chr(nvim: Nvim, code: int) -> str: |
79
|
|
|
"""Return a corresponding char of `code`. |
80
|
|
|
|
81
|
|
|
It uses "nr2char()" in Vim script when 'encoding' option is not utf-8. |
82
|
|
|
Otherwise it uses "chr()" in Python to improve the performance. |
83
|
|
|
|
84
|
|
|
Args: |
85
|
|
|
nvim (neovim.Nvim): A ``neovim.Nvim`` instance. |
86
|
|
|
code (int): A int which represent a single character. |
87
|
|
|
|
88
|
|
|
Example: |
89
|
|
|
>>> from unittest.mock import MagicMock |
90
|
|
|
>>> nvim = MagicMock() |
91
|
|
|
>>> nvim.options = {'encoding': 'utf-8'} |
92
|
|
|
>>> int2chr(nvim, 97) |
93
|
|
|
'a' |
94
|
|
|
|
95
|
|
|
Returns: |
96
|
|
|
str: A str of ``code``. |
97
|
|
|
""" |
98
|
|
|
encoding = get_encoding(nvim) |
99
|
|
|
if encoding in ('utf-8', 'utf8'): |
100
|
|
|
return chr(code) |
101
|
|
|
return nvim.call('nr2char', code) |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
def int2repr(nvim: Nvim, code: Union[int, bytes]) -> str: |
105
|
|
|
from .key import Key |
106
|
|
|
if isinstance(code, int): |
107
|
|
|
return int2chr(nvim, code) |
108
|
|
|
return Key.represent(nvim, ensure_bytes(nvim, code)) |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
def getchar(nvim: Nvim, *args) -> Union[int, bytes]: |
112
|
|
|
"""Call getchar and return int or bytes instance. |
113
|
|
|
|
114
|
|
|
Args: |
115
|
|
|
nvim (neovim.Nvim): A ``neovim.Nvim`` instance. |
116
|
|
|
*args: Arguments passed to getchar function in Vim. |
117
|
|
|
|
118
|
|
|
Returns: |
119
|
|
|
Union[int, bytes]: A int or bytes. |
120
|
|
|
""" |
121
|
|
|
ret = nvim.call('getchar', *args) |
122
|
|
|
if isinstance(ret, int): |
123
|
|
|
return ret |
124
|
|
|
return ensure_bytes(nvim, ret) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
def safeget(l: list, index: int, default=None): |
128
|
|
|
"""Return an index item of list or default.""" |
129
|
|
|
try: |
130
|
|
|
return l[index] |
131
|
|
|
except IndexError: |
132
|
|
|
return default |
133
|
|
|
|