• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevplatform/language/duchain

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • language
  • duchain
  • navigation
abstractincludenavigationcontext.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2007 David Nolden <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17  */
18 
19 #include "abstractincludenavigationcontext.h"
20 
21 #include <KLocalizedString>
22 
23 #include <language/duchain/duchain.h>
24 #include <language/duchain/parsingenvironment.h>
25 #include <language/duchain/declaration.h>
26 #include <language/duchain/duchainlock.h>
27 
28 namespace KDevelop {
29 AbstractIncludeNavigationContext::AbstractIncludeNavigationContext(const IncludeItem& item,
30  const TopDUContextPointer& topContext,
31  const ParsingEnvironmentType& type)
32  : AbstractNavigationContext(topContext)
33  , m_type(type)
34  , m_item(item)
35 {}
36 
37 TopDUContext* pickContextWithData(const QList<TopDUContext*>& duchains, uint maxDepth,
38  const ParsingEnvironmentType& type,
39  bool forcePick = true)
40 {
41  TopDUContext* duchain = nullptr;
42 
43  for (TopDUContext* ctx : duchains) {
44  if (!ctx->parsingEnvironmentFile() || ctx->parsingEnvironmentFile()->type() != type)
45  continue;
46 
47  if (ctx->childContexts().count() != 0
48  && (duchain == nullptr || ctx->childContexts().count() > duchain->childContexts().count())) {
49  duchain = ctx;
50  }
51  if (ctx->localDeclarations().count() != 0
52  && (duchain == nullptr || ctx->localDeclarations().count() > duchain->localDeclarations().count())) {
53  duchain = ctx;
54  }
55  }
56 
57  if (!duchain && maxDepth != 0) {
58  if (maxDepth != 0) {
59  for (TopDUContext* ctx : duchains) {
60  QList<TopDUContext*> children;
61  const auto importedParentContexts = ctx->importedParentContexts();
62  for (const DUContext::Import& import : importedParentContexts) {
63  if (import.context(nullptr))
64  children << import.context(nullptr)->topContext();
65  }
66 
67  duchain = pickContextWithData(children, maxDepth - 1, type, false);
68  if (duchain)
69  break;
70  }
71  }
72  }
73 
74  if (!duchain && !duchains.isEmpty() && forcePick)
75  duchain = duchains.first();
76 
77  return duchain;
78 }
79 
80 QString AbstractIncludeNavigationContext::html(bool shorten)
81 {
82  clear();
83  modifyHtml() += QStringLiteral("<html><body><p>");
84 
85  QUrl u = m_item.url();
86  NavigationAction action(u, KTextEditor::Cursor(0, 0));
87  makeLink(u.toDisplayString(QUrl::PreferLocalFile), u.toString(), action);
88  modifyHtml() += QStringLiteral("<br />");
89 
90  DUChainReadLocker lock;
91  QList<TopDUContext*> duchains = DUChain::self()->chainsForDocument(u);
92  //Pick the one duchain for this document that has the most child-contexts/declarations.
93  //This prevents picking a context that is empty due to header-guards.
94  TopDUContext* duchain = pickContextWithData(duchains, 2, m_type);
95 
96  if (duchain) {
97  getFileInfo(duchain);
98  if (!shorten) {
99  modifyHtml() += labelHighlight(i18n("Declarations:")) + QLatin1String("<br />");
100  bool first = true;
101  QVector<IdentifierPair> decs;
102  addDeclarationsFromContext(duchain, first, decs);
103  }
104  } else if (duchains.isEmpty()) {
105  modifyHtml() += i18n("not parsed yet");
106  }
107 
108  modifyHtml() += QStringLiteral("</p></body></html>");
109  return currentHtml();
110 }
111 
112 void AbstractIncludeNavigationContext::getFileInfo(TopDUContext* duchain)
113 {
114  modifyHtml() +=
115  QStringLiteral("%1: %2 %3: %4").arg(labelHighlight(i18nc("Files included into this file", "Includes"))).arg(
116  duchain->importedParentContexts().count()).arg(labelHighlight(i18nc(
117  "Count of files this file was included into",
118  "Included by"))).arg(
119  duchain->importers().count());
120  modifyHtml() += QStringLiteral("<br />");
121 }
122 
123 QString AbstractIncludeNavigationContext::name() const
124 {
125  return m_item.name;
126 }
127 
128 bool AbstractIncludeNavigationContext::filterDeclaration(Declaration* /*decl*/)
129 {
130  return true;
131 }
132 
133 void AbstractIncludeNavigationContext::addDeclarationsFromContext(KDevelop::DUContext* ctx, bool& first,
134  QVector<IdentifierPair>& addedDeclarations,
135  const QString& indent)
136 {
137  //modifyHtml() += indent + ctx->localScopeIdentifier().toString() + "{<br />";
138  QVector<DUContext*> children = ctx->childContexts();
139  QVector<Declaration*> declarations = ctx->localDeclarations();
140 
141  QVector<DUContext*>::const_iterator childIterator = children.constBegin();
142  QVector<Declaration*>::const_iterator declarationIterator = declarations.constBegin();
143 
144  while (childIterator != children.constEnd() || declarationIterator != declarations.constEnd()) {
145  //Show declarations/contexts in the order they appear in the file
146  int currentDeclarationLine = -1;
147  int currentContextLine = -1;
148  if (declarationIterator != declarations.constEnd())
149  currentDeclarationLine = (*declarationIterator)->rangeInCurrentRevision().start().line();
150 
151  if (childIterator != children.constEnd())
152  currentDeclarationLine = (*childIterator)->rangeInCurrentRevision().start().line();
153 
154  if ((currentDeclarationLine <= currentContextLine || currentContextLine == -1 ||
155  childIterator == children.constEnd()) && declarationIterator != declarations.constEnd()) {
156  IdentifierPair id = qMakePair(static_cast<int>((*declarationIterator)->kind()),
157  (*declarationIterator)->qualifiedIdentifier().index());
158  if (!addedDeclarations.contains(id) && filterDeclaration(*declarationIterator)) {
159  //Show the declaration
160  if (!first)
161  modifyHtml() += QStringLiteral(", ");
162  else
163  first = false;
164 
165  modifyHtml() += QString(indent + declarationKind(DeclarationPointer(
166  *declarationIterator)) +
167  QLatin1Char(' ')).toHtmlEscaped();
168  makeLink((*declarationIterator)->qualifiedIdentifier().toString(),
169  DeclarationPointer(*declarationIterator), NavigationAction::NavigateDeclaration);
170 
171  addedDeclarations << id;
172  }
173  ++declarationIterator;
174  } else {
175  //Eventually Recurse into the context
176  if ((*childIterator)->type() == DUContext::Global ||
177  (*childIterator)->type() == DUContext::Namespace /*|| (*childIterator)->type() == DUContext::Class*/)
178  addDeclarationsFromContext(*childIterator, first, addedDeclarations, indent + QLatin1Char(' '));
179  ++childIterator;
180  }
181  }
182  //modifyHtml() += "}<br />";
183 }
184 }
duchainlock.h
KDevelop::DUChainReadLocker
Customized read locker for the definition-use chain.
Definition: duchainlock.h:114
KDevelop::DUContext::Global
A context that declares functions, namespaces or classes.
Definition: ducontext.h:103
KDevelop::AbstractNavigationContext::currentHtml
QString currentHtml() const
Returns the html text being built in its current state.
Definition: abstractnavigationcontext.cpp:587
QUrl
KDevelop::AbstractNavigationContext::declarationKind
virtual QString declarationKind(const DeclarationPointer &decl)
Definition: abstractnavigationcontext.cpp:497
KDevelop::AbstractNavigationContext::labelHighlight
static const Colorizer labelHighlight
Definition: abstractnavigationcontext.h:164
KDevelop::TopDUContext::importedParentContexts
QVector< Import > importedParentContexts() const override
Returns the list of imported parent contexts for this context.
Definition: topducontext.cpp:1024
QVector::constEnd
const_iterator constEnd() const
KDevelop::DUChainPointer< TopDUContext >
KDevelop::pickContextWithData
TopDUContext * pickContextWithData(const QList< TopDUContext * > &duchains, uint maxDepth, const ParsingEnvironmentType &type, bool forcePick=true)
Definition: abstractincludenavigationcontext.cpp:37
QList
KDevelop::Declaration
Represents a single declaration in a definition-use chain.
Definition: declaration.h:51
QUrl::toString
QString toString(QFlags< QUrl::FormattingOption > options) const
KDevelop::AbstractNavigationContext::makeLink
virtual void makeLink(const QString &name, const DeclarationPointer &declaration, NavigationAction::Type actionType)
Creates and registers a link to the given declaration, labeled by the given name.
Definition: abstractnavigationcontext.cpp:93
KDevelop::AbstractIncludeNavigationContext::html
QString html(bool shorten) override
Here the context can return html to be displayed.
Definition: abstractincludenavigationcontext.cpp:80
declaration.h
KDevelop::AbstractIncludeNavigationContext::AbstractIncludeNavigationContext
AbstractIncludeNavigationContext(const IncludeItem &item, const TopDUContextPointer &topContext, const ParsingEnvironmentType &type)
Definition: abstractincludenavigationcontext.cpp:29
KDevelop::NavigationAction
Definition: navigationaction.h:31
KDevelop::TopDUContext::importers
QVector< DUContext * > importers() const override
Returns the list of contexts importing this context.
Definition: topducontext.cpp:1007
KDevelop::AbstractIncludeNavigationContext::name
QString name() const override
Definition: abstractincludenavigationcontext.cpp:123
QString
KDevelop::AbstractIncludeNavigationContext::filterDeclaration
virtual bool filterDeclaration(Declaration *decl)
Should return true if this declaration should be shown, and false if not The duchain is locked when t...
Definition: abstractincludenavigationcontext.cpp:128
KDevelop::AbstractNavigationContext::clear
void clear()
Definition: abstractnavigationcontext.cpp:146
KDevelop::AbstractNavigationContext::modifyHtml
TextHandler modifyHtml()
Returns a convenience object that allows writing "modifyHtml() += "Hallo";".
Definition: abstractnavigationcontext.h:139
KDevelop::TopDUContext
The top context in a definition-use chain for one source file.
Definition: topducontext.h:113
QVector::contains
bool contains(const T &value) const
QList::isEmpty
bool isEmpty() const
QLatin1String
KDevelop::DUContext::childContexts
QVector< DUContext * > childContexts() const
Returns the list of immediate child contexts for this context.
Definition: ducontext.cpp:479
KDevelop::NavigationAction::NavigateDeclaration
Definition: navigationaction.h:35
KDevelop::AbstractNavigationContext
Definition: abstractnavigationcontext.h:64
abstractincludenavigationcontext.h
QLatin1Char
duchain.h
KDevelop
Definition: abstractfunctiondeclaration.cpp:27
KDevelop::DUContext
A single context in source code, represented as a node in a directed acyclic graph.
Definition: ducontext.h:72
KDevelop::DUContext::Namespace
A context that declares namespace members.
Definition: ducontext.h:104
parsingenvironment.h
QVector
KDevelop::AbstractIncludeNavigationContext::getFileInfo
virtual void getFileInfo(KDevelop::TopDUContext *duchain)
Overwrite this to add language dependent information for a given file.
Definition: abstractincludenavigationcontext.cpp:112
QVector::constBegin
const_iterator constBegin() const
KDevelop::DUContext::localDeclarations
virtual QVector< Declaration * > localDeclarations(const TopDUContext *source=nullptr) const
Returns all local declarations.
Definition: ducontext.cpp:967
KDevelop::DeclarationPointer
DUChainPointer< Declaration > DeclarationPointer
Definition: duchainpointer.h:200
KDevelop::DUChain::chainsForDocument
QList< TopDUContext * > chainsForDocument(const QUrl &document) const
Return all chains for the given document that are currently in memory.
Definition: duchain.cpp:1459
QObject::children
const QObjectList & children() const
KDevelop::ParsingEnvironmentType
ParsingEnvironmentType
Just an enumeration of a few parsing-environment types.
Definition: parsingenvironment.h:43
KDevelop::DUChain::self
static DUChain * self()
Returns the global static instance.
Definition: duchain.cpp:1230
KDevelop::DUContext::Import
Represents an imported parent context.
Definition: ducontext.h:256
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Mar 3 2021 00:37:28 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/language/duchain

Skip menu "kdevplatform/language/duchain"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  • kdevplatform
  •   debugger
  •   documentation
  •   interfaces
  •   language
  •     assistant
  •     backgroundparser
  •     checks
  •     classmodel
  •     codecompletion
  •     codegen
  •     duchain
  •     editor
  •     highlighting
  •     interfaces
  •     util
  •   outputview
  •   project
  •   serialization
  •   shell
  •   sublime
  •   tests
  •   util
  •   vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal