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()
{
m_appContext->codeModel()->updateFormatting(m_doc); //update old formatting
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)
{}
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();

6
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;
};

14
mix/CodeModel.cpp

@ -24,7 +24,6 @@
#include <QDebug>
#include <QApplication>
#include <QtQml>
#include <libsolidity/Scanner.h>
#include <libsolidity/CompilerStack.h>
#include <libsolidity/SourceReferenceFormatter.h>
#include <libevmcore/Instruction.h>
@ -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<CodeHighlighter>();
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());
}

1
mix/CodeModel.h

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

10
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 {

Loading…
Cancel
Save