Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Development => CodeCompletion redesign => Topic started by: Loaden on March 24, 2010, 03:19:47 am

Title: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Loaden on March 24, 2010, 03:19:47 am
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]
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Loaden on March 24, 2010, 06:12:53 am
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]
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private
Post by: killerbot on March 24, 2010, 07:09:30 am
I like this, but I guess some people do want the indent ?
Should it therefor be configurable ?
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private
Post by: MortenMacFly on March 24, 2010, 04:17:31 pm
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.
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Loaden on March 24, 2010, 05:22:32 pm
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...
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: danselmi on March 24, 2010, 09:18:25 pm
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.
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Seronis on March 25, 2010, 02:06:02 am
Isnt this already handled by Astyle ?
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: ptDev on March 25, 2010, 08:18:04 am
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.
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Seronis on March 26, 2010, 12:26:55 am
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 ?
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: MortenMacFly on March 26, 2010, 06:48:51 am
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.
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Loaden on April 23, 2010, 01:37:40 am
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]
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: thynson on May 29, 2010, 01:08:56 pm
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?
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Loaden on May 31, 2010, 02:53:20 pm
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:
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: danselmi on May 31, 2010, 08:40:00 pm
I still believe that this code should go to cbEditor, to where the other indent related code resides! Am I wrong?
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: MortenMacFly on May 31, 2010, 08:42:37 pm
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.
Title: Re: Patch for auto cancel indent when type 'public:' or 'protected:' or 'private:'
Post by: Loaden on June 01, 2010, 03:18:11 pm
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.
OK, I will do that.