-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.py
More file actions
773 lines (712 loc) · 30.2 KB
/
Copy patheditor.py
File metadata and controls
773 lines (712 loc) · 30.2 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
import os
from collections import deque
import blf
import bpy
import gpu
from gpu_extras.batch import batch_for_shader
from . import ast_defs, file_loading
from .backends.builtin_nodes import (
NodeInstance,
geometry_node_aliases,
instances,
levenshtein_distance,
nodes,
shader_geo_node_aliases,
shader_node_aliases,
)
from .backends.geometry_nodes import geometry_nodes
from .backends.shader_nodes import shader_nodes
from .backends.type_defs import string_to_data_type
from .compiler import Error
from .mf_parser import Parser
from .scanner import Scanner, Token, TokenType
add_on_dir = os.path.dirname(os.path.realpath(__file__))
font_directory = os.path.join(add_on_dir, "fonts")
font_paths = {
"regular": os.path.join(font_directory, "Anonymous_Pro.ttf"),
"italic": os.path.join(font_directory, "Anonymous_Pro_I.ttf"),
"bold": os.path.join(font_directory, "Anonymous_Pro_0.ttf"),
"bold_italic": os.path.join(font_directory, "Anonymous_Pro_BI.ttf"),
}
fonts = {
"bfont": 0,
}
def register():
Editor.reload_fonts()
def unregister():
for font_path in font_paths.values():
blf.unload(font_path)
rect_vertices = ((0, 0), (1, 0), (0, 1), (1, 1))
rect_indices = ((0, 1, 2), (2, 1, 3))
if not bpy.app.background:
rect_shader = gpu.shader.from_builtin("UNIFORM_COLOR")
rect_batch = batch_for_shader(
rect_shader, "TRIS", {"pos": rect_vertices}, indices=rect_indices
)
class Editor:
def __init__(self, pos: tuple[float, float]) -> None:
self.pos = pos
self.lines: list[str] = [""]
self.line_tokens: list[list[Token]] = [[]]
self.cursor_col: int = 0
self.cursor_row: int = 0
self.draw_cursor_col: int = 0
self.scanner = Scanner("")
self.errors: list[Error] = []
self.suggestions: deque[str] = deque()
self.reload_fonts()
@staticmethod
def reload_fonts():
global fonts
fonts = {
"bfont": 0,
}
for name, font_path in font_paths.items():
fonts[name] = blf.load(font_path)
def replace_text(self, text: str):
Editor.__init__(self, self.pos)
self.paste_after_cursor(text)
@staticmethod
def get_function_output_names(
func: NodeInstance | str, aliases: list[dict[str, NodeInstance]]
) -> None | list[str]:
if isinstance(func, NodeInstance):
node = nodes[func.key]
return [out[0] for out in node.outputs]
for alias_dict in aliases:
if func in alias_dict:
node = nodes[alias_dict[func].key]
return [out[0] for out in node.outputs]
return None
def attribute_suggestions(
self, prev_token: Token, token_under_cursor: Token, tree_type: str
):
token_text = token_under_cursor.lexeme
text_start = token_under_cursor.start
dot_token = None
if token_under_cursor.lexeme == ".":
token_text = ""
text_start += 1
dot_token = token_under_cursor
else:
dot_token = prev_token
text_start += sum(
[len(line) + 1 for i, line in enumerate(self.lines) if i < self.cursor_row]
)
text = self.get_text()[:text_start]
parser = Parser(text)
ast = parser.parse()
# Find where we are in the ast.
# We know that we're always in the last statement, so we can speed up the search a little.
node = ast_defs.find(ast.body[-1], dot_token)
if node is None:
return
assert isinstance(
node, ast_defs.Attribute
), "Dot token should be from attribute node"
if not isinstance(node.value, ast_defs.Call):
return
func = node.value.func
func_name = ""
if isinstance(func, ast_defs.Attribute):
func_name = func.attr
else:
func_name = func.id
# Find which node this belongs to.
suggestions = None
if func_name in instances:
suggestions = self.get_function_output_names(
instances[func_name][0], [shader_geo_node_aliases]
)
elif tree_type == "GeometryNodeTree":
if func_name in geometry_nodes:
suggestions = self.get_function_output_names(
geometry_nodes[func_name][0],
[shader_geo_node_aliases, geometry_node_aliases],
)
else:
suggestions = self.get_function_output_names(
func_name, [shader_geo_node_aliases, geometry_node_aliases]
)
elif tree_type == "ShaderNodeTree":
if func_name in shader_nodes:
suggestions = self.get_function_output_names(
shader_nodes[func_name][0],
[shader_geo_node_aliases, shader_node_aliases],
)
else:
suggestions = self.get_function_output_names(
func_name, [shader_geo_node_aliases, shader_node_aliases]
)
if suggestions is not None:
self.suggestions += [
name for name in suggestions if name.startswith(token_text)
]
return
ty_func = None
if (
tree_type == "GeometryNodeTree"
and func_name in file_loading.file_data.geometry_nodes
):
ty_func = file_loading.file_data.geometry_nodes[func_name][0]
elif (
tree_type == "ShaderNodeTree"
and func_name in file_loading.file_data.shader_nodes
):
ty_func = file_loading.file_data.shader_nodes[func_name][0]
if ty_func is not None:
self.suggestions += [
out.name for out in ty_func.outputs if out.name.startswith(token_text)
]
def token_under_cursor(self) -> tuple[None | Token, None | Token]:
"""Returns the token under the cursor, and the token before that"""
prev_token = None
for token in self.line_tokens[self.cursor_row]:
if token.start < self.draw_cursor_col <= token.start + len(token.lexeme):
return token, prev_token
prev_token = token
return (None, None)
def try_auto_complete(self, tree_type: str) -> None:
token_under_cursor, prev_token = self.token_under_cursor()
if token_under_cursor is None:
return
if len(self.suggestions) != 0:
# Already calculated suggestions, so just use those.
suggestion = self.suggestions.popleft()
token_to_replace = token_under_cursor
if token_under_cursor.token_type is TokenType.LEFT_PAREN:
# Last thing we completed was a function, so we have
# an extra "()" at the end.
assert (
prev_token is not None
), "We completed a function, there should be a token before the '('"
token_to_replace = prev_token
# Remove the extra "()"
self.lines[self.cursor_row] = (
self.lines[self.cursor_row][: self.cursor_col - 1]
+ self.lines[self.cursor_row][self.cursor_col + 1 :]
)
if " " in suggestion:
self.replace_token(token_to_replace, f"n'{suggestion}'")
else:
self.replace_token(token_to_replace, suggestion)
if suggestion.endswith("()"):
# Place the cursor between the parenthesis.
self.draw_cursor_col -= 1
self.cursor_col = self.draw_cursor_col
self.suggestions.append(suggestion)
return
if prev_token is not None:
if prev_token.lexeme == "." or token_under_cursor.lexeme == ".":
self.attribute_suggestions(prev_token, token_under_cursor, tree_type)
options = list(instances.keys())
if tree_type == "GeometryNodeTree":
options += list(geometry_nodes.keys())
options += list(file_loading.file_data.geometry_nodes.keys())
options += list(shader_geo_node_aliases.keys())
options += list(geometry_node_aliases.keys())
else:
options += list(shader_nodes.keys())
options += list(file_loading.file_data.shader_nodes.keys())
options += list(shader_geo_node_aliases.keys())
options += list(shader_node_aliases.keys())
for name in options:
if name.startswith(token_under_cursor.lexeme):
self.suggestions.append(name + "()")
if len(self.suggestions) == 0:
# No exact matches, try with Levensthein distance
# Only do this if we have at least some text
if len(token_under_cursor.lexeme) < 4:
return
options_with_dist = []
for option in options:
d = levenshtein_distance(option, token_under_cursor.lexeme)
# Only add the best options
if d < 5:
options_with_dist.append((option + "()", d))
sorted_options = sorted(options_with_dist, key=lambda x: x[1])
self.suggestions += list(map(lambda x: x[0], sorted_options))
else:
self.suggestions = deque(sorted(self.suggestions, key=len))
if len(self.suggestions) == 0:
return
suggestion = self.suggestions.popleft()
if token_under_cursor.lexeme == ".":
self.text_after_cursor(suggestion)
else:
self.replace_token(token_under_cursor, suggestion)
if suggestion.endswith("()"):
# Place the cursor between the parenthesis.
self.draw_cursor_col -= 1
self.cursor_col = self.draw_cursor_col
self.suggestions.append(suggestion)
def text_after_cursor(self, text: str) -> None:
line = self.lines[self.cursor_row]
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col] + text + line[self.draw_cursor_col :]
)
self.draw_cursor_col += len(text)
self.cursor_col = self.draw_cursor_col
self.rescan_line()
def replace_token(self, token: Token, text: str) -> None:
start = token.start
end = start + len(token.lexeme)
line = self.lines[self.cursor_row]
first = line[:start] + text
self.draw_cursor_col = len(first)
self.cursor_col = self.draw_cursor_col
self.lines[self.cursor_row] = first + line[end:]
self.rescan_line()
def cursor_up(self) -> None:
self.suggestions.clear()
if self.cursor_row == 0:
return
else:
self.cursor_row -= 1
# Make sure that we don't draw outside of the line, but
# at the same time keep track of where the actual cursor is
# in case we jump to a longer line next.
self.draw_cursor_col = min(
len(self.lines[self.cursor_row]), self.cursor_col
)
def cursor_down(self) -> None:
self.suggestions.clear()
if self.cursor_row == len(self.lines) - 1:
return
else:
self.cursor_row += 1
# Make sure that we don't draw outside of the line, but
# at the same time keep track of where the actual cursor is
# in case we jump to a longer line next.
self.draw_cursor_col = min(
len(self.lines[self.cursor_row]), self.cursor_col
)
def cursor_left(self) -> None:
self.suggestions.clear()
if self.draw_cursor_col == 0:
if self.cursor_row != 0:
self.cursor_row -= 1
self.draw_cursor_col = len(self.lines[self.cursor_row])
else:
self.draw_cursor_col -= 1
self.cursor_col = self.draw_cursor_col
def cursor_right(self) -> None:
self.suggestions.clear()
if self.draw_cursor_col == len(self.lines[self.cursor_row]):
if self.cursor_row != len(self.lines) - 1:
self.cursor_row += 1
self.draw_cursor_col = 0
else:
self.draw_cursor_col += 1
self.cursor_col = self.draw_cursor_col
def cursor_home(self) -> None:
self.suggestions.clear()
self.draw_cursor_col = 0
self.cursor_col = self.draw_cursor_col
def cursor_end(self) -> None:
self.suggestions.clear()
self.draw_cursor_col = len(self.lines[self.cursor_row])
self.cursor_col = self.draw_cursor_col
def delete_before_cursor(self) -> None:
self.suggestions.clear()
if self.draw_cursor_col == 0:
if self.cursor_row == 0:
self.cursor_col = 0
return
# Merge this line with previous one.
self.draw_cursor_col = len(self.lines[self.cursor_row - 1])
self.lines[self.cursor_row - 1] += self.lines[self.cursor_row]
self.cursor_row -= 1
self.rescan_line()
self.cursor_col = self.draw_cursor_col
self.lines.pop(self.cursor_row + 1)
self.line_tokens.pop(self.cursor_row + 1)
return
line = self.lines[self.cursor_row]
match (self.get_char_before_cursor(), self.get_char_after_cursor()):
case ("(", ")") | ("{", "}") | ("[", "]") | ('"', '"') | ("'", "'"):
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col - 1] + line[self.draw_cursor_col + 1 :]
)
case _:
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col - 1] + line[self.draw_cursor_col :]
)
self.draw_cursor_col -= 1
self.cursor_col = self.draw_cursor_col
self.rescan_line()
def delete_after_cursor(self) -> None:
self.suggestions.clear()
line = self.lines[self.cursor_row]
self.cursor_col = self.draw_cursor_col
if self.draw_cursor_col == len(line):
if self.cursor_row == len(self.lines) - 1:
return
# Merge this next line with this one.
self.lines[self.cursor_row] += self.lines[self.cursor_row + 1]
self.rescan_line()
self.lines.pop(self.cursor_row + 1)
self.line_tokens.pop(self.cursor_row + 1)
return
self.draw_cursor_col += 1
match (self.get_char_before_cursor(), self.get_char_after_cursor()):
case ("(", ")") | ("{", "}") | ("[", "]") | ('"', '"') | ("'", "'"):
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col - 1] + line[self.draw_cursor_col + 1 :]
)
case _:
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col - 1] + line[self.draw_cursor_col :]
)
self.draw_cursor_col -= 1
self.rescan_line()
def paste_after_cursor(self, text: str) -> None:
# Replace tabs with two spaces since the font drawing code doesn't like tabs.
text = text.expandtabs(2)
self.suggestions.clear()
line = self.lines[self.cursor_row]
if (index := text.find("\n")) != -1:
self.lines[self.cursor_row] = line[: self.draw_cursor_col] + text[:index]
self.rescan_line()
line = line[self.draw_cursor_col :]
text = text[index + 1 :]
self.draw_cursor_col = len(self.lines[self.cursor_row])
self.new_line()
while True:
if text == "":
break
if (index := text.find("\n")) != -1:
self.lines[self.cursor_row] = text[:index]
self.rescan_line()
text = text[index + 1 :]
self.draw_cursor_col = len(self.lines[self.cursor_row])
self.new_line()
else:
self.lines[self.cursor_row] = text + line
self.draw_cursor_col = 0
self.rescan_line()
break
else:
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col] + text + line[self.draw_cursor_col :]
)
self.rescan_line()
self.draw_cursor_col += len(text)
self.cursor_col = self.draw_cursor_col
def add_char_after_cursor(self, char: str) -> None:
self.suggestions.clear()
line = self.lines[self.cursor_row]
# Auto-close parenthesis.
match char:
case "(":
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col] + "()" + line[self.draw_cursor_col :]
)
case "[":
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col] + "[]" + line[self.draw_cursor_col :]
)
case "{":
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col] + "{}" + line[self.draw_cursor_col :]
)
# Only add closing bracket if not already there
case ")" | "]" | "}":
if self.get_char_after_cursor() != char:
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col]
+ char
+ line[self.draw_cursor_col :]
)
case '"' | "'":
text = char * 2
if (token := self.token_under_cursor()[0]) is not None:
if token.token_type is TokenType.ERROR:
# Possibly inside an unclosed string,
# so just add the closing " or '
text = char
if self.get_char_after_cursor() == char:
text = ""
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col] + text + line[self.draw_cursor_col :]
)
case _:
self.lines[self.cursor_row] = (
line[: self.draw_cursor_col] + char + line[self.draw_cursor_col :]
)
self.draw_cursor_col += 1
self.cursor_col = self.draw_cursor_col
self.rescan_line()
def indentation(self, row: int) -> int:
"""The number of spaces at the start of the given line"""
line = self.lines[row]
return len(line) - len(line.lstrip())
def get_char_before_cursor(self) -> str | None:
if self.draw_cursor_col <= 0 or self.draw_cursor_col - 1 >= len(
self.lines[self.cursor_row]
):
return None
return self.lines[self.cursor_row][self.draw_cursor_col - 1]
def get_char_after_cursor(self) -> str | None:
if len(self.lines[self.cursor_row]) == 0 or self.draw_cursor_col >= len(
self.lines[self.cursor_row]
):
return None
return self.lines[self.cursor_row][self.draw_cursor_col]
def new_line(self) -> None:
self.suggestions.clear()
indentation = self.indentation(self.cursor_row)
if self.get_char_before_cursor() == "{":
indentation += 2
if self.draw_cursor_col != len(self.lines[self.cursor_row]):
closing_brace = self.get_char_after_cursor() == "}"
line = self.lines[self.cursor_row]
self.lines[self.cursor_row] = line[: self.draw_cursor_col]
self.rescan_line()
self.cursor_row += 1
if closing_brace:
self.lines.insert(self.cursor_row, " " * indentation)
self.line_tokens.insert(self.cursor_row, [])
self.rescan_line()
self.cursor_row += 1
self.lines.insert(
self.cursor_row,
" " * max(0, indentation - 2) + line[self.draw_cursor_col :],
)
self.line_tokens.insert(self.cursor_row, [])
self.rescan_line()
self.cursor_row -= 1
else:
self.lines.insert(
self.cursor_row, " " * indentation + line[self.draw_cursor_col :]
)
self.line_tokens.insert(self.cursor_row, [])
self.rescan_line()
self.cursor_col = indentation
self.draw_cursor_col = indentation
return
self.cursor_row += 1
self.lines.insert(self.cursor_row, " " * indentation)
self.line_tokens.insert(self.cursor_row, [])
self.cursor_col = indentation
self.draw_cursor_col = indentation
def rescan_line(self) -> None:
line = self.cursor_row
self.scanner.reset(self.lines[line])
# Expects a 1-based index
self.scanner.line = line + 1
self.line_tokens[line] = []
while (token := self.scanner.scan_token()).token_type != TokenType.EOL:
self.line_tokens[line].append(token)
def get_text(self) -> str:
return "\n".join(self.lines)
def draw_suggestions(
self,
formula_pos: tuple[float, float],
padding: float,
char_size: tuple[float, float],
num_suggestions: int,
bg_color: tuple[float, float, float],
font_color: tuple[float, float, float],
font_id: int,
):
# First calculate where the suggestion will appear
posx, posy = formula_pos
char_width, char_height = char_size
posy -= self.cursor_row * char_height
suggestion_offset = char_width * self.draw_cursor_col
token_uc, prev_token_uc = self.token_under_cursor()
if token_uc is not None and token_uc.token_type is TokenType.LEFT_PAREN:
assert prev_token_uc is not None, "Should be the function identifier"
suggestion_offset -= char_width * (len(prev_token_uc.lexeme) + 1)
elif token_uc is not None and token_uc.token_type is TokenType.IDENTIFIER:
suggestion_offset -= char_width * (len(token_uc.lexeme))
gpu.matrix.translate(
[
posx + suggestion_offset - padding,
posy - 0.3 * char_height,
]
)
gpu.matrix.scale(
[
max(
[
len(suggestion)
for _, suggestion in zip(
range(num_suggestions), self.suggestions
)
]
)
* char_width
+ 2 * padding,
-min(num_suggestions + 1, len(self.suggestions)) * char_height
- padding,
]
)
rect_shader.uniform_float("color", (bg_color[0], bg_color[1], bg_color[2], 1))
rect_batch.draw(rect_shader)
gpu.matrix.load_identity()
# Show the suggestions.
for i, suggestion in zip(range(num_suggestions + 1), self.suggestions):
# Decrease alpha with each suggestion.
blf.color(
font_id,
font_color[0],
font_color[1],
font_color[2],
max(0.6 ** (i + 1), 0.03),
)
blf.position(
font_id,
posx + suggestion_offset,
posy - (i + 1) * char_height,
0,
)
if i == num_suggestions:
# Just show that there are more suggestions
suggestion = "..."
blf.draw(font_id, suggestion)
def draw_callback_px(self, context: bpy.types.Context):
prefs = context.preferences.addons[__package__].preferences
font_id = fonts["regular"]
font_size = prefs.font_size # type: ignore
blf.size(font_id, font_size)
char_width = blf.dimensions(font_id, "H")[0]
char_height = blf.dimensions(font_id, "Hq")[1] * 1.3
# Set the initial positions of the text
posx = self.pos[0]
posy = self.pos[1]
posz = 0
# Get the dimensions so that we know where to place the next stuff
formula_width: float = blf.dimensions(font_id, "Formula: ")[0]
info_message = (
"(Press CTRL + ENTER to confirm, ESC to cancel)"
+ f" (Line:{self.cursor_row+1} Col:{self.draw_cursor_col+1})"
)
info_width = blf.dimensions(font_id, info_message)[0]
# Bounding box for draw area
bb_width = max(
max([len(line) for line in self.lines]) * char_width + formula_width,
info_width,
)
bb_height = (len(self.lines) + 1) * char_height
# Draw the background
gpu.state.blend_set("ALPHA")
padding = 20
gpu.matrix.translate([posx - padding, posy + 2 * char_height + padding])
gpu.matrix.scale([bb_width + 2 * padding, -bb_height - 2 * padding])
bg = prefs.background_color # type: ignore
alpha = prefs.background_alpha # type: ignore
rect_shader.uniform_float("color", (bg[0], bg[1], bg[2], alpha))
rect_batch.draw(rect_shader)
gpu.matrix.load_identity()
gpu.state.blend_set("NONE")
# Draw the info message
blf.color(font_id, 0.4, 0.5, 0.1, 1.0)
blf.position(font_id, posx, posy + char_height, posz)
blf.draw(font_id, info_message)
blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
blf.position(font_id, posx, posy, posz)
blf.draw(font_id, "Formula: ")
for line_num, tokens in enumerate(self.line_tokens):
line = self.lines[line_num]
prev = 0
line_posx = posx + formula_width
line_posy = posy - char_height * line_num
for i, token in enumerate(tokens):
blf.position(font_id, line_posx, line_posy, posz)
text = token.lexeme
start = token.start
# Draw white space
white_space = line[prev:start]
for char in white_space:
blf.position(font_id, line_posx, line_posy, posz)
blf.draw(font_id, char)
line_posx += char_width
token_font_style = font_id
prev_token = tokens[i - 1] if i > 0 else token
if token.token_type == TokenType.IDENTIFIER:
if prev_token.token_type == TokenType.COLON:
# Check if it's a valid type
color(
token_font_style,
prefs.type_color # type: ignore
if token.lexeme in string_to_data_type
else prefs.identifier_color, # type: ignore
)
else:
next_token = tokens[i + 1] if i + 1 < len(tokens) else token
if next_token.token_type == TokenType.LEFT_PAREN:
color(
token_font_style, prefs.function_color # type: ignore
)
else:
color(token_font_style, prefs.identifier_color) # type: ignore
elif (
TokenType.OUT.value <= token.token_type.value <= TokenType.AND.value
):
color(token_font_style, prefs.keyword_color) # type: ignore
elif token.token_type in (TokenType.INT, TokenType.FLOAT):
color(token_font_style, prefs.number_color) # type: ignore
elif token.token_type == TokenType.PYTHON:
token_font_style = fonts["bold"]
color(token_font_style, prefs.python_color) # type: ignore
elif token.token_type == TokenType.ERROR:
token_font_style = fonts["italic"]
color(token_font_style, prefs.error_color) # type: ignore
elif token.token_type == TokenType.STRING:
color(token_font_style, prefs.string_color) # type: ignore
elif token.token_type == TokenType.GROUP_NAME:
color(token_font_style, prefs.function_color) # type: ignore
else:
color(token_font_style, prefs.default_color) # type: ignore
blf.size(token_font_style, font_size)
# Draw manually to ensure equal spacing and no kerning.
for char in text:
blf.position(token_font_style, line_posx, line_posy, posz)
blf.draw(token_font_style, char)
line_posx += char_width
prev = start + len(text)
# Errors
color(font_id, prefs.error_color) # type: ignore
error_base_y = posy - char_height * (len(self.lines) + 1)
for n, error in enumerate(self.errors):
blf.position(
font_id, posx + formula_width, error_base_y - n * char_height, posz
)
blf.draw(font_id, str(error.message))
macro_token = error.token
error_col = macro_token.col - 1
error_row = macro_token.line - 1
blf.position(
font_id,
posx + formula_width + char_width * error_col,
posy - char_height * error_row - char_height * 0.75,
posz,
)
blf.draw(font_id, "^" * len(error.token.lexeme))
# Draw cursor
blf.color(font_id, 0.1, 0.4, 0.7, 1.0)
blf.position(
font_id,
posx + formula_width + self.draw_cursor_col * char_width - char_width / 2,
posy - char_height * self.cursor_row,
posz,
)
blf.draw(font_id, "|")
# Show suggestions.
# Needs to be done after drawing text,
# since the suggestions appear above text.
# Only show if we have more than one suggestion.
if len(self.suggestions) > 1:
self.draw_suggestions(
(posx + formula_width, posy),
10,
(char_width, char_height),
num_suggestions=10,
bg_color=prefs.background_color, # type: ignore
font_color=prefs.default_color, # type: ignore
font_id=font_id,
)
def color(font_id, color):
blf.color(font_id, color[0], color[1], color[2], 1.0)