diff --git a/mix/CodeModel.cpp b/mix/CodeModel.cpp index 1dc47185f..46fc572b3 100644 --- a/mix/CodeModel.cpp +++ b/mix/CodeModel.cpp @@ -380,7 +380,7 @@ void CodeModel::gasEstimation(solidity::CompilerStack const& _cs) GasMeter::GasConsumption cost = gasItem->second; std::stringstream v; v << cost.value; - m_gasCostsMaps->push(sourceName, location.start, location.end, QString::fromStdString(v.str()), cost.isInfinite, "statement"); + m_gasCostsMaps->push(sourceName, location.start, location.end, QString::fromStdString(v.str()), cost.isInfinite, GasMap::type::Statement); } if (contractDefinition.getConstructor() != nullptr) @@ -388,7 +388,7 @@ void CodeModel::gasEstimation(solidity::CompilerStack const& _cs) GasMeter::GasConsumption cost = GasEstimator::functionalEstimation(*_cs.getRuntimeAssemblyItems(n), contractDefinition.getConstructor()->externalSignature()); std::stringstream v; v << cost.value; - m_gasCostsMaps->push(sourceName, contractDefinition.getConstructor()->getLocation().start, contractDefinition.getConstructor()->getLocation().end, QString::fromStdString(v.str()), cost.isInfinite, "constructor"); + m_gasCostsMaps->push(sourceName, contractDefinition.getConstructor()->getLocation().start, contractDefinition.getConstructor()->getLocation().end, QString::fromStdString(v.str()), cost.isInfinite, GasMap::type::Constructor); } for (auto func: contractDefinition.getDefinedFunctions()) @@ -396,7 +396,7 @@ void CodeModel::gasEstimation(solidity::CompilerStack const& _cs) GasMeter::GasConsumption cost = GasEstimator::functionalEstimation(*_cs.getRuntimeAssemblyItems(n), func->externalSignature()); std::stringstream v; v << cost.value; - m_gasCostsMaps->push(sourceName, func->getLocation().start, func->getLocation().end, QString::fromStdString(v.str()), cost.isInfinite, "function"); + m_gasCostsMaps->push(sourceName, func->getLocation().start, func->getLocation().end, QString::fromStdString(v.str()), cost.isInfinite, GasMap::type::Function); } } } @@ -605,9 +605,9 @@ void CodeModel::setOptimizeCode(bool _value) emit scheduleCompilationJob(++m_backgroundJobId); } -void GasMapWrapper::push(QString _source, int _start, int _end, QString _value, bool _isInfinite, QString _payload) +void GasMapWrapper::push(QString _source, int _start, int _end, QString _value, bool _isInfinite, GasMap::type _type) { - GasMap* gas = new GasMap(_start, _end, _value, _isInfinite, _payload, this); + GasMap* gas = new GasMap(_start, _end, _value, _isInfinite, _type, this); m_gasMaps.find(_source).value().push_back(QVariant::fromValue(gas)); } diff --git a/mix/CodeModel.h b/mix/CodeModel.h index a51e85cec..516e8e6d0 100644 --- a/mix/CodeModel.h +++ b/mix/CodeModel.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -130,41 +131,60 @@ struct SourceMap using SourceMaps = QMap; //by source id using GasCostsMaps = QMap; //gas cost by contract name -class GasMapWrapper: public QObject -{ - Q_OBJECT - - Q_PROPERTY(GasCostsMaps gasMaps MEMBER m_gasMaps CONSTANT) - -public: - GasMapWrapper(QObject* _parent = nullptr): QObject(_parent){} - void push(QString _source, int _start, int _end, QString _value, bool _isInfinite, QString _payload); - bool contains(QString _key); - void insert(QString _source, QVariantList _variantList); - QVariantList gasCostsByDocId(QString _source); - -private: - GasCostsMaps m_gasMaps; -}; - class GasMap: public QObject { Q_OBJECT - + Q_ENUMS(type) Q_PROPERTY(int start MEMBER m_start CONSTANT) Q_PROPERTY(int end MEMBER m_end CONSTANT) Q_PROPERTY(QString gas MEMBER m_gas CONSTANT) Q_PROPERTY(bool isInfinite MEMBER m_isInfinite CONSTANT) - Q_PROPERTY(QString payload MEMBER m_payload CONSTANT) + Q_PROPERTY(QString codeBlockType READ codeBlockType CONSTANT) public: - GasMap(int _start, int _end, QString _gas, bool _isInfinite, QString _payload, QObject* _parent): QObject(_parent), m_start(_start), m_end(_end), m_gas(_gas), m_isInfinite(_isInfinite), m_payload(_payload) {} + + enum type + { + Statement, + Function, + Constructor + }; + + GasMap(int _start, int _end, QString _gas, bool _isInfinite, type _type, QObject* _parent): QObject(_parent), m_start(_start), m_end(_end), m_gas(_gas), m_isInfinite(_isInfinite), m_type(_type) {} int m_start; int m_end; QString m_gas; bool m_isInfinite; - QString m_payload; + type m_type; + + QString codeBlockType() const + { + QMetaEnum units = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("type")); + if (m_type) + { + const char* key = units.valueToKey(m_type); + return QString(key).toLower(); + } + return QString(""); + } +}; + +class GasMapWrapper: public QObject +{ + Q_OBJECT + + Q_PROPERTY(GasCostsMaps gasMaps MEMBER m_gasMaps CONSTANT) + +public: + GasMapWrapper(QObject* _parent = nullptr): QObject(_parent){} + void push(QString _source, int _start, int _end, QString _value, bool _isInfinite, GasMap::type _type); + bool contains(QString _key); + void insert(QString _source, QVariantList _variantList); + QVariantList gasCostsByDocId(QString _source); + +private: + GasCostsMaps m_gasMaps; }; /// Code compilation model. Compiles contracts in background an provides compiled contract data @@ -264,5 +284,3 @@ private: } } - -//Q_DECLARE_METATYPE(dev::mix::GasMap) diff --git a/mix/qml/html/codeeditor.js b/mix/qml/html/codeeditor.js index cabab107b..108df5952 100644 --- a/mix/qml/html/codeeditor.js +++ b/mix/qml/html/codeeditor.js @@ -253,12 +253,12 @@ displayGasEstimation = function(show) var className = "CodeMirror-gasCosts" + i; var line = editor.posFromIndex(gasCosts[i].start); var endChar; - if (gasCosts[i].payload === "statement") + if (gasCosts[i].codeBlockType === "statement" || gasCosts[i].codeBlockType === "") { endChar = editor.posFromIndex(gasCosts[i].end); gasMarkText.push({ line: line, markText: editor.markText(line, endChar, { inclusiveLeft: true, inclusiveRight: true, handleMouseEvents: true, className: className, css: "background-color:" + color })}); } - else if (gasCosts[i].payload === "function" || gasCosts[i].payload === "constructor") + else if (gasCosts[i].codeBlockType === "function" || gasCosts[i].codeBlockType === "constructor") { var l = editor.getLine(line.line); endChar = { line: line.line, ch: line.ch + l.length };