From b531bab1cef58fcebc6c786bd63d6217a135204d Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 24 Mar 2019 19:20:29 +1100 Subject: [PATCH] Add support for -shell-script-* completion in :prompt Fixes #2754 --- doc/pages/commands.asciidoc | 9 ++++++++ src/commands.cc | 21 +++++++++++++++++++ test/shell/prompt-shell-script-candidates/cmd | 1 + test/shell/prompt-shell-script-candidates/in | 1 + test/shell/prompt-shell-script-candidates/out | 1 + test/shell/prompt-shell-script-candidates/rc | 3 +++ test/shell/prompt-shell-script-completion/cmd | 1 + test/shell/prompt-shell-script-completion/in | 1 + test/shell/prompt-shell-script-completion/out | 1 + test/shell/prompt-shell-script-completion/rc | 3 +++ 10 files changed, 42 insertions(+) create mode 100644 test/shell/prompt-shell-script-candidates/cmd create mode 100644 test/shell/prompt-shell-script-candidates/in create mode 100644 test/shell/prompt-shell-script-candidates/out create mode 100644 test/shell/prompt-shell-script-candidates/rc create mode 100644 test/shell/prompt-shell-script-completion/cmd create mode 100644 test/shell/prompt-shell-script-completion/in create mode 100644 test/shell/prompt-shell-script-completion/out create mode 100644 test/shell/prompt-shell-script-completion/rc diff --git a/doc/pages/commands.asciidoc b/doc/pages/commands.asciidoc index d4eda26a..8a215e3d 100644 --- a/doc/pages/commands.asciidoc +++ b/doc/pages/commands.asciidoc @@ -247,6 +247,15 @@ but not really useful in that context. will have this command executed whenever the prompt content changes or the prompt is aborted, respectively. + Completion support can be controlled with the same switches provided + by the *define-command* command, see + <>. + + For *-shell-script-completions* and *-shell-script-candidates* + completions, token_to_complete will always be 1, and the full + prompt content will be passed as a single token. In other words, + word splitting does not take place. + *on-key* :: wait for next key from user, then execute , the key is available through the `key` value, accessible through `$kak_key` diff --git a/src/commands.cc b/src/commands.cc index d2809254..31470f8f 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -272,6 +272,21 @@ private: int m_token = -1; }; +template +struct PromptCompleterAdapter +{ + PromptCompleterAdapter(Completer completer) : m_completer{completer} {} + + Completions operator()(const Context& context, CompletionFlags flags, + StringView prefix, ByteCount cursor_pos) + { + return m_completer(context, flags, {String{String::NoCopy{}, prefix}}, 0, cursor_pos); + } + +private: + Completer m_completer; +}; + Scope* get_scope_ifp(StringView scope, const Context& context) { if (prefix_match("global", scope)) @@ -1862,6 +1877,8 @@ const CommandDesc prompt_cmd = { { "buffer-completion", { false, "use buffer completion for prompt" } }, { "command-completion", { false, "use command completion for prompt" } }, { "shell-completion", { false, "use shell command completion for prompt" } }, + { "shell-script-completion", { true, "use shell command completion for prompt" } }, + { "shell-script-candidates", { true, "use shell command completion for prompt" } }, { "on-change", { true, "command to execute whenever the prompt changes" } }, { "on-abort", { true, "command to execute whenever the prompt is canceled" } } }, ParameterDesc::Flags::None, 2, 2 @@ -1899,6 +1916,10 @@ const CommandDesc prompt_cmd = { }; else if (parser.get_switch("shell-completion")) completer = shell_complete; + else if (auto shell_script = parser.get_switch("shell-script-completion")) + completer = PromptCompleterAdapter{ShellScriptCompleter{shell_script->str()}}; + else if (auto shell_script = parser.get_switch("shell-script-candidates")) + completer = PromptCompleterAdapter{ShellCandidatesCompleter{shell_script->str()}}; const auto flags = parser.get_switch("password") ? PromptFlags::Password : PromptFlags::None; diff --git a/test/shell/prompt-shell-script-candidates/cmd b/test/shell/prompt-shell-script-candidates/cmd new file mode 100644 index 00000000..7cc38390 --- /dev/null +++ b/test/shell/prompt-shell-script-candidates/cmd @@ -0,0 +1 @@ +:foob diff --git a/test/shell/prompt-shell-script-candidates/in b/test/shell/prompt-shell-script-candidates/in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/shell/prompt-shell-script-candidates/in @@ -0,0 +1 @@ + diff --git a/test/shell/prompt-shell-script-candidates/out b/test/shell/prompt-shell-script-candidates/out new file mode 100644 index 00000000..5716ca59 --- /dev/null +++ b/test/shell/prompt-shell-script-candidates/out @@ -0,0 +1 @@ +bar diff --git a/test/shell/prompt-shell-script-candidates/rc b/test/shell/prompt-shell-script-candidates/rc new file mode 100644 index 00000000..6aaaf259 --- /dev/null +++ b/test/shell/prompt-shell-script-candidates/rc @@ -0,0 +1,3 @@ +define-command foo %{ + prompt -shell-script-candidates %{ printf 'foo\nbar\nhaz\n' } ': ' %{exec i %val{text} } +} diff --git a/test/shell/prompt-shell-script-completion/cmd b/test/shell/prompt-shell-script-completion/cmd new file mode 100644 index 00000000..7cc38390 --- /dev/null +++ b/test/shell/prompt-shell-script-completion/cmd @@ -0,0 +1 @@ +:foob diff --git a/test/shell/prompt-shell-script-completion/in b/test/shell/prompt-shell-script-completion/in new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/shell/prompt-shell-script-completion/in @@ -0,0 +1 @@ + diff --git a/test/shell/prompt-shell-script-completion/out b/test/shell/prompt-shell-script-completion/out new file mode 100644 index 00000000..257cc564 --- /dev/null +++ b/test/shell/prompt-shell-script-completion/out @@ -0,0 +1 @@ +foo diff --git a/test/shell/prompt-shell-script-completion/rc b/test/shell/prompt-shell-script-completion/rc new file mode 100644 index 00000000..3fd111ba --- /dev/null +++ b/test/shell/prompt-shell-script-completion/rc @@ -0,0 +1,3 @@ +define-command foo %{ + prompt -shell-script-completion %{ printf 'foo\nbar\nhaz\n' } ': ' %{exec i %val{text} } +}