LSP: Fix error when opening a file with spaces in its path

When doing Follow Symbol Under Cursor to a file that has a path with
spaces, QtCreator will show a message box with an error.

The issue is that the resolved file path can still have %20 encoding
after the QUrl::toString convertion to QString.
This is because toString does not support QUrl::FullyDecoded and fallback
to PrettyDecoded instead which won't replace %20 with a space.

To fix this, QUrl::fromPercentEncoding must be used.

Also, an existing use of QUrl::fromPercentEncoding is changed to have
an utf-8 input QByteArray instead of local8Bit as fromPercentEncoding
convert %-encoded values to raw values and then convert the result
from utf-8 to unicode QString.

Change-Id: I784bcc30a67b45dcd218170854d480ebeef4237e
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Alexis Murzeau
2020-04-26 21:51:07 +02:00
parent 9ca930554f
commit e430bfa05a

View File

@@ -399,12 +399,22 @@ Utils::Link Location::toLink() const
{ {
if (!isValid(nullptr)) if (!isValid(nullptr))
return Utils::Link(); return Utils::Link();
// QUrl::FullyDecoded is not supported by QUrl::toString.
// Ensure %xx like %20 are really decoded using fromPercentEncoding
// Else, a path with spaces would keep its %20 which would cause failure
// to open the file by the text editor. This is the cases with compilers in
// C:\Programs Files on Windows.
auto file = uri().toString(QUrl::FullyDecoded | QUrl::PreferLocalFile); auto file = uri().toString(QUrl::FullyDecoded | QUrl::PreferLocalFile);
// fromPercentEncoding convert %xx encoding to raw values and then interpret
// the result as utf-8, so toUtf8() must be used here.
file = QUrl::fromPercentEncoding(file.toUtf8());
return Utils::Link(file, range().start().line() + 1, range().start().character()); return Utils::Link(file, range().start().line() + 1, range().start().character());
} }
DocumentUri::DocumentUri(const QString &other) DocumentUri::DocumentUri(const QString &other)
: QUrl(QUrl::fromPercentEncoding(other.toLocal8Bit())) : QUrl(QUrl::fromPercentEncoding(other.toUtf8()))
{ } { }
DocumentUri::DocumentUri(const Utils::FilePath &other) DocumentUri::DocumentUri(const Utils::FilePath &other)