Skip to content

Commit

Permalink
completion: make config available
Browse files Browse the repository at this point in the history
Completion functions need information from the config, e.g. for completing
user-configured aliases.
  • Loading branch information
senekor committed Nov 11, 2024
1 parent 6739dcc commit 482d2c4
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cli/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use clap::FromArgMatches as _;
use clap_complete::CompletionCandidate;
use config::Config;
use jj_lib::workspace::DefaultWorkspaceLoaderFactory;
use jj_lib::workspace::WorkspaceLoaderFactory as _;

Expand All @@ -27,7 +28,7 @@ use crate::config::LayeredConfigs;
use crate::ui::Ui;

pub fn local_bookmarks() -> Vec<CompletionCandidate> {
with_jj(|mut jj| {
with_jj(|mut jj, _| {
let output = jj
.arg("bookmark")
.arg("list")
Expand All @@ -48,10 +49,10 @@ pub fn local_bookmarks() -> Vec<CompletionCandidate> {
/// In case of errors, print them and early return an empty vector.
fn with_jj<F>(completion_fn: F) -> Vec<CompletionCandidate>
where
F: FnOnce(std::process::Command) -> Result<Vec<CompletionCandidate>, CommandError>,
F: FnOnce(std::process::Command, Config) -> Result<Vec<CompletionCandidate>, CommandError>,
{
get_jj_command()
.and_then(completion_fn)
.and_then(|(jj, config)| completion_fn(jj, config))
.unwrap_or_else(|e| {
eprintln!("{}", e.error);
Vec::new()
Expand All @@ -67,7 +68,7 @@ where
/// give completion code access to custom backends. Shelling out was chosen as
/// the preferred method, because it's more maintainable and the performance
/// requirements of completions aren't very high.
fn get_jj_command() -> Result<std::process::Command, CommandError> {
fn get_jj_command() -> Result<(std::process::Command, Config), CommandError> {
let current_exe = std::env::current_exe().map_err(user_error)?;
let mut command = std::process::Command::new(current_exe);

Expand Down Expand Up @@ -97,7 +98,7 @@ fn get_jj_command() -> Result<std::process::Command, CommandError> {
.read_repo_config(loader.repo_path())
.map_err(user_error)?;
}
let config = layered_configs.merge();
let mut config = layered_configs.merge();
// skip 2 because of the clap_complete prelude: jj -- jj <actual args...>
let args = std::env::args_os().skip(2);
let args = expand_args(&ui, &app, args, &config)?;
Expand All @@ -110,6 +111,11 @@ fn get_jj_command() -> Result<std::process::Command, CommandError> {
let args: GlobalArgs = GlobalArgs::from_arg_matches(&args)?;

if let Some(repository) = args.repository {
// Try to update repo-specific config on a best-effort basis.
if let Ok(loader) = DefaultWorkspaceLoaderFactory.create(&cwd.join(&repository)) {
let _ = layered_configs.read_repo_config(loader.repo_path());
config = layered_configs.merge();
}
command.arg("--repository");
command.arg(repository);
}
Expand All @@ -122,5 +128,5 @@ fn get_jj_command() -> Result<std::process::Command, CommandError> {
command.arg(config_toml);
}

Ok(command)
Ok((command, config))
}

0 comments on commit 482d2c4

Please sign in to comment.