-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpTo.py
233 lines (190 loc) · 7.25 KB
/
JumpTo.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import re
import sys
from sublime import *
from sublime_plugin import *
from datetime import datetime, timedelta
from . import Settings
from .RunSnippet import Logger
class JumpToCommand(TextCommand, ViewEventListener):
def __init__(self, *args):
if args and isinstance(args[0], View):
self.view = args[0]
Logger.message("Jumpto init %s %s " % (str(args), sys.version))
def run(self, edit, *args):
ctx = self.parseline()
win = self.view.window()
if not ctx or not win:
return
text = ctx.text
kind = ctx.kind
symbol = ""
if (kind == kindSelected) and None is re.search(r".+[\. /\\].+", text):
symbol = "@"
elif (kind == kindCtags):
return self.ctags(text)
elif (kind == kindSymbol):
self.view.sel().clear()
self.view.sel().add(Region(ctx.begin, ctx.end))
win.run_command("copy",)
win.run_command('goto_symbol_in_project', {'overlay':'goto'})
return win.run_command('paste',)
# "goto_definition", {"side_by_side": True, "clear_to_right": True} )
elif text.startswith("http") or text.startswith("file://"):
return os.popen("start %s" % text)
elif text.startswith("#"):
hash = re.sub(r'[-_#]', lambda x: ' ', text)
return win.run_command(
'show_overlay',
{'overlay': 'goto', 'text': '@'+hash})
for it in range(0, win.num_groups()):
(between, rs) = Settings.get("jump_between_group")
print(("jump_between_group"), between, rs.settings_id)
if not between:
break
grouped = win.views_in_group(it)
if self.view in grouped:
continue
win.focus_group(it)
text = symbol+text.replace("\\", "/")
win.run_command(
"show_overlay",
{"overlay": "goto", "show_file": True, "text": text})
"""
Vim CTags in-file jumping
1. Setting options |set-option|
1. Setting options *set-option* *E764*
more for test ... *set-option* *E764*
"""
keyword1st = "keyword"
lasttime = datetime.now()
duration = timedelta(microseconds=200000)
def ctags(self, ctag):
if not isinstance(ctag, str):
return False
doublejump = datetime.now() - self.lasttime < self.duration
if self.keyword1st != ctag or doublejump:
self.keyword1st = ctag
tags = "[*]{0}[*]".format(ctag)
region = self.view.find(tags, 0, IGNORECASE)
cur = self.view.sel()[0]
if not region.a < cur.a < region.b:
self.view.sel().add(region.a+1)
return self.view.show_at_center(region)
self.lasttime = datetime.now()
tags = r"(\*|\|){0}\1|\[{0}\]".format(ctag)
sets = self.view.find_all(tags, IGNORECASE)
rgn = self.view.sel()[0]
# where I can find the keyboard modifier?
# I need it to reverse the backward direction.
for it in sets:
if it.b < rgn.a or it.a <= rgn.a <= it.b:
if sets[-1] != it:
continue
it = sets[0]
self.view.sel().subtract(rgn)
self.view.sel().add(it.a+1)
self.view.show_at_center(it)
break
def is_enabled(self, *args):
Logger.message("jump to is_enabled %s" % str(args))
return self.parseline() is not None
def parseline(self):
sel = self.view.sel()
if len(sel) == 0:
return
region = sel[0]
if region.a != region.b:
sel = self.view.substr(region)
if len(sel.split("\n")) == 1:
return MatchArea(kindSelected, sel, region.a, region.b)
region_of_line = self.view.line(region.a)
if region_of_line.a == region_of_line.b:
return None
line = self.view.substr(region_of_line)
offset = region.a - region_of_line.a
rp = line[offset:] or ""
lp = line[0:offset] or ""
r = rp.find(">")
t = lp.rfind("<")
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindBlock, txt, pos, region.a+r)
r = rp.find("`")
t = lp.rfind("`")
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindSymbol, txt, pos, region.a+r)
r = rp.find("|")
t = lp.rfind("|")
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindCtags, txt, pos, region.a+r)
r = rp.find("]")
t = lp.rfind("[")
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindCtags, txt, pos, region.a+r)
r = rp.find("'")
t = lp.rfind("'")
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindQuote, txt, pos, region.a+r)
r = rp.find('"')
t = lp.rfind('"')
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindQuote, txt, pos, region.a+r)
r = rp.find(")")
t = lp.rfind("(")
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindQuote, txt, pos, region.a+r)
r = rp.find("*")
t = lp.rfind("*")
if r >= 0 and t >= 0:
pos = region.a-offset+t+1
txt = line[t+1:r+offset]
return MatchArea(kindCtags, txt, pos, region.a+r)
t = lp.rfind(' ')
r = rp.find(' ')
if t == -1:
t = 0
if r == -1:
r = len(rp)
block = line[t:r+offset].strip()
if block.startswith("http") or block.startswith("file://"):
return MatchArea(kindBlock, block, region.a-offset+t+1, region.a+r)
else:
return MatchArea(kindSpaced, block, region.a-offset+t+1, region.a+r)
# def plugin_loaded() -> None:
# print("plugin loaded")
# def plugin_unloaded() -> None:
# print("plugin unloaded")
class MatchKind:
def __init__(self, kind:str):
self.kind = kind
kindSpaced = MatchKind('Spaced')
kindBlock = MatchKind('Block')
kindText = MatchKind('Text')
kindFile = MatchKind('File')
kindSymbol = MatchKind('Symbol')
kindSelected = MatchKind('Selected')
kindQuote = MatchKind('Quote')
kindCtags = MatchKind('Ctags')
class MatchArea:
def __init__(self, kind: MatchKind, text: str, begin: int, end: int):
self.kind = kind
self.text = text
self.begin = begin
self.end = end
def toString(self):
return r'<MatchArea kind=%s text=%s [%d,%d]>' % (
self.kind, self.text, self.begin, self.end)