[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdevelop] languages/cpp/codecompletion: Introduce new technique to lookup forward headers
From:       Kevin Funk <kevin () kfunk ! org>
Date:       2014-01-31 13:36:10
Message-ID: E1W9EGQ-0007rn-JW () scm ! kde ! org
[Download RAW message or body]

Git commit 30181d739a334f7a3a2b2f87afbb520db13114fd by Kevin Funk.
Committed on 31/01/2014 at 13:13.
Pushed by kfunk into branch 'master'.

Introduce new technique to lookup forward headers

We are now able to propose headers in the missing-include assistant,
which were not parsed before (i.e. that are not in the DUChain).

The procedure is simple: Assume we write something like 'QState',
and qstate.h is not yet included nor parsed, in some file foo.cpp.
KDevelop then checks if there is a file called 'QState' in
any of the include paths required to build foo.cpp and then adds it
to the list of possible include directives.

This is possibly useful in other areas, e.g. for the "Create From
Template" feature, as well.

BUG: 287492

M  +41   -24   languages/cpp/codecompletion/missingincludeitem.cpp

http://commits.kde.org/kdevelop/30181d739a334f7a3a2b2f87afbb520db13114fd

diff --git a/languages/cpp/codecompletion/missingincludeitem.cpp \
b/languages/cpp/codecompletion/missingincludeitem.cpp index 38ce060..72b1cd2 100644
--- a/languages/cpp/codecompletion/missingincludeitem.cpp
+++ b/languages/cpp/codecompletion/missingincludeitem.cpp
@@ -17,6 +17,7 @@
 */
 
 #include "missingincludeitem.h"
+
 #include <language/duchain/namespacealiasdeclaration.h>
 #include <language/duchain/persistentsymboltable.h>
 #include <language/duchain/types/abstracttype.h>
@@ -208,6 +209,25 @@ QStringList candidateIncludeFiles(Declaration* decl) {
   return ret;
 }
 
+/**
+ * Try to find include candidates based solely on the string of the unknown id @p id
+ *
+ * Example: We have 'QState' in our source file, it is unknown
+ * This method then looks through the include paths used by @p source and returns \
all + * files matching the file name 'QState'
+ */
+QStringList candidateIncludeFilesFromNameMatcher(const QString& source, const \
QualifiedIdentifier& id) +{
+  const QList<IncludeItem> includeItems = CppUtils::allFilesInIncludePath(source, \
false, QString()); +  QStringList result;
+  for (const IncludeItem& item : includeItems) {
+    if (item.name == id.toString() && !isBlacklistedInclude(item.url())) {
+      result << item.url().toLocalFile();
+    }
+  }
+  return result;
+}
+
 KSharedPtr<MissingIncludeCompletionItem> includeDirectiveFromUrl(const KUrl& \
fromUrl, const IndexedDeclaration& decl) {  KSharedPtr<MissingIncludeCompletionItem> \
item;  if(decl.data()) {
@@ -290,7 +310,7 @@ QList<KDevelop::CompletionTreeItemPointer> \
missingIncludeCompletionItems(const Q  
   KUrl currentUrl(context->topContext()->url().str());
   const auto currentPath = Path(currentUrl).parent();
-  
+
   Cpp::EnvironmentFilePointer \
env(dynamic_cast<Cpp::EnvironmentFile*>(context->topContext()->parsingEnvironmentFile().data()));
  if(!env)
     return ret;
@@ -346,30 +366,25 @@ QList<KDevelop::CompletionTreeItemPointer> \
missingIncludeCompletionItems(const Q  QString \
file(decl->url().toUrl().toLocalFile());  
         bool inBlacklistDir = isBlacklistedInclude(decl->url().toUrl());
-        
-        foreach(KDevelop::ParsingEnvironmentFilePointer ptr, \
                decl->topContext()->parsingEnvironmentFile()->importers()) {
-          if(ptr->imports().count() == 1 || inBlacklistDir) {
-            if(isBlacklistedInclude(ptr->url().toUrl()))
-              continue;
-            //This file is a forwader, add it to the list
-
-            //Forwarders must be completely empty
-            if(ptr->topContext()->localDeclarations().count())
-              continue;
-            
-            QString file(ptr->url().toUrl().toLocalFile());
-            ret += itemsForFile(displayTextPrefix, file, includePaths, currentPath, \
                decl, argumentHintDepth, directives);
-          }
+        auto candidateFiles = candidateIncludeFiles(decl);
+        kDebug() << "candidates from DUChain:" << candidateFiles;
+        for (const QString& file : candidateFiles) {
+          ret += itemsForFile(displayTextPrefix, file, includePaths, currentPath, \
decl, argumentHintDepth, directives);  }
-        
-        if(inBlacklistDir)
+
+        if (inBlacklistDir) {
           blacklistRet += itemsForFile(displayTextPrefix, file, includePaths, \
                currentPath, decl, argumentHintDepth, directives);
-        else
-          ret += itemsForFile(displayTextPrefix, file, includePaths, currentPath, \
decl, argumentHintDepth, directives); +        }
       }
     }
   }
 
+  auto candidateFiles = \
candidateIncludeFilesFromNameMatcher(currentUrl.toLocalFile(), identifier); +  \
kDebug() << "candidates from name matching:" << candidateFiles; +  for (const \
QString& file : candidateFiles) { +    ret += itemsForFile(displayTextPrefix, file, \
includePaths, currentPath, IndexedDeclaration(), argumentHintDepth, directives); +  }
+
   if(ret.isEmpty())
     ret += blacklistRet;
   
@@ -442,13 +457,15 @@ QVariant MissingIncludeCompletionItem::data(const QModelIndex& \
index, int role,  case KTextEditor::CodeCompletionModel::Prefix:
             return i18n("Add include directive");
         case KTextEditor::CodeCompletionModel::Name: {
-          QString suffix = ", #include " + m_addedInclude;
+          const QString suffix = "#include " + m_addedInclude;
+          // note: m_displayTextPrefix already contains the spaces on the right
           if(!m_decl.data())
-            return QString(m_displayTextPrefix + suffix);
+            return i18nc("file content unknown", "%1<unknown contents>, %2", \
m_displayTextPrefix, suffix);  else if(m_decl.data()->kind() == \
                Declaration::Namespace)
-            return QString(m_displayTextPrefix + " namespace " + \
                m_decl.data()->identifier().toString()  + suffix);
-          else
-            return QString(m_displayTextPrefix + m_decl.data()->toString() + \
suffix); +            return QString("%1namespace %2, %3").arg(m_displayTextPrefix, \
m_decl.data()->identifier().toString(), suffix); +          else {
+            return QString("%1%2, %3").arg(m_displayTextPrefix, \
m_decl.data()->toString()).arg(suffix); +          }
         }
       }
       break;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic