Community
    • Login

    XBrackets 2.0 is coming!

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    6 Posts 2 Posters 209 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.
    • Vitalii DovganV
      Vitalii Dovgan
      last edited by

      As I promised, a lot of new things have been coming with a new version of XBrackets Lite!
      There are new functions:

      • Go To Matching Bracket
        when the caret is at a bracket or quote character, jumps to the pair bracket or quote, e.g. |( ) becomes ( )|
      • Sel To Matching Brackets
        when the caret is at a bracket or quote character, selects the pair of the brackets or quotes, e.g. |( ) becomes |( )|
      • Go To Nearest Bracket
        when the caret is between bracket or quote characters, jumps to the nearest surrounding bracket or quote, e.g. “ab|c” becomes “|abc”
      • Sel To Nearest Brackets
        when the caret is between bracket or quote characters, selects the text within the surrounding brackets or quotes, e.g. “ab|c” becomes “|abc|”

      These functions are based on an internal brackets tree. The brackets tree itself is actually an std::vector where each item knows an index of its parent item, thus allowing to travel up to the root very fast.

      XBrackets options - or, rather, XBrackets configuration - are now stored in a required file “XBrackets_Config.json” which literally defines language-specific syntax and autocomplete rules for different file extensions.
      The bracket and quote pairs now can consist of multiple characters, e.g. /* */, """ """ and so on.
      The following languages/syntaxes are already supported:
      Text/Unknown, C, C++, C#, Java, JavaScript, awk, Go, Pascal, ini, reg, JSON, XML, HTML, bat/cmd, PowerShell, sh, AutoHotkey, Python, Perl, Lua.

      If your favorite language is missed here or if you find something incorrect in XBrackets Lite’s behavior, please feel free to contribute while the plugin is hot and is being actively developed :)

      The source code is here:
      https://github.com/d0vgan/npp-XBracketsLite/commits/feature/GoToNearestBr/

      The documentation is here:
      https://github.com/d0vgan/npp-XBracketsLite/blob/feature/GoToNearestBr/XBrackets/XBrackets.txt

      The XBrackets Lite configuration file is here:
      https://github.com/d0vgan/npp-XBracketsLite/blob/feature/GoToNearestBr/XBrackets/XBrackets_Config.json

      The plugin already can auto-complete brackets that consists of words, such as begin end in Pascal (you just need to add them to the “autocomplete” property of the “Pascal” node in “XBrackets_Config.json”), but there is a technical nuance. When you type “beg” and Notepad++ suggests you a word “begin” and you press Enter to accept the word “begin”, there will be no auto-completion with “end” in this case. Here is why: I could not find a way to distinguish between a word “begin” being suggested by Notepad++ and a word “begin” being pasted (e.g. via Ctrl+V). I believe, “begin” should be auto-completed with “end” in case of typing, and should not be auto-completed in case of pasting. If you know how to do that, please let me know.

      rdipardoR 1 Reply Last reply Reply Quote 4
      • rdipardoR
        rdipardo @Vitalii Dovgan
        last edited by

        @Vitalii-Dovgan said in XBrackets 2.0 is coming!:

        I could not find a way to distinguish between a word “begin” being suggested by Notepad++ and a word “begin” being pasted (e.g. via Ctrl+V).

        Have you tried listening for SCN_AUTOCCOMPLETED ?

        This notification is generated after an autocompletion has inserted its text. The fields are identical to the SCN_AUTOCSELECTION notification.

        Vitalii DovganV 1 Reply Last reply Reply Quote 5
        • Vitalii DovganV
          Vitalii Dovgan @rdipardo
          last edited by

          @rdipardo
          Thank you, SCN_AUTOCCOMPLETED is exactly what was needed!

          Vitalii DovganV 1 Reply Last reply Reply Quote 4
          • Vitalii DovganV
            Vitalii Dovgan @Vitalii Dovgan
            last edited by

            By the way, I was able to make XBrackets to highlight a bracket or quote under a caret by applying a custom color to it!
            As it works with user-defined brackets and quotes such as /* */, <!-- --> and so on, this is something really unique.
            A very raw implementation is to add the following code to the beNotified function:

            case SCN_UPDATEUI:
                {
                    CSciMessager sciMsgr(m_nppMsgr.getCurrentScintillaWnd());
                    const Sci_Position pos = sciMsgr.getCurrentPos();
                    const auto pBrItem = m_BracketsLogic.FindBracketsByPos(pos, true);
                    if ( pBrItem != nullptr )
                    {
                        int nStyleId = 34; // Notepad++'s Brace highlight style
                        sciMsgr.SendSciMsg(SCI_STARTSTYLING, pBrItem->nLeftBrPos - pBrItem->pBrPair->leftBr.length(), 0);
                        sciMsgr.SendSciMsg(SCI_SETSTYLING, pBrItem->pBrPair->leftBr.length(), nStyleId);
                        sciMsgr.SendSciMsg(SCI_STARTSTYLING, pBrItem->nRightBrPos, 0);
                        sciMsgr.SendSciMsg(SCI_SETSTYLING, pBrItem->pBrPair->rightBr.length(), nStyleId);
                    }
                }
                break;
            

            I need to additionally take care of removing the given style from the brackets that were under the caret previously and are no more under the caret now. This is something to figure out.

            rdipardoR Vitalii DovganV 2 Replies Last reply Reply Quote 3
            • rdipardoR
              rdipardo @Vitalii Dovgan
              last edited by

              @Vitalii-Dovgan said in XBrackets 2.0 is coming!:

              I need to additionally take care of removing the given style from the brackets that were under the caret previously and are no more under the caret now. This is something to figure out.

              You could store the range in a class object and extract that if-statment body into a method. For example, HTML Tag captures the text of a start tag inside a SciTextRange and highlights it with SciTextRange::mark when it has no match.

              You could probably come up with something more efficient than what HTML Tag has. I simply translated the original developer’s Pascal class hierarchy into C++, which turned out to be a project in itself.

              1 Reply Last reply Reply Quote 2
              • Vitalii DovganV
                Vitalii Dovgan @Vitalii Dovgan
                last edited by

                The further experiments show that not everything is so good with SCN_UPDATEUI and SCI_SETSTYLING. Sometimes the caret stops blinking or just disappears. Sometimes one of the brackets is not repainted.
                I think I’ll try to play with something like PostMessage(NPPM_MSGTOPLUGIN, XBRM_HIGHLIGHTBR) in order to send SCI_SETSTYLING outside of the SCN_UPDATEUI handler.
                If that will not work, then I’ll try SCI_INDICSETSTYLE and SCI_INDICATORFILLRANGE as suggested by AI.
                Anyway, if you have a better idea, please let me know!

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