Commit 09fdd0d1 authored by Kevin Funk's avatar Kevin Funk

Update urlinfo.h to fix string parsing

parent 72a16786
......@@ -43,54 +43,53 @@ public:
: cursor(KTextEditor::Cursor::invalid())
{
/**
* construct url:
* - make relative paths absolute using the current working directory
* - prefer local file, if in doubt!
* first try: just check if the path is an existing file
*/
url = QUrl::fromUserInput(path, QDir::currentPath(), QUrl::AssumeLocalFile);
/**
* in some cases, this will fail, e.g. if you have line/column specs like test.c:10:1
* => fallback: assume a local file and just convert it to an url
*/
if (!url.isValid()) {
if (QFile::exists(path)) {
/**
* create absolute file path, we will e.g. pass this over dbus to other processes
* and then we are done, no cursor can be detected here!
*/
url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path));
return;
}
/**
* Allow opening specific lines in documents, like mydoc.cpp:10
* also supports columns, i.e. mydoc.cpp:10:42
* ignores trailing colons, as compile errors often use that format
* ok, the path as is, is no existing file, now, cut away :xx:yy stuff as cursor
* this will make test:50 to test with line 50
*/
if (url.isLocalFile() && !QFile::exists(url.toLocalFile())) {
const auto match = QRegularExpression(QStringLiteral(":(\\d+)(?::(\\d+))?:?$")).match(path);
if (match.isValid()) {
/**
* update path from url, might have been file://...
* cut away the line/column specification from the path
*/
path = url.toLocalFile();
path.chop(match.capturedLength());
/**
* try to match the line/colum spec, else we are done here
* set right cursor position
* don't use an invalid column when the line is valid
*/
const auto match = QRegularExpression(QStringLiteral(":(\\d+)(?::(\\d+))?:?$")).match(path);
if (!match.isValid())
return;
const int line = match.captured(1).toInt() - 1;
const int column = qMax(0, match.captured(2).toInt() - 1);
cursor.setPosition(line, column);
}
/**
* cut away the line/column specification from the path and update the url
*/
path.chop(match.capturedLength());
url = QUrl::fromLocalFile(path);
/**
* construct url:
* - make relative paths absolute using the current working directory
* - prefer local file, if in doubt!
*/
url = QUrl::fromUserInput(path, QDir::currentPath(), QUrl::AssumeLocalFile);
/**
* in some cases, this will fail, e.g. if you have line/column specs like test.c:10:1
* => fallback: assume a local file and just convert it to an url
*/
if (!url.isValid()) {
/**
* set right cursor position
* create absolute file path, we will e.g. pass this over dbus to other processes
*/
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);
cursor = {line, column};
url = QUrl::fromLocalFile(QDir::current().absoluteFilePath(path));
}
}
......
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