Skip to content

Commit

Permalink
completion: teach log about files
Browse files Browse the repository at this point in the history
  • Loading branch information
senekor committed Dec 1, 2024
1 parent a8c35db commit 652ea19
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
6 changes: 5 additions & 1 deletion cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use clap_complete::ArgValueCandidates;
use clap_complete::ArgValueCompleter;
use jj_lib::backend::CommitId;
use jj_lib::config::ConfigError;
use jj_lib::graph::GraphEdgeType;
Expand Down Expand Up @@ -65,7 +66,10 @@ pub(crate) struct LogArgs {
#[arg(long, short, add = ArgValueCandidates::new(complete::all_revisions))]
revisions: Vec<RevisionArg>,
/// Show revisions modifying the given paths
#[arg(value_hint = clap::ValueHint::AnyPath)]
#[arg(
value_hint = clap::ValueHint::AnyPath,
add = ArgValueCompleter::new(complete::log_files),
)]
paths: Vec<String>,
/// Show revisions in the opposite order (older revisions first)
#[arg(long)]
Expand Down
41 changes: 34 additions & 7 deletions cli/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,25 @@ pub fn interdiff_files(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
})
}

/// Specific function for completing file paths for `jj log`
pub fn log_files(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
let mut rev = parse::log_revisions()
// multiple -r arguments are interpreted as a union
.into_iter()
.fold("none()".into(), |mut buf: String, rev| {
buf.push_str("|(");
buf.push_str(&rev);
buf.push(')');
buf
});
if rev == "none()" {
rev = "@".into();
} else {
rev = format!("latest({rev})"); // limit to one
};
all_files_from_rev(rev, current)
}

/// Shell out to jj during dynamic completion generation
///
/// In case of errors, print them and early return an empty vector.
Expand Down Expand Up @@ -778,7 +797,7 @@ impl JjBuilder {
/// multiple times, the parsing will pick any of the available ones, while the
/// actual execution of the command would fail.
mod parse {
fn parse_flag(candidates: &[&str], mut args: impl Iterator<Item = String>) -> Option<String> {
fn parse_flag(candidates: &[&str], args: &mut impl Iterator<Item = String>) -> Option<String> {
for arg in args.by_ref() {
// -r REV syntax
if candidates.contains(&arg.as_ref()) {
Expand Down Expand Up @@ -806,8 +825,8 @@ mod parse {
None
}

pub fn parse_revision_impl(args: impl Iterator<Item = String>) -> Option<String> {
parse_flag(&["-r", "--revision"], args)
pub fn parse_revision_impl(mut args: impl Iterator<Item = String>) -> Option<String> {
parse_flag(&["-r", "--revision"], &mut args)
}

pub fn revision() -> Option<String> {
Expand All @@ -822,8 +841,8 @@ mod parse {
where
T: Iterator<Item = String>,
{
let from = parse_flag(&["-f", "--from"], args())?;
let to = parse_flag(&["-t", "--to"], args()).unwrap_or_else(|| "@".into());
let from = parse_flag(&["-f", "--from"], &mut args())?;
let to = parse_flag(&["-t", "--to"], &mut args()).unwrap_or_else(|| "@".into());

Some((from, to))
}
Expand All @@ -837,10 +856,18 @@ mod parse {
// the files changed only in some other revision in the range between
// --from and --to cannot be squashed into --to like that.
pub fn squash_revision() -> Option<String> {
if let Some(rev) = parse_flag(&["-r", "--revision"], std::env::args()) {
if let Some(rev) = parse_flag(&["-r", "--revision"], &mut std::env::args()) {
return Some(rev);
}
parse_flag(&["-f", "--from"], std::env::args())
parse_flag(&["-f", "--from"], &mut std::env::args())
}

// Special parse function only for `jj log`. It has a --revisions flag,
// instead of the usual --revision, and it can be supplied multiple times.
pub fn log_revisions() -> Vec<String> {
let candidates = &["-r", "--revisions"];
let mut args = std::env::args();
std::iter::from_fn(|| parse_flag(candidates, &mut args)).collect()
}
}

Expand Down
31 changes: 31 additions & 0 deletions cli/tests/test_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,35 @@ fn test_files() {
f_dir/
f_modified
");

let stdout = test_env.jj_cmd_success(&repo_path, &["--", "jj", "log", "f_"]);
insta::assert_snapshot!(stdout.replace('\\', "/"), @r"
f_added
f_added_2
f_dir/
f_modified
f_not_yet_renamed
f_renamed
f_unchanged
");
let stdout = test_env.jj_cmd_success(
&repo_path,
&[
"--",
"jj",
"log",
"-r=first",
"--revisions",
"conflicted",
"f_",
],
);
insta::assert_snapshot!(stdout.replace('\\', "/"), @r"
f_added_2
f_deleted
f_dir/
f_modified
f_not_yet_renamed
f_unchanged
");
}

0 comments on commit 652ea19

Please sign in to comment.