pythonhighlighting.cpp 3.08 KB
Newer Older
1 2
/*****************************************************************************
 * Copyright (c) 2007 Piyush verma <piyush.verma@gmail.com>                  *
3 4
 * Copyright (c) 2007 Andreas Pakulat <apaku@gmx.de>                         *
 * Copyright (c) 2011 Sven Brauch <svenbrauch@gmail.com>                     *
5
 *                                                                           *
6 7 8 9
 * This program is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU General Public License as            *
 * published by the Free Software Foundation; either version 2 of            *
 * the License, or (at your option) any later version.                       *
10
 *                                                                           *
11 12 13 14
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
15
 *                                                                           *
16 17 18 19
 * You should have received a copy of the GNU General Public License         *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
 *****************************************************************************
 */
20 21
#include "pythonhighlighting.h"

22 23 24 25 26
#include <language/duchain/declaration.h>
#include <language/duchain/types/abstracttype.h>

using namespace KDevelop;

27 28 29 30
namespace Python
{

Highlighting::Highlighting( QObject * parent )
31
       : KDevelop::CodeHighlighting(parent)
32
{
33 34 35 36 37
    
}

void CodeHighlightingInstance::highlightUse(KDevelop::DUContext* context, int index, const QColor& color)
{
38
    KDevelop::CodeHighlightingInstance::highlightUse(context, index, color);
39 40 41
}

CodeHighlightingInstance::CodeHighlightingInstance(const Highlighting* highlighting)
42 43 44
    : KDevelop::CodeHighlightingInstance(highlighting),
      checked_blocks(false),
      has_blocks(false)
45 46 47
{
}

48 49
bool CodeHighlightingInstance::useRainbowColor(KDevelop::Declaration* dec) const
{
50 51 52 53
    if (dec->context()->type() == DUContext::Other) {
        // Normal non-toplevel variable, comprehension variable or lambda parameter.
        return true;
    }
54 55 56 57 58 59 60
    if ( ! checked_blocks ) {
        checkHasBlocks(dec->topContext());
    }
    // no functions/classes in file and it's a normal variable and in the top level
    if ( ! has_blocks && ! dec->internalContext() && dec->context() == dec->topContext() ) {
        return true;
    }
61 62 63
    return KDevelop::CodeHighlightingInstance::useRainbowColor(dec);
}

64 65 66 67 68 69 70 71 72 73 74 75
void CodeHighlightingInstance::checkHasBlocks(TopDUContext* top) const
{
    QVector<Declaration*> declarations = top->localDeclarations();
    for ( int i = 0; i < declarations.size(); i++ ) {
        if ( declarations.at(i)->internalContext() ) {
           has_blocks = true;
           break;
        }
    }
    checked_blocks = true;
}

76 77 78
CodeHighlightingInstance* Highlighting::createInstance() const
{
    return new CodeHighlightingInstance(this);
79 80
}

81
}
82