Fix assembler not rehighlighting when code editor changes source type
MODIFY
src/editor/codeeditor.cpp
src/editor/codeeditor.cpp
@@ -152,6 +152,8 @@ void CodeEditor::setSourceType(SourceType type) {
default:
break;
}
+
+ m_highlighter->clearAndRehighlight();
}
void CodeEditor::highlightCurrentLine() {

MODIFY
src/editor/codeeditor.h
src/editor/codeeditor.h
@@ -26,10 +26,6 @@ public:
void lineNumberAreaPaintEvent(QPaintEvent* event);
int lineNumberAreaWidth();
void setupChangedTimer();
- void reset() {
- m_highlighter->reset();
- clear();
- }
bool syntaxAccepted() const { return m_highlighter->acceptsSyntax(); }
signals:

MODIFY
src/edittab.cpp
src/edittab.cpp
@@ -23,9 +23,9 @@ EditTab::EditTab(QToolBar* toolbar, QWidget* parent) : RipesTab(toolbar, parent)
m_buildAction = new QAction(this);
m_buildAction->setIcon(QIcon(":/icons/build.svg"));
- m_buildAction->setText("Compile C program");
m_buildAction->setEnabled(false);
m_buildAction->setShortcut(QKeySequence("Ctrl+B"));
+ m_buildAction->setText("Compile C program (" + m_buildAction->shortcut().toString() + ")");
connect(m_buildAction, &QAction::triggered, this, &EditTab::compile);
m_toolbar->addAction(m_buildAction);
@@ -51,9 +51,22 @@ EditTab::EditTab(QToolBar* toolbar, QWidget* parent) : RipesTab(toolbar, parent)
}
void EditTab::loadExternalFile(const LoadFileParams& params) {
+ if (params.type == SourceType::C) {
+ // Try to enable C input and verify that source type was changed successfully. This allows us to trigger the
+ // message box associated with C input, if no compiler is set. If so, load the file.
+ enableEditor();
+ m_ui->setCInput->setChecked(true);
+ if (m_currentSourceType == SourceType::C) {
+ loadFile(params);
+ }
+ } else if (params.type == SourceType::Assembly) {
+ m_ui->setAssemblyInput->setChecked(true);
+ loadFile(params);
+ } else {
m_currentSourceType = params.type;
loadFile(params);
}
+}
void EditTab::loadFile(const LoadFileParams& fileParams) {
QFile file(fileParams.filepath);
@@ -67,7 +80,7 @@ void EditTab::loadFile(const LoadFileParams& fileParams) {
switch (fileParams.type) {
case SourceType::C:
case SourceType::Assembly:
- success &= loadAssemblyFile(loadedProgram, file);
+ success &= loadSourceFile(loadedProgram, file);
break;
case SourceType::FlatBinary:
success &= loadFlatBinaryFile(loadedProgram, file, fileParams.binaryEntryPoint, fileParams.binaryLoadAt);
@@ -100,7 +113,7 @@ const QByteArray& EditTab::getBinaryData() {
}
void EditTab::clearAssemblyEditor() {
- m_ui->codeEditor->reset();
+ m_ui->codeEditor->clear();
m_assembler->clear();
}
@@ -197,13 +210,12 @@ EditTab::~EditTab() {
}
void EditTab::newProgram() {
- m_ui->codeEditor->reset();
m_ui->codeEditor->clear();
enableAssemblyInput();
}
-void EditTab::setAssemblyText(const QString& text) {
- m_ui->codeEditor->reset();
+void EditTab::setSourceText(const QString& text) {
+ m_ui->codeEditor->clear();
m_ui->codeEditor->setPlainText(text);
}
@@ -251,9 +263,9 @@ bool EditTab::loadFlatBinaryFile(Program& program, QFile& file, unsigned long en
return true;
}
-bool EditTab::loadAssemblyFile(Program&, QFile& file) {
+bool EditTab::loadSourceFile(Program&, QFile& file) {
enableEditor();
- setAssemblyText(file.readAll());
+ setSourceText(file.readAll());
return true;
}

MODIFY
src/edittab.h
src/edittab.h
@@ -26,7 +26,7 @@ public:
EditTab(QToolBar* toolbar, QWidget* parent = nullptr);
~EditTab() override;
- void setAssemblyText(const QString& text);
+ void setSourceText(const QString& text);
QString getAssemblyText();
void newProgram();
void clearAssemblyEditor();
@@ -73,7 +73,7 @@ private:
void updateProgramViewer();
bool loadFlatBinaryFile(Program& program, QFile& file, unsigned long entryPoint, unsigned long loadAt);
- bool loadAssemblyFile(Program& program, QFile& file);
+ bool loadSourceFile(Program& program, QFile& file);
bool loadElfFile(Program& program, QFile& file);
void setupActions();

MODIFY
src/mainwindow.cpp
src/mainwindow.cpp
@@ -161,9 +161,10 @@ MainWindow::~MainWindow() {
void MainWindow::setupExamplesMenu(QMenu* parent) {
const auto assemblyExamples = QDir(":/examples/assembly/").entryList(QDir::Files);
+ auto* assemblyMenu = parent->addMenu("Assembly");
if (!assemblyExamples.isEmpty()) {
for (const auto& fileName : assemblyExamples) {
- parent->addAction(fileName, [=] {
+ assemblyMenu->addAction(fileName, [=] {
LoadFileParams parms;
parms.filepath = QString(":/examples/assembly/") + fileName;
parms.type = SourceType::Assembly;
@@ -174,9 +175,10 @@ void MainWindow::setupExamplesMenu(QMenu* parent) {
}
const auto cExamples = QDir(":/examples/C/").entryList(QDir::Files);
+ auto* cMenu = parent->addMenu("C");
if (!cExamples.isEmpty()) {
for (const auto& fileName : cExamples) {
- parent->addAction(fileName, [=] {
+ cMenu->addAction(fileName, [=] {
LoadFileParams parms;
parms.filepath = QString(":/examples/C/") + fileName;
parms.type = SourceType::C;
@@ -187,9 +189,10 @@ void MainWindow::setupExamplesMenu(QMenu* parent) {
}
const auto ELFExamples = QDir(":/examples/ELF/").entryList(QDir::Files);
+ auto* elfMenu = parent->addMenu("ELF (precompiled C)");
if (!ELFExamples.isEmpty()) {
for (const auto& fileName : ELFExamples) {
- parent->addAction(fileName, [=] {
+ elfMenu->addAction(fileName, [=] {
// ELFIO Cannot read directly from the bundled resource file, so copy the ELF file to a temporary file
// before loading the program.
QTemporaryFile* tmpELFFile = QTemporaryFile::createNativeFile(":/examples/ELF/" + fileName);

Fix assembler not rehighlighting when code editor changes source type