User forums > Using Code::Blocks
EditorTweaks: new option 'Convert Matching Braces' : little bug
dmoore:
@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.
oBFusCATed:
--- Quote from: dmoore on October 20, 2012, 04:22:18 pm ---...decide where they should go.
--- End quote ---
Base class functions?
dmoore:
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();
--- End code ---
[attachment deleted by admin]
dmoore:
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.
dmoore:
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.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version