• Login
Community
  • Login

How do I create a custom fold in PowerShell

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 3 Posters 344 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D
    David A. Stewart
    last edited by Dec 19, 2024, 6:54 PM

    Is there a way to add a custom collapse option for PowerShell function Param() sections in the config file(s) for .psm1 files?

    P M 2 Replies Last reply Dec 19, 2024, 6:58 PM Reply Quote 0
    • P
      PeterJones @David A. Stewart
      last edited by PeterJones Dec 19, 2024, 7:13 PM Dec 19, 2024, 6:58 PM

      @David-A-Stewart ,

      In general, where things fold is a function of the underlying lexer (from the Lexilla library that Notepad++ uses), and there isn’t customization available for that.

      I think you could use one of the scripting plugins like PythonScript to add a custom fold point using a notification-on-change callback, and some of the commands, but I don’t know how stable custom folds are when Notepad++/Lexilla is also trying to control folding.

      P D 2 Replies Last reply Dec 19, 2024, 7:13 PM Reply Quote 2
      • P
        PeterJones @PeterJones
        last edited by Dec 19, 2024, 7:13 PM

        moderator note: I re-created your post under a different name; something about the previous title used is triggering a weird forum bug. Sorry to all the people who see, but cannot read, multiple copies with a similar name. I will try to clean up the database.

        update: I think I’ve cleaned up the database; sorry for any who were confused by the extra empty/unavailble posts

        1 Reply Last reply Reply Quote 2
        • D
          David A. Stewart @PeterJones
          last edited by Dec 21, 2024, 9:53 PM

          @PeterJones Thanks, so it sounds like a feature request is my best bet, do you agree?

          P 1 Reply Last reply Dec 21, 2024, 11:18 PM Reply Quote 0
          • P
            PeterJones @David A. Stewart
            last edited by Dec 21, 2024, 11:18 PM

            @David-A-Stewart ,

            so it sounds like a feature request is my best bet

            Notepad++ uses a library called Lexilla to handle the lexing (syntax highlighting and folding and the like), so you’d have to check the Lexilla Issues list and see if such a request already exists, and if not, put it in there; once the feature is in Lexilla, then Notepad++ would have to update to the most recent version of Lexilla for a future release. Good luck.

            D 1 Reply Last reply Dec 29, 2024, 8:55 PM Reply Quote 3
            • M
              mpheath @David A. Stewart
              last edited by mpheath Dec 22, 2024, 3:59 AM Dec 22, 2024, 3:58 AM

              @David-A-Stewart You could use #region and #endregion to do explicit folding in Powershell. These are case-sensitive so must be lowercase to be recognized by the Powershell lexer.

              example1 :

              function Test-Remainder {
                  param(
                      [Parameter(Mandatory, Position=0)]
                      [string]$Value,
              
                  [Parameter(Position=1, ValueFromRemainingArguments)]
                      [string[]]$Remaining
                  )
              
                  "Found $($Remaining.Count) elements"
              
                  for ($i = 0; $i -lt $Remaining.Count; $i++) {
                      "${i}: $($Remaining[$i])"
                  }
              }
              Test-Remainder first one,two
              

              can be edited to :

              function Test-Remainder {
                  #region
                  param(
                      [Parameter(Mandatory, Position=0)]
                      [string]$Value,
              
                  [Parameter(Position=1, ValueFromRemainingArguments)]
                      [string[]]$Remaining
                  )
                  #endregion
              
                  "Found $($Remaining.Count) elements"
              
                  for ($i = 0; $i -lt $Remaining.Count; $i++) {
                      "${i}: $($Remaining[$i])"
                  }
              }
              Test-Remainder first one,two
              

              There are no other explicit comments in the Powershell lexer like some lexers have, like #{ and #} to cause extra folding.

              The folding options of the Powershell lexer are fold.comment, fold.compact and fold.at.else which can be changed by a scripting plugin. The fold.comment option does affect the region comment folding and the value set is 1 to allow comment folding.

              D 1 Reply Last reply Dec 22, 2024, 4:04 AM Reply Quote 0
              • D
                David A. Stewart @mpheath
                last edited by Dec 22, 2024, 4:04 AM

                @mpheath Thanks, that is what I have been doing so far, but it’s a band-aid, I do appreciate taking the time to respond. I have not dug into the Notepad++ options too much, except where I use them to write scripts.

                1 Reply Last reply Reply Quote 0
                • D
                  David A. Stewart @PeterJones
                  last edited by Dec 29, 2024, 8:55 PM

                  @PeterJones Thanks, I’ll look into that.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors