PowerShell code folding for regions

PowerShell code folding for regions

10
Basic UserBasic User
10

    Oct 26, 2016#1

    Greetings -

    In a home baked PowerShell wordfile, I was able to use the following:

    Code: Select all

    /Open Comment Fold Strings = "region"
    /Close Comment Fold Strings = "endregion"
    Which would allow code folding for regions, such as

    Code: Select all

    #region blah
         <lots of exciting code here>
    #endregion blah
    I can't seem to get this to work anymore, and wonder if someone has any suggestions? Regions are important, as they can contain thousands of lines of code.

    Thanks!

    6,603548
    Grand MasterGrand Master
    6,603548

      Oct 26, 2016#2

      There are 2 requirements for working open/close comment fold strings:
      1. Open/close fold strings must be also defined even if there are dummy as otherwise code folding feature is disabled for the syntax language. So it is not enough to have specified in a wordfile only open/close comment fold strings.
      2. In this case with # being the line comment starting string and no word delimiter character like space between # and open comment fold string region and close comment fold string endregion the character # must be added to list of /Delimiters =.
      I downloaded the PowerShell wordfile from Wordfiles download page and improved it using the ultimate syntax highlighting tools macro collection.

      The fixes and improvements are:
      1. Changed the language number from 17 to 20 as public wordfiles should have this language number in case of downloaded and used by an UE/UES user using a very old version of UE/UES supporting only a single wordfile with 1 to 20 languages.
      2. Removed all color and font style settings using the macro SettingsDelAll as public wordfiles should not contain them in general. And those settings are also not loaded anymore since UltraEdit for Windows v20.00 and UEStudio v14.00 introducing themes.
      3. Removed useless PS_LANG as this an unknown word for UE/UES. The supported language marker keywords are listed near bottom of Syntax Highlighting help page.
      4. Removed Line Comment Alt = # as specifying twice the same string as line comment starting string is useless.
      5. Removed ` as escape character as only 1 character can be defined as escape character and I suppose the backslash is more often used than ` to escape " or '.
      6. Moved EnableSpellasYouType to left on first line as everything after File Extensions = is interpreted as file extension. With the movement spell checking on typing becomes really active.
      7. Moved EnableMLS to right in first line after String Chars = "' just to have all string highlighting definitions grouped together.
      8. Removed the lower case written file extensions ps1 psm1 psd1 as the file extensions are interpreted by UE/UES case-insensitive. For that reason it is useless specifying the file extensions in lower case additionally to upper case. And I sorted the file extensions alphabetically.
      9. Added # to list of word delimiters, sorted the word delimiters alphabetically and put space between first and second and horizontal tab between second and third non whitespace word delimiter to make it easier to see those 2 whitespace word delimiters.
      10. The regular expression strings defined as function strings are always searched case-insensitive. Therefore the function string regular expression could be simplified.
      11. Added region and endregion as open/close comment fold strings.
      12. Removed all invented STYLE_* style keywords. The style keywords really supported by IntelliTips feature of UEStudio are listed at bottom of Syntax Highlighting help page.
      13. Sorted the keywords of the color groups using macro SortLanguage.
      14. Executed the macro TestForDuplicate and removed foreach from color group 3 because existing also in color group 1 as reported by the macro.
      15. Executed the macro TestForInvalid which found no invalid keyword in any color group.
      I have attached the improved wordfile for PowerShell for verification now by you. (File deleted, see below.) Please let me know if it does a good job on syntax highlighting PowerShell scripts. Please let me also know about any suggestions for further improvements of this wordfile like other or additional function strings, more keywords, etc. I don't know nearly anything about PowerShell. So I can't validate the language definition itself.

      I would send this improved wordfile for PowerShell to IDM support by email in a week with the request to upload it to their server replacing the existing PowerShell wordfile if you or somebody else have no suggestions or comments for further improvements on this wordfile.
      Best regards from an UC/UE/UES for Windows user from Austria

      10
      Basic UserBasic User
      10

        Oct 26, 2016#3

        That completely mangled things. The region code folding does work right. It's folding wrong. See the attachment. The #endregion line for that region is at line 4087. That is followed by several other regions, all of which get swallowed up here.
        code folding.jpg (128.59KiB)
        region code folding issue

          Oct 26, 2016#4

          My script is attached here if you'd like to test further.
          Set-CsFeatures.zip (58.58 KiB)   62
          PowerShell script

          6,603548
          Grand MasterGrand Master
          6,603548

            Oct 27, 2016#5

            The reason for the wrong folding on your PowerShell script can be found at line 2961 which is:

            Code: Select all

                $path = ($file | Split-Path -Parent) +"\"
            There is a string with an escaped double quote according to language definition. The language definition contains in first line EnableMLS which means multi-line string highlighting is turned on. Therefore UltraEdit interprets the string beginning at line 2961 with escaped double quote at end as multi-line string ending at line 2964 where the next not escaped double quote is found.

            This is obviously wrong, but I don't know how to fix that because of not knowing anything about PowerShell.

            Should all "\" replaced by "\\" or "\""?

            Is Escape Char = \ wrong in first line of the wordfile and should be modified to Escape Char = `?

            Does PowerShell not support multi-line strings at all and therefore EnableMLS should be modified to DisableMLS in first line of the wordfile?

            It looks like the code on the lines 2961 to 2965 is for splitting up a file name with path. Therefore I replaced all "\" by "\\" in this code area and code folding of region at line 245 folded to endregion at line 4086 as expected.
            Best regards from an UC/UE/UES for Windows user from Austria

            10
            Basic UserBasic User
            10

              Oct 28, 2016#6

              I'm building a file path there. Not replacing a slash - adding one. No escape characters there. I went back to my old wordfile (attached), and it seems to be working.

              You can have multi-line strings.
              powershell.zip (15.81 KiB)   66
              Original

              6,603548
              Grand MasterGrand Master
              6,603548

                Oct 28, 2016#7

                I could see that in your wordfile for PowerShell Escape Char = ` instead of Escape Char = \ is used which makes the main difference on syntax highlighting of your PowerShell script. Your wordfile contains also much more functions in color group 2 and therefore is definitely better than what is currently available for download.

                I have attached your PowerShell wordfile with small corrections. foreach and prompt were listed twice, unknown style keywords present in file removed, etc.

                I have one more question:

                Must a filter and function name start with a letter or an underscore in PowerShell syntax?

                In many programming and scripting languages a symbol name must start with a letter or an underscore. A symbol name starting with a digit or dash is not allowed. If that is true also for PowerShell, it would be better to use following regular expression as function string to exclude invalid filter/function names from listing in function list:

                Code: Select all

                /Function String = "%[^t ]++^{filter^}^{function^}[^t ]+^([a-z_][0-9a-z_^-]++^)"
                powershell_v2.zip (15.88 KiB)   100
                Version 2 of the PowerShell wordfile with all the functions from wordfile of Pat Richard and changed escape character.
                Best regards from an UC/UE/UES for Windows user from Austria

                10
                Basic UserBasic User
                10

                  Oct 28, 2016#8

                  I *believe* that functions have to start with letters or underscores. I know numbers don't work.