Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions crates/plotnik-cli/src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,20 @@ pub fn build_cli() -> Command {
///
/// Accepts all runtime flags for unified CLI experience.
/// Shows query AST when query is provided, source AST when source is provided.
/// Single positional file is auto-detected: .ptk → query, otherwise → source.
pub fn ast_command() -> Command {
let cmd = Command::new("ast")
.about("Show AST of query and/or source file")
.override_usage(
"\
plotnik ast <QUERY> [SOURCE]
plotnik ast <FILE> # auto-detect by extension
plotnik ast <QUERY> <SOURCE> # both ASTs
plotnik ast -q <TEXT> [SOURCE]
plotnik ast <SOURCE>
plotnik ast -s <TEXT> -l <LANG>",
)
.after_help(
r#"EXAMPLES:
plotnik ast query.ptk # query AST
plotnik ast query.ptk # query AST (.ptk extension)
plotnik ast app.ts # source AST (tree-sitter)
plotnik ast query.ptk app.ts # both ASTs
plotnik ast query.ptk app.ts --raw # CST / include anonymous nodes
Expand Down
30 changes: 30 additions & 0 deletions crates/plotnik-cli/src/cli/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ impl AstParams {
let (query_path, source_path) =
shift_positional_to_source(query_text.is_some(), query_path, source_path);

// Extension-based detection: when a single file is provided without -q,
// .ptk → query, otherwise → source (detect language from extension).
let (query_path, source_path) =
detect_file_type_by_extension(query_path, source_path, query_text.is_some());

Self {
query_path,
query_text,
Expand Down Expand Up @@ -352,3 +357,28 @@ fn shift_positional_to_source(
(query_path, source_path)
}
}

/// Detect file type by extension for ast command.
/// When a single file is provided without -q: .ptk → query, otherwise → source.
fn detect_file_type_by_extension(
query_path: Option<PathBuf>,
source_path: Option<PathBuf>,
has_query_text: bool,
) -> (Option<PathBuf>, Option<PathBuf>) {
// Only apply when: single positional file, no -q flag, no explicit source
if has_query_text || source_path.is_some() {
return (query_path, source_path);
}

let Some(path) = query_path else {
return (None, None);
};

// .ptk extension → treat as query
if path.extension().is_some_and(|ext| ext == "ptk") {
return (Some(path), None);
}

// Any other extension → treat as source file
(None, Some(path))
}
59 changes: 59 additions & 0 deletions crates/plotnik-cli/src/cli/dispatch_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,3 +712,62 @@ fn check_accepts_raw_flag() {
result.err()
);
}

// Extension-based detection tests for ast command

#[test]
fn ast_detects_ptk_as_query() {
let cmd = ast_command();
let result = cmd.try_get_matches_from(["ast", "query.ptk"]);
assert!(result.is_ok());

let m = result.unwrap();
let params = AstParams::from_matches(&m);

// .ptk extension → treat as query
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, None);
}

#[test]
fn ast_detects_non_ptk_as_source() {
let cmd = ast_command();
let result = cmd.try_get_matches_from(["ast", "app.js"]);
assert!(result.is_ok());

let m = result.unwrap();
let params = AstParams::from_matches(&m);

// Non-.ptk extension → treat as source
assert_eq!(params.query_path, None);
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}

#[test]
fn ast_no_extension_detection_with_two_positionals() {
let cmd = ast_command();
let result = cmd.try_get_matches_from(["ast", "query.ptk", "app.js"]);
assert!(result.is_ok());

let m = result.unwrap();
let params = AstParams::from_matches(&m);

// Two positionals → first is query, second is source (no detection)
assert_eq!(params.query_path, Some(PathBuf::from("query.ptk")));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}

#[test]
fn ast_no_extension_detection_with_inline_query() {
let cmd = ast_command();
let result = cmd.try_get_matches_from(["ast", "-q", "(id) @x", "app.js"]);
assert!(result.is_ok());

let m = result.unwrap();
let params = AstParams::from_matches(&m);

// -q provided → positional shift takes precedence, no extension detection
assert_eq!(params.query_path, None);
assert_eq!(params.query_text, Some("(id) @x".to_string()));
assert_eq!(params.source_path, Some(PathBuf::from("app.js")));
}