News:

When registered with our forums, feel free to send a "here I am" post here to differ human beings from SPAM bots.

Main Menu

Patch, Auto add a semicolon for '{}' auto-complete

Started by Loaden, March 24, 2010, 08:47:56 AM

Previous topic - Next topic

Loaden

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:
class A {|}
class B
{
   |
}

struct A {|}
struct B
{
    |
}


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

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


A semicolon add here: };

[attachment deleted by admin]

oBFusCATed

(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!]

Loaden

Sorry, I do not know what you mean.
If not class or struct, then it do nothing.

Loaden

#3
Update: support 'enum' keyword
Update2: support 'union' keyword  :P
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()))
                {

ptDev

#4
A small suggestion for your patch: you should also support the "union" keyword.

Wow, that was fast! You guys are great! :)

MortenMacFly

What happens in my whole file looks like:
{ | }
???
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: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

ollydbg

Quote from: MortenMacFly on March 24, 2010, 04:13:09 PM
What happens in my whole file looks like:
{ | }
???
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:
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.

MortenMacFly

Quote from: ollydbg on March 24, 2010, 04:24:40 PM
Loaden use this for condition below:
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: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

thomas

"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."


Loaden

Quote from: MortenMacFly on March 24, 2010, 04:27:49 PM
Quote from: ollydbg on March 24, 2010, 04:24:40 PM
Loaden use this for condition below:
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:


class A



{

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

MortenMacFly

Quote from: Loaden on March 24, 2010, 04:40:28 PM
Please test this demo:
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:
               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: [url="https://www.codeblocks.org/docs/main_codeblocks_en.html"]https://www.codeblocks.org/docs/main_codeblocks_en.html[/url]
C::B FAQ: [url="https://wiki.codeblocks.org/index.php?title=FAQ"]https://wiki.codeblocks.org/index.php?title=FAQ[/url]

ollydbg

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.

Loaden

Quote from: MortenMacFly on March 24, 2010, 04:43:29 PM
??? Strange. I did not apply in my copy yet, but what if you use:
               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.

oBFusCATed

Quote from: ollydbg on March 24, 2010, 04:46:38 PM
But noone will write this kind of code, search to the previous line is enough.



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


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!]