Skip to content

Commit

Permalink
Merge pull request #92 from cvut/fix-broken-editor-focus-2
Browse files Browse the repository at this point in the history
Fix broken editor focus tab focus
  • Loading branch information
jdupak authored Nov 30, 2023
2 parents f5f8802 + d9394a1 commit d72721b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/gui/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions src/gui/windows/editor/editordock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions src/gui/windows/editor/editordock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,7 @@ public slots:
private:
QSharedPointer<QSettings> settings;
bool line_numbers_visible = true;
size_t unknown_editor_counter = 1;
};

#endif // EDITORDOCK_H
1 change: 1 addition & 0 deletions src/gui/windows/editor/srceditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d72721b

Please sign in to comment.