Skip to content

Commit

Permalink
refactor: rename
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Jul 20, 2024
1 parent d6ada29 commit 654c310
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions parsing/src/identifier_extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pub trait IdentifierExtractor {
fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option<Vec<&'a str>>;
}

pub struct RegularExpressionIdentifierExtractor(&'static str);
pub struct RegularExpression(&'static str);

impl RegularExpressionIdentifierExtractor {
impl RegularExpression {
pub fn new(regex: &'static str) -> Self {
Self(regex)
}
}

impl IdentifierExtractor for RegularExpressionIdentifierExtractor {
impl IdentifierExtractor for RegularExpression {
fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option<Vec<&'a str>> {
let identifier = regex::Regex::new(self.0)
.unwrap()
Expand All @@ -22,15 +22,15 @@ impl IdentifierExtractor for RegularExpressionIdentifierExtractor {
}
}

pub struct TreeSitterQueryIdentifierExtractor(&'static str);
pub struct TreeSitterQuery(&'static str);

impl TreeSitterQueryIdentifierExtractor {
impl TreeSitterQuery {
pub fn new(query: &'static str) -> Self {
Self(query)
}
}

impl IdentifierExtractor for TreeSitterQueryIdentifierExtractor {
impl IdentifierExtractor for TreeSitterQuery {
fn extract_identifier_from_node<'a>(&self, node: Node, src: &'a str) -> Option<Vec<&'a str>> {
let query = Query::new(node.language(), self.0).ok()?;
let mut cursor = QueryCursor::new();
Expand Down
16 changes: 8 additions & 8 deletions parsing/src/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use parsing_handlers::ParsingHandlers;
use std::collections::{HashMap, HashSet};

use crate::identifier_extractor::{
IdentifierExtractor, RegularExpressionIdentifierExtractor, TreeSitterQueryIdentifierExtractor,
IdentifierExtractor, RegularExpression, TreeSitterQuery,
};

pub struct ParserConfiguration {
Expand Down Expand Up @@ -32,38 +32,38 @@ impl From<Language> for ParserConfiguration {
identifier_extractors: {
let mut map: HashMap<&'static str, Box<dyn IdentifierExtractor>> =
HashMap::new();
map.insert("constructor_declaration", Box::new(TreeSitterQueryIdentifierExtractor::new(r#"(constructor_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#)));
map.insert("method_declaration", Box::new(TreeSitterQueryIdentifierExtractor::new(r#"(method_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#)));
map.insert("constructor_declaration", Box::new(TreeSitterQuery::new(r#"(constructor_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#)));
map.insert("method_declaration", Box::new(TreeSitterQuery::new(r#"(method_declaration name: (identifier) @method_name [parameters: (formal_parameters [ (formal_parameter type: (_) @argument_type) (spread_parameter (type_identifier) @spread_parameter "..." @spread_indicator) ]) _ ])"#)));
map.insert(
"field_declaration",
Box::new(TreeSitterQueryIdentifierExtractor::new(
Box::new(TreeSitterQuery::new(
r#"(variable_declarator name: _ @name)"#,
)),
);
map.insert(
"import_declaration",
Box::new(TreeSitterQueryIdentifierExtractor::new(
Box::new(TreeSitterQuery::new(
r#"(import_declaration ( scoped_identifier ) @namespace)"#,
)),
);

map.insert(
"class_declaration",
Box::new(RegularExpressionIdentifierExtractor::new(
Box::new(RegularExpression::new(
r#"class [A-Za-z_][A-Za-z0-9_]*"#,
)),
);

map.insert(
"enum_declaration",
Box::new(RegularExpressionIdentifierExtractor::new(
Box::new(RegularExpression::new(
r#"enum [A-Za-z_][A-Za-z0-9_]*"#,
)),
);

map.insert(
"interface_declaration",
Box::new(RegularExpressionIdentifierExtractor::new(
Box::new(RegularExpression::new(
r#"interface [A-Za-z_][A-Za-z0-9_]*"#,
)),
);
Expand Down

0 comments on commit 654c310

Please sign in to comment.