Hide some calls to localize() via Deref/AsRef

As suggested in https://github.com/fish-shell/fish-shell/pull/11547#discussion_r2133625349
AsRef is needed for OutputStream::append which has this signature:

	pub fn append<Str: AsRef<wstr>>(&mut self, s: Str) -> bool
This commit is contained in:
Johannes Altmanninger
2025-06-07 09:51:43 +02:00
parent 1aec1d3955
commit 894139933d
7 changed files with 31 additions and 20 deletions

View File

@@ -319,9 +319,8 @@ fn fish_parse_opt(args: &mut [WString], opts: &mut FishCmdOpts) -> ControlFlow<i
// A little extra space.
name_width += 2;
for cat in cats.iter() {
let desc = cat.description.localize();
// this is left-justified
printf!("%-*ls %ls\n", name_width, cat.name, desc);
printf!("%-*ls %ls\n", name_width, cat.name, &cat.description);
}
return ControlFlow::Break(0);
}

View File

@@ -244,12 +244,11 @@ pub fn functions(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -
let desc = match props.as_ref() {
Some(p) => {
let localized_description = p.description.localize();
if localized_description.is_empty() {
if p.description.is_empty() {
L!("").to_owned()
} else {
escape_string(
localized_description,
&p.description,
EscapeStringStyle::Script(
EscapeFlags::NO_PRINTABLES | EscapeFlags::NO_QUOTED,
),

View File

@@ -1039,9 +1039,11 @@ fn builtin_false(_parser: &Parser, _streams: &mut IoStreams, _argv: &mut [&wstr]
/// Strings not present in our repo would require a custom MO file for translation to be possible.
fn builtin_gettext(_parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> BuiltinResult {
for arg in &argv[1..] {
streams.out.append(
crate::wutil::LocalizableString::from_external_source((*arg).to_owned()).localize(),
);
streams
.out
.append(crate::wutil::LocalizableString::from_external_source(
(*arg).to_owned(),
));
}
Ok(SUCCESS)
}

View File

@@ -1310,7 +1310,7 @@ fn complete_param_for_command(
}
let (arg_prefix, arg) = s.split_once(arg_offset);
let first_new = self.completions.completions.len();
self.complete_from_args(arg, &o.comp, o.desc.localize(), o.flags);
self.complete_from_args(arg, &o.comp, &o.desc, o.flags);
for compl in &mut self.completions.completions[first_new..] {
if compl.replaces_token() {
compl.completion.insert_utfstr(0, arg_prefix);
@@ -1341,7 +1341,7 @@ fn complete_param_for_command(
if o.result_mode.force_files {
has_force = true;
}
self.complete_from_args(s, &o.comp, o.desc.localize(), o.flags);
self.complete_from_args(s, &o.comp, &o.desc, o.flags);
}
}
@@ -1377,7 +1377,7 @@ fn complete_param_for_command(
if o.result_mode.force_files {
has_force = true;
}
self.complete_from_args(s, &o.comp, o.desc.localize(), o.flags);
self.complete_from_args(s, &o.comp, &o.desc, o.flags);
}
}
}
@@ -1400,7 +1400,7 @@ fn complete_param_for_command(
if o.option.is_empty() {
use_files &= !o.result_mode.no_files;
has_force |= o.result_mode.force_files;
self.complete_from_args(s, &o.comp, o.desc.localize(), o.flags);
self.complete_from_args(s, &o.comp, &o.desc, o.flags);
}
if !use_switches || s.is_empty() {
@@ -1433,12 +1433,11 @@ fn complete_param_for_command(
}
}
// It's a match.
let desc = o.desc.localize();
// Append a short-style option
if !self
.completions
.add(Completion::with_desc(o.option.clone(), desc.to_owned()))
{
if !self.completions.add(Completion::with_desc(
o.option.clone(),
o.desc.localize().to_owned(),
)) {
return false;
}
}
@@ -2414,7 +2413,7 @@ fn completion2string(index: &CompletionEntryIndex, o: &CompleteEntryOpt) -> WStr
CompleteOptionType::DoubleLong => append_switch_short_arg(&mut out, 'l', &o.option),
}
append_switch_short_arg(&mut out, 'd', o.desc.localize());
append_switch_short_arg(&mut out, 'd', &o.desc);
append_switch_short_arg(&mut out, 'a', &o.comp);
for c in &o.conditions {
append_switch_short_arg(&mut out, 'n', c);

View File

@@ -145,7 +145,7 @@ pub fn make_autoclose_pipes() -> nix::Result<AutoClosePipes> {
pipes
}
Err(err) => {
FLOG!(warning, PIPE_ERROR.localize());
FLOG!(warning, &PIPE_ERROR);
perror("pipe2");
return Err(err);
}

View File

@@ -419,7 +419,7 @@ pub fn copy_definition_lineno(&self) -> u32 {
/// Note callers must provide the function name, since the function does not know its own name.
pub fn annotated_definition(&self, name: &wstr) -> WString {
let mut out = WString::new();
let desc = self.description.localize();
let desc = &self.description;
let def = get_function_body_source(self);
let handlers = event::get_function_handlers(name);

View File

@@ -164,6 +164,18 @@ pub fn localize(&self) -> &'static wstr {
}
}
impl std::ops::Deref for LocalizableString {
type Target = wstr;
fn deref(&self) -> &Self::Target {
self.localize()
}
}
impl AsRef<wstr> for LocalizableString {
fn as_ref(&self) -> &wstr {
self
}
}
impl std::fmt::Display for LocalizableString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.localize())