@microsoft/inshellisense 0.0.1-rc.5 → 0.0.1-rc.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,380 @@
1
+ # bash-preexec.sh -- Bash support for ZSH-like 'preexec' and 'precmd' functions.
2
+ # https://github.com/rcaloras/bash-preexec
3
+ #
4
+ #
5
+ # 'preexec' functions are executed before each interactive command is
6
+ # executed, with the interactive command as its argument. The 'precmd'
7
+ # function is executed before each prompt is displayed.
8
+ #
9
+ # Author: Ryan Caloras (ryan@bashhub.com)
10
+ # Forked from Original Author: Glyph Lefkowitz
11
+ #
12
+ # V0.5.0
13
+ #
14
+
15
+ # General Usage:
16
+ #
17
+ # 1. Source this file at the end of your bash profile so as not to interfere
18
+ # with anything else that's using PROMPT_COMMAND.
19
+ #
20
+ # 2. Add any precmd or preexec functions by appending them to their arrays:
21
+ # e.g.
22
+ # precmd_functions+=(my_precmd_function)
23
+ # precmd_functions+=(some_other_precmd_function)
24
+ #
25
+ # preexec_functions+=(my_preexec_function)
26
+ #
27
+ # 3. Consider changing anything using the DEBUG trap or PROMPT_COMMAND
28
+ # to use preexec and precmd instead. Preexisting usages will be
29
+ # preserved, but doing so manually may be less surprising.
30
+ #
31
+ # Note: This module requires two Bash features which you must not otherwise be
32
+ # using: the "DEBUG" trap, and the "PROMPT_COMMAND" variable. If you override
33
+ # either of these after bash-preexec has been installed it will most likely break.
34
+
35
+ # Tell shellcheck what kind of file this is.
36
+ # shellcheck shell=bash
37
+
38
+ # Make sure this is bash that's running and return otherwise.
39
+ # Use POSIX syntax for this line:
40
+ if [ -z "${BASH_VERSION-}" ]; then
41
+ return 1;
42
+ fi
43
+
44
+ # We only support Bash 3.1+.
45
+ # Note: BASH_VERSINFO is first available in Bash-2.0.
46
+ if [[ -z "${BASH_VERSINFO-}" ]] || (( BASH_VERSINFO[0] < 3 || (BASH_VERSINFO[0] == 3 && BASH_VERSINFO[1] < 1) )); then
47
+ return 1
48
+ fi
49
+
50
+ # Avoid duplicate inclusion
51
+ if [[ -n "${bash_preexec_imported:-}" ]]; then
52
+ return 0
53
+ fi
54
+ bash_preexec_imported="defined"
55
+
56
+ # WARNING: This variable is no longer used and should not be relied upon.
57
+ # Use ${bash_preexec_imported} instead.
58
+ # shellcheck disable=SC2034
59
+ __bp_imported="${bash_preexec_imported}"
60
+
61
+ # Should be available to each precmd and preexec
62
+ # functions, should they want it. $? and $_ are available as $? and $_, but
63
+ # $PIPESTATUS is available only in a copy, $BP_PIPESTATUS.
64
+ # TODO: Figure out how to restore PIPESTATUS before each precmd or preexec
65
+ # function.
66
+ __bp_last_ret_value="$?"
67
+ BP_PIPESTATUS=("${PIPESTATUS[@]}")
68
+ __bp_last_argument_prev_command="$_"
69
+
70
+ __bp_inside_precmd=0
71
+ __bp_inside_preexec=0
72
+
73
+ # Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install
74
+ __bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
75
+
76
+ # Fails if any of the given variables are readonly
77
+ # Reference https://stackoverflow.com/a/4441178
78
+ __bp_require_not_readonly() {
79
+ local var
80
+ for var; do
81
+ if ! ( unset "$var" 2> /dev/null ); then
82
+ echo "bash-preexec requires write access to ${var}" >&2
83
+ return 1
84
+ fi
85
+ done
86
+ }
87
+
88
+ # Remove ignorespace and or replace ignoreboth from HISTCONTROL
89
+ # so we can accurately invoke preexec with a command from our
90
+ # history even if it starts with a space.
91
+ __bp_adjust_histcontrol() {
92
+ local histcontrol
93
+ histcontrol="${HISTCONTROL:-}"
94
+ histcontrol="${histcontrol//ignorespace}"
95
+ # Replace ignoreboth with ignoredups
96
+ if [[ "$histcontrol" == *"ignoreboth"* ]]; then
97
+ histcontrol="ignoredups:${histcontrol//ignoreboth}"
98
+ fi;
99
+ export HISTCONTROL="$histcontrol"
100
+ }
101
+
102
+ # This variable describes whether we are currently in "interactive mode";
103
+ # i.e. whether this shell has just executed a prompt and is waiting for user
104
+ # input. It documents whether the current command invoked by the trace hook is
105
+ # run interactively by the user; it's set immediately after the prompt hook,
106
+ # and unset as soon as the trace hook is run.
107
+ __bp_preexec_interactive_mode=""
108
+
109
+ # These arrays are used to add functions to be run before, or after, prompts.
110
+ declare -a precmd_functions
111
+ declare -a preexec_functions
112
+
113
+ # Trims leading and trailing whitespace from $2 and writes it to the variable
114
+ # name passed as $1
115
+ __bp_trim_whitespace() {
116
+ local var=${1:?} text=${2:-}
117
+ text="${text#"${text%%[![:space:]]*}"}" # remove leading whitespace characters
118
+ text="${text%"${text##*[![:space:]]}"}" # remove trailing whitespace characters
119
+ printf -v "$var" '%s' "$text"
120
+ }
121
+
122
+
123
+ # Trims whitespace and removes any leading or trailing semicolons from $2 and
124
+ # writes the resulting string to the variable name passed as $1. Used for
125
+ # manipulating substrings in PROMPT_COMMAND
126
+ __bp_sanitize_string() {
127
+ local var=${1:?} text=${2:-} sanitized
128
+ __bp_trim_whitespace sanitized "$text"
129
+ sanitized=${sanitized%;}
130
+ sanitized=${sanitized#;}
131
+ __bp_trim_whitespace sanitized "$sanitized"
132
+ printf -v "$var" '%s' "$sanitized"
133
+ }
134
+
135
+ # This function is installed as part of the PROMPT_COMMAND;
136
+ # It sets a variable to indicate that the prompt was just displayed,
137
+ # to allow the DEBUG trap to know that the next command is likely interactive.
138
+ __bp_interactive_mode() {
139
+ __bp_preexec_interactive_mode="on";
140
+ }
141
+
142
+
143
+ # This function is installed as part of the PROMPT_COMMAND.
144
+ # It will invoke any functions defined in the precmd_functions array.
145
+ __bp_precmd_invoke_cmd() {
146
+ # Save the returned value from our last command, and from each process in
147
+ # its pipeline. Note: this MUST be the first thing done in this function.
148
+ # BP_PIPESTATUS may be unused, ignore
149
+ # shellcheck disable=SC2034
150
+
151
+ __bp_last_ret_value="$?" BP_PIPESTATUS=("${PIPESTATUS[@]}")
152
+
153
+ # Don't invoke precmds if we are inside an execution of an "original
154
+ # prompt command" by another precmd execution loop. This avoids infinite
155
+ # recursion.
156
+ if (( __bp_inside_precmd > 0 )); then
157
+ return
158
+ fi
159
+ local __bp_inside_precmd=1
160
+
161
+ # Invoke every function defined in our function array.
162
+ local precmd_function
163
+ for precmd_function in "${precmd_functions[@]}"; do
164
+
165
+ # Only execute this function if it actually exists.
166
+ # Test existence of functions with: declare -[Ff]
167
+ if type -t "$precmd_function" 1>/dev/null; then
168
+ __bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
169
+ # Quote our function invocation to prevent issues with IFS
170
+ "$precmd_function"
171
+ fi
172
+ done
173
+
174
+ __bp_set_ret_value "$__bp_last_ret_value"
175
+ }
176
+
177
+ # Sets a return value in $?. We may want to get access to the $? variable in our
178
+ # precmd functions. This is available for instance in zsh. We can simulate it in bash
179
+ # by setting the value here.
180
+ __bp_set_ret_value() {
181
+ return ${1:+"$1"}
182
+ }
183
+
184
+ __bp_in_prompt_command() {
185
+
186
+ local prompt_command_array IFS=$'\n;'
187
+ read -rd '' -a prompt_command_array <<< "${PROMPT_COMMAND[*]:-}"
188
+
189
+ local trimmed_arg
190
+ __bp_trim_whitespace trimmed_arg "${1:-}"
191
+
192
+ local command trimmed_command
193
+ for command in "${prompt_command_array[@]:-}"; do
194
+ __bp_trim_whitespace trimmed_command "$command"
195
+ if [[ "$trimmed_command" == "$trimmed_arg" ]]; then
196
+ return 0
197
+ fi
198
+ done
199
+
200
+ return 1
201
+ }
202
+
203
+ # This function is installed as the DEBUG trap. It is invoked before each
204
+ # interactive prompt display. Its purpose is to inspect the current
205
+ # environment to attempt to detect if the current command is being invoked
206
+ # interactively, and invoke 'preexec' if so.
207
+ __bp_preexec_invoke_exec() {
208
+
209
+ # Save the contents of $_ so that it can be restored later on.
210
+ # https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702
211
+ __bp_last_argument_prev_command="${1:-}"
212
+ # Don't invoke preexecs if we are inside of another preexec.
213
+ if (( __bp_inside_preexec > 0 )); then
214
+ return
215
+ fi
216
+ local __bp_inside_preexec=1
217
+
218
+ # Checks if the file descriptor is not standard out (i.e. '1')
219
+ # __bp_delay_install checks if we're in test. Needed for bats to run.
220
+ # Prevents preexec from being invoked for functions in PS1
221
+ if [[ ! -t 1 && -z "${__bp_delay_install:-}" ]]; then
222
+ return
223
+ fi
224
+
225
+ if [[ -n "${COMP_LINE:-}" ]]; then
226
+ # We're in the middle of a completer. This obviously can't be
227
+ # an interactively issued command.
228
+ return
229
+ fi
230
+ if [[ -z "${__bp_preexec_interactive_mode:-}" ]]; then
231
+ # We're doing something related to displaying the prompt. Let the
232
+ # prompt set the title instead of me.
233
+ return
234
+ else
235
+ # If we're in a subshell, then the prompt won't be re-displayed to put
236
+ # us back into interactive mode, so let's not set the variable back.
237
+ # In other words, if you have a subshell like
238
+ # (sleep 1; sleep 2)
239
+ # You want to see the 'sleep 2' as a set_command_title as well.
240
+ if [[ 0 -eq "${BASH_SUBSHELL:-}" ]]; then
241
+ __bp_preexec_interactive_mode=""
242
+ fi
243
+ fi
244
+
245
+ if __bp_in_prompt_command "${BASH_COMMAND:-}"; then
246
+ # If we're executing something inside our prompt_command then we don't
247
+ # want to call preexec. Bash prior to 3.1 can't detect this at all :/
248
+ __bp_preexec_interactive_mode=""
249
+ return
250
+ fi
251
+
252
+ local this_command
253
+ this_command=$(
254
+ export LC_ALL=C
255
+ HISTTIMEFORMAT='' builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //'
256
+ )
257
+
258
+ # Sanity check to make sure we have something to invoke our function with.
259
+ if [[ -z "$this_command" ]]; then
260
+ return
261
+ fi
262
+
263
+ # Invoke every function defined in our function array.
264
+ local preexec_function
265
+ local preexec_function_ret_value
266
+ local preexec_ret_value=0
267
+ for preexec_function in "${preexec_functions[@]:-}"; do
268
+
269
+ # Only execute each function if it actually exists.
270
+ # Test existence of function with: declare -[fF]
271
+ if type -t "$preexec_function" 1>/dev/null; then
272
+ __bp_set_ret_value "${__bp_last_ret_value:-}"
273
+ # Quote our function invocation to prevent issues with IFS
274
+ "$preexec_function" "$this_command"
275
+ preexec_function_ret_value="$?"
276
+ if [[ "$preexec_function_ret_value" != 0 ]]; then
277
+ preexec_ret_value="$preexec_function_ret_value"
278
+ fi
279
+ fi
280
+ done
281
+
282
+ # Restore the last argument of the last executed command, and set the return
283
+ # value of the DEBUG trap to be the return code of the last preexec function
284
+ # to return an error.
285
+ # If `extdebug` is enabled a non-zero return value from any preexec function
286
+ # will cause the user's command not to execute.
287
+ # Run `shopt -s extdebug` to enable
288
+ __bp_set_ret_value "$preexec_ret_value" "$__bp_last_argument_prev_command"
289
+ }
290
+
291
+ __bp_install() {
292
+ # Exit if we already have this installed.
293
+ if [[ "${PROMPT_COMMAND[*]:-}" == *"__bp_precmd_invoke_cmd"* ]]; then
294
+ return 1;
295
+ fi
296
+
297
+ trap '__bp_preexec_invoke_exec "$_"' DEBUG
298
+
299
+ # Preserve any prior DEBUG trap as a preexec function
300
+ local prior_trap
301
+ # we can't easily do this with variable expansion. Leaving as sed command.
302
+ # shellcheck disable=SC2001
303
+ prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
304
+ unset __bp_trap_string
305
+ if [[ -n "$prior_trap" ]]; then
306
+ eval '__bp_original_debug_trap() {
307
+ '"$prior_trap"'
308
+ }'
309
+ preexec_functions+=(__bp_original_debug_trap)
310
+ fi
311
+
312
+ # Adjust our HISTCONTROL Variable if needed.
313
+ __bp_adjust_histcontrol
314
+
315
+ # Issue #25. Setting debug trap for subshells causes sessions to exit for
316
+ # backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash.
317
+ #
318
+ # Disabling this by default. It can be enabled by setting this variable.
319
+ if [[ -n "${__bp_enable_subshells:-}" ]]; then
320
+
321
+ # Set so debug trap will work be invoked in subshells.
322
+ set -o functrace > /dev/null 2>&1
323
+ shopt -s extdebug > /dev/null 2>&1
324
+ fi;
325
+
326
+ local existing_prompt_command
327
+ # Remove setting our trap install string and sanitize the existing prompt command string
328
+ existing_prompt_command="${PROMPT_COMMAND:-}"
329
+ # Edge case of appending to PROMPT_COMMAND
330
+ existing_prompt_command="${existing_prompt_command//$__bp_install_string/:}" # no-op
331
+ existing_prompt_command="${existing_prompt_command//$'\n':$'\n'/$'\n'}" # remove known-token only
332
+ existing_prompt_command="${existing_prompt_command//$'\n':;/$'\n'}" # remove known-token only
333
+ __bp_sanitize_string existing_prompt_command "$existing_prompt_command"
334
+ if [[ "${existing_prompt_command:-:}" == ":" ]]; then
335
+ existing_prompt_command=
336
+ fi
337
+
338
+ # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've
339
+ # actually entered something.
340
+ PROMPT_COMMAND='__bp_precmd_invoke_cmd'
341
+ PROMPT_COMMAND+=${existing_prompt_command:+$'\n'$existing_prompt_command}
342
+ if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1) )); then
343
+ PROMPT_COMMAND+=('__bp_interactive_mode')
344
+ else
345
+ # shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0
346
+ PROMPT_COMMAND+=$'\n__bp_interactive_mode'
347
+ fi
348
+
349
+ # Add two functions to our arrays for convenience
350
+ # of definition.
351
+ precmd_functions+=(precmd)
352
+ preexec_functions+=(preexec)
353
+
354
+ # Invoke our two functions manually that were added to $PROMPT_COMMAND
355
+ __bp_precmd_invoke_cmd
356
+ __bp_interactive_mode
357
+ }
358
+
359
+ # Sets an installation string as part of our PROMPT_COMMAND to install
360
+ # after our session has started. This allows bash-preexec to be included
361
+ # at any point in our bash profile.
362
+ __bp_install_after_session_init() {
363
+ # bash-preexec needs to modify these variables in order to work correctly
364
+ # if it can't, just stop the installation
365
+ __bp_require_not_readonly PROMPT_COMMAND HISTCONTROL HISTTIMEFORMAT || return
366
+
367
+ local sanitized_prompt_command
368
+ __bp_sanitize_string sanitized_prompt_command "${PROMPT_COMMAND:-}"
369
+ if [[ -n "$sanitized_prompt_command" ]]; then
370
+ # shellcheck disable=SC2178 # PROMPT_COMMAND is not an array in bash <= 5.0
371
+ PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
372
+ fi;
373
+ # shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0
374
+ PROMPT_COMMAND+=${__bp_install_string}
375
+ }
376
+
377
+ # Run our install so long as we're not delaying it.
378
+ if [[ -z "${__bp_delay_install:-}" ]]; then
379
+ __bp_install_after_session_init
380
+ fi;
@@ -11,9 +11,42 @@ __is_prompt_end() {
11
11
  builtin printf '\e]6973;PE\a'
12
12
  }
13
13
 
14
+ __is_escape_value() {
15
+ builtin emulate -L zsh
16
+
17
+ # Process text byte by byte, not by codepoint.
18
+ builtin local LC_ALL=C str="$1" i byte token out=''
19
+
20
+ for (( i = 0; i < ${#str}; ++i )); do
21
+ byte="${str:$i:1}"
22
+
23
+ # Escape backslashes and semi-colons
24
+ if [ "$byte" = "\\" ]; then
25
+ token="\\\\"
26
+ elif [ "$byte" = ";" ]; then
27
+ token="\\x3b"
28
+ else
29
+ token="$byte"
30
+ fi
31
+
32
+ out+="$token"
33
+ done
34
+
35
+ builtin print -r "$out"
36
+ }
37
+
38
+ __is_update_cwd() {
39
+ builtin printf '\e]6973;CWD;%s\a' "$(__vsc_escape_value "${PWD}")"
40
+ }
41
+
14
42
  __is_update_prompt() {
15
43
  __is_prior_prompt="$PS1"
16
44
  PS1="%{$(__is_prompt_start)%}$PS1%{$(__is_prompt_end)%}"
17
45
  }
18
46
 
19
- __is_update_prompt
47
+ __is_precmd() {
48
+ __is_update_cwd
49
+ }
50
+
51
+ __is_update_prompt
52
+ add-zsh-hook precmd __is_precmd
@@ -12,6 +12,10 @@ elif [ -r ~/.profile ]; then
12
12
  . ~/.profile
13
13
  fi
14
14
 
15
+ if [ -r ~/.inshellisense/bash-preexec.sh ]; then
16
+ . ~/.inshellisense/bash-preexec.sh
17
+ fi
18
+
15
19
  __is_prompt_start() {
16
20
  builtin printf '\e]6973;PS\a'
17
21
  }
@@ -20,6 +24,36 @@ __is_prompt_end() {
20
24
  builtin printf '\e]6973;PE\a'
21
25
  }
22
26
 
27
+ __is_escape_value() {
28
+ # Process text byte by byte, not by codepoint.
29
+ builtin local LC_ALL=C str="${1}" i byte token out=''
30
+
31
+ for (( i=0; i < "${#str}"; ++i )); do
32
+ byte="${str:$i:1}"
33
+
34
+ # Escape backslashes and semi-colons
35
+ if [ "$byte" = "\\" ]; then
36
+ token="\\\\"
37
+ elif [ "$byte" = ";" ]; then
38
+ token="\\x3b"
39
+ else
40
+ token="$byte"
41
+ fi
42
+
43
+ out+="$token"
44
+ done
45
+
46
+ builtin printf '%s\n' "${out}"
47
+ }
48
+
49
+ __is_update_cwd() {
50
+ builtin printf '\e]6973;CWD;%s\a' "$(__is_escape_value "$PWD")"
51
+ }
52
+
53
+ if [[ -n "${bash_preexec_imported:-}" ]]; then
54
+ precmd_functions+=(__is_update_cwd)
55
+ fi
56
+
23
57
  __is_update_prompt() {
24
58
  if [[ "$__is_custom_PS1" == "" || "$__is_custom_PS1" != "$PS1" ]]; then
25
59
  __is_original_PS1=$PS1
@@ -2,5 +2,13 @@ function __is_copy_function; functions $argv[1] | sed "s/^function $argv[1]/func
2
2
  function __is_prompt_start; printf '\e]6973;PS\a'; end
3
3
  function __is_prompt_end; printf '\e]6973;PE\a'; end
4
4
 
5
+ function __is_escape_value
6
+ echo $argv \
7
+ | string replace --all '\\' '\\\\' \
8
+ | string replace --all ';' '\\x3b' \
9
+ ;
10
+ end
11
+ function __is_update_cwd --on-event fish_prompt; set __is_cwd (__is_escape_value "$PWD"); printf "\e]6973;CWD;$__is_cwd\a"; end
12
+
5
13
  __is_copy_function fish_prompt is_user_prompt
6
14
  function fish_prompt; printf (__is_prompt_start); printf (is_user_prompt); printf (__is_prompt_end); end
@@ -1,8 +1,17 @@
1
1
  $Global:__IsOriginalPrompt = $function:Prompt
2
2
 
3
+ function Global:__IS-Escape-Value([string]$value) {
4
+ [regex]::Replace($value, '[\\\n;]', { param($match)
5
+ -Join (
6
+ [System.Text.Encoding]::UTF8.GetBytes($match.Value) | ForEach-Object { '\x{0:x2}' -f $_ }
7
+ )
8
+ })
9
+ }
10
+
3
11
  function Global:Prompt() {
4
12
  $Result = "$([char]0x1b)]6973;PS`a"
5
13
  $Result += $Global:__IsOriginalPrompt.Invoke()
6
14
  $Result += "$([char]0x1b)]6973;PE`a"
15
+ $Result += if ($pwd.Provider.Name -eq 'FileSystem') { "$([char]0x1b)]6973;CWD;$(__IS-Escape-Value $pwd.ProviderPath)`a" }
7
16
  return $Result
8
17
  }