1
|
|
|
"""Context module.""" |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class Context: |
5
|
|
|
"""Context class which used to store/restore data. |
6
|
|
|
|
7
|
|
|
Note: |
8
|
|
|
This class defines ``__slots__`` attribute so sub-class must override |
9
|
|
|
the attribute to extend available attributes. |
10
|
|
|
|
11
|
|
|
Attributes: |
12
|
|
|
nvim (Nvim): The ``neovim.Nvim`` instance. |
13
|
|
|
text (str): A user input text of the prompt. |
14
|
|
|
caret_locus (int): A locus index of the caret in the prompt. |
15
|
|
|
|
16
|
|
|
""" |
17
|
|
|
|
18
|
|
|
__slots__ = ('text', 'caret_locus') |
19
|
|
|
|
20
|
|
|
def __init__(self) -> None: |
21
|
|
|
"""Constructor. |
22
|
|
|
|
23
|
|
|
Args: |
24
|
|
|
nvim (Nvim): The ``neovim.Nvim`` instance. |
25
|
|
|
|
26
|
|
|
""" |
27
|
|
|
self.text = '' |
28
|
|
|
self.caret_locus = 0 |
29
|
|
|
|
30
|
|
|
def to_dict(self) -> dict: |
31
|
|
|
"""Convert a context instance into a dictionary. |
32
|
|
|
|
33
|
|
|
Use ``Context.from_dict(d)`` to restore a context instance from a |
34
|
|
|
dictionary. |
35
|
|
|
|
36
|
|
|
Example: |
37
|
|
|
>>> context = Context() |
38
|
|
|
>>> context.text = 'Hello' |
39
|
|
|
>>> context.caret_locus = 3 |
40
|
|
|
>>> d = context.to_dict() |
41
|
|
|
>>> d['text'] |
42
|
|
|
'Hello' |
43
|
|
|
>>> d['caret_locus'] |
44
|
|
|
3 |
45
|
|
|
|
46
|
|
|
Returns: |
47
|
|
|
dict: A context dictionary. |
48
|
|
|
|
49
|
|
|
""" |
50
|
|
|
return { |
51
|
|
|
k: getattr(self, k) |
52
|
|
|
for k in self.__slots__ |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
def extend(self, d: dict) -> None: |
56
|
|
|
"""Extend a context instance from a dictionary. |
57
|
|
|
|
58
|
|
|
Use ``context.to_dict()`` to create a corresponding dictionary. |
59
|
|
|
Keys which is not in ``__slots__`` will be ignored. |
60
|
|
|
|
61
|
|
|
Args: |
62
|
|
|
d (dict): A dictionary. |
63
|
|
|
|
64
|
|
|
Example: |
65
|
|
|
>>> context = Context.from_dict({ |
66
|
|
|
... 'text': 'Hello', |
67
|
|
|
... 'caret_locus': 3, |
68
|
|
|
... }) |
69
|
|
|
>>> context.text |
70
|
|
|
'Hello' |
71
|
|
|
>>> context.caret_locus |
72
|
|
|
3 |
73
|
|
|
>>> context.extend({ |
74
|
|
|
... 'text': 'Bye', |
75
|
|
|
... 'caret_locus': 1, |
76
|
|
|
... }) |
77
|
|
|
>>> context.text |
78
|
|
|
'Bye' |
79
|
|
|
>>> context.caret_locus |
80
|
|
|
1 |
81
|
|
|
""" |
82
|
|
|
for k, v in d.items(): |
83
|
|
|
if k in self.__slots__: |
84
|
|
|
setattr(self, k, v) |
85
|
|
|
|
86
|
|
|
@classmethod |
87
|
|
|
def from_dict(cls, d: dict) -> 'Context': |
88
|
|
|
"""Create a new context instance from a dictionary. |
89
|
|
|
|
90
|
|
|
Use ``context.to_dict()`` to create a corresponding dictionary. |
91
|
|
|
|
92
|
|
|
Args: |
93
|
|
|
d (dict): A corresponding dictionary. |
94
|
|
|
|
95
|
|
|
Example: |
96
|
|
|
>>> context = Context.from_dict({ |
97
|
|
|
... 'text': 'Hello', |
98
|
|
|
... 'caret_locus': 3, |
99
|
|
|
... }) |
100
|
|
|
>>> context.text |
101
|
|
|
'Hello' |
102
|
|
|
>>> context.caret_locus |
103
|
|
|
3 |
104
|
|
|
|
105
|
|
|
Returns: |
106
|
|
|
Context: A context instance. |
107
|
|
|
""" |
108
|
|
|
context = cls.__new__(cls) |
109
|
|
|
context.extend(d) |
110
|
|
|
return context |
111
|
|
|
|