cppcurrentdocumentfilter.cpp 6.72 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** 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
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30

31
#include "cppcurrentdocumentfilter.h"
32

33 34
#include "cppmodelmanager.h"

35
#include <coreplugin/idocument.h>
36 37
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
38

39
#include <QStringMatcher>
Robert Loehning's avatar
Robert Loehning committed
40

41 42 43
using namespace CppTools::Internal;
using namespace CPlusPlus;

44
CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppTools::CppModelManager *manager,
45
                                                   StringTable &stringTable)
46
    : m_modelManager(manager)
47
    , search(stringTable)
48
{
hjk's avatar
hjk committed
49
    setId("Methods in current Document");
50
    setDisplayName(tr("C++ Symbols in Current Document"));
51
    setShortcutString(QString(QLatin1Char('.')));
52
    setPriority(High);
53 54
    setIncludedByDefault(false);

55 56 57 58
    search.setSymbolsToSearchFor(SymbolSearcher::Declarations |
                                 SymbolSearcher::Enums |
                                 SymbolSearcher::Functions |
                                 SymbolSearcher::Classes);
59 60 61

    connect(manager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
            this,    SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
62
    connect(Core::EditorManager::instance(), SIGNAL(currentEditorChanged(Core::IEditor*)),
63
            this,          SLOT(onCurrentEditorChanged(Core::IEditor*)));
64
    connect(Core::EditorManager::instance(), SIGNAL(editorAboutToClose(Core::IEditor*)),
65 66 67
            this,          SLOT(onEditorAboutToClose(Core::IEditor*)));
}

68 69
QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor(
        QFutureInterface<Core::LocatorFilterEntry> &future, const QString & origEntry)
70 71
{
    QString entry = trimWildcards(origEntry);
72 73
    QList<Core::LocatorFilterEntry> goodEntries;
    QList<Core::LocatorFilterEntry> betterEntries;
74
    QStringMatcher matcher(entry, Qt::CaseInsensitive);
75
    const QChar asterisk = QLatin1Char('*');
76
    QRegExp regexp(asterisk + entry + asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
77 78
    if (!regexp.isValid())
        return goodEntries;
79
    bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?')));
80 81
    const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);

82
    foreach (IndexItem::Ptr info, itemsOfCurrentDocument()) {
83 84 85
        if (future.isCanceled())
            break;

86
        QString matchString = info->symbolName();
87
        if (info->type() == IndexItem::Declaration)
88
            matchString = info->representDeclaration();
89
        else if (info->type() == IndexItem::Function)
90
            matchString += info->symbolType();
91 92 93

        if ((hasWildcard && regexp.exactMatch(matchString))
            || (!hasWildcard && matcher.indexIn(matchString) != -1))
94 95
        {
            QVariant id = qVariantFromValue(info);
96
            QString name = matchString;
97
            QString extraInfo = info->symbolScope();
98
            if (info->type() == IndexItem::Function) {
99 100
                if (info->unqualifiedNameAndScope(matchString, &name, &extraInfo))
                    name += info->symbolType();
101
            }
102
            Core::LocatorFilterEntry filterEntry(this, name, id, info->icon());
103
            filterEntry.extraInfo = extraInfo;
104

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

    // entries are unsorted by design!

    betterEntries += goodEntries;
    return betterEntries;
}

118
void CppCurrentDocumentFilter::accept(Core::LocatorFilterEntry selection) const
119
{
120
    IndexItem::Ptr info = qvariant_cast<CppTools::IndexItem::Ptr>(selection.internalData);
121
    Core::EditorManager::openEditorAt(info->fileName(), info->line(), info->column());
122 123 124 125
}

void CppCurrentDocumentFilter::refresh(QFutureInterface<void> &future)
{
126
    Q_UNUSED(future)
127 128 129 130
}

void CppCurrentDocumentFilter::onDocumentUpdated(Document::Ptr doc)
{
131
    QMutexLocker locker(&m_mutex);
132
    if (m_currentFileName == doc->fileName())
133 134 135
        m_itemsOfCurrentDoc.clear();
}

136
void CppCurrentDocumentFilter::onCurrentEditorChanged(Core::IEditor *currentEditor)
137
{
138
    QMutexLocker locker(&m_mutex);
139
    if (currentEditor)
140
        m_currentFileName = currentEditor->document()->filePath().toString();
141
    else
142 143 144 145
        m_currentFileName.clear();
    m_itemsOfCurrentDoc.clear();
}

146
void CppCurrentDocumentFilter::onEditorAboutToClose(Core::IEditor *editorAboutToClose)
147
{
148 149 150 151
    if (!editorAboutToClose)
        return;

    QMutexLocker locker(&m_mutex);
152
    if (m_currentFileName == editorAboutToClose->document()->filePath().toString()) {
153 154 155 156
        m_currentFileName.clear();
        m_itemsOfCurrentDoc.clear();
    }
}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

QList<CppTools::IndexItem::Ptr> CppCurrentDocumentFilter::itemsOfCurrentDocument()
{
    QMutexLocker locker(&m_mutex);

    if (m_currentFileName.isEmpty())
        return QList<CppTools::IndexItem::Ptr>();

    if (m_itemsOfCurrentDoc.isEmpty()) {
        const Snapshot snapshot = m_modelManager->snapshot();
        if (const Document::Ptr thisDocument = snapshot.document(m_currentFileName)) {
            IndexItem::Ptr rootNode = search(thisDocument);
            rootNode->visitAllChildren([&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
                m_itemsOfCurrentDoc.append(info);
                return IndexItem::Recurse;
            });
        }
    }

    return m_itemsOfCurrentDoc;
}