Author Topic: make cc real-time parse  (Read 19193 times)

Offline blueshake

  • Regular
  • ***
  • Posts: 459
Re: make cc real-time parse
« Reply #15 on: January 07, 2010, 12:36:39 pm »
optimize the cc search process based on real-time parse.

before:


parse the contexts between the begin and the caret position to get the function tokens.


now

get the function tokens from the tokentree with filename directly based on real-time parse.and this reduce the parse time.

enjoy it. :D


patch:

Code
Index: src/include/filemanager.h
===================================================================
--- src/include/filemanager.h (revision 5973)
+++ src/include/filemanager.h (working copy)
@@ -144,7 +144,19 @@
     NullLoader(const wxString& name, char* buffer, size_t size) { fileName = name; data = buffer; len = size; Ready(); };
     void operator()(){};
 };
-
+class EditorReuser : public LoaderBase
+{
+public:
+    EditorReuser(const wxString& name, const wxString& s)
+    {
+        fileName = name;
+        len = strlen(s.mb_str(wxConvUTF8));
+        data = new char[len + 1];
+        strcpy(data, (const char*)s.mb_str(wxConvUTF8));
+        Ready();
+    }
+    void operator()(){};
+};
 // ***** class: FileManager *****
 class FileManager : public Mgr<FileManager>
 {
Index: src/plugins/codecompletion/codecompletion.cpp
===================================================================
--- src/plugins/codecompletion/codecompletion.cpp (revision 5973)
+++ src/plugins/codecompletion/codecompletion.cpp (working copy)
@@ -172,7 +172,8 @@
     m_IsAutoPopup(false),
     m_ToolbarChanged(true),
     m_CurrentLine(0),
-    m_LastFile(wxEmptyString)
+    m_LastFile(wxEmptyString),
+    m_NeedReparse(false)
 {
     if(!Manager::LoadResource(_T("codecompletion.zip")))
     {
@@ -2069,7 +2070,11 @@
             }
         }
     }
-
+    if ((event.GetModificationType() & wxSCI_MOD_INSERTTEXT) ||
+        (event.GetModificationType() & wxSCI_MOD_DELETETEXT))
+    {
+        m_NeedReparse = true;
+    }
     if( control->GetCurrentLine() != m_CurrentLine)
     {
         m_CurrentLine = control->GetCurrentLine();
@@ -2093,6 +2098,16 @@
                 m_Scope->SetSelection(wxNOT_FOUND);
             }
         }
+        if (m_NeedReparse)
+        {
+            Parser* parser = m_NativeParser.FindParserFromActiveEditor();
+            if (parser)
+            {
+                parser->Reparse(editor->GetFilename());
+            }
+            m_NeedReparse= false;
+        }
+
     }
 
     // allow others to handle this event
Index: src/plugins/codecompletion/codecompletion.h
===================================================================
--- src/plugins/codecompletion/codecompletion.h (revision 5973)
+++ src/plugins/codecompletion/codecompletion.h (working copy)
@@ -146,7 +146,7 @@
         int                                m_CurrentLine;
         map<wxString, int>                 m_SearchItem;
         wxString                           m_LastFile;
-
+        bool                               m_NeedReparse;
         bool                               m_LexerKeywordsToInclude[9];
 
         DECLARE_EVENT_TABLE()
Index: src/plugins/codecompletion/nativeparser.cpp
===================================================================
--- src/plugins/codecompletion/nativeparser.cpp (revision 5973)
+++ src/plugins/codecompletion/nativeparser.cpp (working copy)
@@ -2152,18 +2152,13 @@
     }
     s_LastEditor = editor;
     s_LastLine = line;
-
-    Parser parser(this);
-    parser.ParseBufferForFunctions(control->GetTextRange(0, pos));
-
-    wxArrayString funcs;
-    TokensTree* tmptree = parser.GetTempTokens();
-
-    // look for implementation functions that enclose our current line number
-    for(size_t i = 0; i < tmptree->size();i++)
+    TokenIdxSet result;
+    m_Parser.FindTokensInFile(editor->GetFilename(), result, tkFunction|tkConstructor|tkDestructor);
+    TokensTree* tree = m_Parser.GetTokens();
+    for (TokenIdxSet::iterator it = result.begin(); it != result.end(); ++it)
     {
-        Token* token = tmptree->at(i);
-        if (token && (token->m_TokenKind == tkFunction || token->m_TokenKind == tkConstructor || token->m_TokenKind == tkDestructor))
+        Token* token = tree->at(*it);
+        if (token)
         {
             // found a function; check its bounds
             if (token->m_ImplLineStart <= (size_t)line && token->m_ImplLineEnd >= (size_t)line)
@@ -2174,6 +2169,7 @@
                                                                 token->DisplayName().wx_str(),
                                                                 token->m_ImplLine));
 
+
                 s_LastNS = token->GetNamespace();
                 s_LastPROC = token->m_Name;
                 s_LastResult = control->PositionFromLine(token->m_ImplLine - 1);
Index: src/plugins/codecompletion/parser/parser.cpp
===================================================================
--- src/plugins/codecompletion/parser/parser.cpp (revision 5973)
+++ src/plugins/codecompletion/parser/parser.cpp (working copy)
@@ -538,7 +538,7 @@
             }
 
             if (!opts.loader) //this should always be true (memory will leak if a loader has already been initialized before this point)
-                opts.loader = Manager::Get()->GetFileManager()->Load(bufferOrFilename, false);
+                opts.loader = Manager::Get()->GetFileManager()->Load(bufferOrFilename, true);
         }
 
 #if PARSER_DEBUG_OUTPUT
@@ -1045,3 +1045,22 @@
     }
     return true;
 }
+size_t Parser::FindTokensInFile(const wxString& fileName,TokenIdxSet& result,short int kindMask)
+{
+    result.clear();
+    wxString file = UnixFilename(fileName);
+    TokenIdxSet tmpresult;
+    wxCriticalSectionLocker lock(s_MutexProtection);
+    if(!m_pTokens->FindTokensInFile(file,tmpresult,kindMask))
+        return 0;
+
+    TokenIdxSet::iterator it;
+    for(it = tmpresult.begin();it!=tmpresult.end();++it)
+    {
+        Token* token = m_pTokens->at(*it);
+        if(token)
+        //result.push_back(token);
+        result.insert(*it);
+    }
+    return result.size();
+}
Index: src/plugins/codecompletion/parser/parser.h
===================================================================
--- src/plugins/codecompletion/parser/parser.h (revision 5973)
+++ src/plugins/codecompletion/parser/parser.h (working copy)
@@ -153,6 +153,7 @@
         Token* FindChildTokenByName(Token* parent, const wxString& name, bool useInheritance = false, short int kindMask = 0xFFFF) const;
         size_t FindMatches(const wxString& s, TokenList&   result, bool caseSensitive = true, bool is_prefix = true);
         size_t FindMatches(const wxString& s, TokenIdxSet& result, bool caseSensitive = true, bool is_prefix = true);
+        size_t FindTokensInFile(const wxString& fileName, TokenIdxSet& result, short int kindMask);
         ParserOptions& Options() { return m_Options; }
         BrowserOptions& ClassBrowserOptions() { return m_BrowserOptions; }
 
Index: src/sdk/filemanager.cpp
===================================================================
--- src/sdk/filemanager.cpp (revision 5973)
+++ src/sdk/filemanager.cpp (working copy)
@@ -138,12 +138,8 @@
                 cbEditor* ed = em->GetBuiltinEditor(em->GetEditor(i));
                 if(ed && fileName == ed->GetFilename())
                 {
-                    wxString s(ed->GetControl()->GetText());
-                    #if wxCHECK_VERSION(2, 9, 0)
-                    NullLoader *nl = new NullLoader(file, (char*) s.wx_str(), s.length() * sizeof(wxChar));
-                    #else
-                    NullLoader *nl = new NullLoader(file, (char*) s.c_str(), s.length() * sizeof(wxChar));
-                    #endif
+
+                    EditorReuser *nl = new EditorReuser(file, ed->GetControl()->GetText());
                     return nl;
                 }
             }


hi,morten :
you are right.so the patch above should be changed.
« Last Edit: January 07, 2010, 12:38:12 pm 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: 459
Re: make cc real-time parse
« Reply #16 on: January 07, 2010, 12:57:22 pm »
optimize the cc search process based on real-time parse.
[...]
Code
Index: src/include/filemanager.h
===================================================================
--- src/plugins/codecompletion/nativeparser.cpp (revision 5973)
+++ src/plugins/codecompletion/nativeparser.cpp (working copy)
@@ -2152,18 +2152,13 @@
     }
     s_LastEditor = editor;
     s_LastLine = line;
-
-    Parser parser(this);
-    parser.ParseBufferForFunctions(control->GetTextRange(0, pos));
-
-    wxArrayString funcs;
-    TokensTree* tmptree = parser.GetTempTokens();
-
-    // look for implementation functions that enclose our current line number
-    for(size_t i = 0; i < tmptree->size();i++)
+    TokenIdxSet result;
+    m_Parser.FindTokensInFile(editor->GetFilename(), result, tkFunction|tkConstructor|tkDestructor);
+    TokensTree* tree = m_Parser.GetTokens();
+    for (TokenIdxSet::iterator it = result.begin(); it != result.end(); ++it)
     {
-        Token* token = tmptree->at(i);
-        if (token && (token->m_TokenKind == tkFunction || token->m_TokenKind == tkConstructor || token->m_TokenKind == tkDestructor))
+        Token* token = tree->at(*it);
+        if (token)
         {
             // found a function; check its bounds
             if (token->m_ImplLineStart <= (size_t)line && token->m_ImplLineEnd >= (size_t)line)
@@ -2174,6 +2169,7 @@
                                                                 token->DisplayName().wx_str(),
                                                                 token->m_ImplLine));
 
+
                 s_LastNS = token->GetNamespace();
                 s_LastPROC = token->m_Name;
                 s_LastResult = control->PositionFromLine(token->m_ImplLine - 1);
This is certainly the cause for the wrong file being returned which I've reported here:
http://forums.codeblocks.org/index.php/topic,11800.msg80076.html#msg80076
Look: Previously the parser was created based on the native parsers current state. Now you just use the attached parser which maybe wrong because it may point to the wrong file!
I believe something like FindParserFromEditor needs to be used here...

I check out the FindParserFromEditor.It just simply return the m_Parser.
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: make cc real-time parse
« Reply #17 on: January 07, 2010, 01:13:44 pm »
I check out the FindParserFromEditor.It just simply return the m_Parser.
You are right. What a messy code that was... From the early stages where there were several parsers per project. I've cleaned it up thoroughly and committed.

Ps.: While I was at it I fixed the bug with the wrong file pointer... ;-)
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