Add AbstractDeclarationNavigationContext, and move the html-method from
[kdevelopdvcssupport.git] / language / duchain / smartconverter.cpp
blob00950a3f8332fd2b5a876ffd75503b11477383eb
1 /*
2 * Copyright 2006 Hamish Rodda <rodda@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "smartconverter.h"
22 #include <ktexteditor/smartrange.h>
23 #include <ktexteditor/document.h>
24 #include <KTextEditor/SmartInterface>
26 #include "../editor/editorintegrator.h"
27 #include "../editor/hashedstring.h"
29 #include "ducontext.h"
30 #include "declaration.h"
31 #include "use.h"
32 #include "topducontext.h"
33 #include "duchain.h"
34 #include "duchainlock.h"
35 #include "indexedstring.h"
37 using namespace KTextEditor;
39 namespace KDevelop
42 class SmartConverterPrivate
44 public:
45 void convertDUChainInternal(const LockedSmartInterface& iface, DUContext* context, bool first = false) const
47 if (!first)
48 context->setSmartRange(m_editor->createRange(iface, context->range().textRange())->toSmartRange());
50 foreach (Declaration* dec, context->localDeclarations()) {
51 dec->setSmartRange(m_editor->createRange(iface, dec->range().textRange())->toSmartRange());
52 m_editor->exitCurrentRange(iface);
55 for(int a = 0; a < context->usesCount(); ++a) {
56 context->setUseSmartRange(a, m_editor->createRange(iface, context->uses()[a].m_range.textRange())->toSmartRange());
57 m_editor->exitCurrentRange(iface);
60 foreach (DUContext* child, context->childContexts())
61 convertDUChainInternal(iface, child);
63 m_editor->exitCurrentRange(iface);
66 void deconvertDUChainInternal(DUContext* context) const
68 foreach (Declaration* dec, context->localDeclarations())
69 dec->clearSmartRange();
71 context->clearUseSmartRanges();
73 foreach (DUContext* child, context->childContexts())
74 deconvertDUChainInternal(child);
76 context->clearSmartRange();
79 KDevelop::EditorIntegrator* m_editor;
82 SmartConverter::SmartConverter(KDevelop::EditorIntegrator* editor)
83 : d(new SmartConverterPrivate)
85 d->m_editor = editor;
88 SmartConverter::~SmartConverter()
90 delete d;
93 void SmartConverter::convertDUChain(DUContext* context) const
95 if(context->inDUChain()) {
96 ENSURE_CHAIN_WRITE_LOCKED
99 d->m_editor->setCurrentUrl( context->url(), true );
101 LockedSmartInterface iface = d->m_editor->smart();
102 Q_ASSERT(iface);
103 if (iface && !context->smartRange()) {
104 context->setSmartRange(d->m_editor->topRange(iface, KDevelop::EditorIntegrator::DefinitionUseChain)->toSmartRange());
105 if (context->range().textRange() != iface.currentDocument()->documentRange())
106 kWarning() << "Context range to be converted" << context->range().textRange() << "does not match the document range" << iface.currentDocument()->documentRange();
107 //Q_ASSERT(context->range().textRange() == iface.currentDocument()->documentRange());
108 Q_ASSERT(context->smartRange() && !context->smartRange()->parentRange() && context->smartRange()->childRanges().isEmpty());
110 d->convertDUChainInternal(iface, context, true);
111 d->m_editor->exitCurrentRange(iface); //topRange(..) opens a range
115 void SmartConverter::deconvertDUChain(DUContext* context) const
117 if(context->inDUChain()) {
118 ENSURE_CHAIN_WRITE_LOCKED
121 d->m_editor->setCurrentUrl( IndexedString(context->url().str()), true );
123 LockedSmartInterface iface = d->m_editor->smart();
124 if (iface) {
125 d->deconvertDUChainInternal(context);
131 // kate: space-indent on; indent-width 2; tab-width 4; replace-tabs on; auto-insert-doxygen on