From 390afc104dbcd1fa15067fdce62019d3a3ee9551 Mon Sep 17 00:00:00 2001
From: Adam Strzelecki <ono@java.pl>
Date: Sun, 2 Nov 2014 12:58:17 +0100
Subject: [PATCH] CMakeProject: Set file target with most includes

This fixes regression 65c113bcbc124a0718444c7eb4238f94eddc7b78 that caused
files to be likely assigned to "all" target when all CMake targets were having
sources residing in same directory using Ninja generator. As "all" came first
then it became best match in such case.

This introduces slight modification, so target with most include paths is
chosen from these having best file system proximity to source file. Doing so we
select likely real target and get all #include preprocessor directives resolved
properly in the editor.

Change-Id: Ifb85bb5954b4cf5618a6d8444c993c69ebab2259
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
Reviewed-by: Stephen Kelly <steveire@gmail.com>
---
 src/plugins/cmakeprojectmanager/cmakeproject.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/plugins/cmakeprojectmanager/cmakeproject.cpp b/src/plugins/cmakeprojectmanager/cmakeproject.cpp
index 28b73b3616d..d19792f49f0 100644
--- a/src/plugins/cmakeprojectmanager/cmakeproject.cpp
+++ b/src/plugins/cmakeprojectmanager/cmakeproject.cpp
@@ -875,12 +875,16 @@ void CMakeCbpParser::sortFiles()
         } else {
             int bestLength = -1;
             int bestIndex = -1;
+            int bestIncludeCount = -1;
 
             for (int i = 0; i < m_buildTargets.size(); ++i) {
                 const CMakeBuildTarget &target = m_buildTargets.at(i);
-                if (fileName.isChildOf(Utils::FileName::fromString(target.sourceDirectory))
-                                       && target.sourceDirectory.size() > bestLength) {
+                if (fileName.isChildOf(Utils::FileName::fromString(target.sourceDirectory)) &&
+                    (target.sourceDirectory.size() > bestLength ||
+                     (target.sourceDirectory.size() == bestLength &&
+                      target.includeFiles.count() > bestIncludeCount))) {
                     bestLength = target.sourceDirectory.size();
+                    bestIncludeCount = target.includeFiles.count();
                     bestIndex = i;
                 }
             }
-- 
GitLab