Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: killerbot on October 20, 2012, 09:28:15 am

Title: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: killerbot on October 20, 2012, 09:28:15 am
Code
int main()

select the opening '(' => press '[' ==>

Code
int main[]

select the opening '[' => press '(' ==>
Code
int main())

==> so we get 2 ')'
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 20, 2012, 02:06:38 pm
I think this is a conflict with SmartIndent  :'(  (Disable smart indent and try again).

So I can
1/ Merge into smart indent.
2/ Change smart indent logic slightly (don't do smart indent's bracing if there is a selection) but keep the feature separate.
3/ Move the smart indent brace completion out of smart indent
4/ Do nothing / revert.

Obviously 4 isn't my preferred. The unappealing thing about 1 is having to do that logic 5+ times over for all of the SmartIndent languages. Not sure where these features really belong. It's not clear to me that a plugin named "Smart Indent" should include brace completions at all, but that's not a huge deal.

I was planning to add additional features:

* autoconversions for <, > (for html/xml), ' and "
* if user has a selection, typing a brace char (or quote char) puts the brace around the selection instead of deleting the selection.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: zabzonk on October 20, 2012, 02:09:36 pm
What are the circumstances in which one would want to do this? I don't think I have ever wanted to convert () to [].
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 20, 2012, 02:54:00 pm
What are the circumstances in which one would want to do this? I don't think I have ever wanted to convert () to [].

Converting the braces is mostly useful for python (in C/C++, probably much less commonly needed). Removing the matching brace is probably generally useful as would be enclosing a selection in braces instead of just deleting it. But fair point, I could just make the changes to the smart indent python code.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 20, 2012, 04:01:57 pm
* if user has a selection, typing a brace char (or quote char) puts the brace around the selection instead of deleting the selection.
This feature already exists.  Do you have Selection brace completion enabled?  (Although, the recent SmartIndent refactor has restricted it to C/C++ only; I am exploring how to make it general purpose again.)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 20, 2012, 04:22:18 pm
@alpha: I do have that option enabled. I was working in python when I decided to make the changes so that's probably why I missed it. We should coordinate on these changes and decide where they should go.

Which reminds me, the value of the brace conversion is it means less typing when porting code from another language. In my case it was Matlab to Python, but it could be useful in lots of other cases.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: oBFusCATed on October 20, 2012, 05:56:12 pm
...decide where they should go.
Base class functions?
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 21, 2012, 01:29:34 am
Ok, I'm trying to reimplement this in the smart indent plugin. For now, just in the Cpp handler, which I will eventually move parts of to the base class. That patch is below and attached.

I am having a couple problems:
1. To do it in SmartIndent I need to handle wxEVT_SCI_KEY. But for some reason the plugin doesn't seem to receive it, despite receiving wsEVT_SCI_CHARADDED? I'm probably just doing something dumb, but any pointers appreciated
2. Many of the methods have been defined const. I would prefer to change them not to be const so I can add a member that holds the bracematch position (I could imagine all of the methods might need to be non-const for some languages). I have worked around this with a global variable for now. Is there a strong reason to make these functions const other than trying to be strict?

Code
Index: src/plugins/contrib/SmartIndent/CppSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/CppSmartIndent.cpp (revision 8464)
+++ src/plugins/contrib/SmartIndent/CppSmartIndent.cpp (working copy)
@@ -19,8 +19,11 @@
 namespace
 {
     PluginRegistrant<CppSmartIndent> reg(_T("CppSmartIndent"));
-}
-void CppSmartIndent::OnEditorHook(cbEditor* ed, wxScintillaEvent& event) const
+}
+
+static int m_bracePos=-1;
+
+void CppSmartIndent::OnEditorHook(cbEditor* ed, wxScintillaEvent& event)const
 {
 
     // check if smart indent is enabled
@@ -32,18 +35,33 @@
 
     if ( !SmartIndentEnabled() )
         return;
-
-    wxEventType type = event.GetEventType();
-    if ( type != wxEVT_SCI_CHARADDED )
-        return;
-
+
     cbStyledTextCtrl* stc = ed->GetControl();
     if (!stc)
         return;
 
     wxString langname = Manager::Get()->GetEditorManager()->GetColourSet()->GetLanguageName(ed->GetLanguage());
     if ( langname != _T("C/C++") && langname != _T("D") && langname != _T("Java") ) return;
+
+    wxEventType type = event.GetEventType();
+    if ( type == wxEVT_SCI_KEY )
+    {
+        int p = stc->GetCurrentPos();
+        int a = stc->GetAnchor();
+        int m=p;
+        if(a>=0 && a<p)
+            m=a;
+        m_bracePos = stc->BraceMatch(m);
+        Manager::Get()->GetLogManager()->Log(wxString::Format(_T("Key Event Brace pos %i"),m_bracePos));
+        event.Skip(true);
+        return;
+    }
 
+    if ( type != wxEVT_SCI_CHARADDED )
+        return;
+
+
+
     ed->AutoIndentDone(); // we are responsible.
 
     const int pos = stc->GetCurrentPos();
@@ -440,13 +458,56 @@
     }
     return false;
 }
+
 void CppSmartIndent::DoSelectionBraceCompletion(cbStyledTextCtrl* control, const wxChar &ch)const
 {
     if (!control->GetLastSelectedText().IsEmpty())
     {
-
         const int pos = control->GetCurrentPos();
-        wxString selectedText = control->GetLastSelectedText();
+        wxString selectedText = control->GetLastSelectedText();
+        Manager::Get()->GetLogManager()->Log(_T("Last Selected Text ")+selectedText);
+        if(selectedText == _T("(") ||
+           selectedText == _T(")") ||
+           selectedText == _T("[") ||
+           selectedText == _T("]") ||
+           selectedText == _T("{") ||
+           selectedText == _T("}")
+           )
+        {
+            int p = pos;
+            wxString opch;
+            switch (ch)
+            {
+                case _T('('):
+                    opch = _T(")");
+                    break;
+                case _T(')'):
+                    opch = _T("(");
+                    break;
+                case _T('['):
+                    opch = _T("]");
+                    break;
+                case _T(']'):
+                    opch = _T("[");
+                    break;
+                case _T('{'):
+                    opch = _T("}");
+                    break;
+                case _T('}'):
+                    opch = _T("{");
+                    break;
+            }
+            //int m = control->BraceMatch(p-1);
+            Manager::Get()->GetLogManager()->Log(wxString::Format(_T("Brace pos %i"),m_bracePos));
+            if (m_bracePos == wxSCI_INVALID_POSITION)
+                return;
+            control->BeginUndoAction();
+            control->InsertText(m_bracePos, opch);
+            control->DeleteRange(m_bracePos+1, 1);
+            control->SetCurrentPos(p);
+            control->EndUndoAction();
+            return;
+        }
         switch (ch)
         {
             case _T('\''):
@@ -547,6 +608,7 @@
         }
     } // SelectionBraceCompletion
 }
+
 void CppSmartIndent::DoBraceCompletion(cbStyledTextCtrl* control, const wxChar& ch)const
 {
     int pos = control->GetCurrentPos();

[attachment deleted by admin]
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 21, 2012, 08:05:25 am
doh, RTFM. wxEVT_SCI_KEY only triggers for keystrokes that aren't processed by scintilla. So I have to do this the same way I did it in EditorTweaks and handle wxEVT_CHAR or wxEVT_KEY_DOWN in order to get matching brace positions before the brace is replaced. I think I can make this work, but I'm even less sure it belongs in SmartIndent.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 21, 2012, 07:55:00 pm
I did what I probably should have just done to start with and fixed the implementation in EditorTweaks so that it doesn't conflict with SmartIndent. This version actually has better undo behavior because a single undo will revert both brace changes. Unless someone feels strongly that this feature shouldn't be in editor tweaks, it can stay there for now.

@Alpha: would still like to have the brace selection behavior for other languages beside C/C++. As obfuscated suggests, just move that method to the base class and have more (all?) of the smart indent implementations call it would be one way to do it.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 23, 2012, 11:16:58 pm
@Alpha: would still like to have the brace selection behavior for other languages beside C/C++. As obfuscated suggests, just move that method to the base class and have more (all?) of the smart indent implementations call it would be one way to do it.
Something like that; what I am planning is to have more generic base function that is called as a fall-back, and smart indent plugins would then (have the option to) specialize the behavior (for example, when used on a double quote ("), it currently utilizes C/C++ escapes, which may not be applicable to all languages).

If you want to work on this, go ahead.  Otherwise, I should have a patch available in a few days.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 23, 2012, 11:50:44 pm
If you want to work on this, go ahead.  Otherwise, I should have a patch available in a few days.

Quite content for you to work on this :) Happy to test of course.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 26, 2012, 11:02:21 pm
Here is a candidate patch.
Code
Index: src/plugins/contrib/SmartIndent/PythonSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/PythonSmartIndent.cpp (revision 8478)
+++ src/plugins/contrib/SmartIndent/PythonSmartIndent.cpp (working copy)
@@ -69,4 +69,7 @@
             stc->EndUndoAction();
         }
     }
+
+    if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
+        ed->DoSelectionBraceCompletion(stc, ch);
 }
Index: src/plugins/contrib/SmartIndent/LuaSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/LuaSmartIndent.cpp (revision 8478)
+++ src/plugins/contrib/SmartIndent/LuaSmartIndent.cpp (working copy)
@@ -67,6 +67,9 @@
             stc->EndUndoAction();
         }
     }
+
+    if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
+        ed->DoSelectionBraceCompletion(stc, ch);
 }
 
 bool LuaSmartIndent::BraceIndent(cbStyledTextCtrl *stc, wxString &indent)const
Index: src/plugins/contrib/SmartIndent/XMLSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/XMLSmartIndent.cpp (revision 8478)
+++ src/plugins/contrib/SmartIndent/XMLSmartIndent.cpp (working copy)
@@ -47,16 +47,28 @@
 
     ed->AutoIndentDone(); // we are responsible
 
-    const int pos = stc->GetCurrentPos();
+    int pos = stc->GetCurrentPos();
     int currLine = stc->LineFromPosition(pos);
 
     const wxChar ch = event.GetKey();
     wxRegEx reTag(wxT("***:<[ \t]*?(|/)[ \t]*?([a-zA-Z][a-zA-Z0-9_-]*).*?(|/)[ \t]*?>"));
 
+    bool complQuote = true;
+    if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
+    {
+        ed->DoSelectionBraceCompletion(stc, ch);
+        if (pos != stc->GetCurrentPos())
+        {
+            complQuote = false;
+            pos = stc->GetCurrentPos();
+            currLine = stc->LineFromPosition(pos);
+        }
+    }
+
     if (BraceCompletionEnabled())
     {
         // finish tag
-        if (ch == wxT('>'))
+        if ( ch == wxT('>') && !stc->IsString(stc->GetStyleAt(pos)) )
         {
             wxString tag;
             for (int i = pos - 2; i > 0; --i)
@@ -72,7 +84,7 @@
                 stc->InsertText(pos, wxT("</") + reTag.GetMatch(tag, 2) + wxT(">"));
         }
         // close string
-        else if (ch == wxT('"') || ch == wxT('\''))
+        else if (complQuote && (ch == wxT('"') || ch == wxT('\'')))
         {
             if (stc->GetCharAt(pos) == ch)
             {
@@ -113,14 +125,14 @@
         }
     }
     // indent
-    if ( (ch == wxT('\n')) || ( (stc->GetEOLMode() == wxSCI_EOL_CR) && (ch == wxT('\r')) ) )
+    if (   AutoIndentEnabled()
+        && ( (ch == wxT('\n')) || ((stc->GetEOLMode() == wxSCI_EOL_CR) && (ch == wxT('\r'))) ) )
     {
-        if (AutoIndentEnabled())
+        wxString indent = ed->GetLineIndentString(currLine - 1);
+        stc->BeginUndoAction();
+        if (SmartIndentEnabled()) // smart indent
         {
-            wxString indent = ed->GetLineIndentString(currLine - 1);
             int idx = stc->GetLine(currLine - 1).Find(wxT('>'), true);
-
-            stc->BeginUndoAction();
             if (idx != wxNOT_FOUND)
             {
                 wxString tag;
@@ -167,10 +179,10 @@
                     }
                 }
             }
-            stc->InsertText(pos, indent);
-            stc->GotoPos(pos + indent.Length());
-            stc->ChooseCaretX();
-            stc->EndUndoAction();
         }
+        stc->InsertText(pos, indent);
+        stc->GotoPos(pos + indent.Length());
+        stc->ChooseCaretX();
+        stc->EndUndoAction();
     }
 }
Index: src/plugins/contrib/SmartIndent/PascalSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/PascalSmartIndent.cpp (revision 8478)
+++ src/plugins/contrib/SmartIndent/PascalSmartIndent.cpp (working copy)
@@ -44,6 +44,9 @@
         DoIndent(ed, langname);   // indent because \n added
     else if ( ch != wxT(' ') )
         DoUnIndent(ed, langname); // un-indent because not a newline added
+
+    if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
+        ed->DoSelectionBraceCompletion(stc, ch);
 }
 
 void PascalSmartIndent::DoIndent(cbEditor* ed, const wxString& WXUNUSED(langname)) const
Index: src/plugins/contrib/SmartIndent/HDLSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/HDLSmartIndent.cpp (revision 8478)
+++ src/plugins/contrib/SmartIndent/HDLSmartIndent.cpp (working copy)
@@ -49,6 +49,9 @@
         DoIndent(ed, langname);   // indent because \n added
     else if ( ch != wxT(' ') )
         DoUnIndent(ed, langname); // un-indent because not a newline added
+
+    if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
+        ed->DoSelectionBraceCompletion(stc, ch);
 }
 
 int HDLSmartIndent::FindBlockStartVHDL(cbEditor* ed, int position, wxString block) const
Index: src/plugins/contrib/SmartIndent/FortranSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/FortranSmartIndent.cpp (revision 8478)
+++ src/plugins/contrib/SmartIndent/FortranSmartIndent.cpp (working copy)
@@ -103,4 +103,7 @@
 
         stc->EndUndoAction();
     }
+
+    if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
+        ed->DoSelectionBraceCompletion(stc, ch);
 }
Index: src/include/cbeditor.h
===================================================================
--- src/include/cbeditor.h (revision 8478)
+++ src/include/cbeditor.h (working copy)
@@ -301,6 +301,7 @@
         static void ApplyStyles(cbStyledTextCtrl* control);
 
         void AutoIndentDone();
+        void DoSelectionBraceCompletion(cbStyledTextCtrl* control, const wxChar& ch) const;
     private:
         cbEditor(const cbEditor& /*rhs*/); // prevent copy construction
 
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp (revision 8478)
+++ src/sdk/cbeditor.cpp (working copy)
@@ -3095,12 +3095,33 @@
                 control->EndUndoAction();
             }
         }
+        if(   Manager::Get()->GetConfigManager(_T("editor"))->ReadBool(_T("/brace_completion"), true)
+           || control->IsBraceShortcutActive())
+            DoSelectionBraceCompletion(control, ch);
     }
 }
 void cbEditor::AutoIndentDone()
 {
     m_autoIndentDone = true;
 }
+void cbEditor::DoSelectionBraceCompletion(cbStyledTextCtrl* control, const wxChar& ch) const
+{
+    if (control->GetLastSelectedText().IsEmpty())
+        return;
+    const wxString braces(wxT("([{<'\")]}>'\""));
+    const int braceAIdx = braces.Find(ch, true); // from end (so caret ends after quotes)
+    if (braceAIdx == wxNOT_FOUND)
+        return;
+    const int braceBIdx = (braceAIdx + (braces.Length() / 2)) % braces.Length();
+    control->BeginUndoAction();
+    control->DeleteBack();
+    if (braceAIdx < braceBIdx)
+        control->InsertText(control->GetCurrentPos(),
+                            braces[braceAIdx] + control->GetLastSelectedText() + braces[braceBIdx]);
+    else
+        control->AddText(braces[braceBIdx] + control->GetLastSelectedText() + braces[braceAIdx]);
+    control->EndUndoAction();
+}
 
 void cbEditor::OnEditorDwellStart(wxScintillaEvent& event)
 {
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on October 27, 2012, 05:46:38 pm
Here is a candidate patch.
dmoore: Do you take action?
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 27, 2012, 10:19:10 pm
Here is a candidate patch.
dmoore: Do you take action?

I haven't but I can. Might take a few days so if you get to it sooner be my guest. Anyone else tested it?
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on October 27, 2012, 10:34:45 pm
Anyone else tested it?
I've applied it in my local copy but didn't test.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 12:50:22 am
Here is a candidate patch.

Having troubles applying this on linux. There are different line endings in at least one of the target files (I thought we had done something to fix this in our SVN). Do you mind attaching a patch file Alpha?
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 01:13:38 am
Also, is there a good reason to put the brace completion stuff in cbEditor instead of the plugin base? I thought the point of making smart indent a set of plugins was to reduce the amount of cruft in the editor code.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 29, 2012, 01:28:06 am
Attached.

I thought we had done something to fix this in our SVN.
Most files are set to match system EOL style, however, it appears as if the files added by Smart Indent are CR LF only.

Also, is there a good reason to put the brace completion stuff in cbEditor instead of the plugin base?
I put it there so the editor has access to use it as fall-back if no plugin supports the current lexer.  The Smart Indent plugins for languages that I am less familiar with just call this more-generic method, whereas the C++ plugin has its own specialized implementation (that I previously wrote - although, it occasionally has some issues, so I may have to look back at it again).
I did this under the same idea that cbEditor contains a minimal auto-indent (just copy the previous line's indentation).  Not sure if this was correct thinking...
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 02:26:45 am
I put it there so the editor has access to use it as fall-back if no plugin supports the current lexer. 

That makes some sense.

Thx for the patch.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on October 29, 2012, 10:13:53 am
Attached.
Did that patch change anything in comparison to the previous one? After applying this I see no differences to what I already had with the previous patch...?! ???
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 01:30:43 pm
Attached.
Did that patch change anything in comparison to the previous one? After applying this I see no differences to what I already had with the previous patch...?! ???

I think it is the same, I just wanted it this way to hopefully make it easier to apply.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 02:56:20 pm
This seems to work as I would expect on different types of files. I don't love the undo behavior, but I guess it makes sense that undo gives you what you would expect if selection brace matching was turned off.

Small style thing: I thought we added at least one line of empty whitespace between class method implementations. (I notice that whitespace is virtually non-existent in most of the smartindent cpp files.)

@morten: can you check/change the newline svn properties of those files? (And any other recently added files)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 03:14:45 pm
Also @Morten, if this is ok with you one of us should apply. (I haven't tested on windows, but nothing seems OS specific in this one)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on October 29, 2012, 03:57:07 pm
Also @Morten, if this is ok with you one of us should apply. (I haven't tested on windows, but nothing seems OS specific in this one)
Feel free to do so - I've tested it under Windows meanwhile. It looks OK.

Concerning the SVN props: I'll have a look...
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 29, 2012, 09:21:09 pm
Did that patch change anything in comparison to the previous one? After applying this I see no differences to what I already had with the previous patch...?! ???
The line endings are different... oh, LF and CR LF look exactly the same in your browser? ;)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 10:27:11 pm
@Alpha: For python, I noticed that smart indent does not provide brace completions for non-selected text. If we are doing it for C/C++, why not python (and other languages) too? I guess this would be a separate patch, and, if so, I am happy to commit the last one.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 29, 2012, 11:28:05 pm
Brace completion and selection brace completion are (in my opinion) completely separate features, so yes, different patch.

Fall-back for brace completion would have to be very conservative, because languages differ so greatly.  Compare editing C/C++ with editing XML/HTML (the only places brace completion is currently implemented); they act quite different.

If I am able, I will see what I can come up with in the next few days.

If we are doing it for C/C++, why not python (and other languages) too?
I guess simply no one got there yet.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 29, 2012, 11:56:31 pm
That's what I thought. Will commit your patch soon.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on October 30, 2012, 12:12:40 am
Committed in rev 8489. Apologies for spacing and forgetting to give you credit in the log message, Alpha.  :-[ Hopefully we give you commit access soon!
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 30, 2012, 10:11:55 pm
Apologies for spacing and forgetting to give you credit in the log message, Alpha.
No worries.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on October 31, 2012, 11:19:54 pm
Fall-back for brace completion would have to be very conservative, because languages differ so greatly.
Here is a fall-back (plus a small fix for the previous patch - it had read from the wrong environment variable).  The language specific implementations will take some more time (and probably will require help from people who use those languages more frequently).
Code
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp (revision 8496)
+++ src/sdk/cbeditor.cpp (working copy)
@@ -3076,9 +3076,9 @@
     {
         const wxChar ch = event.GetKey();
         cbStyledTextCtrl* control = GetControl();
+        const int pos = control->GetCurrentPos();
         if ( (ch == _T('\n')) || ( (control->GetEOLMode() == wxSCI_EOL_CR) && (ch == _T('\r')) ) )
         {
-            const int pos = control->GetCurrentPos();
             const int currLine = control->LineFromPosition(pos);
             const bool autoIndent = Manager::Get()->GetConfigManager(_T("editor"))->ReadBool(_T("/auto_indent"), true);
             if (autoIndent && currLine > 0)
@@ -3093,9 +3093,27 @@
                 control->EndUndoAction();
             }
         }
-        if(   Manager::Get()->GetConfigManager(_T("editor"))->ReadBool(_T("/brace_completion"), true)
+        if(   Manager::Get()->GetConfigManager(wxT("editor"))->ReadBool(wxT("/selection_brace_completion"), false)
            || control->IsBraceShortcutActive())
             DoSelectionBraceCompletion(control, ch);
+        // brace completion
+        const wxString braces(wxT("([{)]}"));
+        const int braceIdx = braces.Find(ch);
+        if (   braceIdx != wxNOT_FOUND && pos == control->GetCurrentPos() // pos != curPos if selection brace completion succeeded
+            && Manager::Get()->GetConfigManager(wxT("editor"))->ReadBool(wxT("/brace_completion"), true))
+        {
+            if (control->GetCharAt(pos) == ch)
+            {
+                control->DeleteBack();
+                control->CharRight();
+            }
+            else if (braceIdx < (braces.Length() / 2))
+            {
+                const int closeIdx = braceIdx + (braces.Length() / 2);
+                if (control->GetCharAt(pos) != braces[closeIdx] || control->BraceMatch(pos) != wxNOT_FOUND)
+                    control->InsertText(pos, braces[closeIdx]);
+            }
+        }
     }
 }
 
I did not make this a separate function because most languages will have very different requirements, so it would therefore remain relatively unused; do you agree?
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 01, 2012, 05:28:44 pm
I did not make this a separate function because most languages will have very different requirements, so it would therefore remain relatively unused; do you agree?

Busy ATM but will take a look in the next few days. In terms of different languages, I would think it's rare to find a language that doesn't expect brace chars to be closed. So that seems like something that should always happen when this feature is turned on, which it appears is what your patch does. I would say same with quote chars, but scintilla doesn't offer a nice bracematch function for quotes so would require more work.

I guess the tricky thing is that you probably don't want auto brace completion inside a string or comment, and different lexers have different style codes for their strings, but this would be hard to do right. (Doesn't look like your patch deals with this?)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: danselmi on November 01, 2012, 08:22:05 pm
I guess the tricky thing is that you probably don't want auto brace completion inside a string or comment, and different lexers have different style codes for their strings, but this would be hard to do right. (Doesn't look like your patch deals with this?)
Code
cbStyledTextCtrl::IsComment()
and
Code
cbStyledTextCtrl::IsString()
should help
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 02, 2012, 01:00:14 am
Thanks.

Attached is another patch.
It handles quotes (with guess work that is often, but not always, close enough) and attempts to act more intelligently during brace completion.  I have also now separated it into a function, and called the function from the SmartIndent plugins that have not yet implemented their own brace completion.

I still want to write some specialized functions; the fall-back function attempts to be generalized, so it misses many language specifics (for example triple quoted python strings).
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 02, 2012, 09:50:25 pm
Here is python triple quote completion (it requires the patch from my last post be applied to everything but PythonSmartIndent.cpp).
Code
Index: src/plugins/contrib/SmartIndent/PythonSmartIndent.cpp
===================================================================
--- src/plugins/contrib/SmartIndent/PythonSmartIndent.cpp (revision 8500)
+++ src/plugins/contrib/SmartIndent/PythonSmartIndent.cpp (working copy)
@@ -71,6 +71,18 @@
         }
     }
 
+    bool braceCompleted = false;
     if ( SelectionBraceCompletionEnabled() || stc->IsBraceShortcutActive() )
-        ed->DoSelectionBraceCompletion(stc, ch);
+        braceCompleted = ed->DoSelectionBraceCompletion(stc, ch);
+    if (!braceCompleted && BraceCompletionEnabled())
+    {
+        ed->DoBraceCompletion(stc, ch);
+        if (  !(stc->IsComment(stc->GetStyleAt(pos)) || stc->IsComment(stc->GetStyleAt(pos - 2)))
+            && (ch == wxT('"') || ch == wxT('\'')) )
+        {
+            const wxString tripleQuote(3, ch);
+            if (stc->GetTextRange(pos - 3, pos) == tripleQuote && !stc->IsString(stc->GetStyleAt(pos - 4)))
+                stc->InsertText(pos, tripleQuote);
+        }
+    }
 }

I also wanted to add term -> end term "brace" completion (similar to how C++ matches PP directives, and XML matches tags), but I realized that I do not use/understand those languages enough to be able to create something that functions as expected.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 02, 2012, 09:59:56 pm
I tried your last patch last night and it seemed to be working well on python files. So should I take this new patch, test a little more then commit over the weekend?

One thing I wondered about was whether you it is better add the sdk functions to cbEditor (as you do now) or to cbStyledTextCtrl. The advantage of the latter is that you don't need to pass around the redundant cbStyleTextCtrl pointer as the first arg.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 03, 2012, 01:51:27 am
Assuming it passes your testing, I believe the patch is ready for commit.

Attached I have refactored to cbStyledTextCtrl (plus a slight improvement to brace completion).
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 03, 2012, 07:21:31 pm
Tested and works well enough for me.

Unless someone has objections to putting the Do*BraceCompletion methods in cbStyleTextCtrl or finds some other major problem, I will commit within the next 24 hours.  (The alternative would be to put the methods in cbEditor but I think this is stuff that doesn't need to be exposed in the SDK to anything other than smart indent plugins, so hiding them away makes some sense.)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 03, 2012, 07:36:24 pm
aaargh! The commit would be a big mess of line ending changes on Linux because I had to dos2unix before applying the patch. (Some of the line ending changes might be unavoidable due to the changes to xml lexer files)

@Morten: please do the propset magic if you have time. (I would do it myself, but only have linux access ATM which will commit a huge batch of unwanted line ending changes if I do the propset)

EDIT: In case I wasn't clear, I am going to hold off on the commit until I resolve the line endings.


Never mind. I figured this out and made the eol propset changes.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on November 04, 2012, 07:48:24 pm
Tested and works well enough for me.
It doesn't compile under wx2.9.x. this line:
if (GetCharAt(nextPos) != braces[closeIdx] || BraceMatch(nextPos) != wxNOT_FOUND)

...raises:
cbstyledtextctrl.cpp:356:54: error: ambiguous overload for 'operator!=' in '((cbStyledTextCtrl*)this)->cbStyledTextCtrl::<anonymous>.wxScintilla::GetCharAt(nextPos) != braces.wxString::operator[](((int)closeIdx))'


This should be fixed before committing...
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 05, 2012, 01:32:44 am
I do not currently have wx2.9 available for testing; does a cast fix this?

if ((wxChar)GetCharAt(nextPos) != braces[closeIdx] || BraceMatch(nextPos) != wxNOT_FOUND)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on November 05, 2012, 08:41:53 am
if ((wxChar)GetCharAt(nextPos) != braces[closeIdx] || BraceMatch(nextPos) != wxNOT_FOUND)
Yes. Not nice and I don't know if it would work for all Unicode characters... but yes. :-)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on November 05, 2012, 01:36:18 pm
Attached I have re-factored to cbStyledTextCtrl (plus a slight improvement to brace completion).
Oh dear... I'm afraid I just screwed the compatibility with the patch after the last commits of mine (renaming of SmartIndent). Maybe you can provide an updated version? Sorry - I didn't have that in mind.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 05, 2012, 05:38:10 pm
Ok, I'm leaving this one to you guys. (Let me know if you want me to look at anything specific.)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on November 05, 2012, 08:03:59 pm
Ok, I'm leaving this one to you guys.
I am not really looking into it. Please continue testing, if possible. I was just the one breaking it. :P
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 06, 2012, 12:55:36 am
Oh dear... I'm afraid I just screwed the compatibility with the patch after the last commits of mine (renaming of SmartIndent). Maybe you can provide an updated version? Sorry - I didn't have that in mind.
Not a problem; update attached.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 06, 2012, 01:20:07 pm
OK. If Morten doesn't break anything in between,  ;D I will test and commit tonight.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: MortenMacFly on November 06, 2012, 03:37:57 pm
OK. If Morten doesn't break anything in between,  ;D I will test and commit tonight.
I'll keep my fingers away - I promise...
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 07, 2012, 03:53:05 am
Committed rev 8514. FYI - I had to clean out my build settings before this one worked properly, but this was probably due to the other SmartIndent changes that Morten made.
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 07, 2012, 05:06:51 am
In rev 8515 I fixed a bug that prevented Convert Matching Braces from being saved. I also tidied up the naming of the config settings for EditorTweaks. Unfortunately, this will means users will need to redo any previously set aligner settings (sorry!)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 09, 2012, 04:46:41 am
Committed rev 8514.
Uh-oh... looks like I killed three of the lexers:
Code
Index: src/sdk/resources/lexers/lexer_batch.xml
===================================================================
--- src/sdk/resources/lexers/lexer_batch.xml    (revision 8529)
+++ src/sdk/resources/lexers/lexer_batch.xml    (working copy)
@@ -78,7 +78,6 @@
                     BoxCommentMid=""
                     BoxCommentEnd=""
                     CaseSensitive="0"
-                    CaseSensitive="1"
                     LexerCommentStyles="1"
                     LexerCharacterStyles=""
                     LexerStringStyles=""
Index: src/sdk/resources/lexers/lexer_css.xml
===================================================================
--- src/sdk/resources/lexers/lexer_css.xml      (revision 8529)
+++ src/sdk/resources/lexers/lexer_css.xml      (working copy)
@@ -110,7 +110,6 @@
                     BoxCommentMid=" * "
                     BoxCommentEnd=" */"
                     CaseSensitive="0"
-                    CaseSensitive="1"
                     LexerCommentStyles="9"
                     LexerCharacterStyles=""
                     LexerStringStyles="13,14"
Index: src/sdk/resources/lexers/lexer_cmake.xml
===================================================================
--- src/sdk/resources/lexers/lexer_cmake.xml    (revision 8529)
+++ src/sdk/resources/lexers/lexer_cmake.xml    (working copy)
@@ -542,10 +542,7 @@
                     CaseSensitive="0"
                     LexerCommentStyles="1"
                     LexerStringStyles="2,3,4"
-                    CaseSensitive="1"
-                    LexerCommentStyles="1"
                     LexerCharacterStyles=""
-                    LexerStringStyles="2,3,4"
                     LexerPreprocessorStyles=""/>
         </Lexer>
 </CodeBlocks_lexer_properties>
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 09, 2012, 12:59:08 pm
Is it just these 3?
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 09, 2012, 10:00:52 pm
From what I can tell, yes.  In the application log for revisions 8514 - 8529, the "Loading lexer_*" failed on these three.
(It seems I had not been careful enough in modifying all the lexers so quickly to ensure valid XML files :-\.)
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: dmoore on November 09, 2012, 10:41:56 pm
Looks like Morten got all these (http://svn.berlios.de/wsvn/codeblocks?manualorder=1&op=comp&compare%5B0%5D=%2Ftrunk%2F&compare_rev%5B0%5D=8529&compare%5B1%5D=%2Ftrunk%2F&compare_rev%5B1%5D=8530&comparesubmit=Compare+Paths)?
Title: Re: EditorTweaks: new option 'Convert Matching Braces' : little bug
Post by: Alpha on November 12, 2012, 02:45:22 am
Yes.  (Sorry, forgot to respond.)