fish_indent: Add a "--check" option to only test indentation

Fixes #7251.
This commit is contained in:
Fabian Homborg 2020-08-08 20:22:18 +02:00
parent b4f5ba6537
commit 2cdd6df257
3 changed files with 22 additions and 1 deletions

View file

@ -22,6 +22,8 @@ The following options are available:
- ``-i`` or ``--no-indent`` do not indent commands; only reformat to one job per line.
- ``-c`` or ``--check`` do not indent, only return 0 if the code is already indented as fish_indent would, 1 otherwise.
- ``-v`` or ``--version`` displays the current fish version and then exits.
- ``--ansi`` colorizes the output using ANSI escape sequences, appropriate for the current $TERM, using the colors defined in the environment (such as ``$fish_color_command``).

View file

@ -836,12 +836,13 @@ int main(int argc, char *argv[]) {
output_type_file,
output_type_ansi,
output_type_pygments_csv,
output_type_check,
output_type_html
} output_type = output_type_plain_text;
const char *output_location = "";
bool do_indent = true;
const char *short_opts = "+d:hvwiD:";
const char *short_opts = "+d:hvwicD:";
const struct option long_opts[] = {{"debug-level", required_argument, nullptr, 'd'},
{"debug-stack-frames", required_argument, nullptr, 'D'},
{"dump-parse-tree", no_argument, nullptr, 'P'},
@ -852,6 +853,7 @@ int main(int argc, char *argv[]) {
{"html", no_argument, nullptr, 1},
{"ansi", no_argument, nullptr, 2},
{"pygments", no_argument, nullptr, 3},
{"check", no_argument, nullptr, 'c'},
{nullptr, 0, nullptr, 0}};
int opt;
@ -889,6 +891,10 @@ int main(int argc, char *argv[]) {
output_type = output_type_pygments_csv;
break;
}
case 'c': {
output_type = output_type_check;
break;
}
case 'd': {
char *end;
long tmp;
@ -997,6 +1003,12 @@ int main(int argc, char *argv[]) {
case output_type_pygments_csv: {
DIE("pygments_csv should have been handled above");
}
case output_type_check: {
if (output_wtext != src) {
return 1;
}
break;
}
}
std::fputws(str2wcstring(colored_output).c_str(), stdout);

View file

@ -318,3 +318,10 @@ command 2' | $fish_indent
#CHECK: {{^}}command 1 |
#CHECK: {{^ }}command 1 cont ||
#CHECK: {{^}}command 2
echo " foo" | fish_indent --check
echo $status
#CHECK: 1
echo "foo" | fish_indent --check
echo $status
#CHECK: 0