Community
    • Login

    Code Folding: Don't include the closing brace in the folded text for UDL or existing language

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    9 Posts 2 Posters 56 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.
    • Yellow FolderzipY
      Yellow Folderzip
      last edited by Yellow Folderzip

      When I fold a code level, I don’t want to also include the final, closing })] delimiter in the hidden part of the fold. How do I modify an existing language or UDL to include this brace folding behavior from python?

      If I select python as the language, and fold a layer using {} braces, the beginning line and the final line will be shown, which allows you to use the margin select to grab the entire block cleanly. Whereas most languages will include the closing line in the hidden part of the fold, and you need to rely on a newline after a block to properly select the entire block while it’s folded.

      Untitled.png

      In this image, the top is CSS syntax, the bottom is python and in the python image, you see the code block’s closing brace is visible, but the CSS text, does not; the next visible line is the closing brace for the fold level above this block. I highlighted level 1 with green and 2 with red.

      https://www.youtube.com/watch?v=cvPrOKPnfD4
      I also made this quick demonstration of how the folds are different.

      PeterJonesP Yellow FolderzipY 2 Replies Last reply Reply Quote 1
      • Yellow FolderzipY
        Yellow Folderzip @Yellow Folderzip
        last edited by

        @Yellow-Folderzip Since i don’t know how to edit a post (can you?), a quick clarification for:

        I don’t want to also include the final closing } brace or any ) delimiter

        By “any delimiter” I mean whichever one delimiter the language happens to use, not any delim in the block.

        PeterJonesP 1 Reply Last reply Reply Quote 1
        • PeterJonesP
          PeterJones @Yellow Folderzip
          last edited by PeterJones

          @Yellow-Folderzip said in Code Folding: Don't include the closing brace in the folded text for UDL or existing language:

          If I select python as the language, and fold a layer using {} braces, … How do I modify an existing language or UDL to include this brace folding behavior from python?

          Python doesn’t fold on braces. Python folds on indentation. So it’s not including the braces in the fold because braces have nothing to do with Python indentation.

          And no, you cannot set UDL to use indentation for folding; it was defined to be keyword/symbol-based for everything, including folding. (It’s been requested in the official feature-request multiple times, and hasn’t been implemented yet.)

          Looking through the Scintilla documentation (Scintilla being the library that Notepad++ uses for doing things like the folding), I don’t think it has an option to “don’t hide the last/closing line of a fold section when folded”… and without that option, there’s nothing that Notepad++ can do to do the folding the way you want. (However, it’s possible that I’ve missed something; one of the other Scintilla experts could chime in and let me know which SCI message would allow it.)

          However, as a workaround, there is a Scintilla message, SCI_SETDEFAULTFOLDDISPLAYTEXT, which can be used to set extra text at the end of the shown fold line… so if you sent that message with “}” as the lParam, it would then do
          5cd606ad-ca67-442e-ac76-760d8d489ebe-image.png => 0d4cc434-165f-4968-bde7-3c732a8b1a0d-image.png
          … if that’s sufficient for you, then you could use the PythonScript plugin set to ATSTARTUP, and in your user startup.py, you could have the lines

          editor1.setDefaultFoldDisplayText("}");
          editor2.setDefaultFoldDisplayText("}");
          

          which would then make it show that extra } at the end of the folding line when it is collapsed.

          Yellow FolderzipY 1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @Yellow Folderzip
            last edited by

            @Yellow-Folderzip said in Code Folding: Don't include the closing brace in the folded text for UDL or existing language:

            @Yellow-Folderzip Since i don’t know how to edit a post (can you?),

            In general, one can edit a post using the ⋮ menu on the post:
            e255c5ce-08e0-49a0-81e2-902f4f81fe56-image.png

            A post is editable for 4 hours after it’s made. (But it might require a user to get a few upvotes before they gain the edit-their-post privilege, as part of the anti-spam settings on the forum.)

            a quick clarification for:

            I don’t want to also include the final closing } brace or any ) delimiter

            By “any delimiter” I mean whichever one delimiter the language happens to use, not any delim in the block.

            My example PythonScript just assumes the }, so it wouldn’t work in general for you. A more complicated script could be used that would change that default extra-text every time you change to a different file/tab/view, and choose that text based on the filetype.

            (But part of me is hoping, for your sake, that I’ve just not found the right message, and that there is one that allows changing whether the last line of folded text is hidden or not. I am hoping someone else will eventually chime in…)

            1 Reply Last reply Reply Quote 1
            • Yellow FolderzipY
              Yellow Folderzip @PeterJones
              last edited by Yellow Folderzip

              @PeterJones This does show the closing brace on the end, but it unfortunately doesn’t do what I am looking to fix here. That being, the ability to select the entire block and its closing brace + newline.

              3.png

              The top image appears to have selected until the closing } brace, but when i expand the section, the bottom image shows that it has only selected the first line.

              PeterJonesP 1 Reply Last reply Reply Quote 0
              • PeterJonesP
                PeterJones @Yellow Folderzip
                last edited by PeterJones

                @Yellow-Folderzip said in Code Folding: Don't include the closing brace in the folded text for UDL or existing language:

                That being, the ability to select the entire block and its closing brace + newline.

                That’s easy, and doesn’t need any change to Notepad++: that happens automatically when selecting a hidden block: if you just select from the beginning of the visible line to the end, it will only select that line, with no newline (just like what happens if you select a fully visible line that way); however, if you select to the start of the next line, then it will include the entire hidden block:

                if I have the code

                if(condition) {
                	blah;
                }
                
                

                as here:
                9a8975e2-534d-41d5-b296-faeec1a6dd1d-image.png

                then hide it.

                If I select just to the end of the line with the { , then it will only select the if(condition) { with no newline and no contents from the block:
                5ff9a655-6a89-49b2-8c59-9f46a4f48afb-image.png

                if(condition) {
                

                But if I do a shift+downarrow from the start of the line, to select to the beginning of the next line:
                307f5e0a-1afd-4883-9655-de3efadc3d3d-image.png
                then the selected text includes the entire block, including the newline:

                if(condition) {
                	blah;
                }
                

                This is the same behavior as selecting fully-visible text:

                • newline not included in selection:
                  1eb4ea8c-b95d-4da9-a4ed-a707a04f5693-image.png
                • newline of first line in selection:
                  e34d7614-7537-4009-a0bc-48d9302f12cb-image.png
                • whole block, but not final newline selected:
                  9ecbbb38-6a10-4606-a60e-a8613a402808-image.png
                • whole block, with final newline selected:
                  dd1d6dd8-4d8e-4072-b59f-7ee6f6e8fe2d-image.png

                Notepad++ indicates whether the newline is selected by extending the visible selection beyond the end of the line when it is. And if you really want to be sure, use View > Show Symbol > Show End of Line:

                • EOL not selected, so not whole block:
                  6daf1470-6af9-41da-96a7-3e86e82092e7-image.png
                • EOL selected, so whole block is actually selected, including final EOL:
                  d0dcc7fd-0ce3-4f7b-a705-48d530f80612-image.png
                Yellow FolderzipY 1 Reply Last reply Reply Quote 0
                • Yellow FolderzipY
                  Yellow Folderzip @PeterJones
                  last edited by Yellow Folderzip

                  @PeterJones This is what I meant to describe by:

                  and you need to rely on a newline after a block to properly select the entire block while it’s folded

                  in the original post. Because without a double newline after the closing brace, selecting down one row will grab the first line of the next block. If I were to rely on this functionality, I would have to insert a double newline after every block, at the green arrow.

                  1.PNG
                  2.PNG

                  PeterJonesP 1 Reply Last reply Reply Quote 0
                  • PeterJonesP
                    PeterJones @Yellow Folderzip
                    last edited by

                    @Yellow-Folderzip said in Code Folding: Don't include the closing brace in the folded text for UDL or existing language:

                    Because without a double newline after the closing brace, selecting down one row will grab the first line of the next block.

                    If you select the whole second fold line, of course it will select from that, just like with non-folded text. If you end the selection at the beginning of the line, rather than selecting the line, then it will work as you desire.

                    {
                    	.class1 {
                    		color: red;
                    		border: 1px solid green;
                    	}
                    	.class2 {
                    		
                    	}
                    
                    }
                    

                    Normal (visible) text:

                    • To select one line (including EOL) of indented text when next line is indented to the same level, stop the selection at the beginning of the next line
                      b00e8b79-e98c-46cb-a401-9a7104b721f9-image.png
                    • To select both lines (including EOL), then stop at the beginning of the line following those lines.
                      f48e4f39-7d2c-4f95-93fa-91db45e44aae-image.png

                    Similarly, with collapsed blocks:

                    • To select just the first .class1 block when collapsed, just select to the beginning of the next line:
                      cadc27f9-4d1b-498f-9f78-a0fb57fa199a-image.png
                    • To select both, then select to the beginning of the line after:
                      9ee2af09-ca22-4417-b470-f43eb698d094-image.png

                    There is no need to have a blank line between them. Just select as much as you want to copy, exactly identical to how it works with no folded text.

                    Yellow FolderzipY 1 Reply Last reply Reply Quote 1
                    • Yellow FolderzipY
                      Yellow Folderzip @PeterJones
                      last edited by Yellow Folderzip

                      @PeterJones Ah yah, I understand now. I suppose this is a fine solution, so thank you for that. This still requires some finesse to get in front of the 1st column versus just click+dragging inside the margin, but definitely better than before. 😎

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