Skip to content

Commit

Permalink
feat(struct): make enable parse method name
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 22, 2021
1 parent c1d953d commit 7857bdd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
10 changes: 10 additions & 0 deletions plugins/coco_struct_analysis/src/coco_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ pub struct MethodInfo {
pub return_type: String,
}

impl MethodInfo {
pub fn new(name: &str) -> Self {
MethodInfo {
name: name.to_string(),
access: "".to_string(),
return_type: "".to_string(),
}
}
}

#[derive(Debug, Clone)]
pub struct ClassInfo {
pub name: String,
Expand Down
86 changes: 74 additions & 12 deletions plugins/coco_struct_analysis/src/ctags_parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::coco_struct::ClassInfo;
use crate::coco_struct::{ClassInfo, MethodInfo};
use regex::Regex;
use std::collections::HashMap;

Expand All @@ -7,6 +7,15 @@ pub struct CtagsParser {
pub classes: Vec<ClassInfo>,
}

impl Default for CtagsParser {
fn default() -> Self {
CtagsParser {
class_map: Default::default(),
classes: vec![],
}
}
}

lazy_static! {
static ref CLASS_RE: Regex = Regex::new(
r"(?x)
Expand All @@ -15,16 +24,20 @@ lazy_static! {
\t([^\t]+)\tclass"
)
.unwrap();
static ref INHERITS_RES: Regex = Regex::new(r"inherits:([A-Za-z0-9_\:,]+)").unwrap();
}

impl Default for CtagsParser {
fn default() -> Self {
CtagsParser {
class_map: Default::default(),
classes: vec![],
}
}
static ref INHERITS_RE: Regex = Regex::new(r"inherits:([A-Za-z0-9_\:,]+)").unwrap();
static ref AVAILABLE_RE: Regex = Regex::new(
r"(?x)
^(?P<name>[A-Za-z0-9_]+)
\t(?P<data_type>[^\t]+)
\t([^\t]+)
\t(?P<tag_type>[A-Za-z]+)"
)
.unwrap();
static ref RE_CLASS: Regex = Regex::new(
r"(?x)
class:(?P<class_name>[A-Za-z0-9_\.]+)"
)
.unwrap();
}

impl CtagsParser {
Expand All @@ -36,7 +49,42 @@ impl CtagsParser {
self.class_map.insert(class_name.to_string(), clazz);
}
}
pub fn parse_method_methods() {}
pub fn parse_method_methods(&mut self, str: &str) {
if !AVAILABLE_RE.is_match(str) {
return;
}

let captures = AVAILABLE_RE.captures(str).unwrap();

let mut class_name = "".to_string();
if let Some(capts) = RE_CLASS.captures(str) {
class_name = capts["class_name"].to_string();
}
let split = class_name.split(".");
if let Some(last) = split.last() {
class_name = last.to_string();
}

if class_name.len() <= 0 {
return;
}
let clazz;
match self.class_map.get_mut(&*class_name) {
Some(clz) => {
clazz = clz;
}
None => {
return;
}
};

let name = &captures["name"];
let tag_type = &captures["tag_type"];
if tag_type.eq("method") {
let method = MethodInfo::new(name);
clazz.method.push(method);
}
}

pub fn classes(&self) -> Vec<ClassInfo> {
let mut classes = vec![];
Expand All @@ -62,4 +110,18 @@ mod test {
assert_eq!(1, classes.len());
assert_eq!("AsyncEventBus", classes[0].name);
}

#[test]
pub fn should_parse_java_method() {
let class_tags = "AsyncEventBus AsyncEventBus.java /^public class AsyncEventBus extends EventBus {$/;\" class line:31 language:Java inherits:EventBus";
let tags = "AsyncEventBus AsyncEventBus.java /^ public AsyncEventBus(Executor executor) {$/;\" method line:69 language:Java class:AsyncEventBus access:public";
let mut parser = CtagsParser::default();
parser.parse_class(class_tags);
parser.parse_method_methods(tags);

let classes = parser.classes();
let methods = &classes[0].method;
assert_eq!(1, methods.len());
assert_eq!("AsyncEventBus", methods[0].name);
}
}

0 comments on commit 7857bdd

Please sign in to comment.