File indexing completed on 2024-04-21 15:25:28

0001 /*
0002     SPDX-FileCopyrightText: 2007 Piyush verma <piyush.verma@gmail.com>
0003     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0004     SPDX-FileCopyrightText: 2011 Sven Brauch <svenbrauch@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "pythonhighlighting.h"
0010 
0011 #include <language/duchain/declaration.h>
0012 #include <language/duchain/types/abstracttype.h>
0013 
0014 using namespace KDevelop;
0015 
0016 namespace Python
0017 {
0018 
0019 Highlighting::Highlighting( QObject * parent )
0020        : KDevelop::CodeHighlighting(parent)
0021 {
0022     
0023 }
0024 
0025 void CodeHighlightingInstance::highlightUse(KDevelop::DUContext* context, int index, const QColor& color)
0026 {
0027     KDevelop::CodeHighlightingInstance::highlightUse(context, index, color);
0028 }
0029 
0030 CodeHighlightingInstance::CodeHighlightingInstance(const Highlighting* highlighting)
0031     : KDevelop::CodeHighlightingInstance(highlighting),
0032       checked_blocks(false),
0033       has_blocks(false)
0034 {
0035 }
0036 
0037 bool CodeHighlightingInstance::useRainbowColor(KDevelop::Declaration* dec) const
0038 {
0039     if (dec->context()->type() == DUContext::Other) {
0040         // Normal non-toplevel variable, comprehension variable or lambda parameter.
0041         return true;
0042     }
0043     if ( ! checked_blocks ) {
0044         checkHasBlocks(dec->topContext());
0045     }
0046     // no functions/classes in file and it's a normal variable and in the top level
0047     if ( ! has_blocks && ! dec->internalContext() && dec->context() == dec->topContext() ) {
0048         return true;
0049     }
0050     return KDevelop::CodeHighlightingInstance::useRainbowColor(dec);
0051 }
0052 
0053 void CodeHighlightingInstance::checkHasBlocks(TopDUContext* top) const
0054 {
0055     QVector<Declaration*> declarations = top->localDeclarations();
0056     for ( int i = 0; i < declarations.size(); i++ ) {
0057         if ( declarations.at(i)->internalContext() ) {
0058            has_blocks = true;
0059            break;
0060         }
0061     }
0062     checked_blocks = true;
0063 }
0064 
0065 CodeHighlightingInstance* Highlighting::createInstance() const
0066 {
0067     return new CodeHighlightingInstance(this);
0068 }
0069 
0070 }
0071 
0072 #include "moc_pythonhighlighting.cpp"