Author Topic: convert "." to "->"  (Read 26191 times)

Offline blueshake

  • Regular
  • ***
  • Posts: 459
convert "." to "->"
« on: October 27, 2009, 12:32:11 pm »
hello,
This patch can convert "." to "->" when the token is pointer.NOT smart but work. :)
any idea/comment is welcome.
« Last Edit: October 28, 2009, 04:07:05 am by blueshake »
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: convert "." to "->"
« Reply #1 on: October 27, 2009, 01:09:29 pm »
This patch can convert "." to "->" when the token is pointer.NOT smart but work. :)
any idea/comment is welcome.
What would happen if I *really* would like to enter a dot? E.g. the parser has parsed incorrectly? Would it always be replaced with the pointer operator then? If yes, then this should surely be configurable.
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 oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: convert "." to "->"
« Reply #2 on: October 27, 2009, 01:23:06 pm »
100% make it configurable, so I can disable it  :lol:
(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 blueshake

  • Regular
  • ***
  • Posts: 459
Re: convert "." to "->"
« Reply #3 on: October 27, 2009, 01:38:55 pm »
Quote
What would happen if I *really* would like to enter a dot? E.g. the parser has parsed incorrectly? Would it always be replaced with the pointer operator then? If yes, then this should surely be configurable.
The following patch can do this.for the second time ,it would not work.(just delete the "->" ,press "." again.)

1.can anyboby make this function be confgurable,I don't know how to do this. :)
2.not work for abc().:,because now I just simplely skip the "."
Code
struct qq
{
    int x;
    int y;
};
qq* abc(int aa)
{

}
int main()
{
    qq* pp;
    pp->
    cout << "Hello world!" << endl;
    return 0;
}



Edit:

need helps to refine this function. :)
« Last Edit: October 28, 2009, 04:06:48 am by blueshake »
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: convert "." to "->"
« Reply #4 on: October 27, 2009, 11:30:06 pm »
sounds nice. From asking around a bit, it seems it should be configurable (some people will turn it of).
The best thing for the moment is to try it out (once you have some more or less final patch), and see how many times the parser would pull our leg.

Some extra thoughts :
* would be nice if it is aware of stl iterators, and do the same trick
* in future this is going to be hell for our parsers,thanks to the auto variable (C++0x will rock, but for parsers/IDE's lofe won't get any easier ;-) )

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: convert "." to "->"
« Reply #5 on: October 28, 2009, 04:03:01 am »
update:can work with function now.
Code
Index: src/plugins/codecompletion/codecompletion.cpp
===================================================================
--- src/plugins/codecompletion/codecompletion.cpp (revision 5891)
+++ src/plugins/codecompletion/codecompletion.cpp (working copy)
@@ -2040,7 +2040,67 @@
                     return;
                 }
             }
+            static bool repeat = true;
+            if (ch == '.' && repeat)
+            {
+                repeat = false;
+                Parser* parser = m_NativeParser.FindParserFromActiveEditor();
+                if (parser)
+                {
+                    TokenIdxSet result;
+                    int pos = control->GetCurrentPos();
+                    wxString line = control->GetLine(control->LineFromPosition(pos));
 
+                    //skip the "."
+                    int startAt = control->GetColumn(pos) -2;
+                    int nest = 0;
+                    if (line.GetChar(startAt) == ')')
+                    {
+                        ++nest;
+                        while ((--startAt >= 0)
+                                && (nest != 0) )
+                            {
+                                #if wxCHECK_VERSION(2, 9, 0)
+                                switch (line.GetChar(startAt).GetValue())
+                                #else
+                                switch (line.GetChar(startAt))
+                                #endif
+                                {
+                                    case ']':
+                                    case ')': ++nest; --startAt; break;
+
+                                    case '[':
+                                    case '(': --nest; --startAt; break;
+
+                                }
+                            }
+                        if ((   (startAt >= 0)
+                                && ((line.GetChar(startAt) == ')')
+                                || (line.GetChar(startAt) == ']') ) ))
+                        ++nest;
+                        ++startAt;
+
+                    }
+                    int endOfWord = control->WordEndPosition(pos -line.Len() + startAt, true);
+                    if (m_NativeParser.MarkItemsByAI(result, true, true, true, endOfWord))
+                    {
+                        if (result.size() == 1)
+                        {
+                            Token* sel = parser->GetTokens()->at(*(result.begin()));
+                            Manager::Get()->GetLogManager()->DebugLog(sel->m_Type);
+                            if (sel->m_Type.find('*') != wxString::npos)
+                            {
+                                control->SetTargetStart(pos-1);
+                                control->SetTargetEnd(pos);
+                                control->ReplaceTarget(_T("->"));
+                                control->GotoPos(pos +1);
+                            }
+                        }
+                    }
+                }
+            }
+            else
+                repeat = true;
             int timerDelay = cfg->ReadInt(_T("/cc_delay"), 500);
             if (autoCC || timerDelay == 0)
             {
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: convert "." to "->"
« Reply #6 on: October 28, 2009, 04:06:05 am »
Quote
would be nice if it is aware of stl iterators, and do the same trick

For now ,I just search "*" in token's m_Type ,it is not easy to do this for current parser. :D
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5915
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: convert "." to "->"
« Reply #7 on: October 28, 2009, 05:32:44 am »
* in future this is going to be hell for our parsers,thanks to the auto variable (C++0x will rock, but for parsers/IDE's lofe won't get any easier ;-) )
Hi, can you explain this sentence? I can't fully understand you. Thanks.

BTW:My lab's networking was broken, so, I can't test this patch these days till the networking was fixed. :(
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 blueshake

  • Regular
  • ***
  • Posts: 459
Re: convert "." to "->"
« Reply #8 on: October 28, 2009, 05:51:41 am »
* in future this is going to be hell for our parsers,thanks to the auto variable (C++0x will rock, but for parsers/IDE's lofe won't get any easier ;-) )
Hi, can you explain this sentence? I can't fully understand you. Thanks.

BTW:My lab's networking was broken, so, I can't test this patch these days till the networking was fixed. :(
Not understood it fully too. :wink:
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: convert "." to "->"
« Reply #9 on: October 28, 2009, 07:28:18 am »
well in C++0x there will be several cases where you no longer will have to specify the type of a variable, the compiler will determine it for you.

Examples :
auto Count = 10;  // --> int

std::vector<std::string> Hello;

for(auto It = Hello.cbegin(); It != Hello.cend(); ++It)
{
  std::cout << *It; // -> std::vector<std::string>const_iterator
}

this last loop can also be written with the new foreach, and also there the compiler will deduce the type.

And so will code completion parsers have to deduce this according to the new language rules.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: convert "." to "->"
« Reply #10 on: October 28, 2009, 09:48:14 am »
I'd very much like if no more extra smartness was added to code completion, it is already bad enough as it is now. It already hampers daily work enough the way it is now. Code completion should be the opposite, it should help.

And yes to C++0x, but it's present, not future :)
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: convert "." to "->"
« Reply #11 on: October 28, 2009, 10:42:25 am »
that means Thomas you have the code completion plug-in enabled ?
What a shock ...  8)

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: convert "." to "->"
« Reply #12 on: October 28, 2009, 10:53:30 am »
I'd very much like if no more extra smartness was added to code completion, it is already bad enough as it is now. It already hampers daily work enough the way it is now. Code completion should be the opposite, it should help.

And yes to C++0x, but it's present, not future :)

I don't understand why it already hampers daily work enough the way it is now.
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: convert "." to "->"
« Reply #13 on: October 28, 2009, 12:37:17 pm »
that means Thomas you have the code completion plug-in enabled ?
Yes, I have it enabled for the odd hope that it might manage with a 1% chance to do a "find definition" correctly when I use a function like snprintf and want to be sure that I'm giving it the correct parameters. Never does, of course. Winning the lottery is more likely to succeed. So I find myself searching through system headers in Windows Explorer instead, every single time.
If CC only was helpful in, say 50% of all cases, and if it was only popping up shit in, say 5% of all cases, I wouldn't be complaining. But it is really only causing extra work and never helping.

I don't understand why it already hampers daily work enough the way it is now.
Because it's fucking dumb and pops up menus that only ever contain shit. And worse, you have to hit ESC to make them go away. If you type fast, it will happen once or twice per week that you type something like
struct SomeName
{

and find that this super smart code completion silently changed it into
struct SomeShitFromWindowsThatIncidentiallyStartsWithSome
{


And once per month, you only notice that after having wasted several minutes on a strange compiler error that you don't understand. Similar thing happens occasionally when changing a variable name in some function. After changing the name and hitting the "up" key 3-4 times (to get to the next line containing the name), you find yourself only scrolling through some flipping popup menu instead of moving the cursor. By the time you've stopped shouting and hitting your monitor with the keyboard, you have forgotten what you actually wanted to change.

Add to that these darn smart braces, which mean that you have double work to fix the indenting for every new scope block, only because the editor deems it a good idea to insert tabs when you never touched the tab key. At least those can be turned off relatively easily in Code::Blocks, but I've yet to find where to disable them in SciTE.

In that sense, I wouldn't want the editor to mess with changing operators silently (and possibly unnoticed). It's the programmer's job to know what operator is the right one, not the editor's. And in this case, it is much more severe than inserting extra tabs (which is only formatting) or inserting some bullshit names (which will not go unnoticed). This is something that even has a realistic (though admittedly small) chance of introducing silent changes which will compile without error, but won't do what the programmer intended.
I mean, you could as well have code completion change all occurrences of var++ to ++var, how does that sound? It's the very same idea.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: convert "." to "->"
« Reply #14 on: October 28, 2009, 02:05:36 pm »
or inserting some bullshit names (which will not go unnoticed).
Turn it off.
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 rhf

  • Multiple posting newcomer
  • *
  • Posts: 123
Re: convert "." to "->"
« Reply #15 on: October 28, 2009, 03:25:15 pm »
Similar thing happens occasionally when changing a variable name in some function. After changing the name and hitting the "up" key 3-4 times (to get to the next line containing the name), you find yourself only scrolling through some flipping popup menu instead of moving the cursor. By the time you've stopped shouting and hitting your monitor with the keyboard, you have forgotten what you actually wanted to change.
Perhaps Thomas is a little harsh, but I definitely agree with the above complaint, which is very irritating. I would much prefer to have the Up/Down keys move the cursor up or down in the file - not in the popup menu. Perhaps a different key sequence could be used for scrolling the popup menu. (By the way, I know I can turn CC off, but I often find it useful.)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5915
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: convert "." to "->"
« Reply #16 on: October 28, 2009, 04:38:49 pm »
Similar thing happens occasionally when changing a variable name in some function. After changing the name and hitting the "up" key 3-4 times (to get to the next line containing the name), you find yourself only scrolling through some flipping popup menu instead of moving the cursor. By the time you've stopped shouting and hitting your monitor with the keyboard, you have forgotten what you actually wanted to change.
Perhaps Thomas is a little harsh, but I definitely agree with the above complaint, which is very irritating. I would much prefer to have the Up/Down keys move the cursor up or down in the file - not in the popup menu. Perhaps a different key sequence could be used for scrolling the popup menu. (By the way, I know I can turn CC off, but I often find it useful.)
Yes, sometimes, the codecompletion list windows was annoying, when I have finished enter the variable name, but they still exist there, I need to press "ESC" key to the suggestion list window closed. If not, arrow up and arrow down can only scroll the list, not the caret in the editor. :(



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 blueshake

  • Regular
  • ***
  • Posts: 459
Re: convert "." to "->"
« Reply #17 on: October 29, 2009, 12:55:23 am »
As I know, there is a way to add key sequence to cancle the suggestion window.Maybe we can use it.
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Offline Loaden

  • Lives here!
  • ****
  • Posts: 1014
Re: convert "." to "->"
« Reply #18 on: August 27, 2010, 10:58:28 am »
100% make it configurable, so I can disable it  :lol:
If we support smart pointer fully, we need this feature.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: convert "." to "->"
« Reply #19 on: August 27, 2010, 11:10:02 am »
Loaden, why, can you show an example?

Code
struct some_class { int get(); }
smart_ptr<some_class> p(new some_class);

p->  <--- I want to get the list with some_class's members
p.    <--- I want to get the list with smart_ptr's members

And p.get() is pretty valid and I don't want to be rewritten to p->get()!

Please don't make me fight my IDE and make this option configurable :)

(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: convert "." to "->"
« Reply #20 on: August 27, 2010, 11:23:37 am »
Loaden, why, can you show an example?

Code
struct some_class { int get(); }
smart_ptr<some_class> p(new some_class);

p->  <--- I want to get the list with some_class's members
p.    <--- I want to get the list with smart_ptr's members

And p.get() is pretty valid and I don't want to be rewritten to p->get()!

Please don't make me fight my IDE and make this option configurable :)



The variable 'p', it is *not* a pointer!
So, If we enter "p.", “." can not be changed to "->", because it's not pointers.

We need to judge what the back of the variable "p".
If there is a dot ("."), we need show smart_ptr's members.
else, we need show some_class's members.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: convert "." to "->"
« Reply #21 on: August 27, 2010, 12:57:02 pm »
how well did the . ==>  -> on regular pointers work ?

Could we have an up to date patch to try it out ?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: convert "." to "->"
« Reply #22 on: August 27, 2010, 01:15:03 pm »
If we support smart pointer fully, we need this feature.
Why we need this feature? One reason please?
I've shown you, that this feature is pretty useless for smart pointers.

Don't try to make the IDE/CC too smart, you'll fail for sure,
because the IDE is not directly connected with the user's brain
and it doesn't know what he/she is thinking when doing something
and what are his/her intentions.
(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 MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: convert "." to "->"
« Reply #23 on: August 27, 2010, 01:57:49 pm »
Why we need this feature? One reason please?
I agree with oBFusCATed in this point. Especially trying to detect "smartly" if the user really means a pointer or not is really error prone. It's the job of the compiler to detect such kind of errors, not CC's.
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: convert "." to "->"
« Reply #24 on: August 27, 2010, 02:50:17 pm »
Please waiting, I will try implement smart pointer parsing.
Currently, there does not need this feature.

If we really need this feature in future, it certainly can be configured, and is not selected default.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: convert "." to "->"
« Reply #25 on: August 28, 2010, 12:04:07 am »
Why we need this feature? One reason please?
The only reason I can imagine, honestly, is if you can't tell the difference between pointers and objects in your code. But then you have a totally different problem.

I'd really like to avoid having to fight with the IDE every single time.
"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: convert "." to "->"
« Reply #26 on: August 28, 2010, 01:11:54 am »
If we support smart pointer fully, we need this feature.
Why we need this feature? One reason please?
In fact, this feature is very convenient, enter "." very easy, enter "->" have to some difficulties.
Qt Creator also has this feature.
« Last Edit: August 28, 2010, 01:13:34 am by Loaden »