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