cppcurrentdocumentfilter.cpp 5.87 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30
#include "cppcurrentdocumentfilter.h"
31

32 33
#include "cppmodelmanager.h"

34
#include <QStringMatcher>
Robert Loehning's avatar
Robert Loehning committed
35

36 37 38
using namespace CppTools::Internal;
using namespace CPlusPlus;

39
CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppModelManager *manager)
40 41
    : m_modelManager(manager)
{
hjk's avatar
hjk committed
42
    setId("Methods in current Document");
43
    setDisplayName(tr("C++ Symbols in Current Document"));
44
    setShortcutString(QString(QLatin1Char('.')));
45 46
    setIncludedByDefault(false);

47 48 49 50
    search.setSymbolsToSearchFor(SymbolSearcher::Declarations |
                                 SymbolSearcher::Enums |
                                 SymbolSearcher::Functions |
                                 SymbolSearcher::Classes);
51 52 53

    connect(manager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
            this,    SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
54
    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
55
            this,          SLOT(onCurrentEditorChanged(Core::IEditor*)));
56
    connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
57 58 59
            this,          SLOT(onEditorAboutToClose(Core::IEditor*)));
}

60
QList<Locator::FilterEntry> CppCurrentDocumentFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString & origEntry)
61 62
{
    QString entry = trimWildcards(origEntry);
con's avatar
con committed
63 64
    QList<Locator::FilterEntry> goodEntries;
    QList<Locator::FilterEntry> betterEntries;
65
    QStringMatcher matcher(entry, Qt::CaseInsensitive);
66
    const QChar asterisk = QLatin1Char('*');
67
    QRegExp regexp(asterisk + entry + asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
68 69
    if (!regexp.isValid())
        return goodEntries;
70
    bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?')));
71 72 73 74 75 76

    if (m_currentFileName.isEmpty())
        return goodEntries;

    if (m_itemsOfCurrentDoc.isEmpty()) {
        Snapshot snapshot = m_modelManager->snapshot();
77
        Document::Ptr thisDocument = snapshot.document(m_currentFileName);
78 79 80 81
        if (thisDocument)
            m_itemsOfCurrentDoc = search(thisDocument);
    }

82 83
    const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);

84 85
    foreach (const ModelItemInfo & info, m_itemsOfCurrentDoc)
    {
86 87 88
        if (future.isCanceled())
            break;

89 90 91 92 93
        QString matchString = info.symbolName;
        if (info.type == ModelItemInfo::Declaration)
            matchString = ModelItemInfo::representDeclaration(info.symbolName, info.symbolType);
        else if (info.type == ModelItemInfo::Method)
            matchString += info.symbolType;
94 95 96

        if ((hasWildcard && regexp.exactMatch(matchString))
            || (!hasWildcard && matcher.indexIn(matchString) != -1))
97 98
        {
            QVariant id = qVariantFromValue(info);
99 100
            QString name = matchString;
            QString extraInfo = info.symbolScope;
101 102 103 104
            if (info.type == ModelItemInfo::Method) {
                if (info.unqualifiedNameAndScope(matchString, &name, &extraInfo))
                    name += info.symbolType;
            }
105 106
            Locator::FilterEntry filterEntry(this, name, id, info.icon);
            filterEntry.extraInfo = extraInfo;
107

108
            if (matchString.startsWith(entry, caseSensitivityForPrefix))
109 110 111 112 113 114 115 116 117 118 119 120
                betterEntries.append(filterEntry);
            else
                goodEntries.append(filterEntry);
        }
    }

    // entries are unsorted by design!

    betterEntries += goodEntries;
    return betterEntries;
}

con's avatar
con committed
121
void CppCurrentDocumentFilter::accept(Locator::FilterEntry selection) const
122
{
123
    ModelItemInfo info = qvariant_cast<CppTools::ModelItemInfo>(selection.internalData);
Eike Ziller's avatar
Eike Ziller committed
124
    Core::EditorManager::openEditorAt(info.fileName, info.line, info.column);
125 126 127 128
}

void CppCurrentDocumentFilter::refresh(QFutureInterface<void> &future)
{
129
    Q_UNUSED(future)
130 131 132 133
}

void CppCurrentDocumentFilter::onDocumentUpdated(Document::Ptr doc)
{
134
    if (m_currentFileName == doc->fileName())
135 136 137 138 139
        m_itemsOfCurrentDoc.clear();
}

void CppCurrentDocumentFilter::onCurrentEditorChanged(Core::IEditor * currentEditor)
{
140
    if (currentEditor)
141
        m_currentFileName = currentEditor->document()->filePath();
142
    else
143 144 145 146 147 148 149
        m_currentFileName.clear();
    m_itemsOfCurrentDoc.clear();
}

void CppCurrentDocumentFilter::onEditorAboutToClose(Core::IEditor * editorAboutToClose)
{
    if (!editorAboutToClose) return;
150
    if (m_currentFileName == editorAboutToClose->document()->filePath()) {
151 152 153 154
        m_currentFileName.clear();
        m_itemsOfCurrentDoc.clear();
    }
}