News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

auto add the "()"

Started by blueshake, September 23, 2009, 09:12:47 AM

Previous topic - Next topic

blueshake

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:
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.next.codeblocks.org/index.php/topic,10873.msg75913.html#msg75913
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?

killerbot

sounds interesting.

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

blueshake

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?

blueshake

#3
hello,killerbot
I have solved the issue .
see my post in this thread.http://forums.next.codeblocks.org/index.php/topic,11187.msg76512.html#msg76512
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?

blueshake

Quote from: blueshake 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:
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.next.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?

blueshake

ok,here is the new patch.
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?

blueshake

#6
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:
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?

thomas

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."