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

Block view creation when opening docs from cmd line

It slows down the startup a lot if you open kate like:

kate dir/*

And even more since the urlbar.

To do this, store cursor position on startup (which is the primary
reason we create the views) and restore this position whenever a view
for a document gets created
parent 0d9fdf63
Pipeline #143835 passed with stage
in 2 minutes and 19 seconds
......@@ -206,6 +206,7 @@ bool KateApp::startupKate()
const QString codec_name = codec ? QString::fromLatin1(codec->name()) : QString();
const auto args = m_args.positionalArguments();
for (const auto &positionalArgument : args) {
UrlInfo info(positionalArgument);
......@@ -217,12 +218,14 @@ bool KateApp::startupKate()
|| !QFileInfo(info.url.toLocalFile()).isDir();
if (noDir) {
doc = openDocUrl(info.url, codec_name, tempfileSet);
if (info.cursor.isValid()) {
setCursor(info.cursor.line(), info.cursor.column());
} else if (hasCursorInArgs()) {
setCursorFromArgs(activeMainWindow()->activeView());
if (!info.cursor.isValid()) {
if (hasCursorInArgs()) {
info.cursor = cursorFromArgs();
} else if (info.url.hasQuery()) {
info.cursor = cursorFromQueryString(info.url);
}
}
doc = openDocUrl(info.url, codec_name, tempfileSet, /*activateView=*/false, info.cursor);
} else if (!KateApp::self()->pluginManager()->plugin(QStringLiteral("kateprojectplugin"))) {
KMessageBox::sorry(activeKateMainWindow(), i18n("Folders can only be opened when the projects plugin is enabled"));
}
......@@ -309,7 +312,7 @@ bool KateApp::isOnActivity(const QString &activity)
return false;
}
KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile)
KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile, bool activateView, KTextEditor::Cursor c)
{
KateMainWindow *mainWindow = activeKateMainWindow();
......@@ -330,12 +333,12 @@ KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encod
if (noDir) {
KateDocumentInfo docInfo;
docInfo.doPostLoadOperations = !url.isLocalFile() && (hasCursorInArgs() || url.hasQuery());
docInfo.startCursor = c;
// open a normal file
if (codec) {
doc = mainWindow->viewManager()->openUrl(url, QString::fromLatin1(codec->name()), true, isTempFile, docInfo);
doc = mainWindow->viewManager()->openUrl(url, QString::fromLatin1(codec->name()), activateView, isTempFile, docInfo);
} else {
doc = mainWindow->viewManager()->openUrl(url, QString(), true, isTempFile, docInfo);
doc = mainWindow->viewManager()->openUrl(url, QString(), activateView, isTempFile, docInfo);
}
} else {
KMessageBox::sorry(mainWindow, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", url.url()));
......@@ -344,62 +347,46 @@ KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encod
return doc;
}
void KateApp::setCursorFromArgs(KTextEditor::View *view)
KTextEditor::Cursor KateApp::cursorFromArgs()
{
int line = 0;
int column = 0;
bool nav = false;
if (!view && !(view = activeKateMainWindow()->activeView())) {
return;
}
int line = -1;
int column = -1;
if (m_args.isSet(QStringLiteral("line"))) {
line = m_args.value(QStringLiteral("line")).toInt() - 1;
nav = true;
}
if (m_args.isSet(QStringLiteral("column"))) {
column = m_args.value(QStringLiteral("column")).toInt() - 1;
nav = true;
}
if (nav) {
view->setCursorPosition(KTextEditor::Cursor(line, column));
activeKateMainWindow()->setAutoSaveSettings();
}
return {line, column};
}
void KateApp::setCursorFromQueryString(KTextEditor::View *view)
KTextEditor::Cursor KateApp::cursorFromQueryString(const QUrl &url)
{
int line = 0;
int column = 0;
bool nav = false;
int line = -1;
int column = -1;
if (!view && !(view = activeKateMainWindow()->activeView())) {
return;
if (!url.hasQuery()) {
return {line, column};
}
QUrlQuery urlQuery(view->document()->url());
QUrlQuery urlQuery(url);
QString lineStr = urlQuery.queryItemValue(QStringLiteral("line"));
QString columnStr = urlQuery.queryItemValue(QStringLiteral("column"));
if (!lineStr.isEmpty()) {
line = lineStr.toInt();
line > 0 && line--;
nav = true;
}
if (!columnStr.isEmpty()) {
column = columnStr.toInt();
column > 0 && column--;
nav = true;
}
if (nav) {
view->setCursorPosition(KTextEditor::Cursor(line, column));
activeKateMainWindow()->setAutoSaveSettings();
}
return {line, column};
}
bool KateApp::setCursor(int line, int column)
......@@ -539,6 +526,7 @@ void KateApp::remoteMessageReceived(const QString &message, QObject *)
return;
}
KTextEditor::Document *doc = nullptr;
/**
* open all passed urls
*/
......@@ -552,14 +540,14 @@ void KateApp::remoteMessageReceived(const QString &message, QObject *)
const int column = urlObject.toObject().value(QLatin1String("column")).toVariant().toInt();
/**
* open file + set line/column if requested
* open file + save line/column if requested
*/
openUrl(url, QString(), false);
if (line >= 0 && column >= 0) {
setCursor(line, column);
}
doc = openDocUrl(url, QString(), false, /*activateView=*/false, KTextEditor::Cursor{line, column});
}
// try to activate current window
m_adaptor.activate();
if (doc && activeMainWindow()) {
activeMainWindow()->activateView(doc);
}
}
......@@ -209,7 +209,8 @@ public:
*/
bool isOnActivity(const QString &activity);
KTextEditor::Document *openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile);
KTextEditor::Document *
openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile, bool activateView = true, KTextEditor::Cursor c = KTextEditor::Cursor::invalid());
void emitDocumentClosed(const QString &token);
......@@ -223,21 +224,19 @@ public:
bool setCursor(int line, int column);
/**
* Checks if --line and/or --column args were provided and attempts
* to set cursor position in the provided or active view accordingly.
*
* @param view Optional view to apply changes on.
* Checks if --line and/or --column args were provided and
* returns the cursor for it
*/
void setCursorFromArgs(KTextEditor::View *view = nullptr);
KTextEditor::Cursor cursorFromArgs();
/**
* Checks if line or column were provided in query string
* (e.g. file:///file1?line=3&column=4) and attempts to set cursor
* position in the provided or active view accordingly.
* (e.g. file:///file1?line=3&column=4) and returns the cursor
* If cursor was not set it will return invalid cursor
*
* @param view Optional view to apply changes on.
* @param url the url to check
*/
void setCursorFromQueryString(KTextEditor::View *view = nullptr);
KTextEditor::Cursor cursorFromQueryString(const QUrl &url);
/**
* @return true if --line or --column command line args were provided
......
......@@ -34,7 +34,7 @@ public:
bool openedByUser = false;
bool openSuccess = true;
bool doPostLoadOperations = false;
KTextEditor::Cursor startCursor = KTextEditor::Cursor::invalid(); // initial cursor position. This can be specified in the command line or as a url query
bool wasDocumentEverModified = false;
};
......
......@@ -463,20 +463,21 @@ KTextEditor::View *KateViewManager::createView(KTextEditor::Document *doc, KateV
* set cursor position for this view if we need to
*/
KateDocumentInfo *docInfo = KateApp::self()->documentManager()->documentInfo(doc);
if (docInfo->doPostLoadOperations) {
docInfo->doPostLoadOperations = false; // do this only once
QSharedPointer<QMetaObject::Connection> conn(new QMetaObject::Connection());
auto handler = [view, conn](KTextEditor::Document *doc) {
disconnect(*conn);
if (doc->url().hasQuery()) {
KateApp::self()->setCursorFromQueryString(view);
} else {
KateApp::self()->setCursorFromArgs(view);
}
};
*conn = connect(doc, &KTextEditor::Document::textChanged, view, handler);
if (docInfo->startCursor.isValid()) {
KTextEditor::Cursor c = docInfo->startCursor; // Was a cursor position requested?
docInfo->startCursor = KTextEditor::Cursor::invalid(); // do this only once
if (!doc->url().isLocalFile()) {
QSharedPointer<QMetaObject::Connection> conn(new QMetaObject::Connection());
auto handler = [view, conn, c](KTextEditor::Document *) {
disconnect(*conn);
view->setCursorPosition(c);
};
*conn = connect(doc, &KTextEditor::Document::textChanged, view, handler);
} else if (c.isValid()) {
view->setCursorPosition(c);
}
}
/**
......
......@@ -1016,8 +1016,12 @@ void KateViewSpace::restoreConfig(KateViewManager *viewMan, const KConfigBase *c
auto *doc = KateApp::self()->documentManager()->documentList().first();
if (!fn.isEmpty()) {
QUrl url(fn);
KateApp::self()->documentManager()->documentInfo(doc)->doPostLoadOperations =
!url.isLocalFile() && (KateApp::self()->hasCursorInArgs() || url.hasQuery());
auto *docInfo = KateApp::self()->documentManager()->documentInfo(doc);
if (KateApp::self()->hasCursorInArgs()) {
docInfo->startCursor = KateApp::self()->cursorFromArgs();
} else if (url.hasQuery()) {
docInfo->startCursor = KateApp::self()->cursorFromQueryString(url);
}
}
viewMan->createView(doc, this);
}
......
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