Skip to content

Commit

Permalink
feat: add languages support for struct
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 24, 2021
1 parent 46a431e commit 711280f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
1 change: 1 addition & 0 deletions coco.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ repos:
- url: https://github.com/coco-rs/coco.fixtures
- url: https://github.com/coco-rs/coco.fixtures2
- url: .
languages: [Rust]
- url: https://github.com/datum-lang/scie
- url: https://github.com/projectfluent/fluent-rs
languages: [Rust, JavaScript]
Expand Down
80 changes: 67 additions & 13 deletions plugins/coco_struct_analysis/src/cmd_ctags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ impl CmdCtags {
for e in &opt.exclude {
args.push(String::from(format!("--exclude={}", e)));
}

if opt.languages.is_some() {
let langs = opt.languages.as_ref().unwrap();
args.push(String::from(format!("--languages={}", langs)));
}

args.append(&mut opt.opt_ctags.clone());

let cmd = CmdCtags::get_cmd(&opt, &args);
Expand Down Expand Up @@ -238,23 +244,11 @@ mod tests {
];
let opt = Opt::from_iter(args.iter());
let mut files = vec![];
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf();
let code_dir = root_dir
.clone()
.join("_fixtures")
.join("ctags")
.join("main.go");
let code_dir = code_dir().join("main.go");

files.push(format!("{}", code_dir.display()));
println!("{:?}", files);
let outputs = CmdCtags::call(&opt, &files).unwrap();
let out_str = str::from_utf8(&outputs[0].stdout).unwrap();
println!("{}", out_str);
let mut lines = out_str.lines();

let first_line = lines.next().unwrap_or("");
Expand All @@ -263,6 +257,66 @@ mod tests {
assert!(first_line.contains("language:Go"));
}

#[test]
fn should_return_none_when_filter_rust_in_golang() {
let args = vec![
"ptags",
"-t",
"1",
// "--bin-ctags=/usr/local/bin/ctags",
"--verbose=true",
"--fields=+latinK",
"--languages=Rust",
];
let opt = Opt::from_iter(args.iter());
let mut files = vec![];
let code_dir = code_dir().join("main.go");

files.push(format!("{}", code_dir.display()));
let outputs = CmdCtags::call(&opt, &files).unwrap();
let out_str = str::from_utf8(&outputs[0].stdout).unwrap();
let mut lines = out_str.lines();

assert!(lines.next().is_none())
}

#[test]
fn should_return_golang_only_with_filter_golang() {
let args = vec![
"ptags",
"-t",
"1",
// "--bin-ctags=/usr/local/bin/ctags",
"--verbose=true",
"--fields=+latinK",
"--languages=Go",
];
let opt = Opt::from_iter(args.iter());
let mut files = vec![];

files.push(format!("{}", code_dir().join("main.go").display()));
files.push(format!(
"{}",
code_dir().join("source").join("field.cpp").display()
));

let outputs = CmdCtags::call(&opt, &files).unwrap();
let out_str = str::from_utf8(&outputs[0].stdout).unwrap();

assert!(!out_str.contains("field.cpp"));
}

fn code_dir() -> PathBuf {
let root_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf();
let code_dir = root_dir.clone().join("_fixtures").join("ctags");
code_dir
}

// #[test]
// fn test_call_with_opt() {
// let args = vec!["ptags", "-t", "1", "--opt-ctags=-u"];
Expand Down
4 changes: 4 additions & 0 deletions plugins/coco_struct_analysis/src/ctags_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ pub struct Opt {
#[structopt(long = "fields")]
pub fields: Option<String>,

/// Languages
#[structopt(long = "languages")]
pub languages: Option<String>,

/// Glob pattern of exclude file ( ex. --exclude '*.rs' )
#[structopt(short = "e", long = "exclude", number_of_values = 1)]
pub exclude: Vec<String>,
Expand Down
6 changes: 5 additions & 1 deletion plugins/coco_struct_analysis/src/struct_analysis_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub fn execute_struct_analysis(config: CocoConfig) {
let origin_files = files_from_path(url_str);
let thread = count_thread(&origin_files);

let opt = build_opt(thread);
let mut opt = build_opt(thread);

if let Some(langs) = repo.languages {
opt.languages = Some(langs.join(","));
}

let files = files_by_thread(origin_files, &opt);
let classes = run_ctags(&opt, &files);
Expand Down

0 comments on commit 711280f

Please sign in to comment.