2
Copyright 2009 Milian Wolff <mail@milianw.de>
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.
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.
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.
19
#include "magicconstantnavigationcontext.h"
21
#include <QtGui/QTextDocument>
22
#include <KLocalizedString>
23
#include <language/duchain/topducontext.h>
24
#include <language/duchain/declaration.h>
25
#include <language/duchain/types/functiontype.h>
27
using namespace KDevelop;
33
MagicConstantNavigationContext::MagicConstantNavigationContext(TopDUContextPointer topContext,
34
const SimpleCursor& position,
35
const QString& constant)
36
: AbstractNavigationContext(topContext, 0), m_position(position), m_constant(constant)
41
QString MagicConstantNavigationContext::name() const
46
DUContext* findContext(TopDUContextPointer topCtx, const SimpleCursor& pos, DUContext::ContextType type) {
47
DUContext* ctx = topCtx->findContextAt(pos);
48
while ( ctx && ctx->type() != type ) {
49
ctx = ctx->parentContext();
51
if ( !ctx || ctx->type() != type ) {
58
QString MagicConstantNavigationContext::html(bool shorten)
60
QString html = "<html><body><p><small><small>";
61
html += typeHighlight(i18n("magic constant"));
63
html += nameHighlight(Qt::escape(m_constant));
67
///TODO: php 5.3: __DIR__, __NAMESPACE__
68
if ( m_constant == "__FILE__" ) {
69
value = Qt::escape(m_topContext->url().str());
70
} else if ( m_constant == "__LINE__" ) {
71
value.setNum(m_position.line + 1);
72
} else if ( m_constant == "__CLASS__" ) {
73
if ( DUContext* ctx = findContext(m_topContext, m_position, DUContext::Class) ) {
74
value = codeHighlight(Qt::escape(ctx->localScopeIdentifier().toString()));
76
value = commentHighlight(i18n("empty (not inside a class)"));
78
} else if ( m_constant == "__METHOD__" ) {
79
SimpleCursor pos = m_position;
80
while ( DUContext* ctx = findContext(m_topContext, pos, DUContext::Other) ) {
81
if ( !ctx->parentContext() ) {
84
if ( ctx->parentContext()->type() == DUContext::Class ) {
85
value = codeHighlight(Qt::escape(
86
ctx->parentContext()->localScopeIdentifier().toString() + "::"
87
+ ctx->localScopeIdentifier().toString()
91
// might be a "normal" function inside a method...
92
pos = ctx->range().start;
94
if ( value.isEmpty() ) {
95
value = commentHighlight(i18n("empty (not inside a method)"));
97
} else if ( m_constant == "__FUNCTION__" ) {
98
SimpleCursor pos = m_position;
99
if ( DUContext* ctx = findContext(m_topContext, pos, DUContext::Other) ) {
100
if ( ctx->owner() && ctx->owner()->type<FunctionType>() ) {
101
value = codeHighlight(Qt::escape(ctx->localScopeIdentifier().toString()));
104
if ( value.isEmpty() ) {
105
value = commentHighlight(i18n("empty (not inside a function)"));
109
html += i18n("current value: %1", value);
111
html += "</small></small></p></body></html>";