File indexing completed on 2024-05-12 04:38:07

0001 /*
0002     SPDX-FileCopyrightText: 2001-2002 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
0003     SPDX-FileCopyrightText: 2001-2002 Bernd Gehrmann <bernd@kdevelop.org>
0004     SPDX-FileCopyrightText: 2001 Sandy Meier <smeier@kdevelop.org>
0005     SPDX-FileCopyrightText: 2002 Daniel Engelschalt <daniel.engelschalt@gmx.net>
0006     SPDX-FileCopyrightText: 2002 Simon Hausmann <hausmann@kde.org>
0007     SPDX-FileCopyrightText: 2002-2003 Roberto Raggi <roberto@kdevelop.org>
0008     SPDX-FileCopyrightText: 2003 Mario Scalas <mario.scalas@libero.it>
0009     SPDX-FileCopyrightText: 2003 Harald Fernengel <harry@kdevelop.org>
0010     SPDX-FileCopyrightText: 2003, 2006, 2008 Hamish Rodda <rodda@kde.org>
0011     SPDX-FileCopyrightText: 2004 Alexander Dymo <adymo@kdevelop.org>
0012     SPDX-FileCopyrightText: 2006 Adam Treat <treat@kde.org>
0013 
0014     SPDX-License-Identifier: LGPL-2.0-or-later
0015 */
0016 
0017 #include "codecontext.h"
0018 
0019 #include <duchain/declaration.h>
0020 #include <duchain/ducontext.h>
0021 #include <duchain/duchainlock.h>
0022 #include <duchain/duchain.h>
0023 #include <duchain/duchainutils.h>
0024 #include <duchain/use.h>
0025 
0026 #include <KTextEditor/Document>
0027 #include <KTextEditor/View>
0028 
0029 namespace KDevelop {
0030 class DUContextContextPrivate
0031 {
0032 public:
0033     explicit DUContextContextPrivate(const IndexedDUContext& item)
0034         : m_item(item)
0035     {}
0036 
0037     IndexedDUContext m_item;
0038 };
0039 
0040 DUContextContext::DUContextContext(const IndexedDUContext& item)
0041     : Context()
0042     , d_ptr(new DUContextContextPrivate(item))
0043 {}
0044 
0045 DUContextContext::~DUContextContext() = default;
0046 
0047 int DUContextContext::type() const
0048 {
0049     return Context::CodeContext;
0050 }
0051 
0052 QList<QUrl> DUContextContext::urls() const
0053 {
0054     Q_D(const DUContextContext);
0055 
0056     DUChainReadLocker lock;
0057     if (auto context = d->m_item.context()) {
0058         return {
0059                    context->url().toUrl()
0060         };
0061     }
0062     return {};
0063 }
0064 
0065 IndexedDUContext DUContextContext::context() const
0066 {
0067     Q_D(const DUContextContext);
0068 
0069     return d->m_item;
0070 }
0071 void DUContextContext::setContext(IndexedDUContext context)
0072 {
0073     Q_D(DUContextContext);
0074 
0075     d->m_item = context;
0076 }
0077 
0078 class DeclarationContextPrivate
0079 {
0080 public:
0081     DeclarationContextPrivate(const IndexedDeclaration& declaration, const DocumentRange& use)
0082         : m_declaration(declaration)
0083         , m_use(use)
0084     {}
0085 
0086     IndexedDeclaration m_declaration;
0087     DocumentRange m_use;
0088 };
0089 
0090 DeclarationContext::DeclarationContext(const IndexedDeclaration& declaration, const DocumentRange& use,
0091                                        const IndexedDUContext& context)
0092     : DUContextContext(context)
0093     , d_ptr(new DeclarationContextPrivate(declaration, use))
0094 {}
0095 
0096 DeclarationContext::DeclarationContext(KTextEditor::View* view, const KTextEditor::Cursor& position) : DUContextContext(
0097         IndexedDUContext())
0098 {
0099     const QUrl& url = view->document()->url();
0100     DUChainReadLocker lock;
0101     DUChainUtils::ItemUnderCursor item = DUChainUtils::itemUnderCursor(url, position);
0102     DocumentRange useRange = DocumentRange(IndexedString(url), item.range);
0103     Declaration* declaration = item.declaration;
0104     IndexedDeclaration indexed;
0105     if (declaration) {
0106         indexed = IndexedDeclaration(declaration);
0107     }
0108     d_ptr.reset(new DeclarationContextPrivate(declaration, useRange));
0109     setContext(IndexedDUContext(item.context));
0110 }
0111 
0112 DeclarationContext::~DeclarationContext() = default;
0113 
0114 int DeclarationContext::type() const
0115 {
0116     return Context::CodeContext;
0117 }
0118 
0119 IndexedDeclaration DeclarationContext::declaration() const
0120 {
0121     Q_D(const DeclarationContext);
0122 
0123     return d->m_declaration;
0124 }
0125 
0126 DocumentRange DeclarationContext::use() const
0127 {
0128     Q_D(const DeclarationContext);
0129 
0130     return d->m_use;
0131 }
0132 }