Patch #3234 2011-11-19 02:04

alpha0010

Brace completion over selected text
Download
3234-Brace_completi.patch (10.7 KB)
Category
Application::FeatureAdd
Status
Accepted
Close date
2012-02-27 19:31
Assigned to
mortenmacfly
Index: src/sdk/editorconfigurationdlg.cpp
===================================================================
--- src/sdk/editorconfigurationdlg.cpp    (revision 7618)
+++ src/sdk/editorconfigurationdlg.cpp    (working copy)
@@ -118,6 +118,7 @@
     XRCCTRL(*this, "chkUseChangebar", wxCheckBox)->SetValue(m_EnableChangebar);
     XRCCTRL(*this, "chkShowIndentGuides", wxCheckBox)->SetValue(cfg->ReadBool(_T("/show_indent_guides"), false));
     XRCCTRL(*this, "chkBraceSmartIndent", wxCheckBox)->SetValue(cfg->ReadBool(_T("/brace_smart_indent"), true));
+    XRCCTRL(*this, "chkSelectionBraceCompletion", wxCheckBox)->SetValue(cfg->ReadBool(_T("/selection_brace_completion"), false));
     XRCCTRL(*this, "chkTabIndents", wxCheckBox)->SetValue(cfg->ReadBool(_T("/tab_indents"), true));
     XRCCTRL(*this, "chkBackspaceUnindents", wxCheckBox)->SetValue(cfg->ReadBool(_T("/backspace_unindents"), true));
     XRCCTRL(*this, "chkWordWrap", wxCheckBox)->SetValue(cfg->ReadBool(_T("/word_wrap"), false));
@@ -795,6 +796,7 @@
         cfg->Write(_T("/use_tab"),                             XRCCTRL(*this, "chkUseTab", wxCheckBox)->GetValue());
         cfg->Write(_T("/show_indent_guides"),                  XRCCTRL(*this, "chkShowIndentGuides", wxCheckBox)->GetValue());
         cfg->Write(_T("/brace_smart_indent"),                  XRCCTRL(*this, "chkBraceSmartIndent", wxCheckBox)->GetValue());
+        cfg->Write(_T("/selection_brace_completion"),          XRCCTRL(*this, "chkSelectionBraceCompletion", wxCheckBox)->GetValue());
         cfg->Write(_T("/tab_indents"),                         XRCCTRL(*this, "chkTabIndents", wxCheckBox)->GetValue());
         cfg->Write(_T("/backspace_unindents"),                 XRCCTRL(*this, "chkBackspaceUnindents", wxCheckBox)->GetValue());
         cfg->Write(_T("/word_wrap"),                           XRCCTRL(*this, "chkWordWrap", wxCheckBox)->GetValue());
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp    (revision 7618)
+++ src/sdk/cbeditor.cpp    (working copy)
@@ -2986,6 +2986,122 @@
     static int autoUnIndentValue = -1;
     static int autoUnIndentLine = -1;
 
+    bool SelectionBraceCompletion = Manager::Get()->GetConfigManager(_T("editor"))->ReadBool(_T("/selection_brace_completion"), false);
+    if ((SelectionBraceCompletion || control->IsBraceShortcutActive()) && !control->GetLastSelectedText().IsEmpty())
+    {
+        wxString selectedText = control->GetLastSelectedText();
+        switch (ch)
+        {
+            case _T('\''):
+            {
+                control->BeginUndoAction();
+                control->DeleteBack();
+                selectedText.Replace(wxT("\\'"), wxT("'"));
+                selectedText.Replace(wxT("'"), wxT("\\'"));
+                control->AddText(wxT("'") + selectedText + wxT("'"));
+                control->EndUndoAction();
+                return;
+            }
+            case _T('"'):
+            {
+                control->BeginUndoAction();
+                control->DeleteBack();
+                selectedText.Replace(wxT("\\\""), wxT("\""));
+                selectedText.Replace(wxT("\""), wxT("\\\""));
+                control->AddText(wxT("\"") + selectedText + wxT("\""));
+                control->SetSelectionVoid(pos - 1, pos + selectedText.Length() + 1);
+                int startLine = control->LineFromPosition(control->GetSelectionStart());
+                int endLine = control->LineFromPosition(control->GetSelectionEnd());
+                if(startLine != endLine)
+                {
+                    int selectionEnd = pos + selectedText.Length() + 1;
+                    for(int i = endLine; i > startLine; i--)
+                    {
+                        control->Home();
+                        for(int j = control->GetCurrentPos(); control->GetCharAt(j) == _T(' ') || control->GetCharAt(j) == _T('\t'); j++)
+                        {
+                            control->CharRight();
+                        }
+                        control->AddText(wxT("\""));
+                        control->SetEmptySelection(control->GetLineEndPosition(i - 1));
+                        control->AddText(wxT("\""));
+                        selectionEnd += control->GetIndent() + 2;
+                    }
+                    control->SetSelectionVoid(pos - 1, selectionEnd);
+                }
+                control->EndUndoAction();
+                return;
+            }
+            case _T('('):
+            case _T(')'):
+            {
+                control->BeginUndoAction();
+                control->DeleteBack();
+                control->InsertText(pos - 1, wxT("(") + selectedText + wxT(")"));
+                if(ch == _T(')'))
+                {
+                    control->SetEmptySelection(pos + selectedText.Length() + 1);
+                }
+                control->EndUndoAction();
+                return;
+            }
+            case
download for full patch...
alpha0010 2011-11-19 02:05
alpha0010 2011-11-21 20:58

Oops... major fail; I used Java syntax for multi-line strings.

Switched to C++ style multi-line strings.

alpha0010 2011-11-27 01:19

Update to work on trunk build.

(The settings option has been sort-of arbitrarily placed; if anyone has a better location for it, feel free to move it.)

mortenmacfly 2012-02-27 19:31

Applied in trunk. Thank you!