|
| 1 | +@tool |
| 2 | +extends EditorPlugin |
| 3 | + |
| 4 | +var use_shift: bool = false ## Hold shift with page-up and page-down to scroll to funcs |
| 5 | + |
| 6 | +## Editor setting path |
| 7 | +const SCRIPT_USE_SHIFT: StringName = &"plugin/gdscript_func_finder/use_shift" |
| 8 | + |
| 9 | + |
| 10 | +func _enter_tree() -> void: |
| 11 | + if ProjectSettings.has_setting(SCRIPT_USE_SHIFT): |
| 12 | + use_shift = ProjectSettings.get_setting(SCRIPT_USE_SHIFT, use_shift) |
| 13 | + else: |
| 14 | + ProjectSettings.set_setting(SCRIPT_USE_SHIFT, use_shift) |
| 15 | + ProjectSettings.set_initial_value(SCRIPT_USE_SHIFT, use_shift) |
| 16 | + ProjectSettings.set_as_basic(SCRIPT_USE_SHIFT, true) |
| 17 | + |
| 18 | + ProjectSettings.settings_changed.connect(sync_settings) |
| 19 | + |
| 20 | + |
| 21 | +func _exit_tree() -> void: |
| 22 | + ProjectSettings.settings_changed.disconnect(sync_settings) |
| 23 | + |
| 24 | + |
| 25 | +func sync_settings() -> void: |
| 26 | + use_shift = ProjectSettings.get_setting(SCRIPT_USE_SHIFT, use_shift) |
| 27 | + |
| 28 | + |
| 29 | +func _input(event: InputEvent) -> void: |
| 30 | + if event is InputEventKey: |
| 31 | + # Page Up |
| 32 | + if event.keycode == KEY_PAGEUP and event.pressed and (!use_shift or event.shift_pressed): |
| 33 | + var code_edit: CodeEdit = EditorInterface.get_script_editor().get_current_editor().get_base_editor() |
| 34 | + if code_edit.has_focus(): |
| 35 | + move_prev_function(code_edit) |
| 36 | + get_viewport().set_input_as_handled() |
| 37 | + |
| 38 | + # Page down |
| 39 | + if event.keycode == KEY_PAGEDOWN and event.pressed and (!use_shift or event.shift_pressed): |
| 40 | + var code_edit: CodeEdit = EditorInterface.get_script_editor().get_current_editor().get_base_editor() |
| 41 | + if code_edit.has_focus(): |
| 42 | + move_next_function(code_edit) |
| 43 | + get_viewport().set_input_as_handled() |
| 44 | + |
| 45 | + |
| 46 | +func move_prev_function(code_edit: CodeEdit) -> void: |
| 47 | + var caret_line = code_edit.get_caret_line() |
| 48 | + var text_lines = code_edit.text.split("\n") |
| 49 | + |
| 50 | + # Search backward for the function definition |
| 51 | + for i in range(caret_line-1, -1, -1): |
| 52 | + var line = text_lines[i].strip_edges() |
| 53 | + if line.begins_with("func "): |
| 54 | + code_edit.set_caret_line(i) |
| 55 | + code_edit.set_caret_column(line.length()) |
| 56 | + return |
| 57 | + |
| 58 | + |
| 59 | +func move_next_function(code_edit: CodeEdit) -> void: |
| 60 | + var caret_line = code_edit.get_caret_line() |
| 61 | + var text_lines = code_edit.text.split("\n") |
| 62 | + |
| 63 | + # Search fowards for the function definition |
| 64 | + for i in range(caret_line+1, text_lines.size()): |
| 65 | + var line = text_lines[i].strip_edges() |
| 66 | + if line.begins_with("func "): |
| 67 | + code_edit.set_caret_line(i) |
| 68 | + code_edit.set_caret_column(line.length()) |
| 69 | + return |
0 commit comments