/
text_intellisense.py
494 lines (358 loc) · 12.5 KB
/
text_intellisense.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****
bl_info = {
"name": "Intellisense for Text Editor",
"author": "Mackraken",
"version": (0, 2, 1),
"blender": (2, 78, 5),
"location": "Ctrl + Space at Text Editor",
"description": "Adds intellense to the Text Editor",
"warning": "Only works with 2.57 intellisense",
"wiki_url": "",
"tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
"category": "Development"}
import bpy
from bpy_extras import keyconfig_utils
def complete(context):
from console import intellisense
from console_python import get_console
sc = context.space_data
text = sc.text
region = context.region
for area in context.screen.areas:
if area.type == "CONSOLE":
region = area.regions[1]
break
console = get_console(hash(region))[0]
line = text.current_line.body
cursor = text.current_character
result = intellisense.expand(line, cursor, console.locals, bpy.app.debug)
return result
class Intellimenu(bpy.types.Menu):
bl_label = ""
bl_idname = "IntelliMenu"
text = ""
def draw(self, context):
layout = self.layout
# Very ugly see how can i fix this
options = complete(context)
options = options[2].split(" ")
for op in options:
layout.operator("text.intellioptions", text=op).text = op
# This operator executes when hits Ctrl+Space at the text editor
class Intellisense(bpy.types.Operator):
bl_idname = "text.intellisense"
bl_label = "Text Editor Intellisense"
text = ""
"""
@classmethod
def poll(cls, context):
return context.active_object is not None
"""
def execute(self, context):
sc = context.space_data
text = sc.text
if text.current_character > 0:
result = complete(context)
# print(result)
if result[2] == "":
text.current_line.body = result[0]
bpy.ops.text.move(type='LINE_END')
else:
bpy.ops.wm.call_menu(name=Intellimenu.bl_idname)
return {'FINISHED'}
# this operator completes the line with the options you choose from the menu
class Intellioptions(bpy.types.Operator):
bl_idname = "text.intellioptions"
bl_label = "Intellisense options"
text = bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
sc = context.space_data
text = sc.text
if not text:
self.report({'WARNING'},
"No Text-Block active. Operation Cancelled")
return {"CANCELLED"}
comp = self.text
line = text.current_line.body
lline = len(line)
lcomp = len(comp)
# intersect text
intersect = [-1, -1]
for i in range(lcomp):
val1 = comp[0: i + 1]
for j in range(lline):
val2 = line[lline - j - 1::]
# print(" ",j, val2)
if val1 == val2:
intersect = [i, j]
break
if intersect[0] > -1:
newline = line[0: lline - intersect[1] - 1] + comp
else:
newline = line + comp
# print(newline)
text.current_line.body = newline
bpy.ops.text.move(type='LINE_END')
return {'FINISHED'}
def send_console(context, alls=0):
sc = context.space_data
text = sc.text
console = None
for area in bpy.context.screen.areas:
if area.type == "CONSOLE":
from console_python import get_console
console = get_console(hash(area.regions[1]))[0]
if console is None:
return {'FINISHED'}
if alls:
for l in text.lines:
console.push(l.body)
else:
# print(console.prompt)
console.push(text.current_line.body)
class TestLine(bpy.types.Operator):
bl_idname = "text.test_line"
bl_label = "Test line"
all = bpy.props.BoolProperty(default=False)
"""
@classmethod
def poll(cls, context):
return context.active_object is not None
"""
def execute(self, context):
# print("test line")
# send_console(context, self.all)
sc = context.space_data
text = sc.text
if not text:
self.report({'WARNING'},
"No Text-Block active. Operation Cancelled")
return {"CANCELLED"}
line = text.current_line.body
console = None
for area in bpy.context.screen.areas:
if area.type == "CONSOLE":
from console_python import get_console
console = get_console(hash(area.regions[1]))[0]
if console is None:
return {'FINISHED'}
command = ""
forindex = line.find("for ")
if forindex > -1:
var = line[forindex + 4: -1]
var = var[0: var.find(" ")]
state = line[line.rindex(" ") + 1: -1]
command = var + " = " + state + "[0]"
else:
command = line
# print(command)
try:
console.push(command)
except:
pass
bpy.ops.text.line_break()
return {'FINISHED'}
class SetBreakPoint(bpy.types.Operator):
bl_idname = "text.set_breakpoint"
bl_label = "Set Breakpoint"
def execute(self, context):
sc = bpy.context.space_data
text = sc.text
if not text:
self.report({'WARNING'},
"No Text-Block active. Operation Cancelled")
return {"CANCELLED"}
line = text.current_line
br = " #breakpoint"
# print(line.body.find(br))
if line.body.find(br) > -1:
line.body = line.body.replace(br, "")
else:
line.body += br
return {'FINISHED'}
class Debug(bpy.types.Operator):
bl_idname = "text.debug"
bl_label = "Debug"
def execute(self, context):
success = False
binpath = bpy.app.binary_path
addonspath = binpath[
0: binpath.rindex("\\") + 1] + \
str(bpy.app.version[0]) + "." + \
str(bpy.app.version[1]) + "\\scripts\\addons\\"
print(addonspath)
sc = context.space_data
text = sc.text
if not text:
self.report({'WARNING'},
"No Text-Block active. Operation Cancelled")
return {"CANCELLED"}
br = " #breakpoint"
filepath = addonspath + "debug.py"
with open(filepath, "w") as files:
files.write("import pdb\n")
for line in text.lines:
l = line.body
if line.body.find(br) > -1:
indent = ""
for letter in line.body:
if not letter.isalpha():
indent += letter
else:
break
files.write(l[0: -len(br)] + "\n")
files.write(indent + "pdb.set_trace()\n")
else:
files.write(line.body + "\n")
files.close()
success = True
# not working as runcall expects a specific function to be called not a string
# import pdb
# import debug
# pdb.runcall("debug")
if success:
self.report({'INFO'},
"Created a debug file: {}".format(filepath))
return {'FINISHED'}
class DebugPanel(bpy.types.Panel):
bl_label = "Debug"
bl_space_type = "TEXT_EDITOR"
bl_region_type = "UI"
# bl_context = "object"
text = bpy.props.StringProperty()
def draw(self, context):
layout = self.layout
row = layout.row()
row = layout.row()
row.operator("text.debug", text="Debug")
row = layout.row()
row.operator("text.set_breakpoint")
row = layout.row()
row.operator("text.test_line").all = False
row = layout.row()
row.operator("text.test_line", text="Test All").all = True
row = layout.row()
row.label(text="Coming Soon ...")
# ASSIGN A KEY
# section = Input section. "Window, Text, ..."
# name = operator name or wm.call_menu
# type = key
# event = keyboard event (Press, release, ...)
# mods = array containing key modifiers (["ctrl", "alt", "shift"]
# propvalue = menu name, if name is set to "wm.call_menu"
# overwrite doesnt work at the moment
def assignKey(section, name, type, event, mods=[], propvalue="", overwrite=0):
kconf = bpy.context.window_manager.keyconfigs.active
# check section
validsections = [item.name for item in kconf.keymaps]
if section not in validsections:
print(section + " is not a valid section.")
# print(validsections)
return False
# check type
type = type.upper()
validkeys = [item.identifier for item in bpy.types.KeyMapItem.bl_rna.properties['type'].enum_items]
if type not in validkeys:
print(type + " is not a valid key.")
# print(validkeys)
return False
# check event
event = event.upper()
validevents = [item.identifier for item in bpy.types.KeyMapItem.bl_rna.properties['value'].enum_items]
if event not in validevents:
print(event + " is not a valid event.")
# print(validevents)
kmap = kconf.keymaps[section]
# get mods
for i, mod in enumerate(mods):
mods[i] = mod.lower()
# any, shift, ctrl, alt, oskey
kmod = [False, False, False, False, False]
if "any" in mods:
kmod[0] = True
if "shift" in mods:
kmod[1] = True
if "ctrl" in mods:
kmod[2] = True
if "alt" in mods:
kmod[3] = True
if "oskey" in mods:
kmod[4] = True
# check if key exist
kexists = False
for key in kmap.keymap_items:
keymods = [key.any, key.shift, key.ctrl, key.alt, key.oskey]
if key.type == type and keymods == kmod:
kexists = True
print(key, "key exists")
break
if kexists:
# overwrite?
if overwrite:
key.idname = name
# key.type = type
else:
# create key
key = kmap.keymap_items.new(name, type, event, False)
key.any = kmod[0]
key.shift = kmod[1]
key.ctrl = kmod[2]
key.alt = kmod[3]
key.oskey = kmod[4]
if propvalue != "":
key.properties.name = propvalue
# ------------------- REGISTER ------------------------------------------------
# For explanation of the data structure look in modules\bpy_extras\keyconfig_utils
# (1) Identifier, (2) operator call/key assigment, (3) properties passed on call
KEYMAPS = (
(("Text", "VIEW_3D", "WINDOW", False), ( # (1)
({"idname": Intellisense.bl_idname,
"type": 'SPACE', "value": 'PRESS', "ctrl": True}, # (2)
()), # (3)
({"idname": TestLine.bl_idname,
"type": 'RET', "value": 'PRESS'}, # (2)
()), # (3)
)),
)
classes = (
Intellisense,
Intellioptions,
Intellimenu,
DebugPanel,
TestLine,
SetBreakPoint,
Debug,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
keyconfig_utils.addon_keymap_register(bpy.context.window_manager, KEYMAPS)
# note: these keys were assigned and never removed
# moved to the new system introduced in 2.78.x (the old function is still here)
# assignKey("Text", "text.intellisense", "SPACE", "Press", ["ctrl"])
# assignKey("Text", "text.test_line", "RET", "Press", [], "", 1)
def unregister():
keyconfig_utils.addon_keymap_unregister(bpy.context.window_manager, KEYMAPS)
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()