completion: migrate to UserSettings API

config::Config will soon be replaced with StackedConfig, and UserSettings
provides more convenient methods than StackedConfig.
This commit is contained in:
Yuya Nishihara 2024-11-28 16:46:40 +09:00
parent 0a0f8c5116
commit c4eaef6dfc

View file

@ -17,9 +17,9 @@ use std::io::BufRead;
use clap::builder::StyledStr; use clap::builder::StyledStr;
use clap::FromArgMatches as _; use clap::FromArgMatches as _;
use clap_complete::CompletionCandidate; use clap_complete::CompletionCandidate;
use config::Config;
use itertools::Itertools; use itertools::Itertools;
use jj_lib::config::ConfigNamePathBuf; use jj_lib::config::ConfigNamePathBuf;
use jj_lib::settings::UserSettings;
use jj_lib::workspace::DefaultWorkspaceLoaderFactory; use jj_lib::workspace::DefaultWorkspaceLoaderFactory;
use jj_lib::workspace::WorkspaceLoaderFactory as _; use jj_lib::workspace::WorkspaceLoaderFactory as _;
@ -101,7 +101,7 @@ pub fn tracked_bookmarks() -> Vec<CompletionCandidate> {
} }
pub fn untracked_bookmarks() -> Vec<CompletionCandidate> { pub fn untracked_bookmarks() -> Vec<CompletionCandidate> {
with_jj(|jj, config| { with_jj(|jj, settings| {
let output = jj let output = jj
.build() .build()
.arg("bookmark") .arg("bookmark")
@ -118,7 +118,7 @@ pub fn untracked_bookmarks() -> Vec<CompletionCandidate> {
.output() .output()
.map_err(user_error)?; .map_err(user_error)?;
let prefix = config.get::<String>("git.push-bookmark-prefix").ok(); let prefix = settings.get_string("git.push-bookmark-prefix").ok();
Ok(String::from_utf8_lossy(&output.stdout) Ok(String::from_utf8_lossy(&output.stdout)
.lines() .lines()
@ -139,7 +139,7 @@ pub fn untracked_bookmarks() -> Vec<CompletionCandidate> {
} }
pub fn bookmarks() -> Vec<CompletionCandidate> { pub fn bookmarks() -> Vec<CompletionCandidate> {
with_jj(|jj, config| { with_jj(|jj, settings| {
let output = jj let output = jj
.build() .build()
.arg("bookmark") .arg("bookmark")
@ -156,7 +156,7 @@ pub fn bookmarks() -> Vec<CompletionCandidate> {
.map_err(user_error)?; .map_err(user_error)?;
let stdout = String::from_utf8_lossy(&output.stdout); let stdout = String::from_utf8_lossy(&output.stdout);
let prefix = config.get::<String>("git.push-bookmark-prefix").ok(); let prefix = settings.get_string("git.push-bookmark-prefix").ok();
Ok((&stdout Ok((&stdout
.lines() .lines()
@ -204,8 +204,8 @@ pub fn git_remotes() -> Vec<CompletionCandidate> {
} }
pub fn aliases() -> Vec<CompletionCandidate> { pub fn aliases() -> Vec<CompletionCandidate> {
with_jj(|_, config| { with_jj(|_, settings| {
Ok(config Ok(settings
.get_table("aliases")? .get_table("aliases")?
.into_keys() .into_keys()
// This is opinionated, but many people probably have several // This is opinionated, but many people probably have several
@ -219,7 +219,7 @@ pub fn aliases() -> Vec<CompletionCandidate> {
} }
fn revisions(revisions: Option<&str>) -> Vec<CompletionCandidate> { fn revisions(revisions: Option<&str>) -> Vec<CompletionCandidate> {
with_jj(|jj, config| { with_jj(|jj, settings| {
// display order // display order
const LOCAL_BOOKMARK_MINE: usize = 0; const LOCAL_BOOKMARK_MINE: usize = 0;
const LOCAL_BOOKMARK: usize = 1; const LOCAL_BOOKMARK: usize = 1;
@ -232,7 +232,7 @@ fn revisions(revisions: Option<&str>) -> Vec<CompletionCandidate> {
// bookmarks // bookmarks
let prefix = config.get::<String>("git.push-bookmark-prefix").ok(); let prefix = settings.get_string("git.push-bookmark-prefix").ok();
let mut cmd = jj.build(); let mut cmd = jj.build();
cmd.arg("bookmark") cmd.arg("bookmark")
@ -298,8 +298,8 @@ fn revisions(revisions: Option<&str>) -> Vec<CompletionCandidate> {
let revisions = revisions let revisions = revisions
.map(String::from) .map(String::from)
.or_else(|| config.get_string("revsets.short-prefixes").ok()) .or_else(|| settings.get_string("revsets.short-prefixes").ok())
.or_else(|| config.get_string("revsets.log").ok()) .or_else(|| settings.get_string("revsets.log").ok())
.unwrap_or_default(); .unwrap_or_default();
let output = jj let output = jj
@ -643,10 +643,10 @@ pub fn interdiff_files(current: &std::ffi::OsStr) -> Vec<CompletionCandidate> {
/// In case of errors, print them and early return an empty vector. /// In case of errors, print them and early return an empty vector.
fn with_jj<F>(completion_fn: F) -> Vec<CompletionCandidate> fn with_jj<F>(completion_fn: F) -> Vec<CompletionCandidate>
where where
F: FnOnce(JjBuilder, &Config) -> Result<Vec<CompletionCandidate>, CommandError>, F: FnOnce(JjBuilder, &UserSettings) -> Result<Vec<CompletionCandidate>, CommandError>,
{ {
get_jj_command() get_jj_command()
.and_then(|(jj, config)| completion_fn(jj, &config)) .and_then(|(jj, settings)| completion_fn(jj, &settings))
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
eprintln!("{}", e.error); eprintln!("{}", e.error);
Vec::new() Vec::new()
@ -662,7 +662,7 @@ where
/// give completion code access to custom backends. Shelling out was chosen as /// give completion code access to custom backends. Shelling out was chosen as
/// the preferred method, because it's more maintainable and the performance /// the preferred method, because it's more maintainable and the performance
/// requirements of completions aren't very high. /// requirements of completions aren't very high.
fn get_jj_command() -> Result<(JjBuilder, Config), CommandError> { fn get_jj_command() -> Result<(JjBuilder, UserSettings), CommandError> {
let current_exe = std::env::current_exe().map_err(user_error)?; let current_exe = std::env::current_exe().map_err(user_error)?;
let mut cmd_args = Vec::<String>::new(); let mut cmd_args = Vec::<String>::new();
@ -692,7 +692,7 @@ fn get_jj_command() -> Result<(JjBuilder, Config), CommandError> {
config_env.reset_repo_path(loader.repo_path()); config_env.reset_repo_path(loader.repo_path());
let _ = config_env.reload_repo_config(&mut stacked_config); let _ = config_env.reload_repo_config(&mut stacked_config);
} }
let mut config = stacked_config.merge(); let config = stacked_config.merge();
// skip 2 because of the clap_complete prelude: jj -- jj <actual args...> // skip 2 because of the clap_complete prelude: jj -- jj <actual args...>
let args = std::env::args_os().skip(2); let args = std::env::args_os().skip(2);
let args = expand_args(&ui, &app, args, &config)?; let args = expand_args(&ui, &app, args, &config)?;
@ -709,7 +709,6 @@ fn get_jj_command() -> Result<(JjBuilder, Config), CommandError> {
if let Ok(loader) = DefaultWorkspaceLoaderFactory.create(&cwd.join(&repository)) { if let Ok(loader) = DefaultWorkspaceLoaderFactory.create(&cwd.join(&repository)) {
config_env.reset_repo_path(loader.repo_path()); config_env.reset_repo_path(loader.repo_path());
let _ = config_env.reload_repo_config(&mut stacked_config); let _ = config_env.reload_repo_config(&mut stacked_config);
config = stacked_config.merge();
} }
cmd_args.push("--repository".into()); cmd_args.push("--repository".into());
cmd_args.push(repository); cmd_args.push(repository);
@ -750,8 +749,9 @@ fn get_jj_command() -> Result<(JjBuilder, Config), CommandError> {
cmd: current_exe, cmd: current_exe,
args: cmd_args, args: cmd_args,
}; };
let settings = UserSettings::from_config(stacked_config);
Ok((builder, config)) Ok((builder, settings))
} }
/// A helper struct to allow completion functions to call jj multiple times with /// A helper struct to allow completion functions to call jj multiple times with