Author Topic: auto add the "()"  (Read 14493 times)

Offline blueshake

  • Regular
  • ***
  • Posts: 458
auto add the "()"
« on: September 23, 2009, 09:12:47 am »
hello,if the item in suggestion list is a function ,the "()" will add automaticlly.
explanation:

     1.use the searchItem to store the the function type name in int CodeCompletion::CodeComplete() in codecompletion.cpp
     2.hook the message wxEVT_SCI_AUTOCOMP_SELECTION,if we find the item which wil be completed in the serachItem,
     then cancel the codecompletion.we do it by ourselves,and add the "()",if the function has arguement.the caret go to the
     position beteen the "()".
here is the patch:
Code
Index: src/plugins/codecompletion/codecompletion.cpp
===================================================================
--- src/plugins/codecompletion/codecompletion.cpp (revision 5813)
+++ src/plugins/codecompletion/codecompletion.cpp (working copy)
@@ -564,6 +564,7 @@
             TokensTree* tokens = parser->GetTokens();
             for (TokenIdxSet::iterator it = result.begin(); it != result.end(); ++it)
             {
+                searchItem.clear();
                 Token* token = tokens->at(*it);
                 if (!token || token->m_Name.IsEmpty())
                     continue;
@@ -580,7 +581,10 @@
                 wxString tmp;
                 tmp << token->m_Name << wxString::Format(_T("?%d"), iidx);
                 items.Add(tmp);
-
+                if (token->m_TokenKind == tkFunction || token->m_TokenKind == tkConstructor || token->m_TokenKind == tkDestructor)
+                {
+                    searchItem[token->m_Name] = token->m_Args.size();
+                }
                 if (token->m_TokenKind == tkNamespace && token->m_Aliases.size())
                 {
                     for (size_t i = 0; i < token->m_Aliases.size(); ++i)
@@ -1940,7 +1944,32 @@
 
     if ((event.GetKey() == '.') && control->AutoCompActive())
         control->AutoCompCancel();
+    if(event.GetEventType() == wxEVT_SCI_AUTOCOMP_SELECTION)
+    {
+        //for (map<wxString, int>::iterator it = searchItem.begin(); it != searchItem.end(); ++it)
+        //{
+        wxString itemText = event.GetText();
+        map<wxString, int>::iterator it = searchItem.find(itemText);
+        if (it != searchItem.end())
+        {
+            //Manager::Get()->GetLogManager()->DebugLog((*it).first);
+            control->AutoCompCancel();
+            int pos = control->GetCurrentPos();
+            int start = control->WordStartPosition(pos, true);
 
+            control->AddText(itemText.substr(pos - start)+_T("()"));
+            //Parser* parser = m_NativeParsers.FindParserFromEditor(editor);
+            //TokensTree* tokens = parser->GetTokens();
+            if ((*it).second)
+            {
+                pos = control->GetCurrentPos();
+                control->GotoPos(pos - 1);
+            }
+                //Manager::Get()->GetLogManager()->DebugLog((*it).first+_T("param"));
+        }
+        //}
+        //Manager::Get()->GetLogManager()->DebugLog((*it).first);
+    }
     if (event.GetEventType() == wxEVT_SCI_CHARADDED)
     {
         // a character was just added in the editor
Index: src/plugins/codecompletion/codecompletion.h
===================================================================
--- src/plugins/codecompletion/codecompletion.h (revision 5813)
+++ src/plugins/codecompletion/codecompletion.h (working copy)
@@ -142,6 +142,7 @@
 
         int StartIdxNameSpaceInScope;
         int m_CurrentLine;
+        map<wxString, int> searchItem;
         wxString m_LastFile;
 
         wxTimer m_FunctionsParsingTimer;
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp (revision 5813)
+++ src/sdk/cbeditor.cpp (working copy)
@@ -1045,7 +1045,7 @@
         wxEVT_SCI_HOTSPOT_CLICK,
         wxEVT_SCI_HOTSPOT_DCLICK,
         wxEVT_SCI_CALLTIP_CLICK,
-//        wxEVT_SCI_AUTOCOMP_SELECTION,
+        wxEVT_SCI_AUTOCOMP_SELECTION,
 //        wxEVT_SCI_INDICATOR_CLICK,
 //        wxEVT_SCI_INDICATOR_RELEASE,
 


EDIT:
this need to turn message wxEVT_SCI_AUTOCOMP_SELECTION on .
I asked the question before in this thread.
http://forums.codeblocks.org/index.php/topic,10873.msg75913.html#msg75913
« Last Edit: September 23, 2009, 09:16:02 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: 5519
Re: auto add the "()"
« Reply #1 on: September 23, 2009, 09:35:13 am »
sounds interesting.

By the way : any news on the new issues we have. Morton told me you would look into some of them.

Offline blueshake

  • Regular
  • ***
  • Posts: 458
Re: auto add the "()"
« Reply #2 on: September 23, 2009, 09:41:30 am »
not yet.maybe this issue is out of my ability. :(
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: 458
Re: auto add the "()"
« Reply #3 on: September 24, 2009, 05:11:38 am »
hello,killerbot
I have solved the issue .
see my post in this thread.http://forums.codeblocks.org/index.php/topic,11187.msg76512.html#msg76512
« Last Edit: September 24, 2009, 08:08:23 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 blueshake

  • Regular
  • ***
  • Posts: 458
Re: auto add the "()"
« Reply #4 on: September 24, 2009, 09:05:28 am »
hello,if the item in suggestion list is a function ,the "()" will add automaticlly.
explanation:

     1.use the searchItem to store the the function type name in int CodeCompletion::CodeComplete() in codecompletion.cpp
     2.hook the message wxEVT_SCI_AUTOCOMP_SELECTION,if we find the item which wil be completed in the serachItem,
     then cancel the codecompletion.we do it by ourselves,and add the "()",if the function has arguement.the caret go to the
     position beteen the "()".
here is the patch:
Code
Index: src/plugins/codecompletion/codecompletion.cpp
===================================================================
--- src/plugins/codecompletion/codecompletion.cpp (revision 5813)
+++ src/plugins/codecompletion/codecompletion.cpp (working copy)
@@ -564,6 +564,7 @@
             TokensTree* tokens = parser->GetTokens();
             for (TokenIdxSet::iterator it = result.begin(); it != result.end(); ++it)
             {
+                searchItem.clear();
                 Token* token = tokens->at(*it);
                 if (!token || token->m_Name.IsEmpty())
                     continue;
@@ -580,7 +581,10 @@
                 wxString tmp;
                 tmp << token->m_Name << wxString::Format(_T("?%d"), iidx);
                 items.Add(tmp);
-
+                if (token->m_TokenKind == tkFunction || token->m_TokenKind == tkConstructor || token->m_TokenKind == tkDestructor)
+                {
+                    searchItem[token->m_Name] = token->m_Args.size();
+                }
                 if (token->m_TokenKind == tkNamespace && token->m_Aliases.size())
                 {
                     for (size_t i = 0; i < token->m_Aliases.size(); ++i)
@@ -1940,7 +1944,32 @@
 
     if ((event.GetKey() == '.') && control->AutoCompActive())
         control->AutoCompCancel();
+    if(event.GetEventType() == wxEVT_SCI_AUTOCOMP_SELECTION)
+    {
+        //for (map<wxString, int>::iterator it = searchItem.begin(); it != searchItem.end(); ++it)
+        //{
+        wxString itemText = event.GetText();
+        map<wxString, int>::iterator it = searchItem.find(itemText);
+        if (it != searchItem.end())
+        {
+            //Manager::Get()->GetLogManager()->DebugLog((*it).first);
+            control->AutoCompCancel();
+            int pos = control->GetCurrentPos();
+            int start = control->WordStartPosition(pos, true);
 
+            control->AddText(itemText.substr(pos - start)+_T("()"));
+            //Parser* parser = m_NativeParsers.FindParserFromEditor(editor);
+            //TokensTree* tokens = parser->GetTokens();
+            if ((*it).second)
+            {
+                pos = control->GetCurrentPos();
+                control->GotoPos(pos - 1);
+            }
+                //Manager::Get()->GetLogManager()->DebugLog((*it).first+_T("param"));
+        }
+        //}
+        //Manager::Get()->GetLogManager()->DebugLog((*it).first);
+    }
     if (event.GetEventType() == wxEVT_SCI_CHARADDED)
     {
         // a character was just added in the editor
Index: src/plugins/codecompletion/codecompletion.h
===================================================================
--- src/plugins/codecompletion/codecompletion.h (revision 5813)
+++ src/plugins/codecompletion/codecompletion.h (working copy)
@@ -142,6 +142,7 @@
 
         int StartIdxNameSpaceInScope;
         int m_CurrentLine;
+        map<wxString, int> searchItem;
         wxString m_LastFile;
 
         wxTimer m_FunctionsParsingTimer;
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp (revision 5813)
+++ src/sdk/cbeditor.cpp (working copy)
@@ -1045,7 +1045,7 @@
         wxEVT_SCI_HOTSPOT_CLICK,
         wxEVT_SCI_HOTSPOT_DCLICK,
         wxEVT_SCI_CALLTIP_CLICK,
-//        wxEVT_SCI_AUTOCOMP_SELECTION,
+        wxEVT_SCI_AUTOCOMP_SELECTION,
 //        wxEVT_SCI_INDICATOR_CLICK,
 //        wxEVT_SCI_INDICATOR_RELEASE,
 


EDIT:
this need to turn message wxEVT_SCI_AUTOCOMP_SELECTION on .
I asked the question before in this thread.
http://forums.codeblocks.org/index.php/topic,10873.msg75913.html#msg75913




find bug ,need to check futher more.
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: 458
Re: auto add the "()"
« Reply #5 on: September 24, 2009, 09:23:39 am »
ok,here is the new patch.
Code
Index: src/plugins/codecompletion/codecompletion.cpp
===================================================================
--- src/plugins/codecompletion/codecompletion.cpp (revision 5816)
+++ src/plugins/codecompletion/codecompletion.cpp (working copy)
@@ -564,6 +564,7 @@
             TokensTree* tokens = parser->GetTokens();
             for (TokenIdxSet::iterator it = result.begin(); it != result.end(); ++it)
             {
+                searchItem.clear();
                 Token* token = tokens->at(*it);
                 if (!token || token->m_Name.IsEmpty())
                     continue;
@@ -580,7 +581,12 @@
                 wxString tmp;
                 tmp << token->m_Name << wxString::Format(_T("?%d"), iidx);
                 items.Add(tmp);
-
+                if (token->m_TokenKind == tkFunction || token->m_TokenKind == tkConstructor || token->m_TokenKind == tkDestructor)
+                {
+                    searchItem[token->m_Name] = token->m_Args.size()-2;
+                    //Manager::Get()->GetLogManager()->DebugLog(token->m_Name);
+                    //Manager::Get()->GetLogManager()->DebugLog(token->m_Args);
+                }
                 if (token->m_TokenKind == tkNamespace && token->m_Aliases.size())
                 {
                     for (size_t i = 0; i < token->m_Aliases.size(); ++i)
@@ -594,7 +600,10 @@
                     }
                 }
             }
-
+//    for (unsigned int i = 0; i < items.GetCount(); ++i)
+//    {
+        //Manager::Get()->GetLogManager()()DebugLog(items[i]);
+//    }
             if (m_NativeParsers.LastAISearchWasGlobal())
             {
                 // empty or partial search phrase: add theme keywords in search list
@@ -1940,7 +1949,37 @@
 
     if ((event.GetKey() == '.') && control->AutoCompActive())
         control->AutoCompCancel();
-
+    if(event.GetEventType() == wxEVT_SCI_AUTOCOMP_SELECTION)
+    {
+        //for (map<wxString, int>::iterator it = searchItem.begin(); it != searchItem.end(); ++it)
+        //{
+        wxString itemText = event.GetText();
+        map<wxString, int>::iterator it = searchItem.find(itemText);
+        if (it != searchItem.end())
+        {
+            //Manager::Get()->GetLogManager()->DebugLog((*it).first);
+            control->AutoCompCancel();
+            int pos = control->GetCurrentPos();
+            int start = control->WordStartPosition(pos, true);
+            control->SetTargetStart(start);
+            control->SetTargetEnd(pos);
+            control->ReplaceTarget(itemText+_T("()"));
+            pos = control->GetCurrentPos();
+            control->GotoPos(pos + itemText.size()+2);
+            //control->AddText(itemText.substr(pos - start)+_T("()"));
+            //Parser* parser = m_NativeParsers.FindParserFromEditor(editor);
+            //TokensTree* tokens = parser->GetTokens();
+            if ((*it).second != 0)
+            {
+                //Manager::Get()->GetLogManager()->DebugLog(F(_T("count:%d"),(*it).second));
+                pos = control->GetCurrentPos();
+                control->GotoPos(pos - 1);
+            }
+                //Manager::Get()->GetLogManager()->DebugLog((*it).first+_T("param"));
+        }
+        //}
+        //Manager::Get()->GetLogManager()->DebugLog((*it).first);
+    }
     if (event.GetEventType() == wxEVT_SCI_CHARADDED)
     {
         // a character was just added in the editor
Index: src/plugins/codecompletion/codecompletion.h
===================================================================
--- src/plugins/codecompletion/codecompletion.h (revision 5816)
+++ src/plugins/codecompletion/codecompletion.h (working copy)
@@ -142,6 +142,7 @@
 
         int StartIdxNameSpaceInScope;
         int m_CurrentLine;
+        map<wxString, int> searchItem;
         wxString m_LastFile;
 
         wxTimer m_FunctionsParsingTimer;
Index: src/sdk/cbeditor.cpp
===================================================================
--- src/sdk/cbeditor.cpp (revision 5816)
+++ src/sdk/cbeditor.cpp (working copy)
@@ -1045,7 +1045,7 @@
         wxEVT_SCI_HOTSPOT_CLICK,
         wxEVT_SCI_HOTSPOT_DCLICK,
         wxEVT_SCI_CALLTIP_CLICK,
-//        wxEVT_SCI_AUTOCOMP_SELECTION,
+        wxEVT_SCI_AUTOCOMP_SELECTION,
 //        wxEVT_SCI_INDICATOR_CLICK,
 //        wxEVT_SCI_INDICATOR_RELEASE,
 
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: 458
Re: auto add the "()"
« Reply #6 on: October 28, 2009, 06:06:15 am »
Hello,Morton:
Does the patch above still work for you ,why it don't work for me now .what kind of joke it is. :shock:
« Last Edit: October 28, 2009, 06:09:17 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 thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: auto add the "()"
« Reply #7 on: October 28, 2009, 09:50:57 am »
Mmmh... same comment from me like for turning "." into "->".
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."