Skip to content

Commit

Permalink
add search to disassembler
Browse files Browse the repository at this point in the history
closes #425
  • Loading branch information
lievenhey committed Jan 6, 2023
1 parent 1c7765c commit c34e53e
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/models/sourcecodemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,41 @@ void SourceCodeModel::setSysroot(const QString& sysroot)
{
m_sysroot = sysroot;
}

void SourceCodeModel::findForward(const QString& search, const QModelIndex& start)
{
for (int i = 1; i < m_numLines; i++) {
// start searching at start and wrap around when you reach the end
int lineIndex = i + (start.isValid() ? start.row() : 0);
lineIndex %= m_numLines;

const auto block = m_document->findBlockByLineNumber(lineIndex + m_startLine);
if (!block.isValid())
continue;

if (block.text().contains(search)) {
emit searchResultFound(index(lineIndex, SourceCodeColumn));
return;
}
}
emit noSearchResult();
}

void SourceCodeModel::findBackwards(const QString& search, const QModelIndex& start)
{
for (int i = m_numLines - 1; i > 0; i--) {
// same as above but we move in the other direction
int lineIndex = i + (start.isValid() ? start.row() : 0);
lineIndex %= m_numLines;

const auto block = m_document->findBlockByLineNumber(lineIndex + m_startLine);
if (!block.isValid())
continue;

if (block.text().contains(search)) {
emit searchResultFound(index(lineIndex, SourceCodeColumn));
return;
}
}
emit noSearchResult();
}
7 changes: 7 additions & 0 deletions src/models/sourcecodemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,17 @@ class SourceCodeModel : public QAbstractTableModel
FileLineRole,
};

signals:
void searchResultFound(const QModelIndex& index);
void noSearchResult();

public slots:
void updateHighlighting(int line);
void setSysroot(const QString& sysroot);

void findForward(const QString& search, const QModelIndex& start);
void findBackwards(const QString& search, const QModelIndex& start);

private:
QString m_sysroot;
QSet<int> m_validLineNumbers;
Expand Down
38 changes: 38 additions & 0 deletions src/resultsdisassemblypage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QMenu>
#include <QMessageBox>
#include <QProcess>
#include <QShortcut>
#include <QStandardItemModel>
#include <QStandardPaths>
#include <QString>
Expand Down Expand Up @@ -168,6 +169,37 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(CostContextMenu* costContextMenu,
showDisassembly();
});

auto searchShortcut = new QShortcut(QKeySequence::Find, this);
connect(searchShortcut, &QShortcut::activated, this, [this] {
ui->searchWidget->setVisible(true);
ui->searchEdit->setFocus();
});

connect(ui->closeSearch, &QPushButton::pressed, this, [this] { ui->searchWidget->setVisible(false); });

connect(ui->nextSearchResult, &QPushButton::pressed, this,
[this] { m_sourceCodeModel->findForward(ui->searchEdit->text(), m_searchResultIndex); });

connect(ui->prevSearchResult, &QPushButton::pressed, this,
[this] { m_sourceCodeModel->findBackwards(ui->searchEdit->text(), m_searchResultIndex); });

connect(m_sourceCodeModel, &SourceCodeModel::searchResultFound, this, [this](const QModelIndex& index) {
m_searchResultIndex = index;
ui->sourceCodeView->scrollTo(m_searchResultIndex, QAbstractItemView::ScrollHint::PositionAtCenter);
ui->searchResultWarning->hide();
});

// search if enter is pressed
connect(ui->searchEdit, &QLineEdit::returnPressed, ui->nextSearchResult, &QPushButton::pressed);

// I don't know how to show an error so I use a warning widget
// kate, ... set the backgroundcolor to red but I don't know how they do this
// google say ->setStylesheet("background: red");
connect(m_sourceCodeModel, &SourceCodeModel::noSearchResult, this, [this] {
ui->searchResultWarning->setText(tr("%1 was not found").arg(ui->searchEdit->text()));
ui->searchResultWarning->show();
});

#if KF5SyntaxHighlighting_FOUND
QStringList schemes;

Expand Down Expand Up @@ -258,6 +290,12 @@ void ResultsDisassemblyPage::showDisassembly()
return isArm ? QStringLiteral("arm-linux-gnueabi-objdump") : QStringLiteral("objdump");
};

// hide search
ui->searchWidget->hide();
ui->searchResultWarning->hide();
ui->searchEdit->clear();
m_searchResultIndex = {};

showDisassembly(DisassemblyOutput::disassemble(objdump(), m_arch, m_symbolStack[m_stackIndex]));
}

Expand Down
2 changes: 2 additions & 0 deletions src/resultsdisassemblypage.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class ResultsDisassemblyPage : public QWidget
CodeDelegate* m_disassemblyDelegate;
CodeDelegate* m_sourceCodeDelegate;

QModelIndex m_searchResultIndex = {};

QVector<Data::Symbol> m_symbolStack;
int m_stackIndex = 0;
};
73 changes: 71 additions & 2 deletions src/resultsdisassemblypage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
<string/>
</property>
<property name="icon">
<iconset theme="go-previous"/>
<iconset theme="go-previous">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
Expand All @@ -97,7 +98,8 @@
<string/>
</property>
<property name="icon">
<iconset theme="go-next"/>
<iconset theme="go-next">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
Expand Down Expand Up @@ -140,6 +142,16 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="KMessageWidget" name="searchResultWarning">
<property name="enabled">
<bool>true</bool>
</property>
<property name="messageType">
<enum>KMessageWidget::Warning</enum>
</property>
</widget>
</item>
<item>
<widget class="QTreeView" name="sourceCodeView">
<property name="alternatingRowColors">
Expand All @@ -153,6 +165,63 @@
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="searchWidget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="searchEdit"/>
</item>
<item>
<widget class="QPushButton" name="nextSearchResult">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="go-down">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="prevSearchResult">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="go-up">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="closeSearch">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset theme="window-close">
<normaloff>.</normaloff>.</iconset>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="customSourceCodeHighlighting" native="true">
<property name="enabled">
Expand Down

1 comment on commit c34e53e

@GitMensch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature itself works fine. To allow people to find it I highly suggest to add that in the context menu (currently consistsing of only "Open in Editor").

Please # to comment.