Changeset View
Changeset View
Standalone View
Standalone View
app/urlinfo.h
Show All 15 Lines | 1 | /* This file is part of the KDE project | |||
---|---|---|---|---|---|
16 | 16 | | |||
17 | You should have received a copy of the GNU Lesser General Public | 17 | You should have received a copy of the GNU Lesser General Public | ||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #ifndef URLINFO_H | 21 | #ifndef URLINFO_H | ||
22 | #define URLINFO_H | 22 | #define URLINFO_H | ||
23 | 23 | | |||
24 | #include <util/texteditorhelpers.h> | ||||
24 | #include <KTextEditor/Cursor> | 25 | #include <KTextEditor/Cursor> | ||
25 | 26 | | |||
26 | #include <QRegularExpression> | | |||
27 | #include <QDataStream> | 27 | #include <QDataStream> | ||
28 | #include <QDir> | 28 | #include <QDir> | ||
29 | #include <QString> | 29 | #include <QString> | ||
30 | #include <QUrl> | 30 | #include <QUrl> | ||
31 | 31 | | |||
32 | /** | 32 | /** | ||
33 | * Represents a file to be opened, consisting of its URL and the cursor to jump to. | 33 | * Represents a file to be opened, consisting of its URL and the cursor to jump to. | ||
34 | */ | 34 | */ | ||
35 | class UrlInfo | 35 | class UrlInfo | ||
36 | { | 36 | { | ||
37 | public: | 37 | public: | ||
38 | /** | 38 | /** | ||
39 | * Parses a file path argument and determines its line number and column and full path | 39 | * Parses a file path argument and determines its line number and column and full path | ||
40 | * @param path path passed on e.g. command line to parse into an URL | 40 | * @param path path passed on e.g. command line to parse into an URL | ||
41 | */ | 41 | */ | ||
42 | UrlInfo(QString path = QString()) | 42 | explicit UrlInfo(QString path = QString()) | ||
43 | : cursor(KTextEditor::Cursor::invalid()) | 43 | : cursor(KTextEditor::Cursor::invalid()) | ||
44 | { | 44 | { | ||
45 | /** | 45 | /** | ||
46 | * first try: just check if the path is an existing file | 46 | * first try: just check if the path is an existing file | ||
47 | */ | 47 | */ | ||
48 | if (QFile::exists(path)) { | 48 | if (QFile::exists(path)) { | ||
49 | /** | 49 | /** | ||
50 | * create absolute file path, we will e.g. pass this over dbus to other processes | 50 | * create absolute file path, we will e.g. pass this over dbus to other processes | ||
51 | * and then we are done, no cursor can be detected here! | 51 | * and then we are done, no cursor can be detected here! | ||
52 | */ | 52 | */ | ||
53 | url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path)); | 53 | url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path)); | ||
54 | return; | 54 | return; | ||
55 | } | 55 | } | ||
56 | 56 | | |||
57 | /** | 57 | /** | ||
58 | * ok, the path as is, is no existing file, now, cut away :xx:yy stuff as cursor | 58 | * ok, the path as is, is no existing file, now, cut away :xx:yy stuff as cursor | ||
59 | * this will make test:50 to test with line 50 | 59 | * this will make test:50 to test with line 50 | ||
60 | */ | 60 | */ | ||
61 | const auto match = QRegularExpression(QStringLiteral(":(\\d+)(?::(\\d+))?:?$")).match(path); | 61 | int pathLength; | ||
62 | if (match.isValid()) { | 62 | cursor = KDevelop::KTextEditorHelpers::extractCursor(path, &pathLength); | ||
63 | if (cursor.isValid()) { | ||||
63 | /** | 64 | /** | ||
64 | * cut away the line/column specification from the path | 65 | * cut away the line/column specification from the path | ||
65 | */ | 66 | */ | ||
66 | path.chop(match.capturedLength()); | 67 | path.truncate(pathLength); | ||
67 | | ||||
68 | /** | | |||
69 | * set right cursor position | | |||
70 | * don't use an invalid column when the line is valid | | |||
71 | */ | | |||
72 | const int line = match.captured(1).toInt() - 1; | | |||
73 | const int column = qMax(0, match.captured(2).toInt() - 1); | | |||
74 | cursor.setPosition(line, column); | | |||
75 | } | 68 | } | ||
76 | 69 | | |||
77 | /** | 70 | /** | ||
78 | * construct url: | 71 | * construct url: | ||
79 | * - make relative paths absolute using the current working directory | 72 | * - make relative paths absolute using the current working directory | ||
80 | * - prefer local file, if in doubt! | 73 | * - prefer local file, if in doubt! | ||
81 | */ | 74 | */ | ||
82 | url = QUrl::fromUserInput(path, QDir::currentPath(), QUrl::AssumeLocalFile); | 75 | url = QUrl::fromUserInput(path, QDir::currentPath(), QUrl::AssumeLocalFile); | ||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |