Author Topic: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'  (Read 21696 times)

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Code
Index: src/plugins/codecompletion/codecompletion.cpp

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

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

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

@@ -2106,6 +2106,29 @@

             }
         }
     }
+    
+    if (event.GetEventType() == wxEVT_SCI_CHARADDED)
+    {
+        wxChar ch = event.GetKey();
+        if (ch == _T(':'))
+        {
+            if (control->AutoCompActive()) control->AutoCompCancel();
+            int origPos = control->GetCurrentPos() - 1;
+            int start = control->WordStartPosition(origPos, true);
+            int end = control->WordEndPosition(origPos, true);
+            const wxString text = control->GetTextRange(start, end);
+            if (text == _T("public") || text == _T("protected") || text == _T("private"))
+            {
+                control->CharLeft();
+                control->WordLeft();
+                control->BackTab();
+                control->WordRight();
+                control->CharRight();
+                control->NewLine();
+                control->Tab();
+            }
+        }
+    }
 
     Parser* parser = m_NativeParser.GetParserPtr();
     if (   parser && parser->Options().whileTyping

OLD:
Code
class A
{
    public:
    void test()
    {
        int i = 0;
    }
    
    private:
    int good()
    {
        
    }
    
    protected:
    
};

AFTER PATCH:
Code
class A
{
public:
    void test()
    {
        int i = 0;
    }
    
private:
    int good()
    {
        
    }
    
protected:
    
};


EDIT: support 'case'/ 'default'.


[attachment deleted by admin]
« Last Edit: April 28, 2010, 02:58:25 am by Loaden »

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
A better implementation, to prevent mistaken judgments.
Code
Index: src/plugins/codecompletion/codecompletion.cpp

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

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

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

@@ -2106,6 +2106,26 @@

             }
         }
     }
+     
+    if (event.GetEventType() == wxEVT_SCI_CHARADDED)
+    {
+        if (event.GetKey() == _T(':'))
+        {
+            if (control->AutoCompActive()) control->AutoCompCancel();
+            wxString text = control->GetCurLine().Trim(false);
+            text = text.Remove(text.Find(_T(':'), true));
+            text = text.Trim();
+            if (text == _T("public") || text == _T("protected") || text == _T("private"))
+            {
+                int curLine = control->GetCurrentLine();
+                control->GotoPos(control->GetLineIndentPosition(curLine));
+                control->BackTab();
+                control->GotoPos(control->GetLineEndPosition(curLine));
+                control->NewLine();
+                control->Tab();
+            }
+        }
+    }
 
     Parser* parser = m_NativeParser.GetParserPtr();
     if (   parser && parser->Options().whileTyping


[attachment deleted by admin]

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
I like this, but I guess some people do want the indent ?
Should it therefor be configurable ?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Should it therefor be configurable ?
Yes, every such option should be configurable. Because basically by experience I can tell that a lot people won't like it (most likely including Thomas btw...). I also can tell that a lot people will need to modify this because it's in conflict with certain coding styles.
« Last Edit: March 24, 2010, 04:19:54 pm by MortenMacFly »
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
I agree with configurable.
As far as I know, VS, Eclipse CDT, Qt Creator and CodeLite handled this way by default.

And a lot of people are using the VS, CDT ...
When them trying Code::Blocks...
« Last Edit: March 24, 2010, 05:24:55 pm by Loaden »

Offline danselmi

  • Developer
  • Almost regular
  • *****
  • Posts: 203
I believe this code fits better somewhere in
Code
void cbEditor::OnEditorCharAdded(wxScintillaEvent& event)
since all other indent related code is there too.
Additionally it should check the currently set lexer and needs to be configurable.

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Isnt this already handled by Astyle ?

Offline ptDev

  • Almost regular
  • **
  • Posts: 222
Isnt this already handled by Astyle ?

AStyle reformats already written code. This patch would make the behaviour apply (optionally) while typing the code. I wouldn't mind having the option, I should say.

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Isnt this already handled by Astyle ?
AStyle reformats already written code. This patch would make the behaviour apply (optionally) while typing the code. I wouldn't mind having the option, I should say.
Gotcha.  In that case shouldnt any of these options be set to inherit the users astyle preferences and let that be the point of configuration ?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Gotcha.  In that case shouldnt any of these options be set to inherit the users astyle preferences and let that be the point of configuration ?
As the astyle plugin is... erm... a plugin and therefore might not be available / disabled / installed you cannot rely on these settings.
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
When type ':', determine whether the need to add a new line.

Code
Index: src/plugins/codecompletion/codecompletion.cpp

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

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

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

@@ -2080,6 +2080,33 @@

         }
     }
 
+    if (event.GetEventType() == wxEVT_SCI_CHARADDED)
+    {
+        if (event.GetKey() == _T(':'))
+        {
+            if (control->AutoCompActive()) control->AutoCompCancel();
+            wxString text = control->GetCurLine().Trim(false);
+            text = text.Remove(text.Find(_T(':'), true));
+            text = text.Trim();
+            if (text == _T("public") || text == _T("protected") || text == _T("private"))
+            {
+                int curLine = control->GetCurrentLine();
+                control->GotoPos(control->PositionFromLine(curLine));
+                control->BackTab();
+                const int column = control->GetColumn(control->GetCurrentPos());
+                control->GotoPos(control->GetLineEndPosition(curLine));
+                if (control->GetLineCount() > curLine)
+                {
+                    if (control->GetColumn(control->GetLineIndentPosition(curLine + 1)) == column)
+                    {
+                        control->NewLine();
+                        control->Tab();
+                    }
+                }
+            }
+        }
+    }
+
     Parser* parser = m_NativeParser.GetParserPtr();
     if (   parser && parser->Options().whileTyping
         && (   (event.GetModificationType() & wxSCI_MOD_INSERTTEXT)

[attachment deleted by admin]

Offline thynson

  • Multiple posting newcomer
  • *
  • Posts: 15
In my opinion, a label(included'public:', 'private:' and 'protected')should not be indented to keep it easy to be find.
I think the parser could do it like this way: when the ENTER is pressed, check the prev line, if there is just a label, then cancel indent.

BTW, I think when we typed a '{' or '}', the indent should be adjusted automatically. For example, when we may write

for(vector<int>::iterator i = v.begin();
     i != v.end();
    ++i)

Then we would type ENTER, and the CodeBlocks will generate '{' and '}' for us, but it looks like that:

for(vector<int>::iterator i = v.begin();
     i != v.end();
    ++i)
    {
    }

This is not what we want. Could someone solve this problem?
« Last Edit: May 29, 2010, 01:16:46 pm by thynson »

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
In my opinion, a label(included'public:', 'private:' and 'protected')should not be indented to keep it easy to be find.
I think the parser could do it like this way: when the ENTER is pressed, check the prev line, if there is just a label, then cancel indent.

BTW, I think when we typed a '{' or '}', the indent should be adjusted automatically. For example, when we may write

for(vector<int>::iterator i = v.begin();
     i != v.end();
    ++i)

Then we would type ENTER, and the CodeBlocks will generate '{' and '}' for us, but it looks like that:

for(vector<int>::iterator i = v.begin();
     i != v.end();
    ++i)
    {
    }

This is not what we want. Could someone solve this problem?
The feature added in cc_branch. :lol:

Offline danselmi

  • Developer
  • Almost regular
  • *****
  • Posts: 203
I still believe that this code should go to cbEditor, to where the other indent related code resides! Am I wrong?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
I still believe that this code should go to cbEditor, to where the other indent related code resides!
That is actually true. I'd second that.
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