From d841a38c0987e5c766bc86d5046bb5c77a34aecc Mon Sep 17 00:00:00 2001 From: malkoG Date: Mon, 14 Aug 2023 01:11:59 +0900 Subject: [PATCH] Refactor `Analyzer`'s implementation * Moved language-specific behaviours which are related to tree-sitter node type. * These behaviour are rather proper with the `Language` enum. --- src/analyzer.rs | 70 ++++--------------------------------------------- src/language.rs | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 65 deletions(-) diff --git a/src/analyzer.rs b/src/analyzer.rs index bb2f2b2..b9cd84a 100644 --- a/src/analyzer.rs +++ b/src/analyzer.rs @@ -14,63 +14,12 @@ pub struct Analyzer { pub trait Traversable<'tree> { fn get_indent_comment_pool(&self) -> Vec; - fn get_annotation_whitelist(&self) -> Vec<&str>; fn analyze(&self) -> VecDeque; fn get_syntax_tree(&self) -> Tree; - fn get_nested_traversable_symbols(&self) -> Vec<&str>; fn get_whitelist_nodes(&self, tree: &'tree Tree) -> Vec<(Node<'tree>, (usize, usize, usize))>; - fn decorator_node_type(&self) -> &str; - fn comment_node_type(&self) -> &str; - fn top_level_node_type(&self) -> &str; } impl<'tree> Traversable<'tree> for Analyzer { - fn top_level_node_type(&self) -> &str { - match self.language { - Language::Rust => "source_file", - Language::Python => "module", - _ => "", - } - } - - fn decorator_node_type(&self) -> &str { - match self.language { - Language::Rust => "attribute_item", - Language::Python => "null", - _ => "", - } - } - - fn comment_node_type(&self) -> &str { - match self.language { - Language::Rust => "line_comment", - Language::Python => "comment", - _ => "", - } - } - - fn get_annotation_whitelist(&self) -> Vec<&str> { - match self.language { - Language::Rust => vec![ - "attribute_item", - "mod_item", - "enum_item", - "type_item", - "impl_item", - "function_item", - "struct_item", - "trait_item", - "macro_definition", - ], - Language::Python => vec![ - "class_definition", - "function_definition", - "decorated_definition", - ], - _ => vec![], - } - } - fn get_indent_comment_pool(&self) -> Vec { let comment = match self.language { Language::Rust => "/// [TODO]", @@ -88,15 +37,6 @@ impl<'tree> Traversable<'tree> for Analyzer { .collect() } - fn get_nested_traversable_symbols(&self) -> Vec<&str> { - match self.language { - Language::Rust => vec!["mod_item", "impl_item"], - Language::Python => vec!["class_definition"], - _ => vec![], - } - - } - fn get_syntax_tree(&self) -> Tree { let parser = RefCell::new(Parser::new()); let language = get_language(self.language.as_str()).unwrap(); @@ -115,7 +55,7 @@ impl<'tree> Traversable<'tree> for Analyzer { let tree = self.get_syntax_tree(); let nodes = self.get_whitelist_nodes(&tree); - let nested_traversable_symbols = self.get_nested_traversable_symbols(); + let nested_traversable_symbols = self.language.nested_traversable_symbols(); let mut writer_queue = VecDeque::new(); let mut pending_queue = VecDeque::new(); @@ -154,7 +94,7 @@ impl<'tree> Traversable<'tree> for Analyzer { match Range::from_node(*current_node) { node_range if cursor_position.is_member_of(node_range) => { let node_type = current_node.kind(); - if node_type == self.decorator_node_type() { + if node_type == self.language.decorator_node_type() { pending_queue.push_back(line); } else { for (node, node_symbol) in indentation_context.iter() { @@ -235,8 +175,8 @@ impl<'tree> Traversable<'tree> for Analyzer { /// All of tree sitter nodes are ordered by non decreasing order fn get_whitelist_nodes(&self, tree: &'tree Tree) -> Vec<(Node<'tree>, (usize, usize, usize))> { let mut deq = VecDeque::new(); - let whitelist = self.get_annotation_whitelist(); - let nested_traversable_symbols = self.get_nested_traversable_symbols(); + let whitelist = self.language.annotation_whitelist(); + let nested_traversable_symbols = self.language.nested_traversable_symbols(); let mut result = Vec::new(); deq.push_back(tree.root_node()); @@ -250,7 +190,7 @@ impl<'tree> Traversable<'tree> for Analyzer { } if !nested_traversable_symbols.contains(&node_type) - && node_type != self.top_level_node_type() + && node_type != self.language.top_level_node_type() { continue; } diff --git a/src/language.rs b/src/language.rs index 6966034..72c8db0 100644 --- a/src/language.rs +++ b/src/language.rs @@ -22,6 +22,61 @@ impl Language { other_extension => Self::Other(other_extension.to_string()) } } + + /// language specific tree-sitter node types + pub fn top_level_node_type(&self) -> &str { + match self { + Language::Rust => "source_file", + Language::Python => "module", + _ => "", + } + } + + pub fn decorator_node_type(&self) -> &str { + match self { + Language::Rust => "attribute_item", + Language::Python => "null", + _ => "", + } + } + + pub fn comment_node_type(&self) -> &str { + match self { + Language::Rust => "line_comment", + Language::Python => "comment", + _ => "", + } + } + + pub fn annotation_whitelist(&self) -> Vec<&str> { + match self { + Language::Rust => vec![ + "attribute_item", + "mod_item", + "enum_item", + "type_item", + "impl_item", + "function_item", + "struct_item", + "trait_item", + "macro_definition", + ], + Language::Python => vec![ + "class_definition", + "function_definition", + "decorated_definition", + ], + _ => vec![], + } + } + + pub fn nested_traversable_symbols(&self) -> Vec<&str> { + match self { + Language::Rust => vec!["mod_item", "impl_item"], + Language::Python => vec!["class_definition"], + _ => vec![], + } + } } impl From<&str> for Language{