-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjetbrains.py
321 lines (280 loc) · 10.9 KB
/
jetbrains.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import os
import requests
import talon.clip as clip
from talon import ctrl
from talon.ui import active_app
from talon.voice import Context, ContextGroup, Key
from user.std import text, parse_word
try:
from user.mouse import delayed_click
except ImportError:
def delayed_click():
ctrl.mouse_click(button=0)
# region Supporting Code
_numeral_map = dict((str(n), n) for n in range(0, 20))
for n in range(20, 101, 10):
_numeral_map[str(n)] = n
for n in range(100, 1001, 100):
_numeral_map[str(n)] = n
for n in range(1000, 10001, 1000):
_numeral_map[str(n)] = n
_numeral_map["oh"] = 0 # synonym for zero
_numeral_map["and"] = None # drop me
_numerals = " (" + " | ".join(sorted(_numeral_map.keys())) + ")++"
_optional_numerals = " (" + " | ".join(sorted(_numeral_map.keys())) + ")**"
def text_to_number(words):
tmp = [str(s).lower() for s in words]
words = [parse_word(word) for word in tmp]
result = 0
factor = 1
for word in reversed(words):
print("{} {} {}".format(result, factor, word))
if word not in _numerals:
raise Exception("not a number: {}".format(words))
number = _numeral_map[word]
if number is None:
continue
number = int(number)
if number > 10:
result = result + number
else:
result = result + factor * number
factor = (10 ** len(str(number))) * factor
return result
def text_to_range(words, delimiter="until"):
tmp = [str(s).lower() for s in words]
split = tmp.index(delimiter)
start = text_to_number(words[:split])
end = text_to_number(words[split + 1 :])
return start, end
# endregion
# Each IDE gets its own port, as otherwise you wouldn't be able
# to run two at the same time and switch between them.
# Note that MPS and IntelliJ ultimate will conflict...
port_mapping = {
"com.jetbrains.intellij": 8653,
"com.jetbrains.intellij.ce": 8654,
"com.jetbrains.AppCode": 8655,
"com.jetbrains.CLion": 8657,
"com.jetbrains.datagrip": 8664,
"com.jetbrains.goland": 8659,
"com.jetbrains.PhpStorm": 8662,
"com.jetbrains.pycharm": 8658,
"com.jetbrains.rider": 8660,
"com.jetbrains.rubymine": 8661,
"com.jetbrains.WebStorm": 8663,
"com.google.android.studio": 8652,
}
def _get_nonce(port):
try:
with open(os.path.join("/tmp", "vcidea_" + str(port)), "r") as fh:
return fh.read()
except IOError:
return None
def send_idea_command(cmd):
print("Sending {}".format(cmd))
bundle = active_app().bundle
port = port_mapping.get(bundle, None)
nonce = _get_nonce(port)
if port and nonce:
response = requests.get(
"http://localhost:{}/{}/{}".format(port, nonce, cmd), timeout=(0.05, 3.05)
)
response.raise_for_status()
return response.text
def get_idea_location():
return send_idea_command("location").split()
def idea(cmd):
return lambda _: send_idea_command(cmd)
def idea_num(cmd, drop=1):
def handler(m):
line = text_to_number(m._words[drop:])
print(cmd.format(line))
if int(line) == 0:
print("Not sending, arg was 0")
return
send_idea_command(cmd.format(line))
return handler
def idea_range(cmd, drop=1):
def handler(m):
start, end = text_to_range(m._words[drop:])
print(cmd.format(start, end))
send_idea_command(cmd.format(start, end))
return handler
def idea_words(cmd, join=" "):
def handler(m):
args = [str(w) for w in m.dgndictation[0]._words]
print(args)
send_idea_command(cmd.format(join.join(args)))
return handler
def grab_identifier(m):
old_clip = clip.get()
times = text_to_number(m._words[1:]) # hardcoded prefix length?
if not times:
times = 1
try:
old_line, old_col = get_idea_location()
delayed_click(m, button=0, times=2)
for _ in range(times):
send_idea_command("action EditorSelectWord")
send_idea_command("action EditorCopy")
send_idea_command("goto {} {}".format(old_line, old_col))
send_idea_command("action EditorPaste")
finally:
clip.set(old_clip)
def is_real_jetbrains_editor(app, window):
if app.bundle not in port_mapping:
return False
# XXX Expose "does editor have focus" as plugin endpoint.
# XXX Window title empty in full screen.
return "[" in window.title or len(window.title) == 0
# group = ContextGroup("jetbrains")
ctx = Context("jetbrains", func=is_real_jetbrains_editor) # , group=group)
keymap = {}
keymap.update(
{
"complete": idea("action CodeCompletion"),
"smarter": idea("action SmartTypeCompletion"),
"finish": idea("action EditorCompleteStatement"),
"zoom": idea("action HideAllWindows"),
"find (usage | usages)": idea("action FindUsages"),
"(refactor | reflector) [<dgndictation>]": [
idea("action Refactorings.QuickListPopupAction"),
text,
],
"fix [this]": idea("action ShowIntentionActions"),
"fix next [error]": [
idea("action GotoNextError"),
idea("action ShowIntentionActions"),
],
"fix previous [error]": [
idea("action GotoPreviousError"),
idea("action ShowIntentionActions"),
],
"visit declaration": idea("action GotoDeclaration"),
"visit (implementers | implementations)": idea("action GotoImplementation"),
"visit type": idea("action GotoTypeDeclaration"),
"(select previous | trail) [<dgndictation>]": idea_words("find prev {}"),
"(select next | crew) [<dgndictation>]": idea_words("find next {}"),
"search everywhere [for] [<dgndictation>]": [
idea("action SearchEverywhere"),
text,
],
"find [<dgndictation>]": [idea("action Find"), text],
"find this": idea("action FindWordAtCaret"),
"next": idea("action FindNext"),
"last": idea("action FindPrevious"),
"surround [this] [<dgndictation>]": [idea("action SurroundWith"), text],
"generate [<dgndictation>]": [idea("action Generate"), text],
"template [<dgndictation>]": [idea("action InsertLiveTemplate"), text],
"select less": idea("action EditorUnSelectWord"),
"select more": idea("action EditorSelectWord"),
"select line"
+ _optional_numerals: [
idea_num("goto {} 0", drop=2),
idea("action EditorLineStart"),
idea("action EditorLineEndWithSelection"),
],
"select block": [
idea("action EditorCodeBlockStart"),
idea("action EditorCodeBlockEndWithSelection"),
],
"select this line": [
idea("action EditorLineStart"),
idea("action EditorLineEndWithSelection"),
],
"select lines {} until {}".format(
_optional_numerals, _optional_numerals
): idea_range("range {} {}", drop=2),
"select until" + _optional_numerals: idea_num("extend {}", drop=2),
"(go | jump) to end of" + _optional_numerals: idea_num("goto {} 9999", drop=4),
"(clean | clear) line": [
idea("action EditorLineEnd"),
idea("action EditorDeleteToLineStart"),
],
"(delete | remove) line": idea(
"action EditorDeleteLine"
), # xxx optional line number
"(delete | clear) to end": idea("action EditorDeleteToLineEnd"),
"(delete | clear) to start": idea("action EditorDeleteToLineStart"),
"drag up": idea("action MoveLineUp"),
"drag down": idea("action MoveLineDown"),
"duplicate": idea("action EditorDuplicate"),
"(go | jump) back": idea("action Back"),
"(go | jump) forward": idea("action Forward"),
"(synchronizing | synchronize)": idea("action Synchronize"),
"comment": idea("action CommentByLineComment"),
"(action | please) [<dgndictation>]": [idea("action GotoAction"), text],
"(go to | jump to)" + _optional_numerals: idea_num("goto {} 0", drop=2),
"clone line" + _optional_numerals: idea_num("clone {}", drop=2),
"fix this": idea("action ShowIntentionActions"),
"fix next": [idea("action GotoNextError"), idea("action ShowIntentionActions")],
"fix previous": [
idea("action GotoPreviousError"),
idea("action ShowIntentionActions"),
],
"grab" + _optional_numerals: grab_identifier,
"(start | stop) recording": idea("action StartStopMacroRecording"),
"edit (recording | recordings)": idea("action EditMacros"),
"play recording": idea("action PlaybackLastMacro"),
"play recording <dgndictation>": [
idea("action PlaySavedMacrosAction"),
text,
Key("enter"),
],
# my old commands
"comment declaration": ["/**", Key("space")],
"comment block": ["/**", Key("enter")],
"block comment": Key("cmd-alt-/"),
"(dupe | duplicate)": Key("cmd-d"),
"import class": Key("alt-enter enter"),
"quickfix": Key("alt-enter"),
"go class": Key("cmd-o"),
"go file": idea("action GotoFile"),
"(distraction free | normal mode)": idea("action ToggleDistractionFreeMode"),
# "go file": Key("cmd-shift-o"),
"(go implement | go definition)": Key("cmd-b"),
"preev method": Key("ctrl-up"),
"neck method": Key("ctrl-down"),
"refactor": Key("shift-f6"),
"generate": Key("cmd-n"),
"recent": Key("cmd-e"),
"replace": Key("cmd-r"),
"find action": Key("cmd-shift-a"),
"settings": Key("cmd-,"),
"grab": Key("alt-up"),
"shrink": Key("alt-down"),
"grab left": Key("cmd-shift-left"),
"grab right": Key("cmd-shift-right"),
"close": Key("cmd-w"),
"go top": Key("cmd-home"),
"go bottom": Key("cmd-end"),
"grab up": Key("shift-cmd-pageup"),
"grab down": Key("shift-cmd-pagedown"),
# 'rename': Key('shift-f6'),
"move file": Key("f6"),
"file search": Key("shift shift"),
"global search": Key("cmd-shift-f"),
"go to file": Key("cmd-shift-n"),
"format": Key("cmd-alt-l"),
"expand": Key("cmd-+"),
"collapse": Key("cmd--"),
"erase line": Key("cmd-backspace"),
"last change": Key("cmd-shift-backspace"),
"((open | close) terminal | terminal (open | close) | toggle terminal | terminal)": Key(
"alt-f12"
),
"snip left": Key("cmd-shift-left delete"),
"snip right": Key("cmd-shift-right delete"),
"move up": Key("alt-shift-up"),
"move down": Key("alt-shift-down"),
"path": Key("cmd-shift-f"),
"funk up": Key("cmd-shift-up"),
"funk down": Key("cmd-shift-down"),
"clear": [Key("cmd-a"), Key("backspace")],
"(breadcrumbs | crumbs)": Key("cmd-up"),
"(pretty | prettier)": Key("alt-shift-cmd-p"),
}
)
ctx.keymap(keymap)
# group.load()