Commit e099d0c2 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Fix histories for viewspaces are mixed

Since we store history globally in the mainwindow, history gets messed
if you have two or more viewspaces. With this change, history will be
maintained per viewspace.
parent 0645f417
......@@ -249,26 +249,6 @@ void KateMainWindow::setupImportantActions()
connect(a, &QAction::triggered, this, &KateMainWindow::slotQuickOpen);
a->setWhatsThis(i18n("Open a form to quick open documents."));
a = actionCollection()->addAction(QStringLiteral("view_history_back"));
a->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left")));
a->setText(i18n("Jump to previous location"));
// actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_O));
connect(a, &QAction::triggered, this, &KateMainWindow::goBack);
// ensure they have the right state, start with no history
a->setEnabled(false);
connect(this, &KateMainWindow::backButtonEnabled, a, &QAction::setEnabled);
a = actionCollection()->addAction(QStringLiteral("view_history_forward"));
a->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right")));
a->setText(i18n("Jump to next location"));
// actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_O));
connect(a, &QAction::triggered, this, &KateMainWindow::goForward);
// ensure they have the right state, start with no history
a->setEnabled(false);
connect(this, &KateMainWindow::forwardButtonEnabled, a, &QAction::setEnabled);
// kate command bar
a = actionCollection()->addAction(QStringLiteral("view_commandbar_open"));
actionCollection()->setDefaultShortcut(a, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_I));
......@@ -1209,38 +1189,6 @@ QObject *KateMainWindow::pluginView(const QString &name)
return m_pluginViews.contains(plugin) ? m_pluginViews.value(plugin) : nullptr;
}
void KateMainWindow::addJump(const QUrl &url, KTextEditor::Cursor c)
{
// we are in the middle of jumps somewhere?
if (!m_locations.isEmpty() && currentLocation + 1 < m_locations.size()) {
// erase all forward history
m_locations.erase(m_locations.begin() + currentLocation + 1, m_locations.end());
}
// if same line, remove last entry
if (!m_locations.isEmpty() && m_locations.back().url == url && m_locations.back().cursor.line() == c.line()) {
m_locations.pop_back();
}
// limit size to 100, remove first 20
if (m_locations.size() >= 100) {
m_locations.erase(m_locations.begin(), m_locations.begin() + 20);
}
// this is our new forward
m_locations.push_back({url, c});
// set to last
currentLocation = m_locations.size() - 1;
// disable forward button as we are at the end now
Q_EMIT forwardButtonEnabled(false);
// renable back
if (currentLocation > 0) {
Q_EMIT backButtonEnabled(true);
}
}
void KateMainWindow::mousePressEvent(QMouseEvent *e)
{
switch (e->button()) {
......@@ -1302,87 +1250,6 @@ void KateMainWindow::slotCommandBarOpen()
m_lastUsedCmdBarActions = commandBar.lastUsedCmdBarActions();
}
void KateMainWindow::goBack()
{
if (m_locations.isEmpty() || currentLocation == 0) {
return;
}
const auto &location = m_locations.at(currentLocation - 1);
currentLocation--;
if (currentLocation <= 0) {
Q_EMIT backButtonEnabled(false);
}
if (!location.url.isValid() || !location.cursor.isValid()) {
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), QStringLiteral("Error"));
genericMessage.insert(QStringLiteral("category"), i18n("Git"));
genericMessage.insert(QStringLiteral("text"),
i18n("Failed to jump to: %1 %2 %3", location.url.toDisplayString(), location.cursor.line(), location.cursor.column()));
m_outputView->slotMessage(genericMessage);
m_locations.remove(currentLocation);
return;
}
if (activeView() && activeView()->document() && activeView()->document()->url() == location.url) {
const QSignalBlocker blocker(activeView());
activeView()->setCursorPosition(location.cursor);
// enable forward
Q_EMIT forwardButtonEnabled(true);
return;
}
auto v = openUrl(location.url);
const QSignalBlocker blocker(v);
v->setCursorPosition(location.cursor);
// enable forward
Q_EMIT forwardButtonEnabled(true);
}
void KateMainWindow::goForward()
{
if (m_locations.isEmpty()) {
return;
}
if (currentLocation == m_locations.size() - 1) {
return;
}
const auto &location = m_locations.at(currentLocation + 1);
currentLocation++;
if (currentLocation + 1 >= m_locations.size()) {
Q_EMIT forwardButtonEnabled(false);
}
Q_EMIT backButtonEnabled(true);
if (!location.url.isValid() || !location.cursor.isValid()) {
QVariantMap genericMessage;
genericMessage.insert(QStringLiteral("type"), QStringLiteral("Error"));
genericMessage.insert(QStringLiteral("category"), i18n("Git"));
genericMessage.insert(QStringLiteral("text"),
i18n("Failed to jump to: %1 %2 %3", location.url.toDisplayString(), location.cursor.line(), location.cursor.column()));
m_outputView->slotMessage(genericMessage);
m_locations.remove(currentLocation);
return;
}
if (activeView() && activeView()->document() && activeView()->document()->url() == location.url) {
const QSignalBlocker blocker(activeView());
activeView()->setCursorPosition(location.cursor);
return;
}
auto v = openUrl(location.url);
const QSignalBlocker blocker(v);
v->setCursorPosition(location.cursor);
}
QWidget *KateMainWindow::createToolView(KTextEditor::Plugin *plugin,
const QString &identifier,
KTextEditor::MainWindow::ToolViewPosition pos,
......
......@@ -211,10 +211,6 @@ private Q_SLOTS:
void slotListRecursiveEntries(KIO::Job *job, const KIO::UDSEntryList &list);
// jump in location history
void goBack();
void goForward();
private Q_SLOTS:
void toggleShowMenuBar(bool showMessage = true);
void toggleShowStatusBar();
......@@ -228,8 +224,6 @@ Q_SIGNALS:
void statusBarToggled();
void tabBarToggled();
void unhandledShortcutOverride(QEvent *e);
void backButtonEnabled(bool enabled);
void forwardButtonEnabled(bool enabled);
public:
void openUrl(const QString &name = QString());
......@@ -521,11 +515,6 @@ public Q_SLOTS:
*/
QObject *pluginView(const QString &name);
/**
* Add a jump location for jumping back and forth between history
*/
void addJump(const QUrl &url, KTextEditor::Cursor);
private Q_SLOTS:
void slotUpdateBottomViewBar();
......@@ -580,14 +569,6 @@ private:
QVector<QString> m_lastUsedCmdBarActions;
struct Location {
QUrl url;
KTextEditor::Cursor cursor;
};
QVector<Location> m_locations;
int currentLocation = 0;
class BarState
{
public:
......
......@@ -452,10 +452,6 @@ KTextEditor::View *KateViewManager::createView(KTextEditor::Document *doc, KateV
// clang-format on
connect(view, &KTextEditor::View::focusIn, this, &KateViewManager::activateSpace);
connect(view, &KTextEditor::View::cursorPositionChanged, this, [this](KTextEditor::View *view, const KTextEditor::Cursor &newPosition) {
m_mainWindow->addJump(view->document()->url(), newPosition);
});
viewCreated(view);
if (!vs) {
......
......@@ -49,24 +49,28 @@ KateViewSpace::KateViewSpace(KateViewManager *viewManager, QWidget *parent, cons
// add left <-> right history buttons
m_historyBack = new QToolButton(this);
auto hlAct = m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_history_back"));
m_historyBack->setDefaultAction(hlAct);
m_historyBack->setToolTip(hlAct->text());
m_historyBack->setIcon(hlAct->icon());
m_historyBack->setToolTip(i18n("Go Back"));
m_historyBack->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left")));
m_historyBack->setAutoRaise(true);
KAcceleratorManager::setNoAccel(m_historyBack);
m_historyBack->installEventFilter(this); // on click, active this view space
hLayout->addWidget(m_historyBack);
connect(m_historyBack, &QToolButton::clicked, this, [this] {
goBack();
});
m_historyBack->setEnabled(false);
m_historyForward = new QToolButton(this);
auto hrAct = m_viewManager->mainWindow()->actionCollection()->action(QStringLiteral("view_history_forward"));
m_historyForward->setDefaultAction(hrAct);
m_historyForward->setIcon(hrAct->icon());
m_historyForward->setToolTip(hrAct->text());
m_historyForward->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right")));
m_historyForward->setToolTip(i18n("Go Forward"));
m_historyForward->setAutoRaise(true);
KAcceleratorManager::setNoAccel(m_historyForward);
m_historyForward->installEventFilter(this); // on click, active this view space
hLayout->addWidget(m_historyForward);
connect(m_historyForward, &QToolButton::clicked, this, [this] {
goForward();
});
m_historyForward->setEnabled(false);
// add tab bar
m_tabBar = new KateTabBar(this);
......@@ -207,6 +211,11 @@ KTextEditor::View *KateViewSpace::createView(KTextEditor::Document *doc)
}
}
connect(v, &KTextEditor::View::cursorPositionChanged, this, [this](KTextEditor::View *view, const KTextEditor::Cursor &newPosition) {
if (view && view->document())
addJump(view->document()->url(), newPosition);
});
// register document, it is shown below through showView() then
registerDocument(doc);
......@@ -465,6 +474,37 @@ void KateViewSpace::focusNextTab()
}
}
void KateViewSpace::addJump(const QUrl &url, KTextEditor::Cursor c)
{
// we are in the middle of jumps somewhere?
if (!m_locations.isEmpty() && currentLocation + 1 < m_locations.size()) {
// erase all forward history
m_locations.erase(m_locations.begin() + currentLocation + 1, m_locations.end());
}
// if same line, remove last entry
if (!m_locations.isEmpty() && m_locations.back().url == url && m_locations.back().cursor.line() == c.line()) {
m_locations.pop_back();
}
// limit size to 100, remove first 20
if (m_locations.size() >= 100) {
m_locations.erase(m_locations.begin(), m_locations.begin() + 20);
}
// this is our new forward
m_locations.push_back({url, c});
// set to last
currentLocation = m_locations.size() - 1;
// disable forward button as we are at the end now
m_historyForward->setEnabled(false);
// renable back
if (currentLocation > 0) {
m_historyBack->setEnabled(true);
}
}
int KateViewSpace::hiddenDocuments() const
{
const int hiddenDocs = KateApp::self()->documents().count() - m_tabBar->count();
......@@ -647,4 +687,71 @@ void KateViewSpace::restoreConfig(KateViewManager *viewMan, const KConfigBase *c
m_group = groupname; // used for restroing view configs later
}
void KateViewSpace::goBack()
{
if (m_locations.isEmpty() || currentLocation == 0) {
return;
}
const auto &location = m_locations.at(currentLocation - 1);
currentLocation--;
if (currentLocation <= 0) {
m_historyBack->setEnabled(false);
}
if (auto v = m_viewManager->activeView()) {
if (v->document() && v->document()->url() == location.url) {
const QSignalBlocker blocker(m_viewManager->activeView());
m_viewManager->activeView()->setCursorPosition(location.cursor);
// enable forward
m_historyForward->setEnabled(true);
return;
}
}
auto v = m_viewManager->openUrlWithView(location.url, QString());
const QSignalBlocker blocker(v);
v->setCursorPosition(location.cursor);
// enable forward
m_historyForward->setEnabled(true);
}
void KateViewSpace::goForward()
{
if (m_locations.isEmpty()) {
return;
}
if (currentLocation == m_locations.size() - 1) {
return;
}
const auto &location = m_locations.at(currentLocation + 1);
currentLocation++;
if (currentLocation + 1 >= m_locations.size()) {
m_historyForward->setEnabled(false);
}
if (!location.url.isValid() || !location.cursor.isValid()) {
m_locations.remove(currentLocation);
return;
}
m_historyBack->setEnabled(true);
if (auto v = m_viewManager->activeView()) {
if (v->document() && v->document()->url() == location.url) {
const QSignalBlocker blocker(m_viewManager->activeView());
m_viewManager->activeView()->setCursorPosition(location.cursor);
return;
}
}
auto v = m_viewManager->openUrlWithView(location.url, QString());
const QSignalBlocker blocker(v);
v->setCursorPosition(location.cursor);
}
// END KateViewSpace
......@@ -136,6 +136,14 @@ private:
*/
int hiddenDocuments() const;
/**
* Add a jump location for jumping back and forth between history
*/
void addJump(const QUrl &url, KTextEditor::Cursor);
void goForward();
void goBack();
private:
// Kate's view manager
KateViewManager *m_viewManager;
......@@ -150,6 +158,15 @@ private:
// widget stack that contains all KTE::Views
QStackedWidget *stack;
// jump location history for this view-space
struct Location {
QUrl url;
KTextEditor::Cursor cursor;
};
QVector<Location> m_locations;
int currentLocation = 0;
/**
* all documents this view space is aware of
* depending on the limit of tabs, not all will have a corresponding
......
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