Commit 4fab2d4e authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

fix quick open for untitled documents

untitled documents can no again properly
opened via quick open

we will show all of them, too
parent 799f66de
......@@ -330,13 +330,6 @@ void KateQuickOpen::updateState()
void KateQuickOpen::slotReturnPressed()
{
const QModelIndex index = m_listView->model()->index(m_listView->currentIndex().row(), 0);
const QUrl url = index.data(Qt::UserRole).toUrl();
if (!url.isValid()) {
return;
}
// save current position before opening new url for location history
KateViewManager *vm = m_mainWindow->viewManager();
if (vm) {
......@@ -345,7 +338,14 @@ void KateQuickOpen::slotReturnPressed()
}
}
KTextEditor::View *view = m_mainWindow->wrapper()->openUrl(url);
// either get view via document pointer or url
const QModelIndex index = m_listView->model()->index(m_listView->currentIndex().row(), 0);
KTextEditor::View *view = nullptr;
if (auto doc = index.data(KateQuickOpenModel::Document).value<KTextEditor::Document *>()) {
view = m_mainWindow->activateView(doc);
} else {
view = m_mainWindow->wrapper()->openUrl(index.data(Qt::UserRole).toUrl());
}
const auto strs = m_inputLine->text().split(QLatin1Char(':'));
if (view && strs.count() > 1) {
......
......@@ -56,7 +56,7 @@ QVariant KateQuickOpenModel::data(const QModelIndex &idx, int role) const
return path.startsWith(m_projectBase) ? path.mid(m_projectBase.size()) : path;
}
case Qt::FontRole: {
if (entry.bold) {
if (entry.document) {
QFont font;
font.setBold(true);
return font;
......@@ -69,6 +69,8 @@ QVariant KateQuickOpenModel::data(const QModelIndex &idx, int role) const
return entry.url.isEmpty() ? QUrl::fromLocalFile(entry.filePath) : entry.url;
case Role::Score:
return entry.score;
case Role::Document:
return QVariant::fromValue(entry.document);
default:
return {};
}
......@@ -111,24 +113,25 @@ void KateQuickOpenModel::refresh(KateMainWindow *mainWindow)
allDocuments.reserve(sortedViews.size() + projectDocs.size());
std::unordered_set<QString> openedDocUrls;
std::unordered_set<KTextEditor::Document *> seenDocuments;
openedDocUrls.reserve(sortedViews.size());
const auto collectDoc = [&openedDocUrls, &allDocuments](KTextEditor::Document *doc) {
auto path = doc->url().toString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile);
// We don't want any duplicates
if (openedDocUrls.count(path) != 0) {
const auto collectDoc = [&openedDocUrls, &seenDocuments, &allDocuments](KTextEditor::Document *doc) {
// We don't want any duplicates, beside for untitled documents
if (!seenDocuments.insert(doc).second) {
return;
}
openedDocUrls.insert(path);
// prefer the real filename, since documentName might be `foo (2)`
// (which is not a suffix of the path)
auto fileName = QFileInfo(path).fileName();
if (fileName.isEmpty()) {
fileName = doc->documentName();
// document with set url => use the url for displaying
if (!doc->url().isEmpty()) {
auto path = doc->url().toString(QUrl::NormalizePathSegments | QUrl::PreferLocalFile);
openedDocUrls.insert(path);
allDocuments.push_back({doc->url(), QFileInfo(path).fileName(), path, doc, -1});
return;
}
allDocuments.push_back({doc->url(), fileName, path, true, -1});
// untitled document
allDocuments.push_back({doc->url(), doc->documentName(), QString(), doc, -1});
};
for (auto *view : sortedViews) {
......@@ -141,14 +144,14 @@ void KateQuickOpenModel::refresh(KateMainWindow *mainWindow)
for (const auto &filePath : projectDocs) {
// No duplicates
if (openedDocUrls.count(filePath) != 0) {
if (!openedDocUrls.insert(filePath).second) {
continue;
}
// QFileInfo is too expensive just for fileName computation
const int slashIndex = filePath.lastIndexOf(QLatin1Char('/'));
QString fileName = filePath.mid(slashIndex + 1);
allDocuments.push_back({QUrl(), std::move(fileName), filePath, false, -1});
allDocuments.push_back({QUrl(), std::move(fileName), filePath, nullptr, -1});
}
beginResetModel();
......
......@@ -15,13 +15,17 @@
#include <vector>
class KateMainWindow;
namespace KTextEditor
{
class Document;
}
struct ModelEntry {
QUrl url;
QString fileName; // display string for left column
QString filePath; // display string for right column
bool bold; // format line in bold text or not
int score;
KTextEditor::Document *document = nullptr; // document for entry, if already open
int score = -1;
};
// needs to be defined outside of class to support forward declaration elsewhere
......@@ -31,7 +35,7 @@ class KateQuickOpenModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Role { FileName = Qt::UserRole + 1, FilePath, Score };
enum Role { FileName = Qt::UserRole + 1, FilePath, Score, Document };
explicit KateQuickOpenModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent) const override;
......@@ -83,7 +87,7 @@ public:
if (!idx.isValid()) {
return {};
}
return m_modelEntries.at(idx.row()).bold;
return m_modelEntries.at(idx.row()).document;
}
private:
......
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