CppTools: Use cursor range for better outline navigation

Clang provides cursor range for each declaration in
symbol outline. Use that information to search for
more accurate correspondence between a cursor position
in editor and an entry in symbol outline.

For example skip indexes with not matching ranges to
prevent pure declarations from automatically become
parents of everything coming after them.

Change-Id: I0ef95c26772050cd6655e830288c46118aba38bb
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-06-01 09:18:45 +02:00
parent 92530fd05d
commit b0c0119345
7 changed files with 48 additions and 5 deletions

View File

@@ -32,6 +32,8 @@
#include <QSharedPointer>
#include <utility>
namespace CPlusPlus { class Document; }
namespace Utils {
@@ -91,6 +93,9 @@ public:
virtual Utils::Link linkFromIndex(const QModelIndex &) const = 0;
virtual Utils::LineColumn lineColumnFromIndex(const QModelIndex &) const = 0;
using Range = std::pair<Utils::LineColumn, Utils::LineColumn>;
virtual Range rangeFromIndex(const QModelIndex &) const = 0;
signals:
void needsUpdate();
};

View File

@@ -263,6 +263,17 @@ void CppEditorOutline::gotoSymbolInEditor()
emit m_editorWidget->activateEditor();
}
static bool contains(const AbstractOverviewModel::Range &range, int line, int column)
{
if (line < range.first.line || line > range.second.line)
return false;
if (line == range.first.line && column < range.first.column)
return false;
if (line == range.second.line && column > range.second.column)
return false;
return true;
}
QModelIndex CppEditorOutline::indexForPosition(int line, int column,
const QModelIndex &rootIndex) const
{
@@ -270,8 +281,12 @@ QModelIndex CppEditorOutline::indexForPosition(int line, int column,
const int rowCount = m_model->rowCount(rootIndex);
for (int row = 0; row < rowCount; ++row) {
const QModelIndex index = m_model->index(row, 0, rootIndex);
if (m_model->lineColumnFromIndex(index).line > line)
const AbstractOverviewModel::Range range = m_model->rangeFromIndex(index);
if (range.first.line > line)
break;
// Skip ranges that do not include current line and column.
if (range.second != range.first && !contains(range, line, column))
continue;
lastIndex = index;
}

View File

@@ -190,6 +190,12 @@ Utils::LineColumn OverviewModel::lineColumnFromIndex(const QModelIndex &sourceIn
return lineColumn;
}
OverviewModel::Range OverviewModel::rangeFromIndex(const QModelIndex &sourceIndex) const
{
Utils::LineColumn lineColumn = lineColumnFromIndex(sourceIndex);
return std::make_pair(lineColumn, lineColumn);
}
void OverviewModel::buildTree(SymbolItem *root, bool isRoot)
{
if (!root)

View File

@@ -54,6 +54,7 @@ public:
bool isGenerated(const QModelIndex &sourceIndex) const override;
Utils::Link linkFromIndex(const QModelIndex &sourceIndex) const override;
Utils::LineColumn lineColumnFromIndex(const QModelIndex &sourceIndex) const override;
Range rangeFromIndex(const QModelIndex &sourceIndex) const override;
private:
CPlusPlus::Symbol *symbolFromIndex(const QModelIndex &index) const;