Skip to content

Commit

Permalink
cli: complete: add dynamic completions for -T template aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Feb 1, 2025
1 parent 7c74866 commit f7429f2
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2846,7 +2846,7 @@ pub fn update_working_copy(
Ok(stats)
}

fn load_template_aliases(
pub fn load_template_aliases(
ui: &Ui,
stacked_config: &StackedConfig,
) -> Result<TemplateAliasesMap, CommandError> {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/bookmark/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct BookmarkListArgs {
///
/// [`RefName` type]:
/// https://jj-vcs.github.io/jj/latest/templates/#refname-type
#[arg(long, short = 'T')]
#[arg(long, short = 'T', add = ArgValueCandidates::new(complete::template_aliases))]
template: Option<String>,
}

Expand Down
6 changes: 5 additions & 1 deletion cli/src/commands/config/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ pub struct ConfigListArgs {
///
/// [template expression]:
/// https://jj-vcs.github.io/jj/latest/templates/
#[arg(long, short = 'T', verbatim_doc_comment)]
#[arg(
long, short = 'T',
verbatim_doc_comment,
add = ArgValueCandidates::new(complete::template_aliases)
)]
template: Option<String>,
}

Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/evolog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) struct EvologArgs {
///
/// [built-in keywords]:
/// https://jj-vcs.github.io/jj/latest/templates/#commit-keywords
#[arg(long, short = 'T')]
#[arg(long, short = 'T', add = ArgValueCandidates::new(complete::template_aliases))]
template: Option<String>,
/// Show patch compared to the previous version of this change
///
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub(crate) struct LogArgs {
/// https://jj-vcs.github.io/jj/latest/templates/
/// [built-in keywords]:
/// https://jj-vcs.github.io/jj/latest/templates/#commit-keywords
#[arg(long, short = 'T')]
#[arg(long, short = 'T', add = ArgValueCandidates::new(complete::template_aliases))]
template: Option<String>,
/// Show patch
#[arg(long, short = 'p')]
Expand Down
4 changes: 3 additions & 1 deletion cli/src/commands/operation/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::slice;

use clap_complete::ArgValueCandidates;
use itertools::Itertools as _;
use jj_lib::config::ConfigGetError;
use jj_lib::config::ConfigGetResultExt as _;
Expand All @@ -32,6 +33,7 @@ use crate::cli_util::LogContentFormat;
use crate::cli_util::WorkspaceCommandEnvironment;
use crate::command_error::CommandError;
use crate::commit_templater::CommitTemplateLanguage;
use crate::complete;
use crate::diff_util::diff_formats_for_log;
use crate::diff_util::DiffFormatArgs;
use crate::diff_util::DiffRenderer;
Expand Down Expand Up @@ -70,7 +72,7 @@ pub struct OperationLogArgs {
///
/// [built-in keywords]:
/// https://jj-vcs.github.io/jj/latest/templates/#operation-keywords
#[arg(long, short = 'T')]
#[arg(long, short = 'T', add = ArgValueCandidates::new(complete::template_aliases))]
template: Option<String>,
/// Show changes to the repository at each operation
#[arg(long)]
Expand Down
2 changes: 1 addition & 1 deletion cli/src/commands/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) struct ShowArgs {
///
/// [built-in keywords]:
/// https://jj-vcs.github.io/jj/latest/templates/#commit-keywords
#[arg(long, short = 'T')]
#[arg(long, short = 'T', add = ArgValueCandidates::new(complete::template_aliases))]
template: Option<String>,
#[command(flatten)]
format: DiffFormatArgs,
Expand Down
4 changes: 3 additions & 1 deletion cli/src/commands/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use clap_complete::ArgValueCandidates;
use jj_lib::str_util::StringPattern;

use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::commit_templater::CommitTemplateLanguage;
use crate::commit_templater::RefName;
use crate::complete;
use crate::ui::Ui;

/// Manage tags.
Expand Down Expand Up @@ -49,7 +51,7 @@ pub struct TagListArgs {
///
/// [`RefName` type]:
/// https://jj-vcs.github.io/jj/latest/templates/#refname-type
#[arg(long, short = 'T')]
#[arg(long, short = 'T', add = ArgValueCandidates::new(complete::template_aliases))]
template: Option<String>,
}

Expand Down
14 changes: 14 additions & 0 deletions cli/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use jj_lib::workspace::WorkspaceLoaderFactory as _;

use crate::cli_util::expand_args;
use crate::cli_util::find_workspace_dir;
use crate::cli_util::load_template_aliases;
use crate::cli_util::GlobalArgs;
use crate::command_error::user_error;
use crate::command_error::CommandError;
Expand Down Expand Up @@ -201,6 +202,19 @@ pub fn git_remotes() -> Vec<CompletionCandidate> {
})
}

pub fn template_aliases() -> Vec<CompletionCandidate> {
with_jj(|_, settings| {
let Ok(template_aliases) = load_template_aliases(&Ui::null(), settings.config()) else {
return Ok(Vec::new());
};
Ok(template_aliases
.symbol_names()
.map(CompletionCandidate::new)
.sorted()
.collect())
})
}

pub fn aliases() -> Vec<CompletionCandidate> {
with_jj(|_, settings| {
Ok(settings
Expand Down
27 changes: 27 additions & 0 deletions cli/tests/test_completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,33 @@ fn test_config() {
");
}

#[test]
fn test_template_alias() {
let mut test_env = TestEnvironment::default();
test_env.add_env_var("COMPLETE", "fish");
let dir = test_env.env_root();

let stdout = test_env.jj_cmd_success(dir, &["--", "jj", "log", "-T", ""]);
insta::assert_snapshot!(stdout, @r"
builtin_log_comfortable
builtin_log_compact
builtin_log_compact_full_description
builtin_log_detailed
builtin_log_node
builtin_log_node_ascii
builtin_log_oneline
builtin_op_log_comfortable
builtin_op_log_compact
builtin_op_log_node
builtin_op_log_node_ascii
builtin_op_log_oneline
commit_summary_separator
description_placeholder
email_placeholder
name_placeholder
");
}

fn create_commit(
test_env: &TestEnvironment,
repo_path: &std::path::Path,
Expand Down

0 comments on commit f7429f2

Please sign in to comment.