Skip to content

Commit

Permalink
feat(ident): add java function name support #6
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Oct 8, 2022
1 parent 94bfac5 commit c516f1c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
5 changes: 4 additions & 1 deletion fkl_cli/src/class_info/code_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::class_info::{CodePoint, Location};
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
pub struct CodeFunction {
pub name: String,
pub vars: Vec<String>,
// todo: add support
pub return_type: String,
// todo: add support
pub variable: Vec<String>,
pub start: CodePoint,
pub end: CodePoint,
}
Expand Down
2 changes: 1 addition & 1 deletion fkl_cli/src/inserter/base_ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub trait CodeIdent {
model.set_end(node.end_position().row, node.end_position().column);
}

fn create_function(capture: QueryCapture, text: &str) -> CodeFunction {
fn insert_function(capture: QueryCapture, text: &str) -> CodeFunction {
let mut function = CodeFunction::default();
function.name = text.to_string();

Expand Down
60 changes: 37 additions & 23 deletions fkl_cli/src/inserter/java_ident.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use tree_sitter::{Node, Parser, Query, QueryCursor};
use crate::class_info::{CodeClass, CodeFile};

use crate::class_info::{CodeClass, CodeFile, CodeFunction};
use crate::inserter::base_ident::CodeIdent;

const JAVA_QUERY: &'static str = "
Expand All @@ -9,23 +10,26 @@ const JAVA_QUERY: &'static str = "
(import_declaration
(scoped_identifier) @import-name)
(method_declaration
(modifiers
(annotation
name: (identifier) @annotation.name
arguments: (annotation_argument_list)? @annotation.key_values
)
)
)
(program
(class_declaration
name: (identifier) @class-name
interfaces: (super_interfaces (interface_type_list (type_identifier) @impl-name))?
body: (class_body (method_declaration
(modifiers
(annotation
name: (identifier) @annotation.name
arguments: (annotation_argument_list)? @annotation.key_values
)
)?
type: (type_identifier) @return-type
name: (identifier) @function-name
parameters: (formal_parameters (formal_parameter
type: (type_identifier) @param-type
name: (identifier) @param-name
))?
))?
)
)
";
)";


pub struct JavaIdent {
Expand Down Expand Up @@ -64,9 +68,10 @@ impl JavaIdent {
let captures = query_cursor.captures(&ident.query, tree.root_node(), text_callback);

let mut code_file = CodeFile::default();
let mut class = CodeClass::default();
let mut is_last_node = false;

let mut class = CodeClass::default();

let capture_names = ident.query.capture_names();

for (mat, capture_index) in captures {
Expand Down Expand Up @@ -99,15 +104,18 @@ impl JavaIdent {
"impl-name" => {
class.implements.push(text.to_string());
}
"function-name" => {
class.functions.push(JavaIdent::insert_function(capture, text));
}
"parameter" => {}
&_ => {
println!(
" pattern: {}, capture: {}, row: {}, text: {:?}",
mat.pattern_index,
capture_name,
capture.node.start_position().row,
capture.node.utf8_text((&code).as_ref()).unwrap_or("")
);
// println!(
// " pattern: {}, capture: {}, row: {}, text: {:?}",
// mat.pattern_index,
// capture_name,
// capture.node.start_position().row,
// capture.node.utf8_text((&code).as_ref()).unwrap_or("")
// );
}
}
}
Expand All @@ -123,7 +131,7 @@ impl JavaIdent {

#[cfg(test)]
mod tests {
use crate::class_info::{CodeClass, CodePoint};
use crate::class_info::{CodeClass, CodeFunction, CodePoint};
use crate::inserter::base_ident::CodeIdent;
use crate::inserter::java_ident::JavaIdent;

Expand Down Expand Up @@ -213,7 +221,13 @@ class DateTimeImpl2 {
module: "".to_string(),
package: "".to_string(),
implements: vec![],
functions: vec![],
functions: vec![CodeFunction {
name: "getDate".to_string(),
return_type: "".to_string(),
variable: vec![],
start: CodePoint { row: 1, column: 4 },
end: CodePoint { row: 3, column: 5 },
}],
start: CodePoint { row: 0, column: 0 },
end: CodePoint { row: 4, column: 1 },
});
Expand Down

0 comments on commit c516f1c

Please sign in to comment.