~ubuntu-branches/ubuntu/utopic/kdevelop-php/utopic

« back to all changes in this revision

Viewing changes to duchain/navigation/magicconstantnavigationcontext.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2010-01-17 17:10:22 UTC
  • Revision ID: james.westby@ubuntu.com-20100117171022-q2xlgd9ekewo2ijx
Tags: upstream-1.0.0~beta2
ImportĀ upstreamĀ versionĀ 1.0.0~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright 2009 Milian Wolff <mail@milianw.de>
 
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 "magicconstantnavigationcontext.h"
 
20
 
 
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>
 
26
 
 
27
using namespace KDevelop;
 
28
 
 
29
namespace Php {
 
30
 
 
31
const int foo = 5;
 
32
 
 
33
MagicConstantNavigationContext::MagicConstantNavigationContext(TopDUContextPointer topContext,
 
34
                                                                const SimpleCursor& position,
 
35
                                                                const QString& constant)
 
36
    : AbstractNavigationContext(topContext, 0), m_position(position), m_constant(constant)
 
37
{
 
38
}
 
39
 
 
40
 
 
41
QString MagicConstantNavigationContext::name() const
 
42
{
 
43
    return m_constant;
 
44
}
 
45
 
 
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();
 
50
    }
 
51
    if ( !ctx || ctx->type() != type ) {
 
52
        return 0;
 
53
    } else {
 
54
        return ctx;
 
55
    }
 
56
}
 
57
 
 
58
QString MagicConstantNavigationContext::html(bool shorten)
 
59
{
 
60
    QString html = "<html><body><p><small><small>";
 
61
    html += typeHighlight(i18n("magic constant"));
 
62
    html += ' ';
 
63
    html += nameHighlight(Qt::escape(m_constant));
 
64
    html += "<br/>\n";
 
65
 
 
66
    QString value;
 
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()));
 
75
        } else {
 
76
            value = commentHighlight(i18n("empty (not inside a class)"));
 
77
        }
 
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() ) {
 
82
                break;
 
83
            }
 
84
            if ( ctx->parentContext()->type() == DUContext::Class ) {
 
85
                value = codeHighlight(Qt::escape(
 
86
                            ctx->parentContext()->localScopeIdentifier().toString() + "::"
 
87
                            + ctx->localScopeIdentifier().toString()
 
88
                        ));
 
89
                break;
 
90
            }
 
91
            // might be a "normal" function inside a method...
 
92
            pos = ctx->range().start;
 
93
        }
 
94
        if ( value.isEmpty() ) {
 
95
            value = commentHighlight(i18n("empty (not inside a method)"));
 
96
        }
 
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()));
 
102
            }
 
103
        }
 
104
        if ( value.isEmpty() ) {
 
105
            value = commentHighlight(i18n("empty (not inside a function)"));
 
106
        }
 
107
    }
 
108
 
 
109
    html += i18n("current value: %1", value);
 
110
 
 
111
    html += "</small></small></p></body></html>";
 
112
 
 
113
    return html;
 
114
}
 
115
 
 
116
 
 
117
}