diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp index a61cfde23ff7fcc6afcbdff77a210d21e7ee5f59..5176dd6d307c204ab8ff56737d657a8c6bf36aa5 100644 --- a/src/plugins/cpptools/cppcodecompletion.cpp +++ b/src/plugins/cpptools/cppcodecompletion.cpp @@ -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; diff --git a/src/plugins/cpptools/cppcodecompletion.h b/src/plugins/cpptools/cppcodecompletion.h index 8d908e0b3e8f282c9d42835e488112cb62c2ea28..0b0bb16236854db2b0f7557a394b23c8ebd80902 100644 --- a/src/plugins/cpptools/cppcodecompletion.h +++ b/src/plugins/cpptools/cppcodecompletion.h @@ -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); diff --git a/src/plugins/texteditor/completionsupport.cpp b/src/plugins/texteditor/completionsupport.cpp index 5657650c46f3ca767ccd58f612f68d2c6dae5516..ea5865f3a2805e10ab660e061b40d914ab0cb1f2 100644 --- a/src/plugins/texteditor/completionsupport.cpp +++ b/src/plugins/texteditor/completionsupport.cpp @@ -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; - - 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++; - } - } + if (m_completionCollector) + return m_completionCollector->getCompletions(); - return uniquelist; + return QList<CompletionItem>(); } diff --git a/src/plugins/texteditor/icompletioncollector.cpp b/src/plugins/texteditor/icompletioncollector.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaf68699069ae09d4fbaf0055427c65e2190faf9 --- /dev/null +++ b/src/plugins/texteditor/icompletioncollector.cpp @@ -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; +} + diff --git a/src/plugins/texteditor/icompletioncollector.h b/src/plugins/texteditor/icompletioncollector.h index da5373d035fcb13148f71db9a9b22254fb54cf8b..57941952225d48bfb25c81898999283adb90f217 100644 --- a/src/plugins/texteditor/icompletioncollector.h +++ b/src/plugins/texteditor/icompletioncollector.h @@ -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 diff --git a/src/plugins/texteditor/texteditor.pro b/src/plugins/texteditor/texteditor.pro index e7f90f29a69e3853a9a5085d523c4c0298c3835d..db06739e63969e96afd64f737fe8ce552928e96c 100644 --- a/src/plugins/texteditor/texteditor.pro +++ b/src/plugins/texteditor/texteditor.pro @@ -11,6 +11,7 @@ SOURCES += texteditorplugin.cpp \ basetexteditor.cpp \ behaviorsettingspage.cpp \ texteditoractionhandler.cpp \ + icompletioncollector.cpp \ completionsupport.cpp \ completionwidget.cpp \ fontsettingspage.cpp \