-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_delete_replace.py
178 lines (147 loc) · 5.94 KB
/
insert_delete_replace.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
175
176
177
178
from typing import Optional
import sublime
import sublime_plugin
from sublime import Edit, Region, Selection, View, active_window
from sublime_api import view_add_regions # pyright: ignore
from sublime_api import view_erase # pyright:ignore
from sublime_api import view_selection_add_point as add_pt # pyright: ignore
from sublime_api import view_selection_add_region as add_reg # pyright: ignore
from sublime_plugin import WindowCommand
class CommandModeCommand(WindowCommand):
def run(self) -> None:
view: Optional[View] = active_window().active_view()
if view is None:
return
view.settings().set(key="block_caret", value=True)
view.settings().set(key="command_mode", value=True)
view.settings().set(key="needs_char", value=False)
view.run_command("hide_popup")
class DeleteSingleCharCommand(sublime_plugin.TextCommand):
def run(self, e: Edit, forward=False) -> None:
s = self.view.sel()
vi = self.view.id()
pt = 1 if forward else -1
[view_erase(vi, e.edit_token, Region(r.b, r.b + pt)) for r in s]
class SmartDeleteLineCommand(sublime_plugin.TextCommand):
def run(self, edit: Edit) -> None:
buf = self.view
for region in reversed(buf.sel()):
if region.empty():
if region.a == buf.size():
reg = buf.full_line(region.begin() - 1)
else:
reg = buf.full_line(region.begin())
else:
begin_line, _ = buf.rowcol(region.begin())
end_line, col = buf.rowcol(region.end())
if col != 0:
end_line += 1
reg_beg = buf.text_point(begin_line, 0)
reg_end = buf.text_point(end_line, 0) - 1
reg = Region(reg_beg, reg_end + 1)
buf.erase(edit, reg)
class CallbackCommand(sublime_plugin.TextCommand):
def run(self, edit, cmd, args):
callback = {"cmd": cmd, "args": args}
self.view.settings().set("callback", callback)
class InsertModeCommand(sublime_plugin.TextCommand):
def change_case(self) -> bool:
v = self.view
if (multiplier := self.view.settings().get("multiplier")) is not None:
if multiplier == 1:
v.run_command("upper_case")
elif multiplier == 2:
v.run_command("lower_case")
elif multiplier == 3:
v.run_command("title_case")
elif multiplier == 4:
v.run_command("convert_to_snake")
elif multiplier == 5:
v.run_command("convert_to_pascal")
else:
return False
return True
return False
def run(self, edit: Edit, after=False, before=False) -> None:
v = self.view
if v.settings().get("set_number") is not None:
v.settings().erase("set_number")
if before and not after:
self.change_case()
elif not before and after:
if (multiplier := v.settings().get("multiplier")) is not None:
if multiplier == 1:
v.run_command("transpose")
elif multiplier == 2:
v.run_command("toggle_true_false")
v.settings().erase("multiplier")
return
if v.is_read_only():
sublime.status_message("Buffer is read only")
return
for r in reversed(v.sel()):
begin = r.begin()
end = r.end()
if r.empty():
end += 1
start = end if after else begin
stop = begin if before else end
v.sel().subtract(r)
new_region = Region(stop, start) if r.a > r.b else Region(start, stop)
v.sel().add(new_region)
if before and after:
v.erase(edit, new_region)
v.settings().set(key="block_caret", value=False)
v.settings().set(key="command_mode", value=False)
class DeleteRestOfLineAndInsertModeCommand(sublime_plugin.TextCommand):
def run(self, edit: Edit) -> None:
v = self.view
if v.is_read_only():
sublime.status_message("Buffer is read only")
return
sels: Selection = v.sel()
for reg in reversed(sels):
if reg.empty():
line = v.line(reg.begin())
v.erase(edit, Region(reg.begin(), line.end()))
else:
v.erase(edit, reg)
v.settings().set(key="block_caret", value=False)
v.settings().set(key="command_mode", value=False)
class InsertBeforeOrAfterCommand(sublime_plugin.TextCommand):
def run(self, _, after=False, plusone=False) -> None:
if plusone == True:
offset = 1
else:
offset = 0
buf = self.view
selections = buf.sel()
for region in selections:
if region.empty():
if len(selections) == 1:
return
selections.subtract(region)
if after == True:
reg = region.end() + offset
else:
reg = region.begin() - offset
selections.subtract(region)
selections.add(reg)
buf.settings().set(key="block_caret", value=False)
buf.settings().set(key="command_mode", value=False)
class InsertSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit: Edit):
view: View = self.view
vi = view.id()
for r in view.sel():
view.sel().subtract(r)
view.insert(edit, r.b, " ")
add_pt(vi, r.b if r.b < r.a else r.b + 1, 0.0)
class ReplaceSingleChar(sublime_plugin.TextCommand):
def run(self, edit, character):
view: View = self.view
for r in view.sel():
if r.empty():
view.replace(edit, Region(r.a, r.a + 1), character)
else:
view.replace(edit, r, character)