D12295: Supporting nested brackets for Kate autobrackets

Subramaniyam Raizada noreply at phabricator.kde.org
Tue Apr 17 18:55:39 UTC 2018


sraizada created this revision.
sraizada added a reviewer: KTextEditor.
sraizada added a project: KTextEditor.
Restricted Application added projects: Kate, Frameworks.
Restricted Application added a subscriber: Frameworks.
sraizada requested review of this revision.

REVISION SUMMARY
  With autobrackets enabled, Kate adds a closing bracket when opening a bracket. E.g. typing "if(" will result in "if(|)" appearing on screen, where | is the cursor position. Then, if the closing ) is manually typed in, it gets 'eaten' by the autogenerated one. So starting from "if(|)", typing in ")" will give "if()".
  
  However, this fails when any types of brackets are nested. Typing in "if({})" results in the innermost "}" getting eaten, but the closing ")" is not eaten, so the resulting text is "if({}))". This is for any combination of same/different brackets or quotes, as long as they are nested.
  
  The patch uses a QStack<QChar> (instead of just a QChar) to keep track of this and correctly eat closing brackets. That behaviour does work correctly (see attached video - only ({["' and their closing pairs are typed, the cursor is not manually moved). However, the stack should be reset when the cursor is moved away from the area where brackets are being inserted. I have not been able to figure out how to get that to work properly.
  
  On line 1357, upon entering an open bracket/quote, m_currentAutobraceRange seems to get set to (cursorposition-1, insertedTextPosition). Or at least that's what I think, my C++ knowledge doesn't really go that far, and I'm not at all familiar with the codebase or program structure. This needs to take into account nesting, instead of just setting the range to cursor position +- 1 unit for the bracket.
  
  Sven posted on the kwrite-devel mailing list https://mail.kde.org/pipermail/kwrite-devel/2018-April/000346.html that there are methods called to move the cursor left or right, as well as a cursorPositionChanged event. Hooking into this would allow for a basic solution, where moving the cursor at all would clear the autobracket history and result in future autobrackets not getting eaten. However, as long as Backspace and Enter events don't cause the stack to get cleared, that should work for cases when one is just typing a statement that contains multiple levels of parentheses in one shot without any errors. I'm a bit busy right now, but will try to see if I can implement this or a more comprehensive solution within the next few days. I would appreciate any tips on where to look in the code, again this is my first experience with C++ in a long, long time and I'm just putting code in random places until it works :)
  
  Note: in order to get nested autobraces to get eaten with this patch, line 3095 has to be commented out, otherwise the stack is cleared upon entering any type of closing bracket. Making m_currentAutobracketRange fit the actual range instead of one bracket will fix that.
  
  F5810863: recording.mp4 <https://phabricator.kde.org/F5810863>

REPOSITORY
  R39 KTextEditor

REVISION DETAIL
  https://phabricator.kde.org/D12295

AFFECTED FILES
  src/document/katedocument.cpp
  src/document/katedocument.h

To: sraizada, #ktexteditor
Cc: #ktexteditor, #frameworks, michaelh, kevinapavew, ngraham, bruns, demsking, head7, cullmann, kfunk, sars, dhaumann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-frameworks-devel/attachments/20180417/25ab26be/attachment.html>


More information about the Kde-frameworks-devel mailing list