Changeset View
Changeset View
Standalone View
Standalone View
app/urlinfo.h
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2015 Milian Wolff <mail@milianw.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
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/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #ifndef URLINFO_H | ||||
22 | #define URLINFO_H | ||||
23 | | ||||
24 | #include <KTextEditor/Cursor> | ||||
25 | | ||||
26 | #include <QRegularExpression> | ||||
27 | #include <QDataStream> | ||||
28 | #include <QDir> | ||||
29 | #include <QString> | ||||
30 | #include <QUrl> | ||||
31 | | ||||
32 | /** | ||||
33 | * Represents a file to be opened, consisting of its URL and the cursor to jump to. | ||||
34 | */ | ||||
35 | class UrlInfo | ||||
36 | { | ||||
37 | public: | ||||
38 | /** | ||||
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 | ||||
41 | */ | ||||
42 | UrlInfo(QString path = QString()) | ||||
43 | : cursor(KTextEditor::Cursor::invalid()) | ||||
44 | { | ||||
45 | /** | ||||
46 | * construct url: | ||||
47 | * - make relative paths absolute using the current working directory | ||||
48 | * - prefer local file, if in doubt! | ||||
49 | */ | ||||
50 | url = QUrl::fromUserInput(path, QDir::currentPath(), QUrl::AssumeLocalFile); | ||||
51 | | ||||
52 | /** | ||||
53 | * in some cases, this will fail, e.g. if you have line/column specs like test.c:10:1 | ||||
54 | * => fallback: assume a local file and just convert it to an url | ||||
55 | */ | ||||
56 | if (!url.isValid()) { | ||||
57 | /** | ||||
58 | * create absolute file path, we will e.g. pass this over dbus to other processes | ||||
59 | */ | ||||
60 | url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path)); | ||||
61 | } | ||||
62 | | ||||
63 | /** | ||||
64 | * Allow opening specific lines in documents, like mydoc.cpp:10 | ||||
65 | * also supports columns, i.e. mydoc.cpp:10:42 | ||||
66 | * ignores trailing colons, as compile errors often use that format | ||||
67 | */ | ||||
68 | if (url.isLocalFile() && !QFile::exists(url.toLocalFile())) { | ||||
69 | /** | ||||
70 | * update path from url, might have been file://... | ||||
71 | */ | ||||
72 | path = url.toLocalFile(); | ||||
73 | | ||||
74 | /** | ||||
75 | * try to match the line/colum spec, else we are done here | ||||
76 | */ | ||||
77 | const auto match = QRegularExpression(QStringLiteral(":(\\d+)(?::(\\d+))?:?$")).match(path); | ||||
78 | if (!match.isValid()) | ||||
79 | return; | ||||
80 | | ||||
81 | /** | ||||
82 | * cut away the line/column specification from the path and update the url | ||||
83 | */ | ||||
84 | path.chop(match.capturedLength()); | ||||
85 | url = QUrl::fromLocalFile(path); | ||||
86 | | ||||
87 | /** | ||||
88 | * set right cursor position | ||||
89 | */ | ||||
90 | int line = match.captured(1).toInt() - 1; | ||||
91 | // don't use an invalid column when the line is valid | ||||
92 | int column = qMax(0, match.captured(2).toInt() - 1); | ||||
93 | cursor = {line, column}; | ||||
94 | } | ||||
95 | } | ||||
96 | | ||||
97 | /** | ||||
98 | * url computed out of the passed path | ||||
99 | */ | ||||
100 | QUrl url; | ||||
101 | | ||||
102 | /** | ||||
103 | * initial cursor position, if any found inside the path as line/colum specification at the end | ||||
104 | */ | ||||
105 | KTextEditor::Cursor cursor; | ||||
106 | }; | ||||
107 | | ||||
108 | QDataStream& operator<<(QDataStream& stream, const UrlInfo& info) | ||||
109 | { | ||||
110 | stream << info.url; | ||||
111 | stream << info.cursor.line(); | ||||
112 | stream << info.cursor.column(); | ||||
113 | return stream; | ||||
114 | } | ||||
115 | | ||||
116 | QDataStream& operator>>(QDataStream& stream, UrlInfo& info) | ||||
117 | { | ||||
118 | stream >> info.url; | ||||
119 | int line, column; | ||||
120 | stream >> line; | ||||
121 | stream >> column; | ||||
122 | info.cursor.setLine(line); | ||||
123 | info.cursor.setColumn(column); | ||||
124 | return stream; | ||||
125 | } | ||||
126 | | ||||
127 | #endif // URLINFO_H |