Browse Source

comment highlighting

cl-refactor
arkpar 10 years ago
parent
commit
fcbd25a8be
  1. 1
      mix/CodeEditorExtensionManager.cpp
  2. 48
      mix/CodeHighlighter.cpp
  3. 6
      mix/CodeHighlighter.h
  4. 14
      mix/CodeModel.cpp
  5. 1
      mix/CodeModel.h
  6. 10
      mix/qml/MainContent.qml

1
mix/CodeEditorExtensionManager.cpp

@ -114,6 +114,7 @@ void CodeEditorExtensionManager::setEditor(QQuickItem* _editor)
void CodeEditorExtensionManager::onCodeChange() void CodeEditorExtensionManager::onCodeChange()
{ {
m_appContext->codeModel()->updateFormatting(m_doc); //update old formatting
m_appContext->codeModel()->registerCodeChange(m_doc->toPlainText()); m_appContext->codeModel()->registerCodeChange(m_doc->toPlainText());
} }

48
mix/CodeHighlighter.cpp

@ -70,22 +70,25 @@ CodeHighlighter::FormatRange::FormatRange(CodeHighlighterSettings::Token _t, sol
token(_t), start(_location.start), length(_location.end - _location.start) 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) while (token != Token::EOS)
{ {
if ((token >= Token::BREAK && token < Token::TYPES_END) || 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) 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) 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) 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) 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()); 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()); 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) void CodeHighlighter::updateFormatting(QTextDocument* _document, CodeHighlighterSettings const& _settings)
{ {
QTextBlock block = _document->firstBlock(); QTextBlock block = _document->firstBlock();

6
mix/CodeHighlighter.h

@ -34,7 +34,6 @@ namespace dev
namespace solidity namespace solidity
{ {
class ASTNode; class ASTNode;
class Scanner;
struct Location; struct Location;
} }
@ -81,10 +80,13 @@ public:
public: public:
/// Collect highligting information /// Collect highligting information
void processSource(solidity::Scanner* _scanner); void processSource(std::string const& _source);
void processAST(solidity::ASTNode const& _ast); void processAST(solidity::ASTNode const& _ast);
void updateFormatting(QTextDocument* _document, CodeHighlighterSettings const& _settings); void updateFormatting(QTextDocument* _document, CodeHighlighterSettings const& _settings);
private:
void processComments(std::string const& _source);
private: private:
Formats m_formats; Formats m_formats;
}; };

14
mix/CodeModel.cpp

@ -24,7 +24,6 @@
#include <QDebug> #include <QDebug>
#include <QApplication> #include <QApplication>
#include <QtQml> #include <QtQml>
#include <libsolidity/Scanner.h>
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
@ -47,7 +46,8 @@ void BackgroundWorker::queueCodeChange(int _jobId, QString const& _content)
CompilationResult::CompilationResult(): CompilationResult::CompilationResult():
QObject(nullptr), m_successfull(false), QObject(nullptr), m_successfull(false),
m_contract(new QContractDefinition()), m_contract(new QContractDefinition()),
m_codeHighlighter(new CodeHighlighter()) m_codeHighlighter(new CodeHighlighter()),
m_codeHash(qHash(QString()))
{} {}
CompilationResult::CompilationResult(const solidity::CompilerStack& _compiler): CompilationResult::CompilationResult(const solidity::CompilerStack& _compiler):
@ -103,6 +103,9 @@ void CodeModel::stop()
void CodeModel::registerCodeChange(const QString &_code) void CodeModel::registerCodeChange(const QString &_code)
{ {
// launch the background thread // launch the background thread
uint hash = qHash(_code);
if (m_result->m_codeHash == hash)
return;
m_backgroundJobId++; m_backgroundJobId++;
m_compiling = true; m_compiling = true;
emit stateChanged(); emit stateChanged();
@ -121,9 +124,7 @@ void CodeModel::runCompilationJob(int _jobId, QString const& _code)
// run syntax highlighting first // run syntax highlighting first
// @todo combine this with compilation step // @todo combine this with compilation step
auto codeHighlighter = std::make_shared<CodeHighlighter>(); auto codeHighlighter = std::make_shared<CodeHighlighter>();
solidity::CharStream stream(source); codeHighlighter->processSource(source);
solidity::Scanner scanner(stream);
codeHighlighter->processSource(&scanner);
// run compilation // run compilation
try try
@ -139,9 +140,10 @@ void CodeModel::runCompilationJob(int _jobId, QString const& _code)
std::ostringstream error; std::ostringstream error;
solidity::SourceReferenceFormatter::printExceptionInformation(error, _exception, "Error", cs); solidity::SourceReferenceFormatter::printExceptionInformation(error, _exception, "Error", cs);
result.reset(new CompilationResult(*m_result, QString::fromStdString(error.str()))); 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_codeHighlighter = codeHighlighter;
result->m_codeHash = qHash(_code);
emit compilationCompleteInternal(result.release()); emit compilationCompleteInternal(result.release());
} }

1
mix/CodeModel.h

@ -98,6 +98,7 @@ private:
std::shared_ptr<CodeHighlighter> m_codeHighlighter; std::shared_ptr<CodeHighlighter> m_codeHighlighter;
///@todo syntax highlighting, etc ///@todo syntax highlighting, etc
friend class CodeModel; friend class CodeModel;
uint m_codeHash;
}; };
/// Background code compiler /// Background code compiler

10
mix/qml/MainContent.qml

@ -42,11 +42,11 @@ Rectangle {
anchors.centerIn: parent anchors.centerIn: parent
tabChangesFocus: false tabChangesFocus: false
Keys.onPressed: { Keys.onPressed: {
if (event.key === Qt.Key_Tab) { if (event.key === Qt.Key_Tab) {
codeEditor.insert(codeEditor.cursorPosition, "\t"); codeEditor.insert(codeEditor.cursorPosition, "\t");
event.accepted = true; event.accepted = true;
} }
} }
} }
} }
Rectangle { Rectangle {

Loading…
Cancel
Save