forked from qt-creator/qt-creator
Moved Completion::getCompletion() in ICompletionCollector and made the filtering of completion items more C++ friendly.
This commit is contained in:
@@ -1466,6 +1466,43 @@ void CppCodeCompletion::completions(QList<TextEditor::CompletionItem> *completio
|
||||
}
|
||||
}
|
||||
|
||||
QList<TextEditor::CompletionItem> CppCodeCompletion::getCompletions()
|
||||
{
|
||||
QList<TextEditor::CompletionItem> completionItems;
|
||||
|
||||
completions(&completionItems);
|
||||
|
||||
qStableSort(completionItems.begin(), completionItems.end(), completionItemLessThan);
|
||||
|
||||
// Remove duplicates
|
||||
QString lastKey;
|
||||
QList<TextEditor::CompletionItem> uniquelist;
|
||||
|
||||
foreach (const TextEditor::CompletionItem &item, completionItems) {
|
||||
if (item.text != lastKey) {
|
||||
uniquelist.append(item);
|
||||
lastKey = item.text;
|
||||
} else {
|
||||
TextEditor::CompletionItem &lastItem = uniquelist.last();
|
||||
Symbol *symbol = qvariant_cast<Symbol *>(item.data);
|
||||
Symbol *lastSymbol = qvariant_cast<Symbol *>(lastItem.data);
|
||||
|
||||
if (symbol && lastSymbol) {
|
||||
Function *funTy = symbol->type()->asFunctionType();
|
||||
Function *lastFunTy = lastSymbol->type()->asFunctionType();
|
||||
if (funTy && lastFunTy) {
|
||||
if (funTy->argumentCount() == lastFunTy->argumentCount())
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
++lastItem.duplicateCount;
|
||||
}
|
||||
}
|
||||
|
||||
return uniquelist;
|
||||
}
|
||||
|
||||
void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
|
||||
{
|
||||
Symbol *symbol = 0;
|
||||
|
@@ -64,6 +64,7 @@ public:
|
||||
void setObjcEnabled(bool objcEnabled)
|
||||
{ m_objcEnabled = objcEnabled; }
|
||||
|
||||
QList<TextEditor::CompletionItem> getCompletions();
|
||||
bool supportsEditor(TextEditor::ITextEditable *editor);
|
||||
bool triggersCompletion(TextEditor::ITextEditable *editor);
|
||||
int startCompletion(TextEditor::ITextEditable *editor);
|
||||
|
@@ -169,54 +169,10 @@ void CompletionSupport::autoComplete_helper(ITextEditable *editor, bool forced,
|
||||
}
|
||||
}
|
||||
|
||||
static bool compareChar(const QChar &l, const QChar &r)
|
||||
{
|
||||
if (l == QLatin1Char('_'))
|
||||
return false;
|
||||
else if (r == QLatin1Char('_'))
|
||||
return true;
|
||||
else
|
||||
return l < r;
|
||||
}
|
||||
|
||||
static bool lessThan(const QString &l, const QString &r)
|
||||
{
|
||||
return std::lexicographical_compare(l.begin(), l.end(),
|
||||
r.begin(), r.end(),
|
||||
compareChar);
|
||||
}
|
||||
|
||||
static bool completionItemLessThan(const CompletionItem &i1, const CompletionItem &i2)
|
||||
{
|
||||
// The order is case-insensitive in principle, but case-sensitive when this would otherwise mean equality
|
||||
const QString lower1 = i1.text.toLower();
|
||||
const QString lower2 = i2.text.toLower();
|
||||
if (lower1 == lower2)
|
||||
return lessThan(i1.text, i2.text);
|
||||
else
|
||||
return lessThan(lower1, lower2);
|
||||
}
|
||||
|
||||
QList<CompletionItem> CompletionSupport::getCompletions() const
|
||||
{
|
||||
QList<CompletionItem> completionItems;
|
||||
if (m_completionCollector)
|
||||
return m_completionCollector->getCompletions();
|
||||
|
||||
m_completionCollector->completions(&completionItems);
|
||||
|
||||
qStableSort(completionItems.begin(), completionItems.end(), completionItemLessThan);
|
||||
|
||||
// Remove duplicates
|
||||
QString lastKey;
|
||||
QList<CompletionItem> uniquelist;
|
||||
|
||||
foreach (const CompletionItem &item, completionItems) {
|
||||
if (item.text != lastKey) {
|
||||
uniquelist.append(item);
|
||||
lastKey = item.text;
|
||||
} else {
|
||||
uniquelist.last().duplicateCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return uniquelist;
|
||||
return QList<CompletionItem>();
|
||||
}
|
||||
|
86
src/plugins/texteditor/icompletioncollector.cpp
Normal file
86
src/plugins/texteditor/icompletioncollector.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/**************************************************************************
|
||||
**
|
||||
** This file is part of Qt Creator
|
||||
**
|
||||
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
||||
**
|
||||
** Contact: Nokia Corporation (qt-info@nokia.com)
|
||||
**
|
||||
** Commercial Usage
|
||||
**
|
||||
** Licensees holding valid Qt Commercial licenses may use this file in
|
||||
** accordance with the Qt Commercial License Agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and Nokia.
|
||||
**
|
||||
** GNU Lesser General Public License Usage
|
||||
**
|
||||
** Alternatively, this file may be used under the terms of the GNU Lesser
|
||||
** General Public License version 2.1 as published by the Free Software
|
||||
** Foundation and appearing in the file LICENSE.LGPL included in the
|
||||
** packaging of this file. Please review the following information to
|
||||
** ensure the GNU Lesser General Public License version 2.1 requirements
|
||||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
||||
**
|
||||
** If you are unsure which license is appropriate for your use, please
|
||||
** contact the sales department at http://qt.nokia.com/contact.
|
||||
**
|
||||
**************************************************************************/
|
||||
|
||||
#include "icompletioncollector.h"
|
||||
#include <algorithm>
|
||||
|
||||
using namespace TextEditor;
|
||||
|
||||
bool ICompletionCollector::compareChar(const QChar &l, const QChar &r)
|
||||
{
|
||||
if (l == QLatin1Char('_'))
|
||||
return false;
|
||||
else if (r == QLatin1Char('_'))
|
||||
return true;
|
||||
else
|
||||
return l < r;
|
||||
}
|
||||
|
||||
bool ICompletionCollector::lessThan(const QString &l, const QString &r)
|
||||
{
|
||||
return std::lexicographical_compare(l.begin(), l.end(),
|
||||
r.begin(), r.end(),
|
||||
compareChar);
|
||||
}
|
||||
|
||||
bool ICompletionCollector::completionItemLessThan(const CompletionItem &i1, const CompletionItem &i2)
|
||||
{
|
||||
// The order is case-insensitive in principle, but case-sensitive when this would otherwise mean equality
|
||||
const QString lower1 = i1.text.toLower();
|
||||
const QString lower2 = i2.text.toLower();
|
||||
if (lower1 == lower2)
|
||||
return lessThan(i1.text, i2.text);
|
||||
else
|
||||
return lessThan(lower1, lower2);
|
||||
}
|
||||
|
||||
QList<CompletionItem> ICompletionCollector::getCompletions()
|
||||
{
|
||||
QList<CompletionItem> completionItems;
|
||||
|
||||
completions(&completionItems);
|
||||
|
||||
qStableSort(completionItems.begin(), completionItems.end(), completionItemLessThan);
|
||||
|
||||
// Remove duplicates
|
||||
QString lastKey;
|
||||
QList<CompletionItem> uniquelist;
|
||||
|
||||
foreach (const CompletionItem &item, completionItems) {
|
||||
if (item.text != lastKey) {
|
||||
uniquelist.append(item);
|
||||
lastKey = item.text;
|
||||
} else {
|
||||
uniquelist.last().duplicateCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return uniquelist;
|
||||
}
|
||||
|
@@ -76,6 +76,8 @@ public:
|
||||
ICompletionCollector(QObject *parent = 0) : QObject(parent) {}
|
||||
virtual ~ICompletionCollector() {}
|
||||
|
||||
virtual QList<CompletionItem> getCompletions();
|
||||
|
||||
/*
|
||||
* Returns true if this completion collector can be used with the given editor.
|
||||
*/
|
||||
@@ -110,6 +112,11 @@ public:
|
||||
/* Called when it's safe to clean up the completion items.
|
||||
*/
|
||||
virtual void cleanup() = 0;
|
||||
|
||||
protected:
|
||||
static bool compareChar(const QChar &item, const QChar &other);
|
||||
static bool lessThan(const QString &item, const QString &other);
|
||||
static bool completionItemLessThan(const CompletionItem &item, const CompletionItem &other);
|
||||
};
|
||||
|
||||
class TEXTEDITOR_EXPORT IQuickFixCollector : public ICompletionCollector
|
||||
|
@@ -11,6 +11,7 @@ SOURCES += texteditorplugin.cpp \
|
||||
basetexteditor.cpp \
|
||||
behaviorsettingspage.cpp \
|
||||
texteditoractionhandler.cpp \
|
||||
icompletioncollector.cpp \
|
||||
completionsupport.cpp \
|
||||
completionwidget.cpp \
|
||||
fontsettingspage.cpp \
|
||||
|
Reference in New Issue
Block a user