Tapatalk

Fold/unfold every method at a specific indentation level?

Fold/unfold every method at a specific indentation level?

2
NewbieNewbie
2

    Dec 28, 2018#1

    Hi,

    I work with long JS files, which is pretty much a big object containing multiple methods (method name as key, function as value).

    I'd like to fold all the methods located on the same indentation level, all at once but I can't find how to do that, instead of one by one.

    That would make it much faster to see all of the methods at once and unfold only the one I'm working on.

    See attached screenshot to illustrate.

    Is there such an option?

    If not, is folding/unfolding the current line supported by the script? I could write a script to do that for me if there's no built-in way. I just can't find that neither.

    Thanks.
    level_based_fold_unfold.png (33.77KiB)
    Screenshot illustrating what should be folded.

    6,685587
    Grand MasterGrand Master
    6,685587

      Dec 29, 2018#2

      UltraEdit v25.20.0.88 has no command to collapse/expand folds of a specific folding level. But what you want can be achieved with a script or macro.

      Script solution:

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Bookmark current line in active file.
         UltraEdit.activeDocument.toggleBookmark();
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define the parameters for the case-sensitive Perl regular expression
         // Find used below to find beginning of a function block at second level.
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
      
         /* Search in a loop for lines
      
            + starting with two horizontal tabs or 8 normal spaces
            + and next having
              - character { or
              - the word function, one or more characters and finally { or
              - a word character, one or more characters left to word
                function, one or more characters and finally {.
      
            Execute on each found line matching the search criteria the command
            to toggle visibility, i.e. collapse or expand the function block.
         */
         while(UltraEdit.activeDocument.findReplace.find("^(?:\\t{2}| {8})(?:(?:function\\b|\\w.+\\bfunction\\b).+)?{"))
         {
            UltraEdit.activeDocument.hideOrShowLines();
         }
      
         // Move caret back to bookmarked line. If this line is inside a collapsed
         // function block, the entire function block is expanded by UltraEdit.
         UltraEdit.activeDocument.gotoBookmark(-1);
         UltraEdit.activeDocument.toggleBookmark();
      }
      
      Please read the comments and adapt (?:\\t{2}| {8}) on using not two horizontal tabs or 2x4 spaces to indent the functions to fold/unfold. There is no scripting command to explicitly fold a block and also no command to explicitly unfold a block. There is also no property with information if current line is first line of a currently folded block. So the script toggles the folding of all function blocks on indentation level 2 on execution.

      Macro solution:

      Code: Select all

      InsertMode
      ColumnModeOff
      ToggleBookmark
      Top
      PerlReOn
      Loop 0
      Find MatchCase RegExp "^(?:\t{2}| {8})(?:(?:function\b|\w.+\bfunction\b).+)?{"
      IfFound
      HideShowSelection
      Else
      ExitLoop
      EndIf
      EndLoop
      GotoBookMark -1
      ToggleBookmark
      
      The macro does exactly the same as the script.

      Further I recommend opening the Function List which shows not only a list of functions in active file (or all files of a project), but marks always also the function on which code the caret is current positioned.

      PS: I tested the script and macro with my own test file created quickly from FileNameFunctions.js because I did not want to copy out the code from your screenshot. So I just can hope the script/macro works also for your JavaScript files.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Jan 04, 2019#3

        Thanks!