User forums > Using Code::Blocks

EditorTweaks: new option 'Convert Matching Braces' : little bug

<< < (7/12) > >>

Alpha:

--- Quote from: dmoore on October 30, 2012, 12:12:40 am ---Apologies for spacing and forgetting to give you credit in the log message, Alpha.

--- End quote ---
No worries.

Alpha:

--- Quote from: Alpha on October 29, 2012, 11:28:05 pm ---Fall-back for brace completion would have to be very conservative, because languages differ so greatly.

--- End quote ---
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]);
+            }
+        }
     }
 }
 

--- End code ---
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?

dmoore:

--- Quote from: Alpha on October 31, 2012, 11:19:54 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?

--- End quote ---

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?)

danselmi:

--- Quote from: dmoore on November 01, 2012, 05:28:44 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?)

--- End quote ---

--- Code: ---cbStyledTextCtrl::IsComment()
--- End code ---
and
--- Code: ---cbStyledTextCtrl::IsString()
--- End code ---
should help

Alpha:
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).

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version