diff --git a/src/gui/mainwindow/mainwindow.cpp b/src/gui/mainwindow/mainwindow.cpp index 887644f7..13755af3 100644 --- a/src/gui/mainwindow/mainwindow.cpp +++ b/src/gui/mainwindow/mainwindow.cpp @@ -573,21 +573,17 @@ void MainWindow::message_selected( (void)hint; if (file.isEmpty()) { return; } - auto editor = (file == "Unknown") ? editor_tabs->get_current_editor() - : editor_tabs->find_tab_by_filename(file)->get_editor(); - if (editor == nullptr) { return; } - editor->setCursorTo(line, column); - editor->setFocus(); + central_widget_tabs->setCurrentWidget(editor_tabs.data()); + editor_tabs->set_cursor_to(file, line, column); // Highlight the line + auto editor = editor_tabs->get_current_editor(); QTextEdit::ExtraSelection selection; selection.format.setBackground(QColor(Qt::red).lighter(160)); selection.format.setProperty(QTextFormat::FullWidthSelection, true); selection.cursor = editor->textCursor(); selection.cursor.clearSelection(); editor->setExtraSelections({ selection }); - - if (editor_tabs != nullptr) { editor_tabs->setCurrentWidget(editor); } } bool SimpleAsmWithEditorCheck::process_file(const QString &filename, QString *error_ptr) { diff --git a/src/gui/windows/editor/editordock.cpp b/src/gui/windows/editor/editordock.cpp index bbf9f545..54ddc1a3 100644 --- a/src/gui/windows/editor/editordock.cpp +++ b/src/gui/windows/editor/editordock.cpp @@ -87,6 +87,14 @@ EditorTab *EditorDock::open_file_if_not_open(const QString &filename, bool save_ EditorTab *EditorDock::create_empty_tab() { auto tab = new EditorTab(line_numbers_visible, this); + while (true) { + auto filename = QString("Unknown %1").arg(unknown_editor_counter++); + if (!find_tab_id_by_filename(filename).has_value()) { + tab->get_editor()->setFileName(filename); + tab->get_editor()->setSaveAsRequired(true); + break; + } + } addTab(tab, tab->title()); setCurrentWidget(tab); return tab; @@ -308,3 +316,14 @@ void EditorDock::confirm_close_tab_dialog(int index) { Qt::QueuedConnection); msgbox->open(); } + +void EditorDock::set_cursor_to(const QString &filename, int line, int column) { + auto tab = (filename == "Unknown") ? get_tab(currentIndex()) : find_tab_by_filename(filename); + if (tab == nullptr) { + WARN( + "Cannot find tab for file '%s'. Unable to set cursor.", filename.toStdString().c_str()); + return; + } + setCurrentWidget(tab); + tab->get_editor()->setCursorTo(line, column); +} diff --git a/src/gui/windows/editor/editordock.h b/src/gui/windows/editor/editordock.h index 2bd449eb..9ab89636 100644 --- a/src/gui/windows/editor/editordock.h +++ b/src/gui/windows/editor/editordock.h @@ -30,6 +30,7 @@ class EditorDock : public HidingTabWidget { BORROWED [[nodiscard]] SrcEditor *get_current_editor() const; [[nodiscard]] QStringList get_open_file_list() const; bool get_modified_tab_filenames(QStringList &output, bool report_unnamed = false) const; + void set_cursor_to(const QString &filename, int line, int column); protected: void tabCountChanged() override; @@ -58,6 +59,7 @@ public slots: private: QSharedPointer settings; bool line_numbers_visible = true; + size_t unknown_editor_counter = 1; }; #endif // EDITORDOCK_H \ No newline at end of file diff --git a/src/gui/windows/editor/srceditor.cpp b/src/gui/windows/editor/srceditor.cpp index 554e737b..d132c0a9 100644 --- a/src/gui/windows/editor/srceditor.cpp +++ b/src/gui/windows/editor/srceditor.cpp @@ -109,6 +109,7 @@ void SrcEditor::setCursorTo(int ln, int col) { QTextCursor cursor(document()->findBlockByNumber(ln - 1)); cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, col - 1); setTextCursor(cursor); + setFocus(); } bool SrcEditor::isModified() const {