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

« back to all changes in this revision

Viewing changes to phphighlighting.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2010-05-18 12:21:48 UTC
  • mto: (5.1.1 squeeze) (1.1.5)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20100518122148-04mq5800fnjfbs48
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    <one line to give the program's name and a brief idea of what it does.>
 
3
    Copyright (C) <year>  <name of author>
 
4
 
 
5
    This program is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU General Public License as published by
 
7
    the Free Software Foundation, either version 3 of the License, or
 
8
    (at your option) any later version.
 
9
 
 
10
    This program is distributed in the hope that it will be useful,
 
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU General Public License
 
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
*/
 
19
 
 
20
#include "phphighlighting.h"
 
21
#include <language/duchain/declaration.h>
 
22
#include <language/duchain/use.h>
 
23
#include <language/highlighting/colorcache.h>
 
24
#include <language/highlighting/configurablecolors.h>
 
25
 
 
26
#include <KTextEditor/SmartRange>
 
27
#include <KTextEditor/SmartInterface>
 
28
#include <KTextEditor/Document>
 
29
 
 
30
using namespace KDevelop;
 
31
using namespace Php;
 
32
using namespace KTextEditor;
 
33
 
 
34
#define LOCK_SMART(range) KTextEditor::SmartInterface* iface = dynamic_cast<KTextEditor::SmartInterface*>(range->document()); QMutexLocker lock(iface ? iface->smartMutex() : 0);
 
35
 
 
36
class HighlightingInstance : public CodeHighlightingInstance {
 
37
public:
 
38
    HighlightingInstance(const CodeHighlighting* highlighting);
 
39
    virtual void highlightDeclaration(Declaration* declaration, const QColor& color) const;
 
40
    virtual void highlightUse(DUContext* context, int index, const QColor& color) const;
 
41
 
 
42
};
 
43
 
 
44
HighlightingInstance::HighlightingInstance(const CodeHighlighting* highlighting)
 
45
    : CodeHighlightingInstance(highlighting)
 
46
{
 
47
}
 
48
 
 
49
bool isConstDeclaration(Declaration* decl) {
 
50
    return decl && !decl->isFunctionDeclaration() &&
 
51
           decl->abstractType() && decl->abstractType()->modifiers() & AbstractType::ConstModifier;
 
52
}
 
53
 
 
54
///TODO: make this easier by rewriting some parts in the KDevplatform CodeHighlightingInstance
 
55
///      essentially we'd need to overwrite TypeForDeclaration
 
56
void HighlightingInstance::highlightDeclaration(Declaration* declaration, const QColor& color) const
 
57
{
 
58
    if (isConstDeclaration(declaration)) {
 
59
        // highlight const class members and defines differently from normal variables
 
60
        if (SmartRange* range = declaration->smartRange()) {
 
61
            LOCK_SMART(range);
 
62
            range->setAttribute(m_highlighting->attributeForType(EnumType, DeclarationContext, color));
 
63
        }
 
64
    } else {
 
65
        CodeHighlightingInstance::highlightDeclaration(declaration, color);
 
66
    }
 
67
}
 
68
 
 
69
///TODO: make this easier by rewriting some parts in the KDevplatform CodeHighlightingInstance
 
70
///      essentially we'd need to overwrite TypeForDeclaration
 
71
void HighlightingInstance::highlightUse(DUContext* context, int index, const QColor& color) const
 
72
{
 
73
    if (SmartRange* range = context->useSmartRange(index)) {
 
74
        Declaration* decl = context->topContext()->usedDeclarationForIndex(context->uses()[index].m_declarationIndex);
 
75
        if (isConstDeclaration(decl)) {
 
76
            LOCK_SMART(range);
 
77
            range->setAttribute(m_highlighting->attributeForType(EnumType, ReferenceContext, color));
 
78
            return;
 
79
        }
 
80
    }
 
81
    KDevelop::CodeHighlightingInstance::highlightUse(context, index, color);
 
82
}
 
83
 
 
84
 
 
85
Highlighting::Highlighting(QObject* parent)
 
86
    : CodeHighlighting(parent)
 
87
{
 
88
 
 
89
}
 
90
 
 
91
CodeHighlightingInstance* Highlighting::createInstance() const
 
92
{
 
93
    return new HighlightingInstance(this);
 
94
}