Skip to content

Commit c48d445

Browse files
committed
Add Control virtual built-in methods to code reordering script, remove option types and array find
Close #184
1 parent 0f0d66f commit c48d445

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

src/reorder.rs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,45 @@
99
//! a code editor command or task when you're met with a messy file.
1010
use tree_sitter::{Node, Query, QueryCursor, StreamingIterator, Tree};
1111

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+
1251
/// This method parses the GDScript content, extracts top-level elements,
1352
/// and reorders them according to the GDScript style guide.
1453
pub fn reorder_gdscript_elements(
@@ -107,29 +146,6 @@ struct PendingAttachment {
107146
text: String,
108147
}
109148

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-
];
133149

134150
impl GDScriptTokenKind {
135151
/// Returns the ordering priority for this kind of declaration. The lower the
@@ -660,10 +676,13 @@ fn classify_element(
660676
MethodType::StaticInit
661677
} else if is_static {
662678
MethodType::StaticFunction
663-
} else if let Some(priority) = get_builtin_virtual_priority(&name) {
664-
MethodType::BuiltinVirtual(priority)
665679
} 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+
}
667686
};
668687

669688
Ok(Some(GDScriptTokenKind::Method(
@@ -814,13 +833,6 @@ fn is_static_method(node: Node, content: &str) -> bool {
814833
text.contains("static func")
815834
}
816835

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-
}
824836

825837
/// Sorts declarations according to the GDScript style guide and returns the ordered list.
826838
fn sort_gdscript_tokens(

0 commit comments

Comments
 (0)