File indexing completed on 2024-03-24 04:38:53
0001 /* 0002 SPDX-FileCopyrightText: 2015 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #ifndef URLINFO_H 0008 #define URLINFO_H 0009 0010 #include <util/texteditorhelpers.h> 0011 #include <KTextEditor/Cursor> 0012 0013 #include <QDataStream> 0014 #include <QDir> 0015 #include <QString> 0016 #include <QUrl> 0017 0018 /** 0019 * Represents a file to be opened, consisting of its URL and the cursor to jump to. 0020 */ 0021 class UrlInfo 0022 { 0023 public: 0024 /** 0025 * Parses a file path argument and determines its line number and column and full path 0026 * @param path path passed on e.g. command line to parse into an URL 0027 */ 0028 explicit UrlInfo(QString path = QString()) 0029 : cursor(KTextEditor::Cursor::invalid()) 0030 { 0031 /** 0032 * first try: just check if the path is an existing file 0033 */ 0034 if (QFile::exists(path)) { 0035 /** 0036 * create absolute file path, we will e.g. pass this over dbus to other processes 0037 * and then we are done, no cursor can be detected here! 0038 */ 0039 url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path)); 0040 return; 0041 } 0042 0043 /** 0044 * ok, the path as is, is no existing file, now, cut away :xx:yy stuff as cursor 0045 * this will make test:50 to test with line 50 0046 */ 0047 int pathLength; 0048 cursor = KDevelop::KTextEditorHelpers::extractCursor(path, &pathLength); 0049 if (cursor.isValid()) { 0050 /** 0051 * cut away the line/column specification from the path 0052 */ 0053 path.truncate(pathLength); 0054 } 0055 0056 /** 0057 * construct url: 0058 * - make relative paths absolute using the current working directory 0059 * - prefer local file, if in doubt! 0060 */ 0061 url = QUrl::fromUserInput(path, QDir::currentPath(), QUrl::AssumeLocalFile); 0062 0063 /** 0064 * in some cases, this will fail, e.g. if you have line/column specs like test.c:10:1 0065 * => fallback: assume a local file and just convert it to an url 0066 */ 0067 if (!url.isValid()) { 0068 /** 0069 * create absolute file path, we will e.g. pass this over dbus to other processes 0070 */ 0071 url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path)); 0072 } 0073 } 0074 0075 bool isDirectory() const 0076 { 0077 return url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir() && !cursor.isValid(); 0078 } 0079 0080 /** 0081 * url computed out of the passed path 0082 */ 0083 QUrl url; 0084 0085 /** 0086 * initial cursor position, if any found inside the path as line/column specification at the end 0087 */ 0088 KTextEditor::Cursor cursor; 0089 }; 0090 0091 Q_DECLARE_TYPEINFO(UrlInfo, Q_MOVABLE_TYPE); 0092 0093 QDataStream& operator<<(QDataStream& stream, const UrlInfo& info) 0094 { 0095 stream << info.url; 0096 stream << info.cursor.line(); 0097 stream << info.cursor.column(); 0098 return stream; 0099 } 0100 0101 QDataStream& operator>>(QDataStream& stream, UrlInfo& info) 0102 { 0103 stream >> info.url; 0104 int line, column; 0105 stream >> line; 0106 stream >> column; 0107 info.cursor.setLine(line); 0108 info.cursor.setColumn(column); 0109 return stream; 0110 } 0111 0112 #endif // URLINFO_H