Patch #2868 2009-12-20 20:33

techy

Try harder when opening includes from context menu
Download
2868-Try_harder_whe.patch (3.3 KB)
Category
Plugin::Refinement
Status
Accepted
Close date
2009-12-31 14:50
Assigned to
mortenmacfly
Index: src/plugins/codecompletion/codecompletion.cpp
===================================================================
--- src/plugins/codecompletion/codecompletion.cpp    (revision 5986)
+++ src/plugins/codecompletion/codecompletion.cpp    (working copy)
@@ -1896,45 +1896,78 @@
         parser = m_NativeParser.FindParserFromActiveProject(); // get parser of active project, then
     }
 
+    wxArrayString foundSet;
     if (parser)
     {
         // search in all parser's include dirs
-        wxString tmp;
-        wxArrayString FoundSet = parser->FindFileInIncludeDirs(NameUnderCursor);
-        if(FoundSet.GetCount() > static_cast<size_t>(1))
-        {    // more then 1 hit : let the user choose
-            SelectIncludeFile Dialog(Manager::Get()->GetAppWindow());
-            Dialog.AddListEntries(FoundSet);
-            PlaceWindow(&Dialog);
-            if(Dialog.ShowModal() == wxID_OK)
+        foundSet = parser->FindFileInIncludeDirs(NameUnderCursor);
+    }
+
+    // look in the same dir as the source file
+    wxFileName fname = NameUnderCursor;
+    NormalizePath(fname, LastIncludeFileFrom);
+    if (wxFileExists(fname.GetFullPath()) )
+    {
+        foundSet.Add(fname.GetFullPath());
+    }
+
+    // search for the file in project files
+    cbProject* project = Manager::Get()->GetProjectManager()->GetActiveProject();
+    if (project)
+    {
+        for (int i = 0; i < project->GetFilesCount(); ++i)
+        {
+            ProjectFile* pf = project->GetFile(i);
+            if (!pf)
+                continue;
+
+            if (IsSuffixOfPath(NameUnderCursor, pf->file.GetFullPath()))
             {
-              tmp = Dialog.GetIncludeFile();
+                foundSet.Add(pf->file.GetFullPath());
             }
+        }
+    }
+
+    // Remove duplicates
+    for (int i = 0; i < foundSet.Count() - 1; i++)
+    {
+        for (int j = i + 1; j < foundSet.Count(); )
+        {
+            if (foundSet.Item(i) == foundSet.Item(j))
+            {
+                foundSet.RemoveAt(j);
+            }
             else
             {
-                return; // user cancelled the dialog...
+                j++;
             }
         }
-        else if(FoundSet.GetCount())
+    }
+
+    wxString selectedFile;
+    if (foundSet.GetCount() > 1)
+    {    // more than 1 hit : let the user choose
+        SelectIncludeFile Dialog(Manager::Get()->GetAppWindow());
+        Dialog.AddListEntries(foundSet);
+        PlaceWindow(&Dialog);
+        if(Dialog.ShowModal() == wxID_OK)
         {
-            tmp = FoundSet[0];
+            selectedFile = Dialog.GetIncludeFile();
         }
-
-        if (!tmp.IsEmpty())
+        else
         {
-            EditorManager* edMan = Manager::Get()->GetEditorManager();
-            edMan->Open(tmp);
-            return;
+            return; // user cancelled the dialog...
         }
     }
+    else if (foundSet.GetCount() == 1)
+    {
+        selectedFile = foundSet[0];
+    }
 
-    // look in the same dir as the source file
-    wxFileName fname = NameUnderCursor;
-    fname.Assign(wxFileName(LastIncludeFileFrom).GetPath(wxPATH_GET_SEPARATOR) + NameUnderCursor);
-    if (wxFileExists(fname.GetFullPath()))
+    if (!selectedFile.IsEmpty())
     {
         EditorManager* edMan = Manager::Get()->GetEditorManager();
-        edMan->Open(fname.GetFullPath());
+        edMan->Open(selectedFile);
         return;
     }
 
techy 2009-12-20 20:41

Very similar problem to the previous patch - the project I'm working on at work is divided into several repositories (directories) and every repository into several subsystems (again new set of directories). Includes are relative to repositories so codeblocks can't find and open the includes (actually the same happens for the codeblocks project itself, just try to open the includes within "" in codecompletion.cpp - it will fail).

The approach taken here is very similar to the previous patch, the only difference is that the found entries are put into list and all of them presented to the user. Before, all duplicates are removed from the list if there are any.

You'll need to apply the previous patch - this patch uses the function that the previous patch puts into globals.cpp.