Skip to content

Commit 485ae36

Browse files
committed
Rename hook environment variables to be shell agnostic
The environment variables used to pass state from the shell hooks to EnvironmentCompletionContext were using names from BASH's completion system, which were meaningless if you're not familiar with writing completion functions for BASH. Since completion isn't limited to the BASH shell any more, I've renamed these environment variables to make them more meaningful. This shouldn't break any existing installations unless hooks have been generated and copy-pasted into a script. To cover for this case, or an update mid-session, this change includes a warning message if calling completion with COMP_LINE set. Resolves #31
1 parent 0cbf123 commit 485ae36

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/EnvironmentCompletionContext.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ class EnvironmentCompletionContext extends CompletionContext
1010
*/
1111
public function __construct()
1212
{
13-
$this->commandLine = getenv('COMP_LINE');
14-
$this->charIndex = intval(getenv('COMP_POINT'));
13+
$this->commandLine = getenv('CMDLINE_CONTENTS');
14+
$this->charIndex = intval(getenv('CMDLINE_CURSOR_INDEX'));
1515

1616
if ($this->commandLine === false) {
17-
throw new \RuntimeException('Failed to configure from environment; Environment var COMP_LINE not set.');
17+
$message = 'Failed to configure from environment; Environment var CMDLINE_CONTENTS not set.';
18+
19+
if (getenv('COMP_LINE')) {
20+
$message .= "\n\nYou appear to be attempting completion using an out-dated hook. If you've just updated,"
21+
. " you probably need to reinitialise the completion shell hook by reloading your shell"
22+
. " profile or starting a new shell session. If you are using a hard-coded (rather than generated)"
23+
. " hook, you will need to update that function with the new environment variable names."
24+
. "\n\nSee here for details: https://github.com/stecman/symfony-console-completion/issues/31";
25+
}
26+
27+
throw new \RuntimeException($message);
1828
}
1929
}
2030

@@ -25,10 +35,10 @@ public function __construct()
2535
*/
2636
public function useWordBreaksFromEnvironment()
2737
{
28-
$breaks = getenv('COMP_WORDBREAKS');
38+
$breaks = getenv('CMDLINE_WORDBREAKS');
2939

3040
if (!$breaks) {
31-
throw new \RuntimeException('Failed to read word breaks from environment; Environment var COMP_WORDBREAKS not set');
41+
throw new \RuntimeException('Failed to read word breaks from environment; Environment var CMDLINE_WORDBREAKS not set');
3242
}
3343

3444
$this->wordBreaks = $breaks;

src/HookFactory.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ final class HookFactory
3030
'bash' => <<<'END'
3131
# BASH completion for %%program_path%%
3232
function %%function_name%% {
33-
export COMP_LINE COMP_POINT COMP_WORDBREAKS;
33+
34+
# Copy BASH's completion variables to the ones the completion command expects
35+
# These line up exactly as the library was originally designed for BASH
36+
local CMDLINE_CONTENTS="$COMP_LINE"
37+
local CMDLINE_CURSOR_INDEX="$COMP_POINT"
38+
local CMDLINE_WORDBREAKS="$COMP_WORDBREAKS";
39+
40+
export CMDLINE_CONTENTS CMDLINE_CURSOR_INDEX CMDLINE_WORDBREAKS
41+
3442
local RESULT STATUS;
3543
3644
RESULT="$(%%completion_command%%)";
@@ -63,15 +71,11 @@ function %%function_name%% {
6371
, 'zsh' => <<<'END'
6472
# ZSH completion for %%program_path%%
6573
function %%function_name%% {
66-
# Emulate BASH's command line contents variable
67-
local -x COMP_LINE="$words"
68-
69-
# Emulate BASH's cursor position variable, setting it to the end of the current word.
70-
local -x COMP_POINT
71-
(( COMP_POINT = ${#${(j. .)words[1,CURRENT]}} ))
74+
local -x CMDLINE_CONTENTS="$words"
75+
local -x CMDLINE_CURSOR_INDEX
76+
(( CMDLINE_CURSOR_INDEX = ${#${(j. .)words[1,CURRENT]}} ))
7277
7378
local RESULT STATUS
74-
local -x COMPOSER_CWD=`pwd`
7579
RESULT=("${(@f)$( %%completion_command%% )}")
7680
STATUS=$?;
7781

0 commit comments

Comments
 (0)