Commit 2d3829ff authored by David Schulz's avatar David Schulz

BookMarks: restructure bookmark map

compare file names case insensitive on windows

Task-number: QTCREATORBUG-19418
Change-Id: I4925a7b33f35ce18e906990ffc104b22ea6d73dc
Reviewed-by: Eike Ziller's avatarEike Ziller <eike.ziller@qt.io>
parent 469edcdd
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
#include <utils/algorithm.h>
#include <utils/icon.h> #include <utils/icon.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/checkablemessagebox.h> #include <utils/checkablemessagebox.h>
...@@ -333,13 +334,7 @@ BookmarkManager::BookmarkManager() : ...@@ -333,13 +334,7 @@ BookmarkManager::BookmarkManager() :
BookmarkManager::~BookmarkManager() BookmarkManager::~BookmarkManager()
{ {
DirectoryFileBookmarksMap::iterator it, end; qDeleteAll(m_bookmarksList);
end = m_bookmarksMap.end();
for (it = m_bookmarksMap.begin(); it != end; ++it) {
FileNameBookmarksMap *bookmarks = it.value();
qDeleteAll(*bookmarks);
delete bookmarks;
}
} }
QItemSelectionModel *BookmarkManager::selectionModel() const QItemSelectionModel *BookmarkManager::selectionModel() const
...@@ -347,7 +342,7 @@ QItemSelectionModel *BookmarkManager::selectionModel() const ...@@ -347,7 +342,7 @@ QItemSelectionModel *BookmarkManager::selectionModel() const
return m_selectionModel; return m_selectionModel;
} }
bool BookmarkManager::hasBookmarkInPosition(const QString &fileName, int lineNumber) bool BookmarkManager::hasBookmarkInPosition(const Utils::FileName &fileName, int lineNumber)
{ {
return findBookmark(fileName, lineNumber); return findBookmark(fileName, lineNumber);
} }
...@@ -430,7 +425,7 @@ QMimeData *BookmarkManager::mimeData(const QModelIndexList &indexes) const ...@@ -430,7 +425,7 @@ QMimeData *BookmarkManager::mimeData(const QModelIndexList &indexes) const
return data; return data;
} }
void BookmarkManager::toggleBookmark(const QString &fileName, int lineNumber) void BookmarkManager::toggleBookmark(const FileName &fileName, int lineNumber)
{ {
if (lineNumber <= 0 || fileName.isEmpty()) if (lineNumber <= 0 || fileName.isEmpty())
return; return;
...@@ -444,7 +439,7 @@ void BookmarkManager::toggleBookmark(const QString &fileName, int lineNumber) ...@@ -444,7 +439,7 @@ void BookmarkManager::toggleBookmark(const QString &fileName, int lineNumber)
// Add a new bookmark if no bookmark existed on this line // Add a new bookmark if no bookmark existed on this line
Bookmark *mark = new Bookmark(lineNumber, this); Bookmark *mark = new Bookmark(lineNumber, this);
mark->updateFileName(fileName); mark->updateFileName(fileName.toString());
addBookmark(mark); addBookmark(mark);
} }
...@@ -463,9 +458,8 @@ void BookmarkManager::updateBookmarkFileName(Bookmark *bookmark, const QString & ...@@ -463,9 +458,8 @@ void BookmarkManager::updateBookmarkFileName(Bookmark *bookmark, const QString &
if (oldFileName == bookmark->fileName()) if (oldFileName == bookmark->fileName())
return; return;
if (removeBookmarkFromMap(bookmark, oldFileName)) m_bookmarksMap[Utils::FileName::fromString(oldFileName)].removeAll(bookmark);
addBookmarkToMap(bookmark); m_bookmarksMap[Utils::FileName::fromString(bookmark->fileName())].append(bookmark);
updateBookmark(bookmark); updateBookmark(bookmark);
} }
...@@ -475,51 +469,18 @@ void BookmarkManager::removeAllBookmarks() ...@@ -475,51 +469,18 @@ void BookmarkManager::removeAllBookmarks()
return; return;
beginRemoveRows(QModelIndex(), 0, m_bookmarksList.size() - 1); beginRemoveRows(QModelIndex(), 0, m_bookmarksList.size() - 1);
DirectoryFileBookmarksMap::const_iterator it, end; qDeleteAll(m_bookmarksList);
end = m_bookmarksMap.constEnd();
for (it = m_bookmarksMap.constBegin(); it != end; ++it) {
FileNameBookmarksMap *files = it.value();
FileNameBookmarksMap::const_iterator jt, jend;
jend = files->constEnd();
for (jt = files->constBegin(); jt != jend; ++jt) {
delete jt.value();
}
files->clear();
delete files;
}
m_bookmarksMap.clear(); m_bookmarksMap.clear();
m_bookmarksList.clear(); m_bookmarksList.clear();
endRemoveRows(); endRemoveRows();
} }
bool BookmarkManager::removeBookmarkFromMap(Bookmark *bookmark, const QString &fileName)
{
bool found = false;
const QFileInfo fi(fileName.isEmpty() ? bookmark->fileName() : fileName);
if (FileNameBookmarksMap *files = m_bookmarksMap.value(fi.path())) {
FileNameBookmarksMap::iterator i = files->begin();
while (i != files->end()) {
if (i.value() == bookmark) {
files->erase(i);
found = true;
break;
}
++i;
}
if (files->count() <= 0) {
m_bookmarksMap.remove(fi.path());
delete files;
}
}
return found;
}
void BookmarkManager::deleteBookmark(Bookmark *bookmark) void BookmarkManager::deleteBookmark(Bookmark *bookmark)
{ {
int idx = m_bookmarksList.indexOf(bookmark); int idx = m_bookmarksList.indexOf(bookmark);
beginRemoveRows(QModelIndex(), idx, idx); beginRemoveRows(QModelIndex(), idx, idx);
removeBookmarkFromMap(bookmark); m_bookmarksMap[Utils::FileName::fromString(bookmark->fileName())].removeAll(bookmark);
delete bookmark; delete bookmark;
m_bookmarksList.removeAt(idx); m_bookmarksList.removeAt(idx);
...@@ -563,15 +524,15 @@ void BookmarkManager::documentPrevNext(bool next) ...@@ -563,15 +524,15 @@ void BookmarkManager::documentPrevNext(bool next)
if (editorLine <= 0) if (editorLine <= 0)
return; return;
const QFileInfo fi = editor->document()->filePath().toFileInfo(); const FileName filePath = editor->document()->filePath();
if (!m_bookmarksMap.contains(fi.path())) if (!m_bookmarksMap.contains(filePath))
return; return;
int firstLine = -1; int firstLine = -1;
int lastLine = -1; int lastLine = -1;
int prevLine = -1; int prevLine = -1;
int nextLine = -1; int nextLine = -1;
const QList<Bookmark *> marks = m_bookmarksMap.value(fi.path())->values(fi.fileName()); const QVector<Bookmark *> marks = m_bookmarksMap[filePath];
for (int i = 0; i < marks.count(); ++i) { for (int i = 0; i < marks.count(); ++i) {
int markLine = marks.at(i)->lineNumber(); int markLine = marks.at(i)->lineNumber();
if (firstLine == -1 || firstLine > markLine) if (firstLine == -1 || firstLine > markLine)
...@@ -647,20 +608,15 @@ void BookmarkManager::prev() ...@@ -647,20 +608,15 @@ void BookmarkManager::prev()
BookmarkManager::State BookmarkManager::state() const BookmarkManager::State BookmarkManager::state() const
{ {
if (m_bookmarksMap.empty()) if (m_bookmarksList.empty())
return NoBookMarks; return NoBookMarks;
IEditor *editor = EditorManager::currentEditor(); IEditor *editor = EditorManager::currentEditor();
if (!editor) if (!editor)
return HasBookMarks; return HasBookMarks;
const QFileInfo fi = editor->document()->filePath().toFileInfo(); return m_bookmarksMap.value(editor->document()->filePath()).isEmpty() ? HasBookMarks
: HasBookmarksInDocument;
const DirectoryFileBookmarksMap::const_iterator dit = m_bookmarksMap.constFind(fi.path());
if (dit == m_bookmarksMap.constEnd())
return HasBookMarks;
return HasBookmarksInDocument;
} }
void BookmarkManager::updateActionStatus() void BookmarkManager::updateActionStatus()
...@@ -714,7 +670,7 @@ void BookmarkManager::moveDown() ...@@ -714,7 +670,7 @@ void BookmarkManager::moveDown()
saveBookmarks(); saveBookmarks();
} }
void BookmarkManager::editByFileAndLine(const QString &fileName, int lineNumber) void BookmarkManager::editByFileAndLine(const FileName &fileName, int lineNumber)
{ {
Bookmark *b = findBookmark(fileName, lineNumber); Bookmark *b = findBookmark(fileName, lineNumber);
QModelIndex current = selectionModel()->currentIndex(); QModelIndex current = selectionModel()->currentIndex();
...@@ -753,26 +709,10 @@ void BookmarkManager::edit() ...@@ -753,26 +709,10 @@ void BookmarkManager::edit()
} }
/* Returns the bookmark at the given file and line number, or 0 if no such bookmark exists. */ /* Returns the bookmark at the given file and line number, or 0 if no such bookmark exists. */
Bookmark *BookmarkManager::findBookmark(const QString &filePath, int lineNumber) Bookmark *BookmarkManager::findBookmark(const FileName &filePath, int lineNumber)
{
QFileInfo fi(filePath);
QString path = fi.path();
if (m_bookmarksMap.contains(path)) {
foreach (Bookmark *bookmark, m_bookmarksMap.value(path)->values(fi.fileName())) {
if (bookmark->lineNumber() == lineNumber)
return bookmark;
}
}
return 0;
}
void BookmarkManager::addBookmarkToMap(Bookmark *bookmark)
{ {
const QFileInfo fi(bookmark->fileName()); return Utils::findOrDefault(m_bookmarksMap.value(filePath),
const QString &path = fi.path(); Utils::equal(&Bookmark::lineNumber, lineNumber));
if (!m_bookmarksMap.contains(path))
m_bookmarksMap.insert(path, new FileNameBookmarksMap());
m_bookmarksMap.value(path)->insert(fi.fileName(), bookmark);
} }
/* Adds a bookmark to the internal data structures. The 'userset' parameter /* Adds a bookmark to the internal data structures. The 'userset' parameter
...@@ -783,8 +723,7 @@ void BookmarkManager::addBookmark(Bookmark *bookmark, bool userset) ...@@ -783,8 +723,7 @@ void BookmarkManager::addBookmark(Bookmark *bookmark, bool userset)
{ {
beginInsertRows(QModelIndex(), m_bookmarksList.size(), m_bookmarksList.size()); beginInsertRows(QModelIndex(), m_bookmarksList.size(), m_bookmarksList.size());
addBookmarkToMap(bookmark); m_bookmarksMap[FileName::fromString(bookmark->fileName())].append(bookmark);
m_bookmarksList.append(bookmark); m_bookmarksList.append(bookmark);
endInsertRows(); endInsertRows();
...@@ -809,7 +748,7 @@ void BookmarkManager::addBookmark(const QString &s) ...@@ -809,7 +748,7 @@ void BookmarkManager::addBookmark(const QString &s)
const QString &filePath = s.mid(index1+1, index2-index1-1); const QString &filePath = s.mid(index1+1, index2-index1-1);
const QString &note = s.mid(index3 + 1); const QString &note = s.mid(index3 + 1);
const int lineNumber = s.midRef(index2 + 1, index3 - index2 - 1).toInt(); const int lineNumber = s.midRef(index2 + 1, index3 - index2 - 1).toInt();
if (!filePath.isEmpty() && !findBookmark(filePath, lineNumber)) { if (!filePath.isEmpty() && !findBookmark(FileName::fromString(filePath), lineNumber)) {
Bookmark *b = new Bookmark(lineNumber, this); Bookmark *b = new Bookmark(lineNumber, this);
b->updateFileName(filePath); b->updateFileName(filePath);
b->setNote(note); b->setNote(note);
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#pragma once #pragma once
#include <utils/itemviews.h> #include <utils/itemviews.h>
#include <utils/fileutils.h>
#include <coreplugin/inavigationwidgetfactory.h> #include <coreplugin/inavigationwidgetfactory.h>
#include <QAbstractItemModel> #include <QAbstractItemModel>
...@@ -76,7 +77,7 @@ public: ...@@ -76,7 +77,7 @@ public:
// this QItemSelectionModel is shared by all views // this QItemSelectionModel is shared by all views
QItemSelectionModel *selectionModel() const; QItemSelectionModel *selectionModel() const;
bool hasBookmarkInPosition(const QString &fileName, int lineNumber); bool hasBookmarkInPosition(const Utils::FileName &fileName, int lineNumber);
enum Roles { enum Roles {
Filename = Qt::UserRole, Filename = Qt::UserRole,
...@@ -86,7 +87,7 @@ public: ...@@ -86,7 +87,7 @@ public:
Note = Qt::UserRole + 4 Note = Qt::UserRole + 4
}; };
void toggleBookmark(const QString &fileName, int lineNumber); void toggleBookmark(const Utils::FileName &fileName, int lineNumber);
void nextInDocument(); void nextInDocument();
void prevInDocument(); void prevInDocument();
void next(); void next();
...@@ -94,7 +95,7 @@ public: ...@@ -94,7 +95,7 @@ public:
void moveUp(); void moveUp();
void moveDown(); void moveDown();
void edit(); void edit();
void editByFileAndLine(const QString &fileName, int lineNumber); void editByFileAndLine(const Utils::FileName &fileName, int lineNumber);
bool gotoBookmark(Bookmark *bookmark); bool gotoBookmark(Bookmark *bookmark);
signals: signals:
...@@ -107,18 +108,13 @@ private: ...@@ -107,18 +108,13 @@ private:
void documentPrevNext(bool next); void documentPrevNext(bool next);
Bookmark *findBookmark(const QString &filePath, int lineNumber); Bookmark *findBookmark(const Utils::FileName &filePath, int lineNumber);
void addBookmark(Bookmark *bookmark, bool userset = true); void addBookmark(Bookmark *bookmark, bool userset = true);
void addBookmark(const QString &s); void addBookmark(const QString &s);
void addBookmarkToMap(Bookmark *bookmark);
bool removeBookmarkFromMap(Bookmark *bookmark, const QString &fileName = QString());
static QString bookmarkToString(const Bookmark *b); static QString bookmarkToString(const Bookmark *b);
void saveBookmarks(); void saveBookmarks();
typedef QMultiMap<QString, Bookmark *> FileNameBookmarksMap; QMap<Utils::FileName, QVector<Bookmark *>> m_bookmarksMap;
typedef QMap<QString, FileNameBookmarksMap *> DirectoryFileBookmarksMap;
DirectoryFileBookmarksMap m_bookmarksMap;
QList<Bookmark *> m_bookmarksList; QList<Bookmark *> m_bookmarksList;
QItemSelectionModel *m_selectionModel; QItemSelectionModel *m_selectionModel;
......
...@@ -112,7 +112,7 @@ bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *) ...@@ -112,7 +112,7 @@ bool BookmarksPlugin::initialize(const QStringList & /*arguments*/, QString *)
connect(m_toggleAction, &QAction::triggered, [this]() { connect(m_toggleAction, &QAction::triggered, [this]() {
BaseTextEditor *editor = BaseTextEditor::currentTextEditor(); BaseTextEditor *editor = BaseTextEditor::currentTextEditor();
if (editor && !editor->document()->isTemporary()) if (editor && !editor->document()->isTemporary())
m_bookmarkManager->toggleBookmark(editor->document()->filePath().toString(), editor->currentLine()); m_bookmarkManager->toggleBookmark(editor->document()->filePath(), editor->currentLine());
}); });
connect(m_prevAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::prev); connect(m_prevAction, &QAction::triggered, m_bookmarkManager, &BookmarkManager::prev);
...@@ -167,7 +167,7 @@ void BookmarksPlugin::editorOpened(IEditor *editor) ...@@ -167,7 +167,7 @@ void BookmarksPlugin::editorOpened(IEditor *editor)
connect(widget, &TextEditorWidget::markRequested, m_bookmarkManager, connect(widget, &TextEditorWidget::markRequested, m_bookmarkManager,
[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().toString(), line); m_bookmarkManager->toggleBookmark(editor->document()->filePath(), line);
}); });
connect(widget, &TextEditorWidget::markContextMenuRequested, connect(widget, &TextEditorWidget::markContextMenuRequested,
...@@ -190,7 +190,7 @@ void BookmarksPlugin::requestContextMenu(TextEditorWidget *widget, ...@@ -190,7 +190,7 @@ void BookmarksPlugin::requestContextMenu(TextEditorWidget *widget,
return; return;
m_bookmarkMarginActionLineNumber = lineNumber; m_bookmarkMarginActionLineNumber = lineNumber;
m_bookmarkMarginActionFileName = widget->textDocument()->filePath().toString(); m_bookmarkMarginActionFileName = widget->textDocument()->filePath();
menu->addAction(m_bookmarkMarginAction); menu->addAction(m_bookmarkMarginAction);
if (m_bookmarkManager->hasBookmarkInPosition(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber)) if (m_bookmarkManager->hasBookmarkInPosition(m_bookmarkMarginActionFileName, m_bookmarkMarginActionLineNumber))
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#pragma once #pragma once
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
#include <utils/fileutils.h>
#include <QObject> #include <QObject>
#include <QMultiMap> #include <QMultiMap>
...@@ -75,7 +76,7 @@ private: ...@@ -75,7 +76,7 @@ private:
QAction *m_bookmarkMarginAction; QAction *m_bookmarkMarginAction;
int m_bookmarkMarginActionLineNumber; int m_bookmarkMarginActionLineNumber;
QString m_bookmarkMarginActionFileName; Utils::FileName m_bookmarkMarginActionFileName;
}; };
} // namespace Internal } // namespace Internal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment