Commit be873c43 authored by Milian Wolff's avatar Milian Wolff
Browse files

Cleanup: Remove HighlightingEnumContainer and use enum classes instead

This was all pretty messy imo - we can just use enum classes instead
to scope the code highlighting types and contexts. While at it, also
use the new type then directly for the ConfigurableHighlightingColors
instead of using int there.
parent d9c11016
......@@ -73,20 +73,22 @@ void CodeHighlighting::adaptToColorChanges()
m_referenceAttributes.clear();
}
KTextEditor::Attribute::Ptr CodeHighlighting::attributeForType(Types type, Contexts context, const QColor& color) const
KTextEditor::Attribute::Ptr CodeHighlighting::attributeForType(CodeHighlightingType type,
CodeHighlightingContext context,
const QColor& color) const
{
QMutexLocker lock(&m_dataMutex);
KTextEditor::Attribute::Ptr a;
switch (context) {
case DefinitionContext:
case CodeHighlightingContext::Definition:
a = m_definitionAttributes[type];
break;
case DeclarationContext:
case CodeHighlightingContext::Declaration:
a = m_declarationAttributes[type];
break;
case ReferenceContext:
case CodeHighlightingContext::Reference:
a = m_referenceAttributes[type];
break;
}
......@@ -95,7 +97,7 @@ KTextEditor::Attribute::Ptr CodeHighlighting::attributeForType(Types type, Conte
a = KTextEditor::Attribute::Ptr(new KTextEditor::Attribute(*ColorCache::self()->defaultColors()->attribute(
type)));
if (context == DefinitionContext || context == DeclarationContext) {
if (context == CodeHighlightingContext::Definition || context == CodeHighlightingContext::Declaration) {
if (ICore::self()->languageController()->completionSettings()->boldDeclarations()) {
a->setFontBold();
}
......@@ -106,13 +108,13 @@ KTextEditor::Attribute::Ptr CodeHighlighting::attributeForType(Types type, Conte
// a->setBackground(QColor(mix(0xffffff-color, backgroundColor(), 255-backgroundTinting)));
} else {
switch (context) {
case DefinitionContext:
case CodeHighlightingContext::Definition:
m_definitionAttributes.insert(type, a);
break;
case DeclarationContext:
case CodeHighlightingContext::Declaration:
m_declarationAttributes.insert(type, a);
break;
case ReferenceContext:
case CodeHighlightingContext::Reference:
m_referenceAttributes.insert(type, a);
break;
}
......@@ -373,7 +375,7 @@ KDevelop::Declaration* CodeHighlightingInstance::localClassFromCodeContext(KDeve
return decl;
}
CodeHighlightingInstance::Types CodeHighlightingInstance::typeForDeclaration(Declaration* dec, DUContext* context) const
CodeHighlightingType CodeHighlightingInstance::typeForDeclaration(Declaration* dec, DUContext* context) const
{
/**
* We highlight in 3 steps by priority:
......@@ -388,14 +390,14 @@ CodeHighlightingInstance::Types CodeHighlightingInstance::typeForDeclaration(Dec
// return ErrorVariableType;
if (!dec)
return ErrorVariableType;
return CodeHighlightingType::ErrorVariable;
Types type = LocalVariableType;
auto type = CodeHighlightingType::LocalVariable;
if (dec->kind() == Declaration::Namespace)
return NamespaceType;
return CodeHighlightingType::Namespace;
if (dec->kind() == Declaration::Macro) {
return MacroType;
return CodeHighlightingType::Macro;
}
if (context && dec->context() && dec->context()->type() == DUContext::Class) {
......@@ -404,43 +406,43 @@ CodeHighlightingInstance::Types CodeHighlightingInstance::typeForDeclaration(Dec
Declaration* klass = localClassFromCodeContext(context);
if (klass) {
if (klass->internalContext() == dec->context())
type = LocalClassMemberType; //Using Member of the local class
type = CodeHighlightingType::LocalClassMember; // Using Member of the local class
else if (klass->internalContext() && klass->internalContext()->imports(dec->context()))
type = InheritedClassMemberType; //Using Member of an inherited class
type = CodeHighlightingType::InheritedClassMember; // Using Member of an inherited class
}
}
if (type == LocalVariableType) {
if (type == CodeHighlightingType::LocalVariable) {
if (dec->kind() == Declaration::Type || dec->type<KDevelop::FunctionType>() ||
dec->type<KDevelop::EnumeratorType>()) {
if (dec->isForwardDeclaration())
type = ForwardDeclarationType;
type = CodeHighlightingType::ForwardDeclaration;
else if (dec->type<KDevelop::FunctionType>())
type = FunctionType;
type = CodeHighlightingType::Function;
else if (dec->type<StructureType>())
type = ClassType;
type = CodeHighlightingType::Class;
else if (dec->type<KDevelop::TypeAliasType>())
type = TypeAliasType;
type = CodeHighlightingType::TypeAlias;
else if (dec->type<EnumerationType>())
type = EnumType;
type = CodeHighlightingType::Enum;
else if (dec->type<KDevelop::EnumeratorType>())
type = EnumeratorType;
type = CodeHighlightingType::Enumerator;
}
}
if (type == LocalVariableType) {
if (type == CodeHighlightingType::LocalVariable) {
switch (dec->context()->type()) {
case DUContext::Namespace:
type = NamespaceVariableType;
type = CodeHighlightingType::NamespaceVariable;
break;
case DUContext::Class:
type = MemberVariableType;
type = CodeHighlightingType::MemberVariable;
break;
case DUContext::Function:
type = FunctionVariableType;
type = CodeHighlightingType::FunctionVariable;
break;
case DUContext::Global:
type = GlobalVariableType;
type = CodeHighlightingType::GlobalVariable;
break;
default:
break;
......@@ -460,22 +462,23 @@ void CodeHighlightingInstance::highlightDeclaration(Declaration* declaration, co
{
HighlightedRange h;
h.range = declaration->range();
h.attribute = m_highlighting->attributeForType(typeForDeclaration(declaration, nullptr), DeclarationContext, color);
h.attribute = m_highlighting->attributeForType(typeForDeclaration(declaration, nullptr),
CodeHighlightingContext::Declaration, color);
m_highlight.push_back(h);
}
void CodeHighlightingInstance::highlightUse(DUContext* context, int index, const QColor& color)
{
Types type = ErrorVariableType;
auto type = CodeHighlightingType::ErrorVariable;
Declaration* decl = context->topContext()->usedDeclarationForIndex(context->uses()[index].m_declarationIndex);
type = typeForDeclaration(decl, context);
if (type != ErrorVariableType ||
ICore::self()->languageController()->completionSettings()->highlightSemanticProblems()) {
if (type != CodeHighlightingType::ErrorVariable
|| ICore::self()->languageController()->completionSettings()->highlightSemanticProblems()) {
HighlightedRange h;
h.range = context->uses()[index].m_range;
h.attribute = m_highlighting->attributeForType(type, ReferenceContext, color);
h.attribute = m_highlighting->attributeForType(type, CodeHighlightingContext::Reference, color);
m_highlight.push_back(h);
}
}
......
......@@ -29,47 +29,49 @@ using ColorMap = QVector<KDevelop::Declaration*>;
class CodeHighlighting;
struct HighlightingEnumContainer
enum class CodeHighlightingType {
Unknown,
// Primary highlighting:
LocalClassMember,
InheritedClassMember,
LocalVariable,
// Other highlighting:
Class,
Function,
ForwardDeclaration,
Enum,
Enumerator,
TypeAlias,
Macro, /// Declaration of a macro such as "#define FOO"
MacroFunctionLike, /// Declaration of a function like macro such as "#define FOO()"
// If none of the above match:
MemberVariable,
NamespaceVariable,
GlobalVariable,
// Most of these are currently not used:
Argument,
Code,
File,
Namespace,
Scope,
Template,
TemplateParameter,
FunctionVariable,
ErrorVariable,
};
inline uint qHash(CodeHighlightingType type, uint seed = 0) noexcept
{
enum Types {
UnknownType,
//Primary highlighting:
LocalClassMemberType,
InheritedClassMemberType,
LocalVariableType,
//Other highlighting:
ClassType,
FunctionType,
ForwardDeclarationType,
EnumType,
EnumeratorType,
TypeAliasType,
MacroType, /// Declaration of a macro such as "#define FOO"
MacroFunctionLikeType, /// Declaration of a function like macro such as "#define FOO()"
//If none of the above match:
MemberVariableType,
NamespaceVariableType,
GlobalVariableType,
//Most of these are currently not used:
ArgumentType,
CodeType,
FileType,
NamespaceType,
ScopeType,
TemplateType,
TemplateParameterType,
FunctionVariableType,
ErrorVariableType
};
return ::qHash(static_cast<int>(type), seed);
}
enum Contexts {
DefinitionContext,
DeclarationContext,
ReferenceContext
};
enum class CodeHighlightingContext {
Definition,
Declaration,
Reference,
};
struct HighlightedRange
......@@ -87,7 +89,6 @@ struct HighlightedRange
* */
class KDEVPLATFORMLANGUAGE_EXPORT CodeHighlightingInstance
: public HighlightingEnumContainer
{
public:
explicit CodeHighlightingInstance(const CodeHighlighting* highlighting) : m_useClassCache(false)
......@@ -110,7 +111,7 @@ public:
/**
* @param context Should be the context from where the declaration is used, if a use is highlighted.
* */
virtual Types typeForDeclaration(KDevelop::Declaration* dec, KDevelop::DUContext* context) const;
virtual CodeHighlightingType typeForDeclaration(KDevelop::Declaration* dec, KDevelop::DUContext* context) const;
/**
* Decides whether to apply auto-generated rainbow colors to @p dec.
* Default implementation only applies that to local variables in functions.
......@@ -133,10 +134,7 @@ public:
/**
* General class representing the code highlighting for one language
* */
class KDEVPLATFORMLANGUAGE_EXPORT CodeHighlighting
: public QObject
, public KDevelop::ICodeHighlighting
, public HighlightingEnumContainer
class KDEVPLATFORMLANGUAGE_EXPORT CodeHighlighting : public QObject, public KDevelop::ICodeHighlighting
{
Q_OBJECT
Q_INTERFACES(KDevelop::ICodeHighlighting)
......@@ -151,7 +149,8 @@ public:
void highlightDUChain(ReferencedTopDUContext context) override;
//color should be zero when undecided
KTextEditor::Attribute::Ptr attributeForType(Types type, Contexts context, const QColor& color) const;
KTextEditor::Attribute::Ptr attributeForType(CodeHighlightingType type, CodeHighlightingContext context,
const QColor& color) const;
KTextEditor::Attribute::Ptr attributeForDepth(int depth) const;
/// This function is thread-safe
......@@ -183,9 +182,9 @@ private:
friend class CodeHighlightingInstance;
mutable QHash<Types, KTextEditor::Attribute::Ptr> m_definitionAttributes;
mutable QHash<Types, KTextEditor::Attribute::Ptr> m_declarationAttributes;
mutable QHash<Types, KTextEditor::Attribute::Ptr> m_referenceAttributes;
mutable QHash<CodeHighlightingType, KTextEditor::Attribute::Ptr> m_definitionAttributes;
mutable QHash<CodeHighlightingType, KTextEditor::Attribute::Ptr> m_declarationAttributes;
mutable QHash<CodeHighlightingType, KTextEditor::Attribute::Ptr> m_referenceAttributes;
mutable QList<KTextEditor::Attribute::Ptr> m_depthAttributes;
// Should be used to enable/disable the colorization of local variables and their uses
bool m_localColorization;
......
......@@ -26,14 +26,15 @@ void ConfigurableHighlightingColors::setDefaultAttribute(const KTextEditor::Attr
m_defaultAttribute = defaultAttrib;
}
KTextEditor::Attribute::Ptr ConfigurableHighlightingColors::attribute(int number) const
KTextEditor::Attribute::Ptr ConfigurableHighlightingColors::attribute(CodeHighlightingType type) const
{
return m_attributes[number];
return m_attributes[type];
}
void ConfigurableHighlightingColors::addAttribute(int number, const KTextEditor::Attribute::Ptr& attribute)
void ConfigurableHighlightingColors::addAttribute(CodeHighlightingType type,
const KTextEditor::Attribute::Ptr& attribute)
{
m_attributes[number] = attribute;
m_attributes[type] = attribute;
}
ConfigurableHighlightingColors::ConfigurableHighlightingColors()
......@@ -42,33 +43,33 @@ ConfigurableHighlightingColors::ConfigurableHighlightingColors()
setDefaultAttribute(a);
}
#define ADD_COLOR(type, color_) \
{ \
KTextEditor::Attribute::Ptr a(new KTextEditor::Attribute); \
a->setForeground(QColor(cache->blendGlobalColor(color_))); \
addAttribute(CodeHighlighting::type, a); \
ifDebug(qCDebug(LANGUAGE) << # type << "color: " << # color_ << "->" << a->foreground().color().name(); ) \
#define ADD_COLOR(type, color_) \
{ \
KTextEditor::Attribute::Ptr a(new KTextEditor::Attribute); \
a->setForeground(QColor(cache->blendGlobalColor(color_))); \
addAttribute(CodeHighlightingType::type, a); \
ifDebug(qCDebug(LANGUAGE) << #type << "color: " << #color_ << "->" << a->foreground().color().name();) \
}
CodeHighlightingColors::CodeHighlightingColors(ColorCache* cache) : ConfigurableHighlightingColors()
{
// TODO: The set of colors doesn't work very well. Many colors simply too dark (even on the maximum "Global colorization intensity" they hardly distinguishable from grey) and look alike.
ADD_COLOR(ClassType, 0x005912) //Dark green
ADD_COLOR(TypeAliasType, 0x35938d)
ADD_COLOR(EnumType, 0x6c101e) //Dark red
ADD_COLOR(EnumeratorType, 0x862a38) //Greyish red
ADD_COLOR(FunctionType, 0x21005A) //Navy blue
ADD_COLOR(MemberVariableType, 0x443069) //Dark Burple (blue/purple)
ADD_COLOR(LocalClassMemberType, 0xae7d00) //Light orange
ADD_COLOR(InheritedClassMemberType, 0x705000) //Dark orange
ADD_COLOR(LocalVariableType, 0x0C4D3C)
ADD_COLOR(FunctionVariableType, 0x300085) //Less dark navy blue
ADD_COLOR(NamespaceVariableType, 0x9F3C5F) //Rose
ADD_COLOR(GlobalVariableType, 0x12762B) //Grass green
ADD_COLOR(NamespaceType, 0x6B2840) //Dark rose
ADD_COLOR(ErrorVariableType, 0x8b0019) //Pure red
ADD_COLOR(ForwardDeclarationType, 0x5C5C5C) //Gray
ADD_COLOR(MacroType, 0xA41239)
ADD_COLOR(MacroFunctionLikeType, 0x008080)
ADD_COLOR(Class, 0x005912) // Dark green
ADD_COLOR(TypeAlias, 0x35938d)
ADD_COLOR(Enum, 0x6c101e) // Dark red
ADD_COLOR(Enumerator, 0x862a38) // Greyish red
ADD_COLOR(Function, 0x21005A) // Navy blue
ADD_COLOR(MemberVariable, 0x443069) // Dark Burple (blue/purple)
ADD_COLOR(LocalClassMember, 0xae7d00) // Light orange
ADD_COLOR(InheritedClassMember, 0x705000) // Dark orange
ADD_COLOR(LocalVariable, 0x0C4D3C)
ADD_COLOR(FunctionVariable, 0x300085) // Less dark navy blue
ADD_COLOR(NamespaceVariable, 0x9F3C5F) // Rose
ADD_COLOR(GlobalVariable, 0x12762B) // Grass green
ADD_COLOR(Namespace, 0x6B2840) // Dark rose
ADD_COLOR(ErrorVariable, 0x8b0019) // Pure red
ADD_COLOR(ForwardDeclaration, 0x5C5C5C) // Gray
ADD_COLOR(Macro, 0xA41239)
ADD_COLOR(MacroFunctionLike, 0x008080)
}
}
......@@ -9,6 +9,8 @@
#ifndef KDEVPLATFORM_CONFIGURABLECOLORS_H
#define KDEVPLATFORM_CONFIGURABLECOLORS_H
#include "codehighlighting.h"
#include <language/languageexport.h>
#include <KTextEditor/Attribute>
......@@ -21,9 +23,9 @@ class KDEVPLATFORMLANGUAGE_EXPORT ConfigurableHighlightingColors
public:
explicit ConfigurableHighlightingColors();
void addAttribute(int number, const KTextEditor::Attribute::Ptr& attribute);
void addAttribute(CodeHighlightingType type, const KTextEditor::Attribute::Ptr& attribute);
KTextEditor::Attribute::Ptr attribute(int number) const;
KTextEditor::Attribute::Ptr attribute(CodeHighlightingType type) const;
void setDefaultAttribute(const KTextEditor::Attribute::Ptr& defaultAttrib);
......@@ -32,6 +34,7 @@ public:
private:
KTextEditor::Attribute::Ptr m_defaultAttribute;
QHash<int, KTextEditor::Attribute::Ptr> m_attributes;
QHash<CodeHighlightingType, KTextEditor::Attribute::Ptr> m_attributes;
QString m_highlightingName;
};
......
......@@ -17,11 +17,11 @@ class ClangHighlighting::Instance : public KDevelop::CodeHighlightingInstance
public:
explicit Instance(const KDevelop::CodeHighlighting* highlighting);
KDevelop::HighlightingEnumContainer::Types typeForDeclaration(KDevelop::Declaration* dec, KDevelop::DUContext* context) const override
CodeHighlightingType typeForDeclaration(KDevelop::Declaration* dec, KDevelop::DUContext* context) const override
{
if (auto macro = dynamic_cast<MacroDefinition*>(dec)) {
if (macro->isFunctionLike()) {
return KDevelop::HighlightingEnumContainer::MacroFunctionLikeType;
return CodeHighlightingType::MacroFunctionLike;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment