- feature: find references to symbol

This commit is contained in:
royqh1979@gmail.com 2021-09-03 11:50:04 +08:00
parent 9cf43026ba
commit 913a300d0f
6 changed files with 104 additions and 14 deletions

View File

@ -144,6 +144,8 @@ public:
QString getPreviousWordAtPositionForSuggestion(const BufferCoord& p);
void reformat();
void checkSyntaxInBack();
void gotoDeclaration(const BufferCoord& pos);
void gotoDefinition(const BufferCoord& pos);
const PCppParser &parser() const;
@ -197,8 +199,6 @@ private:
QString getHintForFunction(const PStatement& statement, const PStatement& scope,
const QString& filename, int line);
void gotoDeclaration(const BufferCoord& pos);
void gotoDefinition(const BufferCoord& pos);
private:
static int newfileCount;

View File

@ -1151,6 +1151,9 @@ void MainWindow::onEditorContextMenu(const QPoint &pos)
menu.addAction(ui->actionCompile_Run);
menu.addAction(ui->actionDebug);
menu.addSeparator();
menu.addAction(ui->actionGoto_Declaration);
menu.addAction(ui->actionGoto_Definition);
menu.addAction(ui->actionFind_references);
} else {
//mouse on gutter
int line;
@ -2183,3 +2186,23 @@ void MainWindow::on_actionBreakpoint_property_triggered()
}
void MainWindow::on_actionGoto_Declaration_triggered()
{
Editor * editor = mEditorList->getEditor();
BufferCoord pos;
if (editor && editor->PointToCharLine(mContextMenuPos,pos)) {
editor->gotoDeclaration(pos);
}
}
void MainWindow::on_actionGoto_Definition_triggered()
{
Editor * editor = mEditorList->getEditor();
BufferCoord pos;
if (editor && editor->PointToCharLine(mContextMenuPos,pos)) {
editor->gotoDefinition(pos);
}
}

View File

@ -270,6 +270,10 @@ private slots:
void on_actionBreakpoint_property_triggered();
void on_actionGoto_Declaration_triggered();
void on_actionGoto_Definition_triggered();
private:
Ui::MainWindow *ui;
EditorList *mEditorList;

View File

@ -1537,6 +1537,21 @@
<string>Breakpoint property...</string>
</property>
</action>
<action name="actionGoto_Declaration">
<property name="text">
<string>Goto Declaration</string>
</property>
</action>
<action name="actionGoto_Definition">
<property name="text">
<string>Goto Definition</string>
</property>
</action>
<action name="actionFind_references">
<property name="text">
<string>Find references</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -9,7 +9,8 @@ PSearchResults SearchResultModel::addSearchResults(const QString &keyword, SynSe
int index=-1;
for (int i=0;i<mSearchResults.size();i++) {
PSearchResults results = mSearchResults[i];
if (results->keyword == keyword && results->scope == scope) {
if (results->keyword == keyword && results->scope == scope
&& results->searchType == SearchType::Search) {
index=i;
break;
}
@ -24,6 +25,36 @@ PSearchResults SearchResultModel::addSearchResults(const QString &keyword, SynSe
results->keyword = keyword;
results->options = options;
results->scope = scope;
results->searchType = SearchType::Search;
mSearchResults.push_front(results);
mCurrentIndex = 0;
return results;
}
PSearchResults SearchResultModel::addSearchResults(const QString &keyword, const QString &filename, int symbolLine)
{
int index=-1;
for (int i=0;i<mSearchResults.size();i++) {
PSearchResults results = mSearchResults[i];
if (results->searchType == SearchType::FindOccurences
&& results->keyword == keyword
&& results->filename == filename
&& results->symbolLine == symbolLine) {
index=i;
break;
}
}
if (index>=0) {
mSearchResults.removeAt(index);
}
if (mSearchResults.size()>=MAX_SEARCH_RESULTS) {
mSearchResults.pop_back();
}
PSearchResults results = std::make_shared<SearchResults>();
results->keyword = keyword;
results->filename = filename;
results->symbolLine = symbolLine;
results->searchType = SearchType::FindOccurences;
mSearchResults.push_front(results);
mCurrentIndex = 0;
return results;
@ -147,7 +178,7 @@ int SearchResultTreeModel::rowCount(const QModelIndex &parent) const
return item->results.count();
}
int SearchResultTreeModel::columnCount(const QModelIndex &parent) const
int SearchResultTreeModel::columnCount(const QModelIndex &) const
{
return 1;
}
@ -199,7 +230,7 @@ SearchResultListModel::SearchResultListModel(SearchResultModel *model, QObject *
this, &SearchResultListModel::onResultModelChanged);
}
int SearchResultListModel::rowCount(const QModelIndex &parent) const
int SearchResultListModel::rowCount(const QModelIndex &) const
{
return mSearchResultModel->resultsCount();
}
@ -212,13 +243,20 @@ QVariant SearchResultListModel::data(const QModelIndex &index, int role) const
PSearchResults results = mSearchResultModel->results(index.row());
if (!results)
return QVariant();
switch (results->scope) {
case SearchFileScope::currentFile:
return tr("Current File:") + QString(" \"%1\"").arg(results->keyword);
case SearchFileScope::wholeProject:
return tr("Files In Project:") + QString(" \"%1\"").arg(results->keyword);
case SearchFileScope::openedFiles:
return tr("Open Files:") + QString(" \"%1\"").arg(results->keyword);
if (results->searchType == SearchType::Search) {
switch (results->scope) {
case SearchFileScope::currentFile:
return tr("Current File:") + QString(" \"%1\"").arg(results->keyword);
case SearchFileScope::wholeProject:
return tr("Files In Project:") + QString(" \"%1\"").arg(results->keyword);
case SearchFileScope::openedFiles:
return tr("Open Files:") + QString(" \"%1\"").arg(results->keyword);
}
} else if (results->searchType == SearchType::FindOccurences) {
return tr("References to symbol \'%1\' at '%2':%3")
.arg(results->keyword)
.arg(baseFileName(results->filename))
.arg(results->symbolLine);
}
}
return QVariant();

View File

@ -13,6 +13,11 @@ using PSearchResultTreeItem = std::shared_ptr<SearchResultTreeItem>;
using SearchResultTreeItemList = QList<PSearchResultTreeItem>;
using PSearchResultTreeItemList = std::shared_ptr<SearchResultTreeItemList>;
enum class SearchType {
Search,
FindOccurences
};
struct SearchResultTreeItem {
QString filename;
int line;
@ -23,12 +28,13 @@ struct SearchResultTreeItem {
SearchResultTreeItemList results;
};
struct SearchResults{
SynSearchOptions options;
QString keyword;
SearchFileScope scope;
SearchType searchType;
QString filename;
int symbolLine;
QList<PSearchResultTreeItem> results;
};
@ -40,6 +46,10 @@ public:
explicit SearchResultModel(QObject* parent=nullptr);
PSearchResults addSearchResults(const QString& keyword,SynSearchOptions options,
SearchFileScope scope);
PSearchResults addSearchResults(
const QString& keyword,
const QString& filename,
int symbolLine);
PSearchResults results(int index);
void notifySearchResultsUpdated();
int currentIndex() const;