Hide non-code lines

Hide non-code lines

7
NewbieNewbie
7

    Aug 13, 2015#1

    I'm trying to find all lines that are either comment lines or blank lines and trying to hide them. Since this is C code, I can find any line that contains whitespace only or begins with whitespace and starts with // or /*. Any suggestions on how to do that?

    I'm also having trouble with whitespace only since %[ ^t]++$ seems to hide the line after it when using the hide option.

    Thanks,
    Bassam

    6,603548
    Grand MasterGrand Master
    6,603548

      Aug 13, 2015#2

      With Perl regular expression engine you could use the search string ^[\t ]*(?:/[/*].*)?$ together with Filter lines and Hide selected.

      But you need to know that that hide lines feature is designed for hiding all lines containing a searched string. It is not designed for hiding all lines found by a regular expression which matches a string over more than one line. So it is not possible to hide a real multi-line block comment. If a regular expression search string matches strings over multiple lines, just the line on which found string starts is hidden by UltraEdit.

      Explanation of the search expression:

      ^ ... beginning of line.

      [\t ]* ... tab or space 0 or more times.

      (?:...)? ... the expression in this non capturing group 0 or 1 times.

      /[/*] ... a slash and one more slash or an asterisk.

      .* ... any character except newline characters 0 or more times greedy (greedy = up to end of line in this case).

      $ ... end of line.

      A Perl regular expression to remove with a Replace All all
      • empty and blank lines,
      • lines containing only a line comment, and
      • block comments starting after 0 or more tabs/spaces of a line
      would be ^(?:[\t ]*(?://.*|/\*[\s\S]*?\*/[\t ]*)?(?:\r?\n|\r|$))+

      Note: Nested block comments are not supported by this expression nor by any regular expression engine. The matching of a block comment always ends on first */ independent on how many /* are within already matched comment block.

      ^ ... beginning of line.

      (?:...)+ ... the expression in this outer non capturing group 1 or more times greedy (greedy = as many lines or blocks as possible).

      [\t ]* ... tab or space 0 or more times.

      (?:...)? ... the expression in this inner non capturing group 0 or 1 times.

      //.* ... two slashes and any character except newline characters 0 or more times greedy (greedy = up to end of line in this case).

      | ... OR

      /\*[\s\S]*?\*/[\t ]* ... a slash and an asterisk (must be escaped here) and any character including newline characters 0 or more times non-greedy (non-greedy = stop on first */ and not on last */) and an asterisk (again escaped with a backslash) and tab or space 0 or more times greedy (greedy = end of line in this case).

      (?:...) ... the expression in this second inner non capturing group.

      \r?\n|\r|$ ... a carriage return 0 or 1 times and a line-feed (DOS/UNIX) OR only a carriage return (MAC) OR end of line being interpreted here as end of file in case of last line in file does not end with a line termination.

      Yes, Perl regular expressions are very powerful, but not easy to learn as it can be seen here especially on question mark having different meanings depending on context (non-capturing, 0 or 1 times, non-greedy).

        Aug 13, 2015#3

        The UltraEdit regular expression %[ ^t]++$ finds nothing on an empty line (= line not containing any character) as well as carriage return of this empty line.

        The UltraEdit regular expression engine has problems to find something and at same time matching nothing since I'm using UltraEdit (v8.00).

        %[ ^t]++$ works unexpected as explained above on empty lines from v10.10c (oldest one archived by me) up to v22.10 (currently latest).

        There is no problem with %[ ^t]++$ if a line is a blank line, i.e. contains 1 or more tabs or spaces as now the expression matches at least one character.
        Best regards from an UC/UE/UES for Windows user from Austria

        7
        NewbieNewbie
        7

          Aug 14, 2015#4

          Thanks, for the response. As you've surmised, I use UE to build my RegEx and not Perl. I seem to remember IDM saying their UE RegEx is buggy and to switch to Perl.

          To clarify, I'm not using it to delete lines (Find/Replace). I'm using it with the "Filter lines | Hide" option to just hide them. However, trying this "^(?:[\t ]*(?://.*|/\*[\s\S]*?\*/[\t ]*)?(?:\r?\n|\r|$))+" with Perl then selecting "Filter lines | Hide" at the bottom of the Find dialog and selecting Next only hides some lines in C code. The comments (/* ... */) are not all hidden and the empty lines still show. My Perl is non-existent and any help would be greatly appreciated.

          Thanks,
          Bassam

          6,603548
          Grand MasterGrand Master
          6,603548

            Aug 14, 2015#5

            I have written already in my previous post that Filter lines feature is not designed for hiding blocks. It is impossible to use a regular expression which matches a block and not just a string within a line and get all lines of the block hidden.

            This should be easy to understand by a C programmer. The function called to find out which lines to hide has only 1 return value, the number of the line in which the searched string or more precisely in which first character of searched string was found. A prototype of this C function could look like

            unsigned int LineToHide(const wchar_t* SearchString, ...);

            and function returns 0 if nothing found or a value greater 0 if a line to hide was found. And this function is executed in a loop until 0 is returned.

            To support hiding all lines of a found multi-line string the function would need to be coded different as two values need to be returned now, the number of the line where the found string begins and the number of the line where the found strings end. But Filter lines feature is not coded for such multi-line return values.

            Note: I don't know how the source code really looks like. But it can be seen how it works.

            So if your C files contain

            Code: Select all

            /* This is a multi-line
               block comment. */
            and not just

            Code: Select all

            /* Block comment within a single line. */
                    /* Another block comment within a single line. */
                /* This is also a multi-line
                 * block comment, but each line of the
                 * block comment starts with an asterisk. */
            it is impossible with Find and Filter Lines - Hide to hide all lines of a block comment.

            The Perl regular expression to hide all lines with Filter lines - Hide for such block comments would be ^[\t ]*(?:/[/*].*|\*.*)?$

            In comparison to first posted expression |\*.* was inserted in non capturing group to find also lines starting with an asterisk after 0 or more tabs or spaces.
            Best regards from an UC/UE/UES for Windows user from Austria

            7
            NewbieNewbie
            7

              Aug 14, 2015#6

              Thanks, I somehow missed that. I suppose I could do a replace and not save the file. I was looking for a way to view inherited code files which are way too big, but more manageable when compacted without comments and blank lines. Thanks for the tutorial.

                Aug 14, 2015#7

                It turns out, some of my block comments did start with *, so your last RegEx for use with folding was extremely useful. Using your first RegEx with replace all, it seems that it replaces all lines with empty lines. I am able to replace two consecutive empty lines with one though. The only issue with that is that if I need to make code changes, which I currently don't, a replace all would not obviously work, but the folding would. So for the most part, working with your folding RegEx is good enough for what I need.

                2 minutes later: Figured out that adding \r\n at the end of the RegEx takes care of my blank lines problem.

                To quote a special someone: D'oh!

                Thanks a lot!

                Bassam

                6,603548
                Grand MasterGrand Master
                6,603548

                  Aug 14, 2015#8

                  The search strings   ^[\t ]*(?:/[/*].*)?$   and   ^[\t ]*(?:/[/*].*|\*.*)?$   are for a Find with Filter lines set to Hide. $ at end of both strings means end of line without matching the newline characters. When using those search strings for a replace all, the newline characters carriage return and line-feed remain. The goal of those expressions is to identify lines to hide and not to remove them.

                  The search string   ^(?:[\t ]*(?://.*|/\*[\s\S]*?\*/[\t ]*)?(?:\r?\n|\r|$))+   is designed for a Replace All. Instead of $ the expression \r?\n|\r|$ is used in this search string to find AND match a DOS (\r\n) or a UNIX (just \n) or a MAC (just \r) line termination. $ as last OR argument makes sure to remove also a comment at end of file if the file does not end with a line termination. This expression is written for removing lines and not just find them.

                  I hope this makes it clear now why using the first expression used with a replace all left behind empty lines.
                  Best regards from an UC/UE/UES for Windows user from Austria

                  7
                  NewbieNewbie
                  7

                    Aug 14, 2015#9

                    I can follow your logic, I just have a hard time creating my own Perl RegEx since I've used UE's RegEx for many years and only used UE for RegEx.

                    The first folding RegEx was fine, but left the comments that started with * when folded.
                    The second folding RegEx was better and did what I needed so far.
                    The ReplaceAll RegEx also works, but removes the indents for the lines of code remaining thereby making it less readable.

                    The second folding RegEx seems the best suited. Now if only UltraEdit can copy and paste unfolded code only while ignoring everything folded.

                    Thanks again!

                    Bassam

                    6,603548
                    Grand MasterGrand Master
                    6,603548

                      Aug 15, 2015#10

                      BAbdulBaki wrote:The ReplaceAll RegEx also works, but removes the indents for the lines of code remaining thereby making it less readable.
                      Yes, you are right. The expression was not good as it removed all leading tabs and spaces, too. I corrected the regular expression for removing empty and blank lines, line and block comments starting at beginning of a line after 0 or more tabs or spaces in all my previous posts. The slightly modified expression does not remove anymore leading spaces and tabs.
                      Best regards from an UC/UE/UES for Windows user from Austria

                      7
                      NewbieNewbie
                      7

                        Aug 18, 2015#11

                        Awesome, thanks!