| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/AssemblyEditor.h" |
| 5 | |
| 6 | #include <QFile> |
| 7 | #include <QPainter> |
| 8 | #include <QTextBlock> |
| 9 | #include <QToolTip> |
| 10 | |
| 11 | #include <filesystem> |
| 12 | #include <utility> |
| 13 | |
| 14 | #include "Common/StringUtil.h" |
| 15 | #include "DolphinQt/Debugger/GekkoSyntaxHighlight.h" |
| 16 | |
| 17 | QSize AsmEditor::LineNumberArea::sizeHint() const |
| 18 | { |
| 19 | return QSize(asm_editor->LineNumberAreaWidth(), 0); |
| 20 | } |
| 21 | |
| 22 | void AsmEditor::LineNumberArea::paintEvent(QPaintEvent* event) |
| 23 | { |
| 24 | asm_editor->LineNumberAreaPaintEvent(event); |
| 25 | } |
| 26 | |
| 27 | AsmEditor::AsmEditor(QString path, int editor_num, bool dark_scheme, QWidget* parent) |
| 28 | : QPlainTextEdit(parent), m_path(std::move(path)), m_base_address(QStringLiteral("0")), |
| 29 | m_editor_num(editor_num), m_dirty(false), m_dark_scheme(dark_scheme) |
| 30 | { |
| 31 | if (!m_path.isEmpty()) |
| 32 | { |
| 33 | m_filename = |
| 34 | QString::fromStdString(std::filesystem::path(m_path.toStdString()).filename().string()); |
| 35 | } |
| 36 | |
| 37 | m_line_number_area = new LineNumberArea(this); |
| 38 | m_highlighter = new GekkoSyntaxHighlight(document(), currentCharFormat(), dark_scheme); |
| 39 | m_last_block = textCursor().block(); |
| 40 | |
| 41 | QFont mono_font(QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); |
| 42 | mono_font.setPointSize(12); |
| 43 | setFont(mono_font); |
| 44 | m_line_number_area->setFont(mono_font); |
| 45 | |
| 46 | UpdateLineNumberAreaWidth(0); |
| 47 | HighlightCurrentLine(); |
| 48 | setMouseTracking(true); |
| 49 | |
| 50 | connect(this, &AsmEditor::blockCountChanged, this, &AsmEditor::UpdateLineNumberAreaWidth); |
| 51 | connect(this, &AsmEditor::updateRequest, this, &AsmEditor::UpdateLineNumberArea); |
| 52 | connect(this, &AsmEditor::cursorPositionChanged, this, &AsmEditor::HighlightCurrentLine); |
| 53 | connect(this, &AsmEditor::textChanged, this, [this] { |
| 54 | m_dirty = true; |
| 55 | emit DirtyChanged(); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | int AsmEditor::LineNumberAreaWidth() |
| 60 | { |
| 61 | int num_digits = 1; |
| 62 | for (int max = qMax(1, blockCount()); max >= 10; max /= 10, ++num_digits) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | return 3 + CharWidth() * qMax(2, num_digits); |
| 67 | } |
| 68 | |
| 69 | void AsmEditor::SetBaseAddress(const QString& ba) |
| 70 | { |
| 71 | if (ba != m_base_address) |
| 72 | { |
| 73 | m_base_address = ba; |
| 74 | m_dirty = true; |
| 75 | emit DirtyChanged(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | bool AsmEditor::LoadFromPath() |
| 80 | { |
| 81 | QFile file(m_path); |
| 82 | if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 83 | { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | const std::string base_addr_line = file.readLine().toStdString(); |
| 88 | std::string base_address = ""; |
| 89 | for (size_t i = 0; i < base_addr_line.length(); i++) |
| 90 | { |
| 91 | if (std::isspace(base_addr_line[i])) |
| 92 | { |
| 93 | continue; |
| 94 | } |
| 95 | else if (base_addr_line[i] == '#') |
| 96 | { |
| 97 | base_address = base_addr_line.substr(i + 1); |
| 98 | break; |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (base_address.empty()) |
| 107 | { |
| 108 | file.seek(0); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | StringPopBackIf(&base_address, '\n'); |
| 113 | if (base_address.empty()) |
| 114 | { |
| 115 | base_address = "0"; |
| 116 | } |
| 117 | m_base_address = QString::fromStdString(base_address); |
| 118 | } |
| 119 | |
| 120 | const bool old_block = blockSignals(true); |
| 121 | setPlainText(QString::fromStdString(file.readAll().toStdString())); |
| 122 | blockSignals(old_block); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool AsmEditor::PathsMatch(const QString& path) const |
| 127 | { |
| 128 | if (m_path.isEmpty() || path.isEmpty()) |
| 129 | { |
| 130 | return false; |
| 131 | } |
| 132 | return std::filesystem::path(m_path.toStdString()) == std::filesystem::path(path.toStdString()); |
| 133 | } |
| 134 | |
| 135 | void AsmEditor::Zoom(int amount) |
| 136 | { |
| 137 | if (amount > 0) |
| 138 | { |
| 139 | zoomIn(amount); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | zoomOut(-amount); |
| 144 | } |
| 145 | m_line_number_area->setFont(font()); |
| 146 | } |
| 147 | |
| 148 | bool AsmEditor::SaveFile(const QString& save_path) |
| 149 | { |
| 150 | QFile file(save_path); |
| 151 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) |
| 152 | { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | if (m_path != save_path) |
| 157 | { |
| 158 | m_path = save_path; |
| 159 | m_filename = |
| 160 | QString::fromStdString(std::filesystem::path(m_path.toStdString()).filename().string()); |
| 161 | emit PathChanged(); |
| 162 | } |
| 163 | |
| 164 | if (file.write(QStringLiteral("#%1\n").arg(m_base_address).toUtf8()) == -1) |
| 165 | { |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | if (file.write(toPlainText().toUtf8()) == -1) |
| 170 | { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | m_dirty = false; |
| 175 | emit DirtyChanged(); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | void AsmEditor::UpdateLineNumberAreaWidth(int) |
| 180 | { |
| 181 | setViewportMargins(LineNumberAreaWidth(), 0, 0, 0); |
| 182 | } |
| 183 | |
| 184 | void AsmEditor::UpdateLineNumberArea(const QRect& rect, int dy) |
| 185 | { |
| 186 | if (dy != 0) |
| 187 | { |
| 188 | m_line_number_area->scroll(0, dy); |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | m_line_number_area->update(0, rect.y(), m_line_number_area->width(), rect.height()); |
| 193 | } |
| 194 | |
| 195 | if (rect.contains(viewport()->rect())) |
| 196 | { |
| 197 | UpdateLineNumberAreaWidth(0); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | int AsmEditor::CharWidth() const |
| 202 | { |
| 203 | return fontMetrics().horizontalAdvance(QLatin1Char(' ')); |
| 204 | } |
| 205 | |
| 206 | void AsmEditor::resizeEvent(QResizeEvent* e) |
| 207 | { |
| 208 | QPlainTextEdit::resizeEvent(e); |
| 209 | |
| 210 | const QRect cr = contentsRect(); |
| 211 | m_line_number_area->setGeometry(QRect(cr.left(), cr.top(), LineNumberAreaWidth(), cr.height())); |
| 212 | } |
| 213 | |
| 214 | void AsmEditor::paintEvent(QPaintEvent* event) |
| 215 | { |
| 216 | QPlainTextEdit::paintEvent(event); |
| 217 | |
| 218 | QPainter painter(viewport()); |
| 219 | QTextCursor tc(document()); |
| 220 | |
| 221 | QPen p = QPen(Qt::red); |
| 222 | p.setStyle(Qt::PenStyle::SolidLine); |
| 223 | p.setWidth(1); |
| 224 | painter.setPen(p); |
| 225 | const int width = CharWidth(); |
| 226 | |
| 227 | for (QTextBlock blk = firstVisibleBlock(); blk.isVisible() && blk.isValid(); blk = blk.next()) |
| 228 | { |
| 229 | if (blk.userData() == nullptr) |
| 230 | { |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | BlockInfo* info = static_cast<BlockInfo*>(blk.userData()); |
| 235 | if (info->error_at_eol) |
| 236 | { |
| 237 | tc.setPosition(blk.position() + blk.length() - 1); |
| 238 | tc.clearSelection(); |
| 239 | const QRect qr = cursorRect(tc); |
| 240 | painter.drawLine(qr.x(), qr.y() + qr.height(), qr.x() + width, qr.y() + qr.height()); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | bool AsmEditor::event(QEvent* e) |
| 246 | { |
| 247 | if (e->type() == QEvent::ToolTip) |
| 248 | { |
| 249 | QHelpEvent* he = static_cast<QHelpEvent*>(e); |
| 250 | QTextCursor hover_cursor = cursorForPosition(he->pos()); |
| 251 | QTextBlock hover_block = hover_cursor.block(); |
| 252 | |
| 253 | BlockInfo* info = static_cast<BlockInfo*>(hover_block.userData()); |
| 254 | if (info == nullptr || !info->error) |
| 255 | { |
| 256 | QToolTip::hideText(); |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | QRect check_rect; |
| 261 | if (info->error_at_eol) |
| 262 | { |
| 263 | hover_cursor.setPosition(hover_block.position() + |
| 264 | static_cast<int>(info->error->col + info->error->len)); |
| 265 | const QRect cursor_left = cursorRect(hover_cursor); |
| 266 | const int area_width = CharWidth(); |
| 267 | check_rect = QRect(cursor_left.x() + LineNumberAreaWidth(), cursor_left.y(), |
| 268 | cursor_left.x() + area_width, cursor_left.height()); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | hover_cursor.setPosition(hover_block.position() + static_cast<int>(info->error->col)); |
| 273 | const QRect cursor_left = cursorRect(hover_cursor); |
| 274 | hover_cursor.setPosition(hover_block.position() + |
| 275 | static_cast<int>(info->error->col + info->error->len)); |
| 276 | const QRect cursor_right = cursorRect(hover_cursor); |
| 277 | check_rect = QRect(cursor_left.x() + LineNumberAreaWidth(), cursor_left.y(), |
| 278 | cursor_right.x() - cursor_left.x(), cursor_left.height()); |
| 279 | } |
| 280 | if (check_rect.contains(he->pos())) |
| 281 | { |
| 282 | QToolTip::showText(he->globalPos(), QString::fromStdString(info->error->message)); |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | QToolTip::hideText(); |
| 287 | } |
| 288 | return true; |
| 289 | } |
| 290 | return QPlainTextEdit::event(e); |
| 291 | } |
| 292 | |
| 293 | void AsmEditor::keyPressEvent(QKeyEvent* event) |
| 294 | { |
| 295 | // HACK: Change shift+enter to enter to keep lines as blocks |
| 296 | if (event->modifiers() & Qt::ShiftModifier && |
| 297 | (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return)) |
| 298 | { |
| 299 | event->setModifiers(event->modifiers() & ~Qt::ShiftModifier); |
| 300 | } |
| 301 | QPlainTextEdit::keyPressEvent(event); |
| 302 | } |
| 303 | |
| 304 | void AsmEditor::wheelEvent(QWheelEvent* event) |
| 305 | { |
| 306 | QPlainTextEdit::wheelEvent(event); |
| 307 | |
| 308 | if (event->modifiers() & Qt::ControlModifier) |
| 309 | { |
| 310 | auto delta = static_cast<int>(std::round((event->angleDelta().y() / 120.0))); |
| 311 | if (delta != 0) |
| 312 | { |
| 313 | emit ZoomRequested(delta); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void AsmEditor::HighlightCurrentLine() |
| 319 | { |
| 320 | const bool old_state = blockSignals(true); |
| 321 | |
| 322 | if (m_last_block.blockNumber() != textCursor().blockNumber()) |
| 323 | { |
| 324 | m_highlighter->SetMode(2); |
| 325 | m_highlighter->rehighlightBlock(m_last_block); |
| 326 | |
| 327 | m_last_block = textCursor().block(); |
| 328 | } |
| 329 | |
| 330 | m_highlighter->SetCursorLoc(textCursor().positionInBlock()); |
| 331 | m_highlighter->SetMode(1); |
| 332 | m_highlighter->rehighlightBlock(textCursor().block()); |
| 333 | m_highlighter->SetMode(0); |
| 334 | |
| 335 | blockSignals(old_state); |
| 336 | } |
| 337 | |
| 338 | void AsmEditor::LineNumberAreaPaintEvent(QPaintEvent* event) |
| 339 | { |
| 340 | QPainter painter(m_line_number_area); |
| 341 | if (m_dark_scheme) |
| 342 | { |
| 343 | painter.fillRect(event->rect(), QColor::fromRgb(76, 76, 76)); |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | painter.fillRect(event->rect(), QColor::fromRgb(180, 180, 180)); |
| 348 | } |
| 349 | |
| 350 | QTextBlock block = firstVisibleBlock(); |
| 351 | int block_num = block.blockNumber(); |
| 352 | int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top()); |
| 353 | int bottom = top + qRound(blockBoundingRect(block).height()); |
| 354 | |
| 355 | while (block.isValid() && top <= event->rect().bottom()) |
| 356 | { |
| 357 | if (block.isVisible() && bottom >= event->rect().top()) |
| 358 | { |
| 359 | const QString num = QString::number(block_num + 1); |
| 360 | painter.drawText(0, top, m_line_number_area->width(), fontMetrics().height(), Qt::AlignRight, |
| 361 | num); |
| 362 | } |
| 363 | |
| 364 | block = block.next(); |
| 365 | top = bottom; |
| 366 | bottom = top + qRound(blockBoundingRect(block).height()); |
| 367 | ++block_num; |
| 368 | } |
| 369 | } |