builtins type/functions: indent interactively-defined functions

This means that in case no editor is defined, "fish_indent" is now required
to fix the indentation.

Fixes #8603
This commit is contained in:
Johannes Altmanninger 2024-04-12 10:53:26 +02:00
parent 222673f339
commit 611a0572b1
4 changed files with 13 additions and 6 deletions

View file

@ -106,6 +106,7 @@ Interactive improvements
- ``read --help`` and friends no longer ignore redirections. This fixes a regression in version 3.1 (:issue:`10274`). - ``read --help`` and friends no longer ignore redirections. This fixes a regression in version 3.1 (:issue:`10274`).
- Command abbreviations (those with ``--position command`` or without a ``--position``) now also expand after decorators like ``command`` (:issue:`10396`). - Command abbreviations (those with ``--position command`` or without a ``--position``) now also expand after decorators like ``command`` (:issue:`10396`).
- Abbreviations now expand after process separators like ``;`` and ``|``. This fixes a regression in version 3.6 (:issue:`9730`). - Abbreviations now expand after process separators like ``;`` and ``|``. This fixes a regression in version 3.6 (:issue:`9730`).
- When exporting interactively defined functions (using ``type``, ``functions`` or ``funcsave``) the function body is now indented, same as in the interactive command line editor (:issue:`8603`).
New or improved bindings New or improved bindings
^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^

View file

@ -47,16 +47,12 @@ function funced --description 'Edit function definition'
if test "$editor" = fish if test "$editor" = fish
if functions -q -- $funcname if functions -q -- $funcname
command -q fish_indent functions --no-details -- $funcname | fish_indent --only-unindent | fish_indent --no-indent | read -z init
and functions --no-details -- $funcname | fish_indent --no-indent | read -z init
or functions --no-details -- $funcname | read -z init
end end
set -l prompt 'printf "%s%s%s> " (set_color green) '$funcname' (set_color normal)' set -l prompt 'printf "%s%s%s> " (set_color green) '$funcname' (set_color normal)'
if read -p $prompt -c "$init" --shell cmd if read -p $prompt -c "$init" --shell cmd
command -q fish_indent echo -n $cmd | fish_indent --only-unindent | read -lz cmd
and echo -n $cmd | fish_indent | read -lz cmd
or echo -n $cmd | read -lz cmd
eval "$cmd" eval "$cmd"
end end
if set -q _flag_save if set -q _flag_save

View file

@ -8,6 +8,8 @@ use crate::event::{self};
use crate::function; use crate::function;
use crate::highlight::colorize; use crate::highlight::colorize;
use crate::highlight::highlight_shell; use crate::highlight::highlight_shell;
use crate::parse_util::apply_indents;
use crate::parse_util::parse_util_compute_indents;
use crate::parser_keywords::parser_keywords_is_reserved; use crate::parser_keywords::parser_keywords_is_reserved;
use crate::termsize::termsize_last; use crate::termsize::termsize_last;
@ -407,6 +409,10 @@ pub fn functions(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -
def = props.annotated_definition(arg); def = props.annotated_definition(arg);
} }
if props.definition_file().is_none() {
def = apply_indents(&def, &parse_util_compute_indents(&def));
}
if streams.out_is_terminal() { if streams.out_is_terminal() {
let mut colors = vec![]; let mut colors = vec![];
highlight_shell(&def, &mut colors, &parser.context(), false, None); highlight_shell(&def, &mut colors, &parser.context(), false, None);

View file

@ -3,6 +3,7 @@ use crate::common::str2wcstring;
use crate::function; use crate::function;
use crate::highlight::{colorize, highlight_shell}; use crate::highlight::{colorize, highlight_shell};
use crate::parse_util::{apply_indents, parse_util_compute_indents};
use crate::path::{path_get_path, path_get_paths}; use crate::path::{path_get_path, path_get_paths};
#[derive(Default)] #[derive(Default)]
@ -128,6 +129,9 @@ pub fn r#type(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> O
comment, comment,
props.annotated_definition(arg) props.annotated_definition(arg)
)); ));
if props.definition_file().is_none() {
def = apply_indents(&def, &parse_util_compute_indents(&def));
}
if streams.out_is_terminal() { if streams.out_is_terminal() {
let mut color = vec![]; let mut color = vec![];