From 722c2137bec84f65810aeba9259463e1470d9022 Mon Sep 17 00:00:00 2001 From: NewbieOrange Date: Sat, 20 Nov 2021 00:29:21 +0800 Subject: [PATCH] Fix incorrect autocomplete if file name contains space --- src/main/java/picocli/AutoComplete.java | 4 +++- src/test/java/picocli/AutoCompleteTest.java | 1 + src/test/resources/picocompletion-demo-help_completion.bash | 2 ++ src/test/resources/picocompletion-demo_completion.bash | 2 ++ 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/picocli/AutoComplete.java b/src/main/java/picocli/AutoComplete.java index dd267fb0e..aed44b954 100644 --- a/src/main/java/picocli/AutoComplete.java +++ b/src/main/java/picocli/AutoComplete.java @@ -288,7 +288,7 @@ private static class CommandDescriptor { final String commandName; final CommandLine commandLine; final CommandLine parent; - + CommandDescriptor(String functionName, String commandName, CommandLine commandLine, CommandLine parent) { this.functionName = functionName; this.commandName = commandName; @@ -714,6 +714,7 @@ private static String generatePositionalParamsCases(List po buff.append(format("%s positionals=$( compgen -W \"$%s_pos_param_args\" -- \"%s\" )\n", indent, paramName, currWord)); } else if (type.equals(File.class) || "java.nio.file.Path".equals(type.getName())) { buff.append(format("%s %s (( currIndex >= %d && currIndex <= %d )); then\n", indent, ifOrElif, min, max)); + buff.append(format("%s local IFS=$'\\n'\n", indent)); buff.append(format("%s compopt -o filenames\n", indent)); buff.append(format("%s positionals=$( compgen -f -- \"%s\" ) # files\n", indent, currWord)); } else if (type.equals(InetAddress.class)) { @@ -758,6 +759,7 @@ private static String generateOptionsCases(List argOptionFields, Str buff.append(format("%s ;;\n", indent)); } else if (type.equals(File.class) || "java.nio.file.Path".equals(type.getName())) { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -f|--file)\n" + buff.append(format("%s local IFS=$'\\n'\n", indent)); buff.append(format("%s compopt -o filenames\n", indent)); buff.append(format("%s COMPREPLY=( $( compgen -f -- \"%s\" ) ) # files\n", indent, currWord)); buff.append(format("%s return $?\n", indent)); diff --git a/src/test/java/picocli/AutoCompleteTest.java b/src/test/java/picocli/AutoCompleteTest.java index 78eb118d5..f948b170c 100644 --- a/src/test/java/picocli/AutoCompleteTest.java +++ b/src/test/java/picocli/AutoCompleteTest.java @@ -779,6 +779,7 @@ private String expectedCompletionScriptForAutoCompleteApp() { " return\n" + " ;;\n" + " -o|--completionScript)\n" + + " local IFS=$'\\n'\n" + " compopt -o filenames\n" + " COMPREPLY=( $( compgen -f -- \"${curr_word}\" ) ) # files\n" + " return $?\n" + diff --git a/src/test/resources/picocompletion-demo-help_completion.bash b/src/test/resources/picocompletion-demo-help_completion.bash index dd6fdf7cd..0efda8c5d 100644 --- a/src/test/resources/picocompletion-demo-help_completion.bash +++ b/src/test/resources/picocompletion-demo-help_completion.bash @@ -201,6 +201,7 @@ function _picocli_picocompletion-demo-help_sub2() { return ;; --directory|-d) + local IFS=$'\n' compopt -o filenames COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files return $? @@ -322,6 +323,7 @@ function _picocli_picocompletion-demo-help_sub2_subsub3() { if (( currIndex >= 0 && currIndex <= 0 )); then positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" ) elif (( currIndex >= 1 && currIndex <= 2 )); then + local IFS=$'\n' compopt -o filenames positionals=$( compgen -f -- "${curr_word}" ) # files elif (( currIndex >= 3 && currIndex <= 2147483647 )); then diff --git a/src/test/resources/picocompletion-demo_completion.bash b/src/test/resources/picocompletion-demo_completion.bash index a25667266..ae2e732c1 100644 --- a/src/test/resources/picocompletion-demo_completion.bash +++ b/src/test/resources/picocompletion-demo_completion.bash @@ -199,6 +199,7 @@ function _picocli_picocompletion-demo_sub2() { return ;; --directory|-d) + local IFS=$'\n' compopt -o filenames COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files return $? @@ -303,6 +304,7 @@ function _picocli_picocompletion-demo_sub2_subsub3() { if (( currIndex >= 0 && currIndex <= 0 )); then positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" ) elif (( currIndex >= 1 && currIndex <= 2 )); then + local IFS=$'\n' compopt -o filenames positionals=$( compgen -f -- "${curr_word}" ) # files elif (( currIndex >= 3 && currIndex <= 2147483647 )); then