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"
32 #include "topducontext.h"
34 #include "duchainlock.h"
35 #include "indexedstring.h"
37 using namespace KTextEditor
;
42 class SmartConverterPrivate
45 void convertDUChainInternal(const LockedSmartInterface
& iface
, DUContext
* context
, bool first
= false) const
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
)
88 SmartConverter::~SmartConverter()
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();
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();
125 d
->deconvertDUChainInternal(context
);
131 // kate: space-indent on; indent-width 2; tab-width 4; replace-tabs on; auto-insert-doxygen on