XBrackets 2.0 is coming!
-
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.txtThe XBrackets Lite configuration file is here:
https://github.com/d0vgan/npp-XBracketsLite/blob/feature/GoToNearestBr/XBrackets/XBrackets_Config.jsonThe 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. - Go To Matching Bracket
-
@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.
-
@rdipardo
Thank you, SCN_AUTOCCOMPLETED is exactly what was needed! -
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 thebeNotified
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.
-
@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 aSciTextRange
and highlights it withSciTextRange::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.