Author Topic: hook "tab" key  (Read 37175 times)

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #15 on: March 27, 2010, 12:19:28 am »
The same like this:
Code
Index: src/include/cbstyledtextctrl.h
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+        {
+            if (style != wxSCI_C_STRING && style != wxSCI_C_CHARACTER)
+                return true;
+        }
+        case wxSCI_LEX_D:
+        {
+            if (style != wxSCI_D_STRING && style != wxSCI_D_CHARACTER)
+                return true;
+        }
+    }
+    return false;
+}
???
:lol: :lol: :lol:
Haha, you're right! Thank you. :P

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: hook "tab" key
« Reply #16 on: March 27, 2010, 12:36:04 am »
Why not this (and get rid of the if-statements) :

Code
Index: src/include/cbstyledtextctrl.h
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+        {
+            return !(style == wxSCI_C_STRING || style == wxSCI_C_CHARACTER);
+        }
+        case wxSCI_LEX_D:
+        {
+            return !(style == wxSCI_D_STRING || style == wxSCI_D_CHARACTER);
+        }
+    }
+    return false;
+}
?

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #17 on: March 27, 2010, 12:43:27 am »
Here, I do not quite understand: Why did not directly break it, because at the end of the function, there implementation: 'event.Skip ();' ?
Yes, but there are computations in between which can be avoided. Namely event.GetKeyCode(); and the switch computation.
Sorry, I still do not quite understand.
If change the code like this:
Code
void cbStyledTextCtrl::OnKeyUp(wxKeyEvent& event)
{
    if (!AllowTabSmartJump())
    {
        event.Skip();
        return;
    }
    const int keyCode = event.GetKeyCode();
    switch (keyCode)
    {
...
    }

    event.Skip();
}

We do not have access to ‘event.GetKeyCode()’ value and 'switch’ calculation, BUT, we need call 'AllowTabSmartJump' each time.
Here, the time occupied may be greater.

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #18 on: March 27, 2010, 12:45:36 am »
Why not this (and get rid of the if-statements) :

Code
Index: src/include/cbstyledtextctrl.h
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+        {
+            return !(style == wxSCI_C_STRING || style == wxSCI_C_CHARACTER);
+        }
+        case wxSCI_LEX_D:
+        {
+            return !(style == wxSCI_D_STRING || style == wxSCI_D_CHARACTER);
+        }
+    }
+    return false;
+}
?
cool! :D

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #19 on: March 27, 2010, 01:04:01 am »
Add an interface for external use :
Code
+        void SetTabSmartJump(bool enable = true) { m_tabSmartJump = enable; }
+

And according to everyone's tips for perfect 'AllowTabSmartJump' function.
Code
Index: src/include/cbstyledtextctrl.h
===================================================================
--- src/include/cbstyledtextctrl.h (revision 6196)
+++ src/include/cbstyledtextctrl.h (working copy)
@@ -19,14 +19,20 @@
         cbStyledTextCtrl(wxWindow* pParent, int id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0);
         virtual ~cbStyledTextCtrl();
         wxDateTime GetLastFocusTime() const {return m_lastFocusTime;}
+        void SetTabSmartJump(bool enable = true) { m_tabSmartJump = enable; }
+
     private:
         void OnContextMenu(wxContextMenuEvent& event);
         void OnKillFocus(wxFocusEvent& event);
         void OnGetFocus(wxFocusEvent& event);
         void OnGPM(wxMouseEvent& event);
+        void OnKeyDown(wxKeyEvent& event);
+        void OnKeyUp(wxKeyEvent& event);
+        bool AllowTabSmartJump();
 
         wxWindow* m_pParent;
         wxLongLong m_lastFocusTime;
+        bool m_tabSmartJump;
 
         DECLARE_EVENT_TABLE()
 };
Index: src/sdk/cbstyledtextctrl.cpp
===================================================================
--- src/sdk/cbstyledtextctrl.cpp (revision 6196)
+++ src/sdk/cbstyledtextctrl.cpp (working copy)
@@ -18,17 +18,23 @@
 #include "editorbase.h" // DisplayContextMenu
 #include "prep.h" // platform::gtk
 
+static const wxString s_leftBrace(_T("([{'\""));
+static const wxString s_rightBrace(_T(")]}'\""));
+
 BEGIN_EVENT_TABLE(cbStyledTextCtrl, wxScintilla)
     EVT_CONTEXT_MENU(cbStyledTextCtrl::OnContextMenu)
     EVT_KILL_FOCUS(cbStyledTextCtrl::OnKillFocus)
     EVT_MIDDLE_DOWN(cbStyledTextCtrl::OnGPM)
     EVT_SET_FOCUS(cbStyledTextCtrl::OnGetFocus)
+    EVT_KEY_DOWN(cbStyledTextCtrl::OnKeyDown)
+    EVT_KEY_UP(cbStyledTextCtrl::OnKeyUp)
 END_EVENT_TABLE()
 
 cbStyledTextCtrl::cbStyledTextCtrl(wxWindow* pParent, int id, const wxPoint& pos, const wxSize& size, long style)
     : wxScintilla(pParent, id, pos, size, style),
     m_pParent(pParent),
-    m_lastFocusTime(0L)
+    m_lastFocusTime(0L),
+    m_tabSmartJump(false)
 {
     //ctor
 }
@@ -103,3 +109,115 @@
         SetSelectionVoid(start, end);
     }
 } // end of OnGPM
+
+void cbStyledTextCtrl::OnKeyDown(wxKeyEvent& event)
+{
+    int keyCode = event.GetKeyCode();
+    switch (keyCode)
+    {
+        case WXK_TAB:
+        {
+            if (m_tabSmartJump)
+            {
+                if (s_rightBrace.Find(GetCharAt(GetCurrentPos())) == wxNOT_FOUND)
+                {
+                    m_tabSmartJump = false;
+                }
+                else
+                {
+                    CharRight();
+                    return;
+                }
+            }
+        }
+        break;
+
+        case WXK_BACK:
+        {
+            if (m_tabSmartJump)
+            {
+                const int pos = GetCurrentPos();
+                const int index = s_leftBrace.Find(GetCharAt(pos - 1));
+                if (index != wxNOT_FOUND && GetCharAt(pos) == s_rightBrace.GetChar(index))
+                {
+                    CharRight();
+                    DeleteBack();
+                }
+            }
+        }
+        break;
+
+        case WXK_ESCAPE:
+        {
+            if (m_tabSmartJump)
+            {
+                m_tabSmartJump = false;
+                CharLeft();
+                Tab();
+                return;
+            }
+        }
+        break;
+
+        case WXK_RETURN:
+        {
+            m_tabSmartJump = false;
+        }
+        break;
+    }
+
+    event.Skip();
+}
+
+void cbStyledTextCtrl::OnKeyUp(wxKeyEvent& event)
+{
+    const int keyCode = event.GetKeyCode();
+    switch (keyCode)
+    {
+        case _T('['):   // [ {
+        case _T('\''):  // ' "
+        case _T('9'):   // (
+        {
+            if (!AllowTabSmartJump()) break;
+
+            wxChar ch = keyCode;
+            if (event.ShiftDown())
+            {
+                if (keyCode == _T('\'')) ch = _T('"');
+                else if (keyCode == _T('9')) ch = _T('(');
+                else if (keyCode == _T('[')) ch = _T('{');
+            }
+
+            int index = s_leftBrace.Find(ch);
+            if (index != wxNOT_FOUND && GetCharAt(GetCurrentPos()) == s_rightBrace.GetChar(index))
+                m_tabSmartJump = true;
+            else if (keyCode == _T('\'')) // ' "
+                m_tabSmartJump = false;
+        }
+        break;
+
+        case _T(']'):   // ] }
+        case _T('0'):   // )
+        {
+            if (!AllowTabSmartJump()) break;
+            if (keyCode == _T('0') && !event.ShiftDown()) break;
+            m_tabSmartJump = false;
+        }
+        break;
+    }
+
+    event.Skip();
+}
+
+bool cbStyledTextCtrl::AllowTabSmartJump()
+{
+    int style = GetStyleAt(GetCurrentPos() - 2);
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+            return !(style == wxSCI_C_STRING || style == wxSCI_C_CHARACTER);
+        case wxSCI_LEX_D:
+            return !(style == wxSCI_D_STRING || style == wxSCI_D_CHARACTER);
+    }
+    return false;
+}

[attachment deleted by admin]
« Last Edit: March 27, 2010, 03:49:15 am by Loaden »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9693
Re: hook "tab" key
« Reply #20 on: March 28, 2010, 03:43:52 pm »
We do not have access to ‘event.GetKeyCode()’ value and 'switch’ calculation, BUT, we need call 'AllowTabSmartJump' each time.
Yes, but in the other case you do call AllowTabSmartJump() all the time, too - right?! You do it in every case statement.

So - I am not sure what causes more CPU time, really. However, I believe it makes the code easier to understand. But I guess this discussion is based on personal habits - so I accept your decision however it is.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #21 on: April 07, 2010, 11:16:45 am »
Auto delete ')', when press backspace.
Code
Index: src/plugins/codecompletion/codecompletion.cpp

===================================================================

--- src/plugins/codecompletion/codecompletion.cpp (revision 6202)

+++ src/plugins/codecompletion/codecompletion.cpp (working copy)

@@ -2014,6 +2014,7 @@

             control->SetTargetStart(start);
             control->SetTargetEnd(pos);
             control->ReplaceTarget(itemText+_T("()"));
+            control->SetTabSmartJump();
             pos = control->GetCurrentPos();
             control->GotoPos(pos + itemText.size()+2);
             if ((*it).second != 0)

test code:
Code
#include <stdio.h>

class A
{
public:
    void test()
    {
       
    }
};

int main(int argc, char* argv[])
{
    A a;
    a.test(|) // HERE, when backspace, it's can delete '(', also ')'.
    return 0;
}

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #22 on: April 20, 2010, 07:29:24 pm »
Rewrote this, it's support jump tip now.

EDIT: fix a bug. Welcome for test!

[attachment deleted by admin]
« Last Edit: April 21, 2010, 05:29:18 am by Loaden »

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: hook "tab" key
« Reply #23 on: August 20, 2010, 01:25:00 pm »
wake up call ;-)
What's the state on this patch, any issues still open, or could we try to apply it ?
Is the patch attached here the same as the one in the berlios tracker, or are they out of sync ?

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #24 on: August 20, 2010, 03:33:10 pm »
Hi, killerbot, The patch is applied in cc branch now.
And looks works well.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5514
Re: hook "tab" key
« Reply #25 on: August 20, 2010, 03:36:57 pm »
ok, then I won't apply it on trunk and wait till cc gets merged towards trunk :-)

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: hook "tab" key
« Reply #26 on: August 20, 2010, 03:47:48 pm »
ok, then I won't apply it on trunk and wait till cc gets merged towards trunk :-)
Yeah, and i hope the merge will be soon, we just need more test.