diff --git a/plugins/documentswitcher/documentswitcherplugin.h b/plugins/documentswitcher/documentswitcherplugin.h --- a/plugins/documentswitcher/documentswitcherplugin.h +++ b/plugins/documentswitcher/documentswitcherplugin.h @@ -26,6 +26,7 @@ namespace Sublime { class View; + class UrlDocument; class MainWindow; class Area; class MainWindow; @@ -65,7 +66,7 @@ void walk(const int from, const int to); // Need to use QObject here as we only have a QObject* in // the removeMainWindow method and cannot cast it to the mainwindow anymore - QMap > > documentLists; + QMap > > documentLists; DocumentSwitcherTreeView* view; QStandardItemModel* model; QAction* forwardAction; diff --git a/plugins/documentswitcher/documentswitcherplugin.cpp b/plugins/documentswitcher/documentswitcherplugin.cpp --- a/plugins/documentswitcher/documentswitcherplugin.cpp +++ b/plugins/documentswitcher/documentswitcherplugin.cpp @@ -31,11 +31,13 @@ #include #include +#include #include #include #include #include #include +#include #include "documentswitchertreeview.h" #include "debug.h" @@ -164,44 +166,41 @@ { model->clear(); auto projectController = KDevelop::ICore::self()->projectController(); - foreach( Sublime::View* v, documentLists[window][window->area()] ) + foreach( Sublime::UrlDocument *doc, documentLists[window][window->area()] ) { using namespace KDevelop; - Sublime::Document const* const slDoc = v->document(); - if( !slDoc ) + if( !doc ) { continue; } - QString itemText = slDoc->title();// file name - IDocument const* const doc = dynamic_cast(v->document()); + QString itemText = doc->title(Sublime::Document::Normal);// file name IProject* project = nullptr; - if( doc ) + + QString path = projectController->prettyFilePath(doc->url(), + IProjectController::FormatPlain); + const bool isPartOfOpenProject = QDir::isRelativePath(path); + if (path.endsWith(QLatin1Char('/'))) { + path.chop(1); + } + if( isPartOfOpenProject ) { - QString path = projectController->prettyFilePath(doc->url(), - IProjectController::FormatPlain); - const bool isPartOfOpenProject = QDir::isRelativePath(path); - if (path.endsWith(QLatin1Char('/'))) { - path.chop(1); - } - if( isPartOfOpenProject ) - { - const int projectNameSize = path.indexOf(QLatin1Char(':')); - - // first: project name, second: path to file in project (might be just '/' when the file is in the project root dir) - const QPair fileInProjectInfo = (projectNameSize < 0) - ? qMakePair(path, QStringLiteral("/")) - : qMakePair(path.left(projectNameSize), path.mid(projectNameSize + 1)); - - itemText = QStringLiteral("%1 (%2:%3)").arg(itemText, - fileInProjectInfo.first, - fileInProjectInfo.second); - } else - { - itemText += QLatin1String(" (") + path + QLatin1Char(')'); - } - project = projectController->findProjectForUrl(doc->url()); + const int projectNameSize = path.indexOf(QLatin1Char(':')); + + // first: project name, second: path to file in project (might be just '/' when the file is in the project root dir) + const QPair fileInProjectInfo = (projectNameSize < 0) + ? qMakePair(path, QStringLiteral("/")) + : qMakePair(path.left(projectNameSize), path.mid(projectNameSize + 1)); + + itemText = QStringLiteral("%1 (%2:%3)").arg(itemText, + fileInProjectInfo.first, + fileInProjectInfo.second); + } else + { + itemText += QLatin1String(" (") + path + QLatin1Char(')'); } - auto item = new QStandardItem( slDoc->icon(), itemText ); + project = projectController->findProjectForUrl(doc->url()); + + auto item = new QStandardItem( doc->icon(), itemText ); item->setData(QVariant::fromValue(project), DocumentSwitcherTreeView::ProjectRole); model->appendRow( item ); } @@ -226,29 +225,30 @@ } int row = view->selectionModel()->selectedRows().first().row(); + KDevelop::IDocumentController *dc = KDevelop::ICore::self()->documentController(); auto* window = qobject_cast( KDevelop::ICore::self()->uiController()->activeMainWindow() ); - Sublime::View* activatedView = nullptr; + Sublime::UrlDocument* activatedDoc = nullptr; if( window && documentLists.contains( window ) && documentLists[window].contains( window->area() ) ) { - const QList l = documentLists[window][window->area()]; + const QList l = documentLists[window][window->area()]; if( row >= 0 && row < l.size() ) { - activatedView = l.at( row ); + activatedDoc = l.at( row ); } } - if( activatedView ) { + + if( activatedDoc && activatedDoc->url() != dc->activeDocument()->url() ) { if( QApplication::mouseButtons() & Qt::MiddleButton ) { - window->area()->closeView( activatedView ); fillModel( window ); if ( model->rowCount() == 0 ) { view->hide(); } else { view->selectionModel()->select( view->model()->index(0, 0), QItemSelectionModel::ClearAndSelect ); } } else { - window->activateView( activatedView ); + dc->openDocument(activatedDoc->url()); view->hide(); } } @@ -270,13 +270,19 @@ { if( !documentLists.contains( mainwindow ) || !documentLists[mainwindow].contains(area) ) { - QHash > areas; + QHash > areas; qCDebug(PLUGIN_DOCUMENTSWITCHER) << "adding area views for area:" << area << area->title() << "mainwindow:" << mainwindow << mainwindow->windowTitle(); + + // All view's document. + QList documents; foreach( Sublime::View* v, area->views() ) { - qCDebug(PLUGIN_DOCUMENTSWITCHER) << "view:" << v << v->document()->title(); + Sublime::UrlDocument *doc = qobject_cast(v->document()); + qCDebug(PLUGIN_DOCUMENTSWITCHER) << "view:" << v << doc->title(Sublime::Document::Normal); + + documents.append(doc); } qCDebug(PLUGIN_DOCUMENTSWITCHER) << "done"; - areas.insert( area, area->views() ); + areas.insert( area, documents); documentLists.insert( mainwindow, areas ); } @@ -315,11 +321,12 @@ if( !mainwindow ) return; + Sublime::UrlDocument *doc = qobject_cast(view->document()); qCDebug(PLUGIN_DOCUMENTSWITCHER) << "got signal from mainwindow:" << mainwindow << mainwindow->windowTitle() << "its area is:" << mainwindow->area() << mainwindow->area()->title() - << "adding view:" << view << view->document()->title(); + << "adding view:" << view << doc->title(Sublime::Document::Normal); enableActions(); - documentLists[mainwindow][mainwindow->area()].append( view ); + documentLists[mainwindow][mainwindow->area()].append(doc); } void DocumentSwitcherPlugin::enableActions() @@ -363,44 +370,49 @@ if( !view ) return; + auto* mainwindow = qobject_cast( sender() ); Q_ASSERT( mainwindow ); + Sublime::UrlDocument *doc = qobject_cast(view->document()); Sublime::Area* area = mainwindow->area(); - int idx = documentLists[mainwindow][area].indexOf( view ); + int idx = documentLists[mainwindow][area].indexOf( doc ); if( idx != -1 ) { documentLists[mainwindow][area].removeAt( idx ); } - qCDebug(PLUGIN_DOCUMENTSWITCHER) << "moving view to front, list should now not contain this view anymore" << view << view->document()->title(); + qCDebug(PLUGIN_DOCUMENTSWITCHER) << "moving view to front, list should now not contain this view anymore" << view << doc->title(Sublime::Document::Normal); qCDebug(PLUGIN_DOCUMENTSWITCHER) << "current area is:" << area << area->title() << "mainwindow:" << mainwindow << mainwindow->windowTitle(); - qCDebug(PLUGIN_DOCUMENTSWITCHER) << "idx of this view in list:" << documentLists[mainwindow][area].indexOf( view ); - documentLists[mainwindow][area].prepend( view ); + qCDebug(PLUGIN_DOCUMENTSWITCHER) << "idx of this view in list:" << documentLists[mainwindow][area].indexOf( doc ); + documentLists[mainwindow][area].prepend( doc ); enableActions(); } void DocumentSwitcherPlugin::removeView( Sublime::View* view ) { if( !view ) return; + auto* mainwindow = qobject_cast( sender() ); Q_ASSERT( mainwindow ); + Sublime::UrlDocument *doc = qobject_cast(view->document()); Sublime::Area* area = mainwindow->area(); - int idx = documentLists[mainwindow][area].indexOf( view ); + int idx = documentLists[mainwindow][area].indexOf( doc ); if( idx != -1 ) { documentLists[mainwindow][area].removeAt( idx ); } - qCDebug(PLUGIN_DOCUMENTSWITCHER) << "removing view, list should now not contain this view anymore" << view << view->document()->title(); + qCDebug(PLUGIN_DOCUMENTSWITCHER) << "removing view, list should now not contain this view anymore" << view << doc->title(Sublime::Document::Normal); qCDebug(PLUGIN_DOCUMENTSWITCHER) << "current area is:" << area << area->title() << "mainwindow:" << mainwindow << mainwindow->windowTitle(); - qCDebug(PLUGIN_DOCUMENTSWITCHER) << "idx of this view in list:" << documentLists[mainwindow][area].indexOf( view ); + qCDebug(PLUGIN_DOCUMENTSWITCHER) << "idx of this view in list:" << documentLists[mainwindow][area].indexOf( doc ); enableActions(); } #include "documentswitcherplugin.moc" +