|
9 | 9 | //! a code editor command or task when you're met with a messy file. |
10 | 10 | use tree_sitter::{Node, Query, QueryCursor, StreamingIterator, Tree}; |
11 | 11 |
|
| 12 | +/// This lists and assigns priority values to Godot built-in virtual methods. |
| 13 | +/// This allows us to first to detect and classify them, and secondly to order |
| 14 | +/// them correctly. Returns 0 if the method is not a built-in virtual method. |
| 15 | +fn get_builtin_virtual_priority(method_name: &str) -> u8 { |
| 16 | + match method_name { |
| 17 | + "_init" => 1, |
| 18 | + "_enter_tree" => 2, |
| 19 | + "_ready" => 3, |
| 20 | + "_process" => 4, |
| 21 | + "_physics_process" => 5, |
| 22 | + "_exit_tree" => 6, |
| 23 | + "_input" => 7, |
| 24 | + "_unhandled_input" => 8, |
| 25 | + "_gui_input" => 9, |
| 26 | + "_draw" => 10, |
| 27 | + "_notification" => 11, |
| 28 | + "_get_configuration_warnings" => 12, |
| 29 | + "_validate_property" => 13, |
| 30 | + "_get_property_list" => 14, |
| 31 | + "_property_can_revert" => 15, |
| 32 | + "_property_get_revert" => 16, |
| 33 | + "_get" => 17, |
| 34 | + "_set" => 18, |
| 35 | + "_to_string" => 19, |
| 36 | + // Control node virtual methods |
| 37 | + "_accessibility_get_contextual_info" => 20, |
| 38 | + "_can_drop_data" => 21, |
| 39 | + "_drop_data" => 22, |
| 40 | + "_get_accessibility_container_name" => 23, |
| 41 | + "_get_drag_data" => 24, |
| 42 | + "_get_minimum_size" => 25, |
| 43 | + "_get_tooltip" => 26, |
| 44 | + "_has_point" => 27, |
| 45 | + "_make_custom_tooltip" => 28, |
| 46 | + "_structured_text_parser" => 29, |
| 47 | + _ => 0, |
| 48 | + } |
| 49 | +} |
| 50 | + |
12 | 51 | /// This method parses the GDScript content, extracts top-level elements, |
13 | 52 | /// and reorders them according to the GDScript style guide. |
14 | 53 | pub fn reorder_gdscript_elements( |
@@ -107,29 +146,6 @@ struct PendingAttachment { |
107 | 146 | text: String, |
108 | 147 | } |
109 | 148 |
|
110 | | -/// This constant lists built-in virtual methods in the order they should appear. |
111 | | -/// The higher the method is in the list, the higher the priority (i.e. _init comes before _ready). |
112 | | -const BUILTIN_VIRTUAL_METHODS: &[&str] = &[ |
113 | | - "_init", |
114 | | - "_enter_tree", |
115 | | - "_ready", |
116 | | - "_process", |
117 | | - "_physics_process", |
118 | | - "_exit_tree", |
119 | | - "_input", |
120 | | - "_unhandled_input", |
121 | | - "_gui_input", |
122 | | - "_draw", |
123 | | - "_notification", |
124 | | - "_get_configuration_warnings", |
125 | | - "_validate_property", |
126 | | - "_get_property_list", |
127 | | - "_property_can_revert", |
128 | | - "_property_get_revert", |
129 | | - "_get", |
130 | | - "_set", |
131 | | - "_to_string", |
132 | | -]; |
133 | 149 |
|
134 | 150 | impl GDScriptTokenKind { |
135 | 151 | /// Returns the ordering priority for this kind of declaration. The lower the |
@@ -660,10 +676,13 @@ fn classify_element( |
660 | 676 | MethodType::StaticInit |
661 | 677 | } else if is_static { |
662 | 678 | MethodType::StaticFunction |
663 | | - } else if let Some(priority) = get_builtin_virtual_priority(&name) { |
664 | | - MethodType::BuiltinVirtual(priority) |
665 | 679 | } else { |
666 | | - MethodType::Custom |
| 680 | + let priority = get_builtin_virtual_priority(&name); |
| 681 | + if priority != 0 { |
| 682 | + MethodType::BuiltinVirtual(priority) |
| 683 | + } else { |
| 684 | + MethodType::Custom |
| 685 | + } |
667 | 686 | }; |
668 | 687 |
|
669 | 688 | Ok(Some(GDScriptTokenKind::Method( |
@@ -814,13 +833,6 @@ fn is_static_method(node: Node, content: &str) -> bool { |
814 | 833 | text.contains("static func") |
815 | 834 | } |
816 | 835 |
|
817 | | -fn get_builtin_virtual_priority(method_name: &str) -> Option<u8> { |
818 | | - BUILTIN_VIRTUAL_METHODS |
819 | | - .iter() |
820 | | - .enumerate() |
821 | | - // Position in the list is the priority |
822 | | - .find_map(|(index, name)| (*name == method_name).then_some((index + 1) as u8)) |
823 | | -} |
824 | 836 |
|
825 | 837 | /// Sorts declarations according to the GDScript style guide and returns the ordered list. |
826 | 838 | fn sort_gdscript_tokens( |
|
0 commit comments