Fix caret position of invalid expansion in command position

Fixes #5812
This commit is contained in:
Johannes Altmanninger 2019-10-04 20:28:47 +02:00 committed by ridiculousfish
parent e2952f230b
commit f91c725ff0
4 changed files with 20 additions and 2 deletions

View File

@ -753,12 +753,18 @@ parse_execution_result_t parse_execution_context_t::expand_command(
// Get the unexpanded command string. We expect to always get it here.
wcstring unexp_cmd = *command_for_plain_statement(statement, pstree->src);
size_t pos_of_command_token = statement.child<0>().source_range()->start;
// Expand the string to produce completions, and report errors.
expand_result_t expand_err =
expand_to_command_and_args(unexp_cmd, parser->vars(), out_cmd, out_args, &errors);
if (expand_err == expand_result_t::error) {
parser->set_last_statuses(statuses_t::just(STATUS_ILLEGAL_CMD));
// Issue #5812 - the expansions were done on the command token,
// excluding prefixes such as " " or "if ".
// This means that the error positions are relative to the beginning
// of the token; we need to make them relative to the original source.
for (auto &error : errors) error.source_start += pos_of_command_token;
return report_errors(errors);
} else if (expand_err == expand_result_t::wildcard_no_match) {
return report_unmatched_wildcard_error(statement);

View File

@ -0,0 +1,13 @@
# RUN: %fish -C 'set -g fish %fish' %s
# caret position (#5812)
printf '<%s>\n' ($fish -c ' $f[a]' 2>&1)
# CHECK: <fish: Invalid index value>
# CHECK: < $f[a]>
# CHECK: < ^>
printf '<%s>\n' ($fish -c 'if $f[a]; end' 2>&1)
# CHECK: <fish: Invalid index value>
# CHECK: <if $f[a]; end>
# CHECK: < ^>

View File

@ -41,4 +41,4 @@ echo {}}
fish: Command substitutions not allowed
command (asd)
^
^

View File

@ -153,6 +153,5 @@ logmsg Test fatal syntax errors
../test/root/bin/fish -c 'echo $,foo'
../test/root/bin/fish -c 'echo {'
../test/root/bin/fish -c 'echo {}}'
# The output for this is wrong in non-interactive mode and will need fixing when #5812 is fixed
../test/root/bin/fish -c 'command (asd)'
true