forked from pirackr/Sublime-Bogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sublime-bogo.py
174 lines (130 loc) · 4.94 KB
/
sublime-bogo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys
import sublime
import sublime_plugin
import os
from itertools import takewhile
sys.path.append(os.path.join(os.path.dirname(__file__), 'bogo/'))
from bogo import core
ENABLED = False
LISTENER = None
# OK, this is how it works. We create keybindings for most printable
# character to call BogoKeyCommand, which passes the key to BogoCommand, which
# calls bogo.process_sequence() and updates the screen.
#
# Every other change to the document is captured by BogoListener, which asks
# BogoCommand to handle or to reset if the change doesn't make sense.
def update_status_line():
for window in sublime.windows():
for view in window.views():
view.set_status('bogo.enabled', 'BoGo: ' + ('OFF', 'ON')[ENABLED])
view.settings().set('bogo.enabled', ENABLED)
def plugin_loaded():
update_status_line()
def plugin_unloaded():
for window in sublime.windows():
for view in window.views():
view.erase_status('bogo.enabled')
class BogoListener(sublime_plugin.EventListener):
def __init__(self, *args, **kwargs):
super(BogoListener, self).__init__(*args, **kwargs)
global LISTENER
LISTENER = self
self.other_command = False
def on_selection_modified(self, view):
if self.other_command:
self.other_command = False
return
if not ENABLED:
return
view.run_command('bogo', {'command': 'reset'})
update_status_line()
class BogoEnableToggleCommand(sublime_plugin.TextCommand):
def run(self, edit):
global ENABLED
ENABLED = not ENABLED
update_status_line()
class BogoKeyCommand(sublime_plugin.TextCommand):
def run(self, edit, key):
LISTENER.other_command = True
bogo_args = {
"command": "new_char",
"args": {
"char": key
}
}
self.view.run_command('bogo', bogo_args)
class BogoCommand(sublime_plugin.TextCommand):
def __init__(self, *args, **kwargs):
super(BogoCommand, self).__init__(*args, **kwargs)
self.reset()
def reset(self):
self.sequence = ""
self.previously_committed_string = ""
# Command dispatcher
def run(self, edit, command, args={}):
self.edit = edit
if command == "new_char":
if args["char"] == "backspace":
self.on_left_delete()
else:
self.on_new_char(args["char"])
elif command == "reset":
self.reset()
def on_new_char(self, char):
if self.sequence == "" and len(self.view.sel()) == 1:
# Try to continue from the existing string at the cursor
# position only if there is only one cursor.
# Things would get weird if there are several cursors
# with different words under each.
word_under_cursor = self.view.substr(
self.view.word(self.view.sel()[0]))
if word_under_cursor.isalpha():
self.sequence = word_under_cursor
self.previously_committed_string = self.sequence
self.sequence += char
result = core.process_sequence(self.sequence)
self.commit(result)
def on_left_delete(self):
if self.sequence == "":
self.reset()
self.view.run_command('left_delete')
else:
new_converted_string, self.sequence = \
core.handle_backspace(
self.previously_committed_string,self.sequence)
self.commit(new_converted_string)
def commit(self, string):
same_initial_chars = list(
takewhile(lambda tupl: tupl[0] == tupl[1],
zip(self.previously_committed_string,
string)))
chars_to_delete = len(
self.previously_committed_string) - len(same_initial_chars)
# We have multiple cursors.
for cursor in self.view.sel():
replace_region = sublime.Region(
cursor.begin() - chars_to_delete, cursor.end())
replace_with = string[len(same_initial_chars):]
self.view.erase(self.edit, replace_region)
self.view.insert(self.edit, replace_region.begin(), replace_with)
self.previously_committed_string = string
def getRule(self):
"""
Get bogo rule definition from setting
Parameters
----------
Return
------
definition : dict
A dictionary of bogo rule definition, be used as parameter of
process_key() method.
"""
# cast setting to str, then in case users give a wrong input, it should
# fallback to Telex rule
bogoRule = \
str(sublime.load_settings('Bogo.sublime-settings')
.get('bogo-rule')).upper()
if bogoRule == 'VNI':
return core.get_vni_definition()
else:
return core.get_telex_definition()