Add --dump-syntax for debugging the syntax tree

ida_star
Wilfred Hughes 2021-08-27 22:32:48 +07:00
parent 33251f9e1a
commit 1d0b1ad6b6
2 changed files with 52 additions and 10 deletions

@ -62,6 +62,8 @@ delimiter over a delimiter that gave contiguous changes.
Removed the `--width` argument.
Added a debug option `--dump-syntax`.
## 0.6
### Parsing

@ -41,19 +41,40 @@ enum Mode {
lhs_path: String,
rhs_path: String,
},
DumpSyntax {
path: String,
},
}
fn parse_args() -> Mode {
let matches = App::new("Difftastic")
.version(VERSION)
.about("A syntax aware diff.")
.author("Wilfred Hughes")
.arg(Arg::with_name("positional_args").multiple(true))
.setting(AppSettings::ArgRequiredElseHelp)
.get_matches();
let matches =
App::new("Difftastic")
.version(VERSION)
.about("A syntax aware diff.")
.author("Wilfred Hughes")
.arg(Arg::with_name("dump-syntax").long("dump-syntax").help(
"Parse a single file with tree-sitter and display the difftastic syntax tree.",
))
.arg(Arg::with_name("positional_args").multiple(true))
.setting(AppSettings::ArgRequiredElseHelp)
.get_matches();
let args: Vec<_> = matches.values_of_lossy("positional_args").unwrap();
if matches.is_present("dump-tree-sitter") {
if args.len() == 1 {
return Mode::DumpSyntax {
path: args[0].clone(),
};
} else {
// TODO: delegate this parsing to clap.
panic!(
"Error: --dump-ts takes one argument, but got: {}",
args.len()
);
}
}
// TODO: document these different ways of calling difftastic.
let (display_path, lhs_path, rhs_path) = match &args[..] {
[lhs_path, rhs_path] => (
@ -96,10 +117,31 @@ fn parse_args() -> Mode {
fn main() {
configure_color();
let arena = Arena::new();
let mode = parse_args();
let (display_path, lhs_path, rhs_path) = match mode {
Mode::Diff { display_path, lhs_path, rhs_path} => (display_path, lhs_path, rhs_path)
Mode::DumpSyntax { path } => {
let extension = Path::new(&path).extension();
let extension = extension.unwrap_or_else(|| OsStr::new(""));
match tsp::from_extension(extension) {
Some(ts_lang) => {
let bytes = read_or_die(&path);
let src = String::from_utf8_lossy(&bytes).to_string();
let ast = tsp::parse(&arena, &src, &ts_lang);
dbg!(ast);
}
None => {
println!("No tree-sitter parser for extension: {:?}", extension);
}
}
return;
}
Mode::Diff {
display_path,
lhs_path,
rhs_path,
} => (display_path, lhs_path, rhs_path),
};
let lhs_bytes = read_or_die(&lhs_path);
@ -120,8 +162,6 @@ fn main() {
.to_string()
.replace("\t", " ");
let arena = Arena::new();
let extension = Path::new(&display_path).extension();
let extension = extension.unwrap_or_else(|| OsStr::new(""));
let ts_lang = if env::var("DFT_TS").is_ok() {