Skip to content

Commit

Permalink
Refactor Analyzer's implementation
Browse files Browse the repository at this point in the history
* Moved language-specific behaviours which are related to tree-sitter node type.
* These behaviour are rather proper with the `Language` enum.
  • Loading branch information
malkoG committed Aug 13, 2023
1 parent b66d08c commit d841a38
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
70 changes: 5 additions & 65 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,12 @@ pub struct Analyzer {

pub trait Traversable<'tree> {
fn get_indent_comment_pool(&self) -> Vec<String>;
fn get_annotation_whitelist(&self) -> Vec<&str>;
fn analyze(&self) -> VecDeque<String>;
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<String> {
let comment = match self.language {
Language::Rust => "/// [TODO]",
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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());

Expand All @@ -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;
}
Expand Down
55 changes: 55 additions & 0 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit d841a38

Please sign in to comment.