Author Topic: Patch, Auto add a semicolon for '{}' auto-complete  (Read 23154 times)

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Patch, Auto add a semicolon for '{}' auto-complete
« on: March 24, 2010, 08:47:56 am »
Code
Index: src/sdk/cbeditor.cpp

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

--- src/sdk/cbeditor.cpp (revision 6196)

+++ src/sdk/cbeditor.cpp (working copy)

@@ -414,6 +414,21 @@

             control->GotoPos(pos);
             if (ch == _T('{'))
             {
+                int curLine = control->GetCurrentLine();
+                int keyLine = curLine;
+                wxString text;
+                do
+                {
+                    int keyPos = control->GetLineIndentPosition(keyLine);
+                    int start = control->WordStartPosition(keyPos, true);
+                    int end = control->WordEndPosition(keyPos, true);
+                    text = control->GetTextRange(start, end);
+                }
+                while (text.IsEmpty() && --keyLine);
+
+                if (text == _T("class") || text == _T("struct"))
+                    control->InsertText(control->GetLineEndPosition(curLine), _T(";"));
+
                 const wxRegEx reg(_T("^[ \t]*{}[ \t]*"));
                 if (reg.Matches(control->GetCurLine()))
                 {

Example, this code:
Code
class A {|}
class B
{
    |
}

struct A {|}
struct B
{
    |
}

After apply this patch, it's auto changed to:
Code
class A {|};
class B
{
    |
};

struct A {|};
struct B
{
    |
};

A semicolon add here: };

[attachment deleted by admin]

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #1 on: March 24, 2010, 10:01:16 am »
What will happen if I type:
Code
{|}
or
{
    |
}
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #2 on: March 24, 2010, 10:10:56 am »
Sorry, I do not know what you mean.
If not class or struct, then it do nothing.

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #3 on: March 24, 2010, 02:05:35 pm »
Update: support 'enum' keyword
Update2: support 'union' keyword  :P
Code
Index: src/sdk/cbeditor.cpp

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

--- src/sdk/cbeditor.cpp (revision 6196)

+++ src/sdk/cbeditor.cpp (working copy)

@@ -414,6 +414,21 @@

             control->GotoPos(pos);
             if (ch == _T('{'))
             {
+                int curLine = control->GetCurrentLine();
+                int keyLine = curLine;
+                wxString text;
+                do
+                {
+                    int keyPos = control->GetLineIndentPosition(keyLine);
+                    int start = control->WordStartPosition(keyPos, true);
+                    int end = control->WordEndPosition(keyPos, true);
+                    text = control->GetTextRange(start, end);
+                }
+                while (text.IsEmpty() && --keyLine);
+
+                if (text == _T("class") || text == _T("struct") || text == _T("enum") || text == _T("union"))
+                    control->InsertText(control->GetLineEndPosition(curLine), _T(";"));
+
                 const wxRegEx reg(_T("^[ \t]*{}[ \t]*"));
                 if (reg.Matches(control->GetCurLine()))
                 {
« Last Edit: March 24, 2010, 03:26:57 pm by Loaden »

Offline ptDev

  • Almost regular
  • **
  • Posts: 222
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #4 on: March 24, 2010, 03:15:56 pm »
A small suggestion for your patch: you should also support the "union" keyword.

Wow, that was fast! You guys are great! :)
« Last Edit: March 24, 2010, 03:37:18 pm by ptDev »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #5 on: March 24, 2010, 04:13:09 pm »
What happens in my whole file looks like:
Code
{ | }
???
You don't check for the first line / character in the editor being reached, or am I wrong?!
If so, your code my result in an infinite loop, maybe even in a crash.
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 ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5913
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #6 on: March 24, 2010, 04:24:40 pm »
What happens in my whole file looks like:
Code
{ | }
???
You don't check for the first line / character in the editor being reached, or am I wrong?!
If so, your code my result in an infinite loop, maybe even in a crash.
@morten:

Loaden use this for condition below:
Code
while (text.IsEmpty() && --keyLine);

Look at the variable: keyLine, when it reaches zero, the loop will break, so, there will never be a infinite loop.

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #7 on: March 24, 2010, 04:27:49 pm »
Loaden use this for condition below:
Code
while (text.IsEmpty() && --keyLine);
Look at the variable: keyLine, when it reaches zero, the loop will break, so, there will never be a infinite loop.
Indeed. I missed that. C++ style calculations inside an if/while statement - I don't allow such in my coding style, so I am not used to it.
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 thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #8 on: March 24, 2010, 04:30:15 pm »
namespace blah{};  ?
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #9 on: March 24, 2010, 04:37:58 pm »
namespace blah{};  ?
OK, I will fix it!  :D

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #10 on: March 24, 2010, 04:40:28 pm »
Loaden use this for condition below:
Code
while (text.IsEmpty() && --keyLine);
Look at the variable: keyLine, when it reaches zero, the loop will break, so, there will never be a infinite loop.
Indeed. I missed that. C++ style calculations inside an if/while statement - I don't allow such in my coding style, so I am not used to it.

Please test this demo:


Code
class A



{
If not use "do/while", when type '{', it can not judge the class type. :(

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #11 on: March 24, 2010, 04:43:29 pm »
Please test this demo:
Code
class A



{
If not use "do/while", when type '{', it can not judge the class type. :(
??? Strange. I did not apply in my copy yet, but what if you use:
Code
                do
                {
                    int keyPos = control->GetLineIndentPosition(keyLine);
                    int start = control->WordStartPosition(keyPos, true);
                    int end = control->WordEndPosition(keyPos, true);
                    text = control->GetTextRange(start, end)
                    text.Trim();
                    --keyLine;
                }
                while ( text.IsEmpty() && (keyLine>0) );
Notice: This is untested.
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 ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5913
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #12 on: March 24, 2010, 04:46:38 pm »
But noone will write this kind of code, search to the previous line is enough.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #13 on: March 24, 2010, 04:51:14 pm »
??? Strange. I did not apply in my copy yet, but what if you use:
Code
                do
                {
                    int keyPos = control->GetLineIndentPosition(keyLine);
                    int start = control->WordStartPosition(keyPos, true);
                    int end = control->WordEndPosition(keyPos, true);
                    text = control->GetTextRange(start, end)
                    text.Trim();
                    --keyLine;
                }
                while ( text.IsEmpty() && (keyLine>0) );
Notice: This is untested.
I test it, but it's not work for 'namespace {' question.
I will check it, and fix this problem.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #14 on: March 24, 2010, 04:55:42 pm »
But noone will write this kind of code, search to the previous line is enough.

Code

class A :
       public B1,
       public B2,
       public B3,
       public B4
{
     |
};

Replace A, B1-4 with very long strings and for sure someone will write such code :)

Code
namespace my
{
}; <--- here the semicolon is not needed!
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #15 on: March 24, 2010, 05:13:36 pm »
namespace blah{};  ?
namespace blah{};
Sorry, I can not reproduce this problem.
Can you give me some demo?

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #16 on: March 24, 2010, 05:57:53 pm »
I make a new patch, welcome test.
Code
Index: src/sdk/cbeditor.cpp

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

--- src/sdk/cbeditor.cpp (revision 6195)

+++ src/sdk/cbeditor.cpp (working copy)

@@ -414,6 +414,23 @@

             control->GotoPos(pos);
             if (ch == _T('{'))
             {
+                int curLine = control->GetCurrentLine();
+                int keyLine = curLine;
+                wxString text;
+                do
+                {
+                    int keyPos = control->GetLineIndentPosition(keyLine);
+                    int start = control->WordStartPosition(keyPos, true);
+                    int end = control->WordEndPosition(keyPos, true);
+                    text = control->GetTextRange(start, end);
+                }
+                while ((text.IsEmpty() || text == _T("public") || text == _T("protected") || text == _T("private"))
+                       && text != _T("namespace")
+                       && (--keyLine));
+
+                if (text == _T("class") || text == _T("struct") || text == _T("enum") || text == _T("union"))
+                    control->InsertText(control->GetLineEndPosition(curLine), _T(";"));
+
                 const wxRegEx reg(_T("^[ \t]*{}[ \t]*"));
                 if (reg.Matches(control->GetCurLine()))
                 {

[attachment deleted by admin]

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Patch, Auto add a semicolon for '{}' auto-complete
« Reply #17 on: March 25, 2010, 10:56:17 am »
Can you give me some demo?
Code
namespace my
{
}; <--- here the semicolon is not needed!
That's what I wanted to point out:)
Although most compilers in non-strict mode will silently convert that trailing semicolon to ((void)0); and ignore it, nevertheless it doesn't belong there.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."