-
Notifications
You must be signed in to change notification settings - Fork 31
/
text_commands.py
105 lines (85 loc) · 3.2 KB
/
text_commands.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
# coding: utf-8
import hashlib
import sublime_plugin
import sublime
try:
from .floo import sublime_utils as sutils
from .floo.common import shared as G
assert G
except (ImportError, ValueError):
from floo import sublime_utils as sutils
from floo.common import shared as G
def transform_selections(selections, start, new_offset):
new_sels = []
for sel in selections:
a = sel.a
b = sel.b
if sel.a > start:
a += new_offset
if sel.b > start:
b += new_offset
new_sels.append(sublime.Region(a, b))
return new_sels
# The new ST3 plugin API sucks
class FlooViewReplaceRegion(sublime_plugin.TextCommand):
def run(self, edit, *args, **kwargs):
selections = [x for x in self.view.sel()] # deep copy
selections = self._run(edit, selections, *args, **kwargs)
self.view.sel().clear()
for sel in selections:
self.view.sel().add(sel)
def _run(self, edit, selections, r, data, view=None):
global ignore_modified_timeout
if not hasattr(self, 'view'):
return selections
start = max(int(r[0]), 0)
stop = min(int(r[1]), self.view.size())
region = sublime.Region(start, stop)
if stop - start > 10000:
self.view.replace(edit, region, data)
G.VIEW_TO_HASH[self.view.buffer_id()] = hashlib.md5(sutils.get_text(self.view).encode('utf-8')).hexdigest()
return transform_selections(selections, stop, 0)
existing = self.view.substr(region)
i = 0
data_len = len(data)
existing_len = len(existing)
length = min(data_len, existing_len)
while (i < length):
if existing[i] != data[i]:
break
i += 1
j = 0
while j < (length - i):
if existing[existing_len - j - 1] != data[data_len - j - 1]:
break
j += 1
region = sublime.Region(start + i, stop - j)
replace_str = data[i:data_len - j]
self.view.replace(edit, region, replace_str)
G.VIEW_TO_HASH[self.view.buffer_id()] = hashlib.md5(sutils.get_text(self.view).encode('utf-8')).hexdigest()
new_offset = len(replace_str) - ((stop - j) - (start + i))
return transform_selections(selections, start + i, new_offset)
def is_visible(self, *args, **kwargs):
return False
def is_enabled(self, *args, **kwargs):
return True
def description(self, *args, **kwargs):
return
# The new ST3 plugin API sucks
class FlooViewReplaceRegions(FlooViewReplaceRegion):
def run(self, edit, commands, *args, **kwargs):
is_read_only = self.view.is_read_only()
self.view.set_read_only(False)
selections = [x for x in self.view.sel()] # deep copy
for command in commands:
selections = self._run(edit, selections, **command)
self.view.set_read_only(is_read_only)
self.view.sel().clear()
for sel in selections:
self.view.sel().add(sel)
def is_visible(self, *args, **kwargs):
return False
def is_enabled(self, *args, **kwargs):
return True
def description(self, *args, **kwargs):
return