|
| 1 | +extends TextForgeMode |
| 2 | + |
| 3 | +var keyword_colors: Dictionary[Color, Array] = { |
| 4 | + Color(1, 0.45, 0.55, 1): ["def", "class", "lambda", "return", "yield", "try", "except", "finally", "raise"], |
| 5 | + Color(1, 0.6, 0.85, 1): ["if", "elif", "else", "for", "while", "break", "continue", "pass", "with", "as", "assert"], |
| 6 | + Color(1, 0.7, 0.5, 1): ["import", "from", "global", "nonlocal", "del", "and", "or", "not", "in", "is"], |
| 7 | + Color(0.9, 0.8, 0.6, 1): ["True", "False", "None"], |
| 8 | +} |
| 9 | +var code_regions: Array[Array] = [ |
| 10 | + [Color(1, 0.91, 0.64, 1), '"', '"', false], |
| 11 | + [Color(1, 0.92, 0.64, 1), "'", "'", false], |
| 12 | + [Color(1, 0.92, 0.65, 1), "'''", "'''", false], |
| 13 | + [Color(1, 0.91, 0.65, 1), '"""', '"""', false], |
| 14 | + [Color(0.8, 0.81, 0.82, 0.5), "#", "", true], |
| 15 | +] |
| 16 | + |
| 17 | +func _initialize_mode() -> Error: |
| 18 | + _initialize_highlighter() |
| 19 | + _setup_panel() |
| 20 | + comment_delimiters.append({ |
| 21 | + "start_key": "#", |
| 22 | + "end_key": "", |
| 23 | + "line_only": true, |
| 24 | + }) |
| 25 | + string_delimiters.append({ |
| 26 | + "start_key": '"', |
| 27 | + "end_key": '"', |
| 28 | + "line_only": false, |
| 29 | + }) |
| 30 | + string_delimiters.append({ |
| 31 | + "start_key": "'", |
| 32 | + "end_key": "'", |
| 33 | + "line_only": false, |
| 34 | + }) |
| 35 | + string_delimiters.append({ |
| 36 | + "start_key": '"""', |
| 37 | + "end_key": '"""', |
| 38 | + "line_only": false, |
| 39 | + }) |
| 40 | + string_delimiters.append({ |
| 41 | + "start_key": "'''", |
| 42 | + "end_key": "'''", |
| 43 | + "line_only": false, |
| 44 | + }) |
| 45 | + _enable_auto_format_feature() |
| 46 | + |
| 47 | + return OK |
| 48 | + |
| 49 | + |
| 50 | +func _auto_format(text: String) -> String: |
| 51 | + var lines := text.split("\n") |
| 52 | + var cleaned := [] |
| 53 | + var spacing_re := RegEx.new() |
| 54 | + spacing_re.compile("\\s*([=+\\-*/%<>&|^~!]=?|==|!=|<=|>=|\\*\\*|//)\\s*") |
| 55 | + |
| 56 | + for raw_line in lines: |
| 57 | + var indent_len := raw_line.length() - raw_line.lstrip(" \t").length() |
| 58 | + var indent := raw_line.substr(0, indent_len) |
| 59 | + var code := raw_line.lstrip(" \t") |
| 60 | + |
| 61 | + code = spacing_re.sub(code, " $1 ", true) |
| 62 | + code = code.replace(", ", ",") |
| 63 | + code = code.replace(",", ", ") |
| 64 | + code = code.replace("( ", "(") |
| 65 | + code = code.replace(" )", ")") |
| 66 | + code = code.replace(" :", ":") |
| 67 | + while code.find(" ") != -1: |
| 68 | + code = code.replace(" ", " ") |
| 69 | + |
| 70 | + cleaned.append(indent + code) |
| 71 | + |
| 72 | + return "\n".join(cleaned) |
| 73 | + |
| 74 | + |
| 75 | +func _update_code_completion_options(text: String) -> void: |
| 76 | + for color in keyword_colors: |
| 77 | + for keyword in keyword_colors[color]: |
| 78 | + Global.get_editor().add_code_completion_option(CodeEdit.KIND_CLASS, keyword, keyword, color) |
| 79 | + |
| 80 | + |
| 81 | +func _generate_outline(text: String) -> Array: |
| 82 | + var outline := Array() |
| 83 | + for l in text.split("\n").size(): |
| 84 | + var line := text.split("\n")[l] |
| 85 | + if line.begins_with("def "): |
| 86 | + outline.append([line.substr(5, line.find("(") - 5),l]) |
| 87 | + return outline |
| 88 | + |
| 89 | + |
| 90 | +# TODO |
| 91 | +func _lint_file(text: String) -> Array[Dictionary]: |
| 92 | + return Array([], TYPE_DICTIONARY, "", null) |
| 93 | + |
| 94 | + |
| 95 | +func _initialize_highlighter() -> void: |
| 96 | + syntax_highlighter = CodeHighlighter.new() |
| 97 | + syntax_highlighter.number_color = Color(0.85, 1, 0.7, 1) |
| 98 | + syntax_highlighter.symbol_color = Color(0.7, 0.8, 1, 1) |
| 99 | + syntax_highlighter.function_color = Color(0.35, 0.7, 1, 1) |
| 100 | + syntax_highlighter.member_variable_color = Color(0.8, 0.85, 1, 1) |
| 101 | + for color in keyword_colors: |
| 102 | + for keyword in keyword_colors[color]: |
| 103 | + syntax_highlighter.add_keyword_color(keyword, color) |
| 104 | + |
| 105 | + for region in code_regions: |
| 106 | + syntax_highlighter.add_color_region(region[1], region[2], region[0], region[3]) |
| 107 | + |
| 108 | + |
| 109 | +func _setup_panel() -> void: |
| 110 | + panel = TextForgePanel.new() |
| 111 | + var run = Button.new() |
| 112 | + var output = TextEdit.new() |
| 113 | + panel.add_child(VBoxContainer.new()) |
| 114 | + panel.get_child(0).size = Vector2(100, 300) |
| 115 | + panel.get_child(0).add_child(run) |
| 116 | + panel.get_child(0).add_child(output) |
| 117 | + run.size_flags_horizontal = Control.SIZE_FILL |
| 118 | + output.size_flags_horizontal = Control.SIZE_FILL |
| 119 | + output.size_flags_vertical = Control.SIZE_EXPAND_FILL |
| 120 | + run.text = "Run Code" |
| 121 | + run.pressed.connect(_execute.bind(output)) |
| 122 | + |
| 123 | + |
| 124 | +func _execute(output) -> void: |
| 125 | + var out: Array |
| 126 | + OS.execute("python", [Global.get_file_path()], out) |
| 127 | + output.text = "\n---\n".join(out) |
0 commit comments