diff --git a/mix/CodeEditorExtensionManager.cpp b/mix/CodeEditorExtensionManager.cpp index 79bbe9a9e..48c928a1f 100644 --- a/mix/CodeEditorExtensionManager.cpp +++ b/mix/CodeEditorExtensionManager.cpp @@ -114,6 +114,7 @@ void CodeEditorExtensionManager::setEditor(QQuickItem* _editor) void CodeEditorExtensionManager::onCodeChange() { + m_appContext->codeModel()->updateFormatting(m_doc); //update old formatting m_appContext->codeModel()->registerCodeChange(m_doc->toPlainText()); } diff --git a/mix/CodeHighlighter.cpp b/mix/CodeHighlighter.cpp index f246e9b7b..6476b0995 100644 --- a/mix/CodeHighlighter.cpp +++ b/mix/CodeHighlighter.cpp @@ -70,22 +70,25 @@ CodeHighlighter::FormatRange::FormatRange(CodeHighlighterSettings::Token _t, sol token(_t), start(_location.start), length(_location.end - _location.start) {} -void CodeHighlighter::processSource(solidity::Scanner* _scanner) +void CodeHighlighter::processSource(std::string const& _source) { - solidity::Token::Value token = _scanner->getCurrentToken(); + processComments(_source); + solidity::CharStream stream(_source); + solidity::Scanner scanner(stream); + solidity::Token::Value token = scanner.getCurrentToken(); while (token != Token::EOS) { if ((token >= Token::BREAK && token < Token::TYPES_END) || token == Token::IN || token == Token::DELETE || token == Token::NULL_LITERAL || token == Token::TRUE_LITERAL || token == Token::FALSE_LITERAL) - m_formats.push_back(FormatRange(CodeHighlighterSettings::Keyword, _scanner->getCurrentLocation())); + m_formats.push_back(FormatRange(CodeHighlighterSettings::Keyword, scanner.getCurrentLocation())); else if (token == Token::STRING_LITERAL) - m_formats.push_back(FormatRange(CodeHighlighterSettings::StringLiteral, _scanner->getCurrentLocation())); + m_formats.push_back(FormatRange(CodeHighlighterSettings::StringLiteral, scanner.getCurrentLocation())); else if (token == Token::COMMENT_LITERAL) - m_formats.push_back(FormatRange(CodeHighlighterSettings::Comment, _scanner->getCurrentLocation())); + m_formats.push_back(FormatRange(CodeHighlighterSettings::Comment, scanner.getCurrentLocation())); else if (token == Token::NUMBER) - m_formats.push_back(FormatRange(CodeHighlighterSettings::NumLiteral, _scanner->getCurrentLocation())); + m_formats.push_back(FormatRange(CodeHighlighterSettings::NumLiteral, scanner.getCurrentLocation())); - token = _scanner->next(); + token = scanner.next(); } std::sort(m_formats.begin(), m_formats.end()); } @@ -98,6 +101,37 @@ void CodeHighlighter::processAST(solidity::ASTNode const& _ast) std::sort(m_formats.begin(), m_formats.end()); } + +void CodeHighlighter::processComments(std::string const& _source) +{ + unsigned i = 0; + unsigned size = _source.size(); + if (size == 0) + return; + while (i < size - 1) + { + if (_source[i] == '/' && _source[i + 1] == '/') + { + //add single line comment + int start = i; + i += 2; + while (_source[i] != '\n' && i < size) + ++i; + m_formats.push_back(FormatRange(CodeHighlighterSettings::Comment, start, i - start)); + } + else if (_source[i] == '/' && _source[i + 1] == '*') + { + //add multiline comment + int start = i; + i += 2; + while ((_source[i] != '/' || _source[i - 1] != '*') && i < size) + ++i; + m_formats.push_back(FormatRange(CodeHighlighterSettings::Comment, start, i - start + 1)); + } + ++i; + } +} + void CodeHighlighter::updateFormatting(QTextDocument* _document, CodeHighlighterSettings const& _settings) { QTextBlock block = _document->firstBlock(); diff --git a/mix/CodeHighlighter.h b/mix/CodeHighlighter.h index baf39bc66..8cc8b69e9 100644 --- a/mix/CodeHighlighter.h +++ b/mix/CodeHighlighter.h @@ -34,7 +34,6 @@ namespace dev namespace solidity { class ASTNode; - class Scanner; struct Location; } @@ -81,10 +80,13 @@ public: public: /// Collect highligting information - void processSource(solidity::Scanner* _scanner); + void processSource(std::string const& _source); void processAST(solidity::ASTNode const& _ast); void updateFormatting(QTextDocument* _document, CodeHighlighterSettings const& _settings); +private: + void processComments(std::string const& _source); + private: Formats m_formats; }; diff --git a/mix/CodeModel.cpp b/mix/CodeModel.cpp index 60c961045..deb58416c 100644 --- a/mix/CodeModel.cpp +++ b/mix/CodeModel.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -47,7 +46,8 @@ void BackgroundWorker::queueCodeChange(int _jobId, QString const& _content) CompilationResult::CompilationResult(): QObject(nullptr), m_successfull(false), m_contract(new QContractDefinition()), - m_codeHighlighter(new CodeHighlighter()) + m_codeHighlighter(new CodeHighlighter()), + m_codeHash(qHash(QString())) {} CompilationResult::CompilationResult(const solidity::CompilerStack& _compiler): @@ -103,6 +103,9 @@ void CodeModel::stop() void CodeModel::registerCodeChange(const QString &_code) { // launch the background thread + uint hash = qHash(_code); + if (m_result->m_codeHash == hash) + return; m_backgroundJobId++; m_compiling = true; emit stateChanged(); @@ -121,9 +124,7 @@ void CodeModel::runCompilationJob(int _jobId, QString const& _code) // run syntax highlighting first // @todo combine this with compilation step auto codeHighlighter = std::make_shared(); - solidity::CharStream stream(source); - solidity::Scanner scanner(stream); - codeHighlighter->processSource(&scanner); + codeHighlighter->processSource(source); // run compilation try @@ -139,9 +140,10 @@ void CodeModel::runCompilationJob(int _jobId, QString const& _code) std::ostringstream error; solidity::SourceReferenceFormatter::printExceptionInformation(error, _exception, "Error", cs); result.reset(new CompilationResult(*m_result, QString::fromStdString(error.str()))); - qDebug() << QString(QApplication::tr("compilation failed:") + " " + m_result->compilerMessage()); + qDebug() << QString(QApplication::tr("compilation failed:") + " " + result->compilerMessage()); } result->m_codeHighlighter = codeHighlighter; + result->m_codeHash = qHash(_code); emit compilationCompleteInternal(result.release()); } diff --git a/mix/CodeModel.h b/mix/CodeModel.h index d148cad87..da093183d 100644 --- a/mix/CodeModel.h +++ b/mix/CodeModel.h @@ -98,6 +98,7 @@ private: std::shared_ptr m_codeHighlighter; ///@todo syntax highlighting, etc friend class CodeModel; + uint m_codeHash; }; /// Background code compiler diff --git a/mix/qml/MainContent.qml b/mix/qml/MainContent.qml index 0b3303aa8..99953344f 100644 --- a/mix/qml/MainContent.qml +++ b/mix/qml/MainContent.qml @@ -42,11 +42,11 @@ Rectangle { anchors.centerIn: parent tabChangesFocus: false Keys.onPressed: { - if (event.key === Qt.Key_Tab) { - codeEditor.insert(codeEditor.cursorPosition, "\t"); - event.accepted = true; - } - } + if (event.key === Qt.Key_Tab) { + codeEditor.insert(codeEditor.cursorPosition, "\t"); + event.accepted = true; + } + } } } Rectangle {