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 Jan 31, 2025
1 parent f67ad1a commit 938e6e9
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 10 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
5 changes: 5 additions & 0 deletions cli/src/commands/config/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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
8 changes: 7 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,11 @@ pub struct OperationLogArgs {
///
/// [built-in keywords]:
/// https://jj-vcs.github.io/jj/latest/templates/#operation-keywords
#[arg(long, short = 'T')]
#[arg(
long, short = 'T',
verbatim_doc_comment,
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
10 changes: 7 additions & 3 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: cli/tests/test_generate_md_cli_help.rs
description: "AUTO-GENERATED FILE, DO NOT EDIT. This cli reference is generated by a test as an `insta` snapshot. MkDocs includes this snapshot from docs/cli-reference.md."
snapshot_kind: text
---
<!-- BEGIN MARKDOWN-->

Expand Down Expand Up @@ -1628,11 +1629,14 @@ Like other commands, `jj op log` snapshots the current working-copy changes and
* `--no-graph` — Don't show the graph, show a flat list of operations
* `-T`, `--template <TEMPLATE>` — Render each operation using the given template

You can specify arbitrary [template expressions] using the [built-in keywords].
You can specify arbitrary [template expressions] using the
[built-in keywords].

[template expression]: https://jj-vcs.github.io/jj/latest/templates/
[template expression]:
https://jj-vcs.github.io/jj/latest/templates/

[built-in keywords]: https://jj-vcs.github.io/jj/latest/templates/#operation-keywords
[built-in keywords]:
https://jj-vcs.github.io/jj/latest/templates/#operation-keywords
* `--op-diff` — Show changes to the repository at each operation
* `-p`, `--patch` — Show patch of modifications to changes (implies --op-diff)

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 938e6e9

Please sign in to comment.