Commit baf10259 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Keep the current explicitly selected item.

Done with: Thorbjørn Lindeijer
parent ced86184
......@@ -696,6 +696,10 @@ int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
// always remove duplicates
m_completions = removeDuplicates(m_completions);
}
for (int i = 0; i < m_completions.size(); ++i)
m_completions[i].originalIndex = i;
return index;
}
......
......@@ -131,6 +131,8 @@ void CompletionSupport::autoComplete_helper(ITextEditable *editor, bool forced,
m_editor = editor;
QList<CompletionItem> completionItems;
int currentIndex = 0;
if (!m_completionList) {
if (!forced) {
const CompletionSettings &completionSettings = m_completionCollector->completionSettings();
......@@ -170,10 +172,24 @@ void CompletionSupport::autoComplete_helper(ITextEditable *editor, bool forced,
m_completionList->closeList();
return;
}
if (m_completionList->explicitlySelected()) {
const int originalIndex = m_completionList->currentCompletionItem().originalIndex;
for (int index = 0; index < completionItems.size(); ++index) {
if (completionItems.at(index).originalIndex == originalIndex) {
currentIndex = index;
break;
}
}
}
}
m_completionList->setCompletionItems(completionItems);
if (currentIndex)
m_completionList->setCurrentIndex(currentIndex);
// Partially complete when completion was forced
if (forced && m_completionCollector->partiallyComplete(completionItems)) {
m_checkCompletionTrigger = true;
......
......@@ -204,6 +204,21 @@ QChar CompletionWidget::typedChar() const
return m_completionListView->m_typedChar;
}
CompletionItem CompletionWidget::currentCompletionItem() const
{
return m_completionListView->currentCompletionItem();
}
bool CompletionWidget::explicitlySelected() const
{
return m_completionListView->explicitlySelected();
}
void CompletionWidget::setCurrentIndex(int index)
{
m_completionListView->setCurrentIndex(m_completionListView->model()->index(index, 0));
}
void CompletionWidget::updatePositionAndSize(int startPos)
{
// Determine size by calculating the space of the visible items
......@@ -257,7 +272,8 @@ CompletionListView::CompletionListView(CompletionSupport *support, ITextEditable
m_editorWidget(editor->widget()),
m_completionWidget(completionWidget),
m_model(new AutoCompletionModel(this)),
m_support(support)
m_support(support),
m_explicitlySelected(false)
{
QTC_ASSERT(m_editorWidget, return);
......@@ -284,6 +300,20 @@ CompletionListView::~CompletionListView()
{
}
CompletionItem CompletionListView::currentCompletionItem() const
{
int row = currentIndex().row();
if (row >= 0 && row < m_model->rowCount())
return m_model->itemAt(currentIndex());
return CompletionItem();
}
bool CompletionListView::explicitlySelected() const
{
return m_explicitlySelected;
}
void CompletionListView::maybeShowInfoTip()
{
QModelIndex current = currentIndex();
......@@ -389,6 +419,7 @@ bool CompletionListView::event(QEvent *e)
return true;
case Qt::Key_Up:
m_explicitlySelected = true;
if (!ke->isAutoRepeat()
&& currentIndex().row() == 0) {
setCurrentIndex(model()->index(model()->rowCount()-1, 0));
......@@ -398,6 +429,7 @@ bool CompletionListView::event(QEvent *e)
break;
case Qt::Key_Down:
m_explicitlySelected = true;
if (!ke->isAutoRepeat()
&& currentIndex().row() == model()->rowCount()-1) {
setCurrentIndex(model()->index(0, 0));
......
......@@ -62,6 +62,10 @@ public:
void showCompletions(int startPos);
QChar typedChar() const;
CompletionItem currentCompletionItem() const;
void setCurrentIndex(int index);
bool explicitlySelected() const;
signals:
void itemSelected(const TextEditor::CompletionItem &item);
......@@ -86,6 +90,9 @@ class CompletionListView : public QListView
public:
~CompletionListView();
CompletionItem currentCompletionItem() const;
bool explicitlySelected() const;
signals:
void itemSelected(const TextEditor::CompletionItem &item);
void completionListClosed();
......@@ -118,6 +125,7 @@ private:
QPointer<CompletionInfoFrame> m_infoFrame;
QTimer m_infoTimer;
QChar m_typedChar;
bool m_explicitlySelected;
};
} // namespace Internal
......
......@@ -52,6 +52,7 @@ struct CompletionItem
: relevance(0),
duplicateCount(0),
order(0),
originalIndex(0),
collector(collector)
{ }
......@@ -65,6 +66,7 @@ struct CompletionItem
int relevance;
int duplicateCount;
int order;
int originalIndex;
ICompletionCollector *collector;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment