aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilian Wolff <[email protected]>2015-10-10 01:02:01 +0200
committerMilian Wolff <[email protected]>2015-10-10 01:09:23 +0200
commitfcf16e1b581896e43e923fb9d11a1a781343ee30 (patch)
tree0d881a4b7c3efa08c6a9531b0288c981eb8c53ef
parent247c26f8c2066c44839bf47843af1a6cbe9efc2f (diff)
Make it possible to open files at a certain line or column.
That was possible before, but now we support the much more common format of file:line:column. All of that is optional of course. You can also open just file:line. Note that this check is only done for local files, not remote ones. Dominik said he'll post-review this patch once I pushed it.
-rw-r--r--kate/src/kateapp.cpp25
-rw-r--r--kate/src/kateappadaptor.cpp11
-rw-r--r--kate/src/kateappadaptor.h2
-rw-r--r--kate/src/main.cpp14
-rw-r--r--kwrite/main.cpp23
-rw-r--r--urlinfo.h60
6 files changed, 97 insertions, 38 deletions
diff --git a/kate/src/kateapp.cpp b/kate/src/kateapp.cpp
index 9e19fb5..aea717b 100644
--- a/kate/src/kateapp.cpp
+++ b/kate/src/kateapp.cpp
@@ -35,6 +35,8 @@
#include <QTextCodec>
#include <QApplication>
+#include "../../urlinfo.h"
+
/**
* singleton instance pointer
*/
@@ -153,32 +155,23 @@ bool KateApp::startupKate()
KTextEditor::Document *doc = 0;
const QString codec_name = codec ? QString::fromLatin1(codec->name()) : QString();
- QList<QUrl> urls;
Q_FOREACH(const QString positionalArgument, m_args.positionalArguments()) {
- QUrl url;
-
- // convert to an url
- QRegExp withProtocol(QStringLiteral("^[a-zA-Z]+:")); // TODO: remove after Qt supports this on its own
- if (withProtocol.indexIn(positionalArgument) == 0) {
- url = QUrl::fromUserInput(positionalArgument);
- } else {
- const QString path = QDir::current().absoluteFilePath(positionalArgument);
- url = QUrl::fromLocalFile(path);
- }
+ UrlInfo info(positionalArgument);
// this file is no local dir, open it, else warn
- bool noDir = !url.isLocalFile() || !QFileInfo(url.toLocalFile()).isDir();
+ bool noDir = !info.url.isLocalFile() || !QFileInfo(info.url.toLocalFile()).isDir();
if (noDir) {
- urls << url;
+ doc = openDocUrl(info.url, codec_name, tempfileSet);
+ if (info.cursor.isValid()) {
+ setCursor(info.cursor.line(), info.cursor.column());
+ }
} else {
KMessageBox::sorry(activeKateMainWindow(),
- i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", url.toString()));
+ i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", info.url.toString()));
}
}
- doc = activeKateMainWindow()->viewManager()->openUrls(urls, codec_name, tempfileSet);
-
// handle stdin input
if (m_args.isSet(QStringLiteral("stdin"))) {
QTextStream input(stdin, QIODevice::ReadOnly);
diff --git a/kate/src/kateappadaptor.cpp b/kate/src/kateappadaptor.cpp
index f589480..94f3a5c 100644
--- a/kate/src/kateappadaptor.cpp
+++ b/kate/src/kateappadaptor.cpp
@@ -78,6 +78,17 @@ QString KateAppAdaptor::tokenOpenUrl(QString url, QString encoding, bool isTempF
}
return QString::fromLatin1("%1").arg((qptrdiff)doc);
}
+
+QString KateAppAdaptor::tokenOpenUrlAt(QString url, int line, int column, QString encoding, bool isTempFile)
+{
+ qCDebug(LOG_KATE) << "openURLAt";
+ KTextEditor::Document *doc = m_app->openDocUrl(QUrl(url), encoding, isTempFile);
+ if (!doc) {
+ return QStringLiteral("ERROR");
+ }
+ m_app->setCursor(line, column);
+ return QString::fromLatin1("%1").arg((qptrdiff)doc);
+}
//--------
bool KateAppAdaptor::setCursor(int line, int column)
diff --git a/kate/src/kateappadaptor.h b/kate/src/kateappadaptor.h
index 524dc22..47b04b2 100644
--- a/kate/src/kateappadaptor.h
+++ b/kate/src/kateappadaptor.h
@@ -67,6 +67,8 @@ public Q_SLOTS:
QString tokenOpenUrl(QString url, QString encoding, bool isTempFile);
+ QString tokenOpenUrlAt(QString url, int line, int column, QString encoding, bool isTempFile);
+
/**
* set cursor of active view in active main window
* will clear selection
diff --git a/kate/src/main.cpp b/kate/src/main.cpp
index f2977a2..9bd43ee 100644
--- a/kate/src/main.cpp
+++ b/kate/src/main.cpp
@@ -39,6 +39,8 @@
#include <QApplication>
#include <QDir>
+#include "../../urlinfo.h"
+
extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
{
/**
@@ -298,19 +300,13 @@ extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
// open given files...
foreach(const QString & url, urls) {
QDBusMessage m = QDBusMessage::createMethodCall(serviceName,
- QStringLiteral("/MainApplication"), QStringLiteral("org.kde.Kate.Application"), QStringLiteral("tokenOpenUrl"));
+ QStringLiteral("/MainApplication"), QStringLiteral("org.kde.Kate.Application"), QStringLiteral("tokenOpenUrlAt"));
+ UrlInfo info(url);
QList<QVariant> dbusargs;
// convert to an url
- QRegExp withProtocol(QStringLiteral("^[a-zA-Z]+:")); // TODO: remove after Qt supports this on its own
- if (withProtocol.indexIn(url) == 0) {
- dbusargs.append(QUrl::fromUserInput(url).toString());
- } else {
- const QString path = QDir::current().absoluteFilePath(url);
- dbusargs.append(QUrl::fromLocalFile(path).toString());
- }
-
+ dbusargs.append(info.url.toString());
dbusargs.append(enc);
dbusargs.append(tempfileSet);
m.setArguments(dbusargs);
diff --git a/kwrite/main.cpp b/kwrite/main.cpp
index 756f352..abbd574 100644
--- a/kwrite/main.cpp
+++ b/kwrite/main.cpp
@@ -35,6 +35,8 @@
#include <QFileInfo>
#include <QDir>
+#include "../urlinfo.h"
+
extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
{
/**
@@ -213,18 +215,13 @@ extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
} else {
int docs_opened = 0;
Q_FOREACH(const QString positionalArgument, parser.positionalArguments()) {
- QUrl url;
-
- // convert to an url
- QRegExp withProtocol(QStringLiteral("^[a-zA-Z]+:")); // TODO: remove after Qt supports this on its own
- if (withProtocol.indexIn(positionalArgument) == 0) {
- url = QUrl::fromUserInput(positionalArgument);
- } else {
- url = QUrl::fromLocalFile(positionalArgument);
+ UrlInfo info(positionalArgument);
+ if (nav) {
+ info.cursor = KTextEditor::Cursor(line, column);
}
// this file is no local dir, open it, else warn
- bool noDir = !url.isLocalFile() || !QFileInfo(url.toLocalFile()).isDir();
+ bool noDir = !info.url.isLocalFile() || !QFileInfo(info.url.toLocalFile()).isDir();
if (noDir) {
++docs_opened;
@@ -234,13 +231,13 @@ extern "C" Q_DECL_EXPORT int kdemain(int argc, char **argv)
t->view()->document()->setEncoding(QString::fromLatin1(codec->name()));
}
- t->loadURL(url);
+ t->loadURL(info.url);
- if (nav) {
- t->view()->setCursorPosition(KTextEditor::Cursor(line, column));
+ if (info.cursor.isValid()) {
+ t->view()->setCursorPosition(info.cursor);
}
} else {
- KMessageBox::sorry(0, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", url.toString()));
+ KMessageBox::sorry(0, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", info.url.toString()));
}
}
if (!docs_opened) {
diff --git a/urlinfo.h b/urlinfo.h
new file mode 100644
index 0000000..c52c421
--- /dev/null
+++ b/urlinfo.h
@@ -0,0 +1,60 @@
+/* This file is part of the KDE project
+ Copyright (C) 2015 Milian Wolff <[email protected]>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) version 3, or any
+ later version accepted by the membership of KDE e.V. (or its
+ successor approved by the membership of KDE e.V.), which shall
+ act as a proxy defined in Section 6 of version 3 of the license.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef URLINFO_H
+#define URLINFO_H
+
+#include <KTextEditor/Cursor>
+#include <QRegularExpression>
+#include <QString>
+
+// Represents a file to be opened, consisting of its URL and the cursor to jump to.
+struct UrlInfo
+{
+ // Parses a file path argument and determines its line number and column and full path
+ UrlInfo(QString path)
+ : cursor(KTextEditor::Cursor::invalid())
+ {
+ if (!path.startsWith(QLatin1Char('/')) && QFileInfo(path).isRelative()) {
+ path = QDir::currentPath() + QLatin1Char('/') + path;
+ }
+ url = QUrl::fromUserInput(path);
+
+ if (url.isLocalFile() && !QFile::exists(path)) {
+ // Allow opening specific lines in documents, like mydoc.cpp:10
+ // also supports columns, i.e. mydoc.cpp:10:42
+ static const QRegularExpression pattern(QStringLiteral(":(\\d+)(?::(\\d+))?$"));
+ const auto match = pattern.match(path);
+ if (match.isValid()) {
+ path.chop(match.capturedLength());
+ int line = match.captured(1).toInt() - 1;
+ // don't use an invalid column when the line is valid
+ int column = qMax(0, match.captured(2).toInt() - 1);
+ url = QUrl::fromLocalFile(path);
+ cursor = {line, column};
+ }
+ }
+ }
+
+ QUrl url;
+ KTextEditor::Cursor cursor;
+};
+
+#endif // URLINFO_H