Tapatalk

BASH Code Folding

BASH Code Folding

2
NewbieNewbie
2

    Dec 20, 2016#1

    Hi All -

    I'm having an issue with code folding in BASH that I've been unsuccessful at fixing. It's not a show stopper, but REALLY REALLY annoying... With a few thousand lines of code it becomes difficult to use. Hopefully someone here has already solved this.

    There are a few commands in BASH that use the pound sign (#) in them. This is also the comment character. Dumb, I know. For example, the following throws off code folding:

    Code: Select all

    function foo {
    	my_array=(1 2 3 4)
    
    	for number in ${my_array[@]}; do
    		echo "The number is ${number}."
    	done
    
    	echo "There are ${#my_array[@]} numbers in the array."
    	
    	echo "Now you can see the code folding just go borked."
    }
    As soon as I use the "#" on the array variable to get the count, it throws off code folding. There are other instances with variable substitution that use pound signs and throw it off as well. I've attached before/after images.

    Has anyone successfully overcome this without losing any other code folding/syntax highlighting function?

    Thanks in advance!
    with_pound.jpg (21.22KiB)
    Code folding with the pound in place.
    without_pound.jpg (22.21KiB)
    Code folding without the pound in place.

    6,685587
    Grand MasterGrand Master
    6,685587

      Dec 21, 2016#2

      The code folding behavior depends on the definitions in wordfile used for syntax highlighting shell scripts.

      With UltraEdit v23.20 is installed shell.uew with following lines at top:

      Code: Select all

      /L20"UNIX Shell Scripts" Line Comment = #  Escape Char = \ String Chars = "' File Extensions = sh ksh csh SH KSH CSH
      /Delimiters = ~&-%^+|\=/:;"'` ,
      /Regexp Type = Perl
      /Function String = "function (.*)\s*\{"
      /Indent Strings = "{" "if" "then" "for" "while" "do" "case" "function"
      /Unindent Strings = "}" "else" "fi" "endif" "done" "esac" "exit" "return"
      /Open Fold Strings = "{" "do" "if" "else" "case"
      /Close Fold Strings = "}" "done" "fi" "else" "esac"
      
      The wordfile shell.uew should be copied from wordfiles subdirectory in program files directory of UltraEdit to the directory displayed at Advanced - Settings/Configuration - Editor Display - Syntax Highlighting if not already copied by UltraEdit itself.

      The syntax highlighting and code folding with those lines in the wordfile is:

      shell_script_code_folding.png (2.43KiB)
      Syntax highlighting with code folding of shell script using shell.uew of UE v23.20.

      Well, I would make three changes on this wordfile in %APPDATA%\IDMComp\UltraEdit\wordfiles:
      1. File extensions are interpreted always case-insensitive. Therefore it is useless to specify after File Extensions = the file extensions completely in lower case and additionally completely in upper case. It would be enough to use CSH KSH SH.
      2. I would add the horizontal tab character to line starting with /Delimiters = containing currently only the whitespace character space between ` and , which means using

        /Delimiters = " %tab&'+,-/:;=\^`|~

        whereby tab is replaced by the horizontal tab character.
      3. There are two spaces after Line Comment = # where one space would be enough. I would add between those two spaces after Line Comment = # the definition Line Comment Preceding Chars = [~!-~] which would result in interpreting # as line comment starting string only if there is any character outside the range 0x21 to 0x7E before (usually space or tab character) or is at beginning of a line. # in ${#my_array[@]} is with current syntax highlighting definition not interpreted as line comment starting character because of # and { are not word delimiters required to highlight everything starting with $ as variable reference.
      shell.uew with the modifications would be:

      Code: Select all

      /L20"UNIX Shell Scripts" Line Comment = # Line Comment Preceding Chars = [~!-~] Escape Char = \ String Chars = "' File Extensions = CSH KSH SH
      /Delimiters = " %	#&'+,-/:;=\^`|~
      /Regexp Type = Perl
      /Function String = "function (.*)\s*\{"
      /Indent Strings = "{" "if" "then" "for" "while" "do" "case" "function"
      /Unindent Strings = "}" "else" "fi" "endif" "done" "esac" "exit" "return"
      /Open Fold Strings = "{" "do" "if" "else" "case"
      /Close Fold Strings = "}" "done" "fi" "else" "esac"
      
      Note: The multiple spaces displayed by the browser between % and # according to HTML specification in second line must be replaced after copying & pasting those lines into the wordfile by a tab character.

      By the way: The image format PNG is much better for screenshots than JPEG. PNG uses a lossless compression resulting in not adding pixel noise to screenshots as JPEG compression does and produces in general smaller files for screenshots than JPEG. PNG is supported by all applications supporting also JPEG because PNG is an open international image format developed for world wide web as a replacement for GIF. PNG should be used for raster graphics and JPEG for photos and paper scans.
      Best regards from an UC/UE/UES for Windows user from Austria

      115
      Power UserPower User
      115

        Dec 21, 2016#3

        I don't use BASH but I do use Korn script and it suffers from the same problem where the # is both comment character and performs special functions like getting a substring value or referring to the number of command line parameters.

        Thanks for the solution Mofi, it works great for me too.

        2
        NewbieNewbie
        2

          Dec 21, 2016#4

          Mofi wrote:The code folding behavior depends on the definitions in wordfile used for syntax highlighting shell scripts.

          There are two spaces after Line Comment = # where one space would be enough. I would add between those two spaces after Line Comment = # the definition Line Comment Preceding Chars = [~!-~] which would result in interpreting # as line comment starting string only if there is any character outside the range 0x21 to 0x7E before (usually space or tab character) or is at beginning of a line. # in ${#my_array[@]} is with current syntax highlighting definition not interpreted as line comment starting character because of # and { are not word delimiters required to highlight everything starting with $ as variable reference.
          Huzzah!! I've been using a bash.uew file I got from the wordfile collection on IDM's site, but the changes you mention above (the Line Comment Preceding Chars) did the trick! It appears the code folding is working as expected now, without affecting any of the other highlighting.

          Thank you very much!!!!

          P.S. - I'll use PNG. :)