0

I'm having a small problem with folding and unfolding things in VSCode shellscript code. When I write a function and add a help section to it, with a heredoc that ends in EOF, naturally - After this, when I fold the function from next to the function name, it folds down until the EOF text, nothing else. The EOF can be further folded to hide most of the function, but this is really getting on my nerves.

I use regions a lot aswell, since I'm used to those, but they also break from EOF.

Is the only way to stop using heredocs/nowdocs, or is there a way to fix this?

Thanks in advance - Included two screenshots, one unfolded, and the other one folded, and as you can see, EOF stops the folding.

I tried to fold a function in Shellscript language file, but if there is "EOF", "EOL" or similar (end to a here/nowdoc), the folding folds only up to that point and no further.

(Screenshot) Unfolded Code (Screenshot) Code Folded Until EOF


UPDATE: Here is the code as is, instead of screenshots:

_validate_input() {
  # If -h|--help specified or no args provided, output help information.
  if [[ $# -lt 2 || $@ =~ '(^-h| -h|--help$|--help |^-[[:alnum:]]*h| -[[:alnum:]]*h)' ]]; then
    cat <<-EOF && return 0
      $funcstack[1] - Query an input string from the user.
 
      Usage: $funcstack[1] [INPUT] [VALIDATION_TYPE]
 
        INPUT
          Input string to validate.
 
        VALIDATION_TYPE
          Type to validate; Currently available: "name", "email".
EOF
  fi
 
  local input="$1" # Get input from first argument.
  local valtype="$2" # Get validation type from second argument.
 
  # Ensure validation type value is valid.
  if [[ $valtype == 'email' ]]; then
    # Check that input is a valid email.
    if echo $input | pcre2grep '^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$' | read input_checked; then
      return 0
    else
      printf "\e[31;1mError: Invalid email "%s". (Matched against: \$'^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\$').\e[0m\n" "$input" >&2
      return 1
    fi
  elif [[ $valtype == 'name' ]]; then
    # Check that input is a valid name.
    if echo $input | pcre2grep $'^[A-Z][a-zA-Z \'.-]*((?![0-9])[^-])$' | read input_checked; then
      return 0
    else
      printf "\e[31;1mError: Invalid name "%s" (Matched against: \$'^[A-Z][a-zA-Z \'.-]*((?![0-9])[^-])$').\e[0m\n" "$input" >&2
      return 1
    fi
  else
    printf '\e[31;1mError in _validate_input(): validation type value "%s" is invalid; Must be "name" or "email".\e[0m\n' "$valtype" >&2
    return 1
  fi
}

That code, when folded in my IDE, folds from the function name until EOF.

10
  • 1
    Please provide your code as text. If you need to show us IDE behavior, perhaps also add screenshots in addition to the text, but don't leave out the text. See also Why should I not upload images of code/data/errors? Commented Jun 17 at 20:33
  • (If someone else spins up a copy of VS Code and wants to reproduce the problem, don't make them retype your code -- text lets folks copy-and-paste; also, text prevents us from needing to guess at which kinds of nonprintable characters you're using -- relevant here in a question where the distinction between tabs and spaces is extremely pertinent). Commented Jun 17 at 20:34
  • That said, since you're using <<-EOF as opposed to <<EOF, as long as you're using tabs and not spaces for indentation, you should be able to indent the EOF sigil to hint to the editor how to handle folding. You will need to reconfigure the editor into tabbed mode, though, if it's not already configured appropriately. Commented Jun 17 at 20:35
  • Perhaps unrelated, but why cat when you can use printf? Commented Jun 17 at 21:08
  • @CharlesDuffy Sorry about that, I don't post here often, almost never really.. I've added code of a function that causes the same folding bug when folding function, it only folds until EOF, not the whole function.
    – dxo
    Commented Jun 17 at 21:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.