Hi, I just got the time to submit some patches which are floating around in my git, so lets go ...
Attached is a patch that adds a QuickOpen filter for symbols in the currently opened document. Coming from using VC++ together with VisualAssist I was really missing the possibility to quickly move to members or methods in the current document (which is Alt+m in VisualAssist IIRC). My first idea was to add a filter to the "Symbol Combo Box" at the top but as I really like the QuickOpen filter a lot I decided to go that way... Details: -------- * I based my filter on the CppQuickOpenFilter which was the easiest way for me and which fits nicely IMHO. * I resurrected the SearchSymbols::visit(Declaration*) method as I wanted to see declarations in headers, too. I'm not sure if my adopted implementation is perfect, though. It would be nice if someone could have a look at it. * The way the m_currentFile is handled in CppLocalFunctionsFilter may be enhanced. My first implementation simply retrieved the currentFile using something like EditorManager::instance()->currentEditor()->file()->fileName() inside matchesFor(). The current implementation gets the m_currentFile through onCurrentEditorChanged() and onEditorAboutToClose() SLOTs. I preferred the current implementation as is a bit fancier ;). I use to hang out on #qt-creator (choenig) if you're looking for me. I'd be happy to get some feedback or ideas for further improvements :-). thanks and take care, have fun /christian
From 2c0ae5d94e4423b63ef2854d443a012c4faa5a8c Mon Sep 17 00:00:00 2001 From: Christian Hoenig <[email protected]> Date: Sat, 25 Apr 2009 13:45:04 +0200 Subject: [PATCH] QuickOpen filter for symbols in current document --- src/plugins/cpptools/cpplocalfunctionsfilter.cpp | 112 ++++++++++++++++++++++ src/plugins/cpptools/cpplocalfunctionsfilter.h | 68 +++++++++++++ src/plugins/cpptools/cppquickopenfilter.h | 2 +- src/plugins/cpptools/cpptools.pro | 2 + src/plugins/cpptools/cpptoolsplugin.cpp | 2 + src/plugins/cpptools/searchsymbols.cpp | 17 ++-- src/plugins/cpptools/searchsymbols.h | 12 +-- 7 files changed, 200 insertions(+), 15 deletions(-) create mode 100644 src/plugins/cpptools/cpplocalfunctionsfilter.cpp create mode 100644 src/plugins/cpptools/cpplocalfunctionsfilter.h diff --git a/src/plugins/cpptools/cpplocalfunctionsfilter.cpp b/src/plugins/cpptools/cpplocalfunctionsfilter.cpp new file mode 100644 index 0000000..5e5bfea --- /dev/null +++ b/src/plugins/cpptools/cpplocalfunctionsfilter.cpp @@ -0,0 +1,112 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information ([email protected]) +** +** 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 [email protected]. +** +**************************************************************************/ + +#include "cpplocalfunctionsfilter.h" + +#include <coreplugin/editormanager/editormanager.h> +#include <texteditor/itexteditor.h> + +using namespace CppTools::Internal; +using namespace TextEditor; + +CppLocalFunctionsFilter::CppLocalFunctionsFilter(CppModelManager *manager, Core::EditorManager *editorManager) + : CppQuickOpenFilter(manager, editorManager) +{ + setShortcutString("."); + setIncludedByDefault(false); + + search.setSymbolsToSearchFor(SearchSymbols::Classes | + SearchSymbols::Functions | + SearchSymbols::Enums | + SearchSymbols::Declerations); + + search.setSeparateScope(true); + + connect(editorManager, SIGNAL(currentEditorChanged(Core::IEditor*)), + this, SLOT(onCurrentEditorChanged(Core::IEditor*))); + connect(editorManager, SIGNAL(editorAboutToClose(Core::IEditor*)), + this, SLOT(onEditorAboutToClose(Core::IEditor*))); +} + +void CppLocalFunctionsFilter::onCurrentEditorChanged(Core::IEditor * currentEditor) +{ + if (!currentEditor) return; + m_currentFile = currentEditor->file()->fileName(); +} + +void CppLocalFunctionsFilter::onEditorAboutToClose(Core::IEditor * editorAboutToClose) +{ + if (!editorAboutToClose) return; + if (m_currentFile == editorAboutToClose->file()->fileName()) m_currentFile.clear(); +} + +CppLocalFunctionsFilter::~CppLocalFunctionsFilter() +{ +} + +QList<QuickOpen::FilterEntry> CppLocalFunctionsFilter::matchesFor(const QString & origEntry) +{ + QString entry = trimWildcards(origEntry); + QList<QuickOpen::FilterEntry> goodEntries; + QList<QuickOpen::FilterEntry> betterEntries; + QStringMatcher matcher(entry, Qt::CaseInsensitive); + const QRegExp regexp("*"+entry+"*", Qt::CaseInsensitive, QRegExp::Wildcard); + if (!regexp.isValid()) + return goodEntries; + bool hasWildcard = (entry.contains('*') || entry.contains('?')); + + if (m_currentFile.isEmpty() || !m_searchList.contains(m_currentFile)) + return goodEntries; + + Info info = m_searchList[m_currentFile]; + if (info.dirty) { + info.dirty = false; + info.items = search(info.doc); + m_searchList[m_currentFile] = info; + } + + foreach (ModelItemInfo info, info.items) { + if ((hasWildcard && regexp.exactMatch(info.symbolName)) + || (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) + { + QVariant id = qVariantFromValue(info); + QuickOpen::FilterEntry filterEntry(this, info.symbolName, id, info.icon); + filterEntry.extraInfo = info.symbolType; + if (info.symbolName.startsWith(entry)) + betterEntries.append(filterEntry); + else + goodEntries.append(filterEntry); + } + } + + // entries are unsorted by design! + + betterEntries += goodEntries; + return betterEntries; +} diff --git a/src/plugins/cpptools/cpplocalfunctionsfilter.h b/src/plugins/cpptools/cpplocalfunctionsfilter.h new file mode 100644 index 0000000..d16efaf --- /dev/null +++ b/src/plugins/cpptools/cpplocalfunctionsfilter.h @@ -0,0 +1,68 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Qt Software Information ([email protected]) +** +** 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 [email protected]. +** +**************************************************************************/ + +#ifndef CPPLOCALFUNCTIONSFILTER_H +#define CPPLOCALFUNCTIONSFILTER_H + +#include <cppquickopenfilter.h> + +namespace Core { + class IEditor; +} + +namespace CppTools { +namespace Internal { + +class CppLocalFunctionsFilter : public CppQuickOpenFilter +{ + Q_OBJECT + +public: + CppLocalFunctionsFilter(CppModelManager *manager, Core::EditorManager *editorManager); + ~CppLocalFunctionsFilter(); + + QString trName() const { return tr("Methods in current Document"); } + QString name() const { return QLatin1String("Methods in current Document"); } + Priority priority() const { return Medium; } + + virtual QList<QuickOpen::FilterEntry> matchesFor(const QString &entry); + +private slots: + void onCurrentEditorChanged(Core::IEditor * currentEditor); + void onEditorAboutToClose(Core::IEditor * currentEditor); + + +private: + QString m_currentFile; +}; + +} // namespace Internal +} // namespace CppTools + +#endif // CPPLOCALFUNCTIONSFILTER_H diff --git a/src/plugins/cpptools/cppquickopenfilter.h b/src/plugins/cpptools/cppquickopenfilter.h index 35c3d77..c138bd9 100644 --- a/src/plugins/cpptools/cppquickopenfilter.h +++ b/src/plugins/cpptools/cppquickopenfilter.h @@ -64,7 +64,7 @@ private slots: void onDocumentUpdated(CPlusPlus::Document::Ptr doc); void onAboutToRemoveFiles(const QStringList &files); -private: +protected: CppModelManager *m_manager; Core::EditorManager *m_editorManager; diff --git a/src/plugins/cpptools/cpptools.pro b/src/plugins/cpptools/cpptools.pro index 5a5763f..97c1a63 100644 --- a/src/plugins/cpptools/cpptools.pro +++ b/src/plugins/cpptools/cpptools.pro @@ -12,6 +12,7 @@ HEADERS += completionsettingspage.h \ cppclassesfilter.h \ cppcodecompletion.h \ cppfunctionsfilter.h \ + cpplocalfunctionsfilter.h \ cppmodelmanager.h \ cppmodelmanagerinterface.h \ cppquickopenfilter.h \ @@ -27,6 +28,7 @@ SOURCES += completionsettingspage.cpp \ cppclassesfilter.cpp \ cppcodecompletion.cpp \ cppfunctionsfilter.cpp \ + cpplocalfunctionsfilter.cpp \ cppmodelmanager.cpp \ cppquickopenfilter.cpp \ cpptoolseditorsupport.cpp \ diff --git a/src/plugins/cpptools/cpptoolsplugin.cpp b/src/plugins/cpptools/cpptoolsplugin.cpp index 3019355..dc7a800 100644 --- a/src/plugins/cpptools/cpptoolsplugin.cpp +++ b/src/plugins/cpptools/cpptoolsplugin.cpp @@ -34,6 +34,7 @@ #include "cppclassesfilter.h" #include "cppcodecompletion.h" #include "cppfunctionsfilter.h" +#include "cpplocalfunctionsfilter.h" #include "cppmodelmanager.h" #include "cpptoolsconstants.h" #include "cppquickopenfilter.h" @@ -90,6 +91,7 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error) addAutoReleasedObject(quickOpenFilter); addAutoReleasedObject(new CppClassesFilter(m_modelManager, core->editorManager())); addAutoReleasedObject(new CppFunctionsFilter(m_modelManager, core->editorManager())); + addAutoReleasedObject(new CppLocalFunctionsFilter(m_modelManager, core->editorManager())); addAutoReleasedObject(new CompletionSettingsPage(m_completion)); addAutoReleasedObject(new CppFileSettingsPage); diff --git a/src/plugins/cpptools/searchsymbols.cpp b/src/plugins/cpptools/searchsymbols.cpp index d0b3234..c7aa6aa 100644 --- a/src/plugins/cpptools/searchsymbols.cpp +++ b/src/plugins/cpptools/searchsymbols.cpp @@ -128,17 +128,20 @@ bool SearchSymbols::visit(Namespace *symbol) return false; } -#if 0 bool SearchSymbols::visit(Declaration *symbol) { - if (symbol->type()->isFunction()) { - QString name = scopedSymbolName(symbol); - QString type = overview.prettyType(symbol->type()); - appendItems(name, type, ModelItemInfo::Method, symbol->fileName()); - } + if (!(symbolsToSearchFor & Declerations)) + return false; + + QString name = symbolName(symbol); + QString scopedName = scopedSymbolName(name); + QString type = overview.prettyType(symbol->type(), + separateScope ? symbol->identity() : 0); + appendItem(separateScope ? type : scopedName, + separateScope ? _scope : type, + ModelItemInfo::Decleration, symbol); return false; } -#endif bool SearchSymbols::visit(Class *symbol) { diff --git a/src/plugins/cpptools/searchsymbols.h b/src/plugins/cpptools/searchsymbols.h index c57dee6..3209044 100644 --- a/src/plugins/cpptools/searchsymbols.h +++ b/src/plugins/cpptools/searchsymbols.h @@ -48,7 +48,7 @@ namespace Internal { struct ModelItemInfo { - enum ItemType { Enum, Class, Method }; + enum ItemType { Enum, Class, Method, Decleration }; ModelItemInfo() { } @@ -80,9 +80,10 @@ class SearchSymbols: public std::unary_function<CPlusPlus::Document::Ptr, QList< { public: enum SymbolType { - Classes = 0x1, - Functions = 0x2, - Enums = 0x4 + Classes = 0x1, + Functions = 0x2, + Enums = 0x4, + Declerations = 0x8 }; Q_DECLARE_FLAGS(SymbolTypes, SymbolType) @@ -106,10 +107,7 @@ protected: virtual bool visit(CPlusPlus::Enum *symbol); virtual bool visit(CPlusPlus::Function *symbol); virtual bool visit(CPlusPlus::Namespace *symbol); -#if 0 - // This visit method would make function declaration be included in QuickOpen virtual bool visit(CPlusPlus::Declaration *symbol); -#endif virtual bool visit(CPlusPlus::Class *symbol); QString scopedSymbolName(const QString &symbolName) const; -- 1.6.0.6
_______________________________________________ Qt-creator mailing list [email protected] http://lists.trolltech.com/mailman/listinfo/qt-creator
