2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2011-04-15 16:19:23 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-04-15 16:19:23 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
#include "basicproposalitemlistmodel.h"
|
|
|
|
|
#include "basicproposalitem.h"
|
2013-08-29 16:36:42 +02:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
|
|
|
|
#include <texteditor/completionsettings.h>
|
2011-04-15 16:19:23 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QRegExp>
|
|
|
|
|
#include <QtAlgorithms>
|
|
|
|
|
#include <QHash>
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
uint qHash(const BasicProposalItem &item)
|
|
|
|
|
{
|
|
|
|
|
return qHash(item.text());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
const int kMaxSort = 1000;
|
|
|
|
|
const int kMaxPrefixFilter = 100;
|
|
|
|
|
|
|
|
|
|
struct ContentLessThan
|
|
|
|
|
{
|
2013-08-28 01:02:26 +04:00
|
|
|
ContentLessThan(const QString &prefix)
|
|
|
|
|
: m_prefix(prefix)
|
|
|
|
|
{}
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
bool operator()(const BasicProposalItem *a, const BasicProposalItem *b)
|
|
|
|
|
{
|
|
|
|
|
// The order is case-insensitive in principle, but case-sensitive when this
|
|
|
|
|
// would otherwise mean equality
|
|
|
|
|
const QString &lowera = a->text().toLower();
|
|
|
|
|
const QString &lowerb = b->text().toLower();
|
2013-08-28 01:02:26 +04:00
|
|
|
const QString &lowerprefix = m_prefix.toLower();
|
|
|
|
|
|
|
|
|
|
// All continuations should go before all fuzzy matches
|
|
|
|
|
if (int diff = lowera.startsWith(lowerprefix) - lowerb.startsWith(lowerprefix))
|
|
|
|
|
return diff > 0;
|
|
|
|
|
if (int diff = a->text().startsWith(m_prefix) - b->text().startsWith(m_prefix))
|
|
|
|
|
return diff > 0;
|
|
|
|
|
|
|
|
|
|
// If order is different, show higher ones first.
|
|
|
|
|
if (a->order() != b->order())
|
|
|
|
|
return a->order() > b->order();
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
if (lowera == lowerb)
|
|
|
|
|
return lessThan(a->text(), b->text());
|
|
|
|
|
else
|
|
|
|
|
return lessThan(lowera, lowerb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool lessThan(const QString &a, const QString &b)
|
|
|
|
|
{
|
2012-06-21 11:29:39 +02:00
|
|
|
QString::const_iterator pa = a.begin();
|
|
|
|
|
QString::const_iterator pb = b.begin();
|
|
|
|
|
|
|
|
|
|
CharLessThan charLessThan;
|
|
|
|
|
enum { Letter, SmallerNumber, BiggerNumber } state = Letter;
|
|
|
|
|
for (; pa != a.end() && pb != b.end(); ++pa, ++pb) {
|
|
|
|
|
if (*pa == *pb)
|
|
|
|
|
continue;
|
|
|
|
|
if (state != Letter) {
|
|
|
|
|
if (!pa->isDigit() || !pb->isDigit())
|
|
|
|
|
break;
|
|
|
|
|
} else if (pa->isDigit() && pb->isDigit()) {
|
|
|
|
|
if (charLessThan(*pa, *pb))
|
|
|
|
|
state = SmallerNumber;
|
|
|
|
|
else
|
|
|
|
|
state = BiggerNumber;
|
|
|
|
|
} else {
|
|
|
|
|
return charLessThan(*pa, *pb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state == Letter)
|
|
|
|
|
return pa == a.end() && pb != b.end();
|
|
|
|
|
if (pa != a.end() && pa->isDigit())
|
|
|
|
|
return false; //more digits
|
|
|
|
|
if (pb != b.end() && pb->isDigit())
|
|
|
|
|
return true; //fewer digits
|
|
|
|
|
return state == SmallerNumber; //same length, compare first different digit in the sequence
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct CharLessThan
|
|
|
|
|
{
|
|
|
|
|
bool operator()(const QChar &a, const QChar &b)
|
|
|
|
|
{
|
|
|
|
|
if (a == QLatin1Char('_'))
|
|
|
|
|
return false;
|
|
|
|
|
else if (b == QLatin1Char('_'))
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return a < b;
|
|
|
|
|
}
|
|
|
|
|
};
|
2013-08-28 01:02:26 +04:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QString m_prefix;
|
2011-04-15 16:19:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // Anonymous
|
|
|
|
|
|
|
|
|
|
BasicProposalItemListModel::BasicProposalItemListModel()
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
BasicProposalItemListModel::BasicProposalItemListModel(const QList<BasicProposalItem *> &items)
|
2013-02-11 13:27:59 +01:00
|
|
|
: m_currentItems(items)
|
|
|
|
|
, m_originalItems(items)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
|
|
|
|
mapPersistentIds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BasicProposalItemListModel::~BasicProposalItemListModel()
|
|
|
|
|
{
|
|
|
|
|
qDeleteAll(m_originalItems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasicProposalItemListModel::loadContent(const QList<BasicProposalItem *> &items)
|
|
|
|
|
{
|
|
|
|
|
m_originalItems = items;
|
|
|
|
|
m_currentItems = items;
|
|
|
|
|
mapPersistentIds();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasicProposalItemListModel::mapPersistentIds()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_originalItems.size(); ++i)
|
|
|
|
|
m_idByText.insert(m_originalItems.at(i)->text(), i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasicProposalItemListModel::reset()
|
|
|
|
|
{
|
|
|
|
|
m_currentItems = m_originalItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int BasicProposalItemListModel::size() const
|
|
|
|
|
{
|
|
|
|
|
return m_currentItems.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BasicProposalItemListModel::text(int index) const
|
|
|
|
|
{
|
|
|
|
|
return m_currentItems.at(index)->text();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QIcon BasicProposalItemListModel::icon(int index) const
|
|
|
|
|
{
|
|
|
|
|
return m_currentItems.at(index)->icon();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString BasicProposalItemListModel::detail(int index) const
|
|
|
|
|
{
|
|
|
|
|
return m_currentItems.at(index)->detail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasicProposalItemListModel::removeDuplicates()
|
|
|
|
|
{
|
|
|
|
|
QHash<QString, QVariant> unique;
|
|
|
|
|
QList<BasicProposalItem *>::iterator it = m_originalItems.begin();
|
|
|
|
|
while (it != m_originalItems.end()) {
|
|
|
|
|
const BasicProposalItem *item = *it;
|
|
|
|
|
if (unique.contains(item->text())
|
|
|
|
|
&& unique.value(item->text(), QVariant()) == item->data()) {
|
|
|
|
|
it = m_originalItems.erase(it);
|
|
|
|
|
} else {
|
|
|
|
|
unique.insert(item->text(), item->data());
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BasicProposalItemListModel::filter(const QString &prefix)
|
|
|
|
|
{
|
|
|
|
|
if (prefix.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This code builds a regular expression in order to more intelligently match
|
2012-10-14 15:52:42 +02:00
|
|
|
* camel-case and underscore names.
|
2011-04-15 16:19:23 +02:00
|
|
|
*
|
2012-10-14 15:52:42 +02:00
|
|
|
* For any but the first letter, the following replacements are made:
|
|
|
|
|
* A => [a-z0-9_]*A
|
|
|
|
|
* a => (?:[a-zA-Z0-9]*_)?a
|
2011-04-15 16:19:23 +02:00
|
|
|
*
|
2012-10-14 15:52:42 +02:00
|
|
|
* That means any sequence of lower-case or underscore characters can preceed an
|
|
|
|
|
* upper-case character. And any sequence of lower-case or upper case characters -
|
|
|
|
|
* followed by an underscore can preceed a lower-case character.
|
2011-04-15 16:19:23 +02:00
|
|
|
*
|
2012-10-14 15:52:42 +02:00
|
|
|
* Examples: (case sensitive mode)
|
|
|
|
|
* gAC matches getActionController
|
|
|
|
|
* gac matches get_action_controller
|
|
|
|
|
*
|
|
|
|
|
* It also implements the fully and first-letter-only case sensitivity.
|
2011-04-15 16:19:23 +02:00
|
|
|
*/
|
|
|
|
|
const TextEditor::CaseSensitivity caseSensitivity =
|
2013-09-19 17:59:27 +02:00
|
|
|
TextEditorSettings::completionSettings().m_caseSensitivity;
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
QString keyRegExp;
|
|
|
|
|
keyRegExp += QLatin1Char('^');
|
|
|
|
|
bool first = true;
|
2012-10-14 15:52:42 +02:00
|
|
|
const QLatin1String uppercaseWordContinuation("[a-z0-9_]*");
|
|
|
|
|
const QLatin1String lowercaseWordContinuation("(?:[a-zA-Z0-9]*_)?");
|
2011-04-15 16:19:23 +02:00
|
|
|
foreach (const QChar &c, prefix) {
|
|
|
|
|
if (caseSensitivity == TextEditor::CaseInsensitive ||
|
|
|
|
|
(caseSensitivity == TextEditor::FirstLetterCaseSensitive && !first)) {
|
|
|
|
|
|
|
|
|
|
keyRegExp += QLatin1String("(?:");
|
2012-10-14 15:52:42 +02:00
|
|
|
if (!first)
|
|
|
|
|
keyRegExp += uppercaseWordContinuation;
|
2011-04-15 16:19:23 +02:00
|
|
|
keyRegExp += QRegExp::escape(c.toUpper());
|
|
|
|
|
keyRegExp += QLatin1Char('|');
|
2012-10-14 15:52:42 +02:00
|
|
|
if (!first)
|
|
|
|
|
keyRegExp += lowercaseWordContinuation;
|
2011-04-15 16:19:23 +02:00
|
|
|
keyRegExp += QRegExp::escape(c.toLower());
|
|
|
|
|
keyRegExp += QLatin1Char(')');
|
|
|
|
|
} else {
|
2012-10-14 15:52:42 +02:00
|
|
|
if (!first) {
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (c.isUpper())
|
2012-10-14 15:52:42 +02:00
|
|
|
keyRegExp += uppercaseWordContinuation;
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
else
|
2012-10-14 15:52:42 +02:00
|
|
|
keyRegExp += lowercaseWordContinuation;
|
|
|
|
|
}
|
2011-04-15 16:19:23 +02:00
|
|
|
keyRegExp += QRegExp::escape(c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
2012-04-30 12:01:15 +02:00
|
|
|
QRegExp regExp(keyRegExp);
|
2011-04-15 16:19:23 +02:00
|
|
|
|
|
|
|
|
m_currentItems.clear();
|
|
|
|
|
for (QList<BasicProposalItem *>::const_iterator it = m_originalItems.begin();
|
|
|
|
|
it != m_originalItems.end();
|
|
|
|
|
++it) {
|
|
|
|
|
BasicProposalItem *item = *it;
|
|
|
|
|
if (regExp.indexIn(item->text()) == 0)
|
|
|
|
|
m_currentItems.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-10 12:58:05 +01:00
|
|
|
bool BasicProposalItemListModel::isSortable(const QString &prefix) const
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2012-02-10 12:58:05 +01:00
|
|
|
Q_UNUSED(prefix);
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
if (m_currentItems.size() < kMaxSort)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 01:02:26 +04:00
|
|
|
void BasicProposalItemListModel::sort(const QString &prefix)
|
2011-04-15 16:19:23 +02:00
|
|
|
{
|
2014-06-16 21:34:13 +04:00
|
|
|
std::stable_sort(m_currentItems.begin(), m_currentItems.end(), ContentLessThan(prefix));
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int BasicProposalItemListModel::persistentId(int index) const
|
|
|
|
|
{
|
|
|
|
|
return m_idByText.value(m_currentItems.at(index)->text());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BasicProposalItemListModel::supportsPrefixExpansion() const
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-21 15:11:16 +02:00
|
|
|
bool BasicProposalItemListModel::keepPerfectMatch(AssistReason reason) const
|
|
|
|
|
{
|
|
|
|
|
return reason != IdleEditor;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 16:19:23 +02:00
|
|
|
QString BasicProposalItemListModel::proposalPrefix() const
|
|
|
|
|
{
|
2013-01-10 16:41:14 +01:00
|
|
|
if (m_currentItems.size() >= kMaxPrefixFilter || m_currentItems.isEmpty())
|
2011-04-15 16:19:23 +02:00
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
// Compute common prefix
|
2013-01-10 16:41:14 +01:00
|
|
|
QString commonPrefix = m_currentItems.first()->text();
|
|
|
|
|
for (int i = 1, ei = m_currentItems.size(); i < ei; ++i) {
|
|
|
|
|
QString nextItem = m_currentItems.at(i)->text();
|
|
|
|
|
const int length = qMin(commonPrefix.length(), nextItem.length());
|
|
|
|
|
commonPrefix.truncate(length);
|
|
|
|
|
nextItem.truncate(length);
|
|
|
|
|
|
|
|
|
|
while (commonPrefix != nextItem) {
|
|
|
|
|
commonPrefix.chop(1);
|
|
|
|
|
nextItem.chop(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (commonPrefix.isEmpty()) // there is no common prefix, so return.
|
|
|
|
|
return commonPrefix;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
2013-01-10 16:41:14 +01:00
|
|
|
return commonPrefix;
|
2011-04-15 16:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAssistProposalItem *BasicProposalItemListModel::proposalItem(int index) const
|
|
|
|
|
{
|
|
|
|
|
return m_currentItems.at(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QPair<QList<BasicProposalItem *>::iterator,
|
|
|
|
|
QList<BasicProposalItem *>::iterator>
|
|
|
|
|
BasicProposalItemListModel::currentItems()
|
|
|
|
|
{
|
|
|
|
|
return qMakePair(m_currentItems.begin(), m_currentItems.end());
|
|
|
|
|
}
|