forked from qt-creator/qt-creator
TextEditor: Move BookmarkManager instance closer to its code
This is similar to the recently promoted setupXXX() pattern, not 100% there, though. Change-Id: Ia6b302af5c8b647ed073ec45179eb2413e988885 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -14,8 +14,7 @@ using namespace Utils;
|
||||
|
||||
namespace TextEditor::Internal {
|
||||
|
||||
BookmarkFilter::BookmarkFilter(BookmarkManager *manager)
|
||||
: m_manager(manager)
|
||||
BookmarkFilter::BookmarkFilter()
|
||||
{
|
||||
setId("Bookmarks");
|
||||
setDisplayName(Tr::tr("Bookmarks"));
|
||||
@@ -37,11 +36,12 @@ LocatorMatcherTasks BookmarkFilter::matchers()
|
||||
|
||||
LocatorFilterEntries BookmarkFilter::match(const QString &input) const
|
||||
{
|
||||
if (m_manager->rowCount() == 0)
|
||||
if (bookmarkManager().rowCount() == 0)
|
||||
return {};
|
||||
const auto match = [this](const QString &name, BookmarkManager::Roles role) {
|
||||
return m_manager->match(m_manager->index(0, 0), role, name, -1,
|
||||
Qt::MatchContains | Qt::MatchWrap);
|
||||
|
||||
const auto match = [](const QString &name, BookmarkManager::Roles role) {
|
||||
const BookmarkManager &manager = bookmarkManager();
|
||||
return manager.match(manager.index(0, 0), role, name, -1, Qt::MatchContains | Qt::MatchWrap);
|
||||
};
|
||||
|
||||
const int colonIndex = input.lastIndexOf(':');
|
||||
@@ -62,8 +62,9 @@ LocatorFilterEntries BookmarkFilter::match(const QString &input) const
|
||||
+ match(input, BookmarkManager::Note)
|
||||
+ match(input, BookmarkManager::LineText));
|
||||
LocatorFilterEntries entries;
|
||||
BookmarkManager &manager = bookmarkManager();
|
||||
for (const QModelIndex &idx : matches) {
|
||||
const Bookmark *bookmark = m_manager->bookmarkForIndex(idx);
|
||||
const Bookmark *bookmark = manager.bookmarkForIndex(idx);
|
||||
const QString filename = bookmark->filePath().fileName();
|
||||
LocatorFilterEntry entry;
|
||||
entry.displayName = QString("%1:%2").arg(filename).arg(bookmark->lineNumber());
|
||||
@@ -71,9 +72,10 @@ LocatorFilterEntries BookmarkFilter::match(const QString &input) const
|
||||
// Model indexes should be used immediately and then discarded.
|
||||
// You should not rely on indexes to remain valid after calling model functions
|
||||
// that change the structure of the model or delete items.
|
||||
entry.acceptor = [manager = m_manager, idx] {
|
||||
if (Bookmark *bookmark = manager->bookmarkForIndex(idx))
|
||||
manager->gotoBookmark(bookmark);
|
||||
entry.acceptor = [idx] {
|
||||
const BookmarkManager &manager = bookmarkManager();
|
||||
if (Bookmark *bookmark = manager.bookmarkForIndex(idx))
|
||||
manager.gotoBookmark(bookmark);
|
||||
return AcceptResult();
|
||||
};
|
||||
if (!bookmark->note().isEmpty())
|
||||
|
@@ -7,18 +7,14 @@
|
||||
|
||||
namespace TextEditor::Internal {
|
||||
|
||||
class BookmarkManager;
|
||||
|
||||
class BookmarkFilter : public Core::ILocatorFilter
|
||||
{
|
||||
public:
|
||||
BookmarkFilter(BookmarkManager *manager);
|
||||
BookmarkFilter();
|
||||
|
||||
private:
|
||||
Core::LocatorMatcherTasks matchers() final;
|
||||
Core::LocatorFilterEntries match(const QString &input) const;
|
||||
|
||||
BookmarkManager *m_manager = nullptr; // not owned
|
||||
};
|
||||
|
||||
} // TextEditor::Internal
|
||||
|
@@ -177,7 +177,7 @@ void BookmarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opti
|
||||
class BookmarkView final : public Utils::ListView
|
||||
{
|
||||
public:
|
||||
explicit BookmarkView(BookmarkManager *manager);
|
||||
BookmarkView();
|
||||
|
||||
QList<QToolButton *> createToolBarWidgets();
|
||||
|
||||
@@ -194,12 +194,10 @@ protected:
|
||||
private:
|
||||
Core::IContext *m_bookmarkContext;
|
||||
QModelIndex m_contextMenuIndex;
|
||||
BookmarkManager *m_manager;
|
||||
};
|
||||
|
||||
BookmarkView::BookmarkView(BookmarkManager *manager) :
|
||||
m_bookmarkContext(new IContext(this)),
|
||||
m_manager(manager)
|
||||
BookmarkView::BookmarkView()
|
||||
: m_bookmarkContext(new IContext(this))
|
||||
{
|
||||
setWindowTitle(Tr::tr("Bookmarks"));
|
||||
|
||||
@@ -208,13 +206,13 @@ BookmarkView::BookmarkView(BookmarkManager *manager) :
|
||||
|
||||
ICore::addContextObject(m_bookmarkContext);
|
||||
|
||||
ListView::setModel(manager);
|
||||
ListView::setModel(&bookmarkManager());
|
||||
|
||||
setItemDelegate(new BookmarkDelegate(this));
|
||||
setFrameStyle(QFrame::NoFrame);
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setAttribute(Qt::WA_MacShowFocusRect, false);
|
||||
setSelectionModel(manager->selectionModel());
|
||||
setSelectionModel(bookmarkManager().selectionModel());
|
||||
setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
setDragEnabled(true);
|
||||
@@ -260,11 +258,12 @@ void BookmarkView::contextMenuEvent(QContextMenuEvent *event)
|
||||
if (model()->rowCount() == 0)
|
||||
removeAll->setEnabled(false);
|
||||
|
||||
connect(moveUp, &QAction::triggered, m_manager, &BookmarkManager::moveUp);
|
||||
connect(moveDown, &QAction::triggered, m_manager, &BookmarkManager::moveDown);
|
||||
BookmarkManager *manager = &bookmarkManager();
|
||||
connect(moveUp, &QAction::triggered, manager, &BookmarkManager::moveUp);
|
||||
connect(moveDown, &QAction::triggered, manager, &BookmarkManager::moveDown);
|
||||
connect(remove, &QAction::triggered, this, &BookmarkView::removeFromContextMenu);
|
||||
connect(removeAll, &QAction::triggered, this, &BookmarkView::removeAll);
|
||||
connect(edit, &QAction::triggered, m_manager, &BookmarkManager::edit);
|
||||
connect(edit, &QAction::triggered, manager, &BookmarkManager::edit);
|
||||
|
||||
menu.exec(mapToGlobal(event->pos()));
|
||||
}
|
||||
@@ -276,8 +275,8 @@ void BookmarkView::removeFromContextMenu()
|
||||
|
||||
void BookmarkView::removeBookmark(const QModelIndex& index)
|
||||
{
|
||||
Bookmark *bm = m_manager->bookmarkForIndex(index);
|
||||
m_manager->deleteBookmark(bm);
|
||||
Bookmark *bm = bookmarkManager().bookmarkForIndex(index);
|
||||
bookmarkManager().deleteBookmark(bm);
|
||||
}
|
||||
|
||||
void BookmarkView::keyPressEvent(QKeyEvent *event)
|
||||
@@ -301,25 +300,27 @@ void BookmarkView::removeAll()
|
||||
return;
|
||||
|
||||
// The performance of this function could be greatly improved.
|
||||
while (m_manager->rowCount()) {
|
||||
QModelIndex index = m_manager->index(0, 0);
|
||||
BookmarkManager *manager = &bookmarkManager();
|
||||
while (manager->rowCount()) {
|
||||
QModelIndex index = manager->index(0, 0);
|
||||
removeBookmark(index);
|
||||
}
|
||||
}
|
||||
|
||||
void BookmarkView::gotoBookmark(const QModelIndex &index)
|
||||
{
|
||||
Bookmark *bk = m_manager->bookmarkForIndex(index);
|
||||
if (!m_manager->gotoBookmark(bk))
|
||||
m_manager->deleteBookmark(bk);
|
||||
BookmarkManager *manager = &bookmarkManager();
|
||||
Bookmark *bk = manager->bookmarkForIndex(index);
|
||||
if (!manager->gotoBookmark(bk))
|
||||
manager->deleteBookmark(bk);
|
||||
}
|
||||
|
||||
////
|
||||
// BookmarkManager
|
||||
////
|
||||
|
||||
BookmarkManager::BookmarkManager() :
|
||||
m_selectionModel(new QItemSelectionModel(this, this))
|
||||
BookmarkManager::BookmarkManager(QObject *parent)
|
||||
: QAbstractItemModel(parent), m_selectionModel(new QItemSelectionModel(this, this))
|
||||
{
|
||||
m_editBookmarkAction.setText(Tr::tr("Edit Bookmark"));
|
||||
m_bookmarkMarginAction.setText(Tr::tr("Toggle Bookmark"));
|
||||
@@ -993,10 +994,23 @@ bool BookmarkManager::isAtCurrentBookmark() const
|
||||
&& currentEditor->currentLine() == bk->lineNumber();
|
||||
}
|
||||
|
||||
static BookmarkManager *s_bookmarkManager;
|
||||
|
||||
void setupBookmarkManager(QObject *guard)
|
||||
{
|
||||
QTC_CHECK(!s_bookmarkManager);
|
||||
s_bookmarkManager = new BookmarkManager(guard);
|
||||
}
|
||||
|
||||
BookmarkManager &bookmarkManager()
|
||||
{
|
||||
QTC_CHECK(s_bookmarkManager);
|
||||
return *s_bookmarkManager;
|
||||
}
|
||||
|
||||
// BookmarkViewFactory
|
||||
|
||||
BookmarkViewFactory::BookmarkViewFactory(BookmarkManager *bm)
|
||||
: m_manager(bm)
|
||||
BookmarkViewFactory::BookmarkViewFactory()
|
||||
{
|
||||
setDisplayName(Tr::tr("Bookmarks"));
|
||||
setPriority(300);
|
||||
@@ -1006,7 +1020,7 @@ BookmarkViewFactory::BookmarkViewFactory(BookmarkManager *bm)
|
||||
|
||||
NavigationView BookmarkViewFactory::createWidget()
|
||||
{
|
||||
auto view = new BookmarkView(m_manager);
|
||||
auto view = new BookmarkView;
|
||||
view->setActivationMode(Utils::DoubleClickActivation); // QUESTION: is this useful ?
|
||||
return {view, view->createToolBarWidgets()};
|
||||
}
|
||||
|
@@ -25,7 +25,7 @@ class BookmarkManager final : public QAbstractItemModel
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
BookmarkManager();
|
||||
explicit BookmarkManager(QObject *parent);
|
||||
~BookmarkManager() final;
|
||||
|
||||
void updateBookmark(Bookmark *bookmark);
|
||||
@@ -113,15 +113,17 @@ private:
|
||||
Utils::FilePath m_marginActionFileName;
|
||||
};
|
||||
|
||||
BookmarkManager &bookmarkManager();
|
||||
|
||||
void setupBookmarkManager(QObject *guard);
|
||||
|
||||
class BookmarkViewFactory : public Core::INavigationWidgetFactory
|
||||
{
|
||||
public:
|
||||
BookmarkViewFactory(BookmarkManager *bm);
|
||||
BookmarkViewFactory();
|
||||
|
||||
private:
|
||||
Core::NavigationView createWidget() override;
|
||||
|
||||
BookmarkManager *m_manager;
|
||||
};
|
||||
|
||||
} // Bookmarks::Internal
|
||||
|
@@ -82,9 +82,8 @@ public:
|
||||
|
||||
void createStandardContextMenu();
|
||||
|
||||
BookmarkManager m_bookmarkManager;
|
||||
BookmarkFilter m_bookmarkFilter{&m_bookmarkManager};
|
||||
BookmarkViewFactory m_bookmarkViewFactory{&m_bookmarkManager};
|
||||
BookmarkFilter m_bookmarkFilter;
|
||||
BookmarkViewFactory m_bookmarkViewFactory;
|
||||
|
||||
TextEditorSettings settings;
|
||||
|
||||
@@ -110,9 +109,9 @@ void TextEditorPluginPrivate::editorOpened(IEditor *editor)
|
||||
{
|
||||
if (auto widget = TextEditorWidget::fromEditor(editor)) {
|
||||
connect(widget, &TextEditorWidget::markRequested,
|
||||
this, [this, editor](TextEditorWidget *, int line, TextMarkRequestKind kind) {
|
||||
this, [editor](TextEditorWidget *, int line, TextMarkRequestKind kind) {
|
||||
if (kind == BookmarkRequest && !editor->document()->isTemporary())
|
||||
m_bookmarkManager.toggleBookmark(editor->document()->filePath(), line);
|
||||
bookmarkManager().toggleBookmark(editor->document()->filePath(), line);
|
||||
});
|
||||
|
||||
connect(widget, &TextEditorWidget::markContextMenuRequested,
|
||||
@@ -134,7 +133,7 @@ void TextEditorPluginPrivate::requestContextMenu(TextEditorWidget *widget,
|
||||
if (widget->textDocument()->isTemporary())
|
||||
return;
|
||||
|
||||
m_bookmarkManager.requestContextMenu(widget->textDocument()->filePath(), lineNumber, menu);
|
||||
bookmarkManager().requestContextMenu(widget->textDocument()->filePath(), lineNumber, menu);
|
||||
}
|
||||
|
||||
static class TextEditorPlugin *m_instance = nullptr;
|
||||
@@ -178,6 +177,8 @@ void TextEditorPlugin::initialize()
|
||||
setupOutlineFactory();
|
||||
setupLineNumberFilter(); // Goto line functionality for quick open
|
||||
|
||||
setupBookmarkManager(this);
|
||||
|
||||
d = new TextEditorPluginPrivate;
|
||||
|
||||
Context context(TextEditor::Constants::C_TEXTEDITOR);
|
||||
|
Reference in New Issue
Block a user