From f8ca730121b1266a452019a0272684dec63d1ab8 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Tue, 18 May 2021 07:05:39 +0200 Subject: [PATCH] LanguageClient: Use Utils::FilePath to get file content Change-Id: Iddb2eb5c02f3e6674f1f71bb61fb1f13dec22794 Reviewed-by: Eike Ziller --- src/plugins/clangcodemodel/clangdclient.cpp | 3 +-- .../languageclientsymbolsupport.cpp | 24 +++++++++---------- .../languageclientsymbolsupport.h | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index c7e543c439d..4c158055406 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -489,8 +489,7 @@ void ClangdClient::Private::handleFindUsagesResult(quint64 key, const QListfileData[loc.uri()].rangesAndLineText << qMakePair(loc.range(), QString()); // TODO: Can we assume that locations for the same file are grouped? for (auto it = refData->fileData.begin(); it != refData->fileData.end(); ++it) { - const QStringList lines = SymbolSupport::getFileContents( - it.key().toFilePath().toString()); + const QStringList lines = SymbolSupport::getFileContents(it.key().toFilePath()); it->fileContent = lines.join('\n'); for (auto &rangeWithText : it.value().rangesAndLineText) { const int lineNo = rangeWithText.first.start().line(); diff --git a/src/plugins/languageclient/languageclientsymbolsupport.cpp b/src/plugins/languageclient/languageclientsymbolsupport.cpp index f4165c87a6e..434d4f80506 100644 --- a/src/plugins/languageclient/languageclientsymbolsupport.cpp +++ b/src/plugins/languageclient/languageclientsymbolsupport.cpp @@ -135,18 +135,18 @@ struct ItemData QVariant userData; }; -QStringList SymbolSupport::getFileContents(const QString &filePath) +QStringList SymbolSupport::getFileContents(const Utils::FilePath &filePath) { QString fileContent; if (TextEditor::TextDocument *document = TextEditor::TextDocument::textDocumentForFilePath( - Utils::FilePath::fromString(filePath))) { + filePath)) { fileContent = document->plainText(); } else { Utils::TextFileFormat format; format.lineTerminationMode = Utils::TextFileFormat::LFLineTerminator; QString error; const QTextCodec *codec = Core::EditorManager::defaultTextCodec(); - if (Utils::TextFileFormat::readFile(filePath, codec, &fileContent, &format, &error) + if (Utils::TextFileFormat::readFile(filePath.toString(), codec, &fileContent, &format, &error) != Utils::TextFileFormat::ReadSuccess) { qDebug() << "Failed to read file" << filePath << ":" << error; } @@ -155,17 +155,17 @@ QStringList SymbolSupport::getFileContents(const QString &filePath) } QList generateSearchResultItems( - const QMap> &rangesInDocument) + const QMap> &rangesInDocument) { QList result; for (auto it = rangesInDocument.begin(); it != rangesInDocument.end(); ++it) { - const QString &fileName = it.key(); + const Utils::FilePath &filePath = it.key(); Core::SearchResultItem item; - item.setFilePath(Utils::FilePath::fromString(fileName)); + item.setFilePath(filePath); item.setUseTextEditorFont(true); - QStringList lines = SymbolSupport::getFileContents(fileName); + QStringList lines = SymbolSupport::getFileContents(filePath); for (const ItemData &data : it.value()) { item.setMainRange(data.range); if (data.range.begin.line > 0 && data.range.begin.line <= lines.size()) @@ -182,9 +182,9 @@ QList generateSearchResultItems( { if (locations.isNull()) return {}; - QMap> rangesInDocument; + QMap> rangesInDocument; for (const Location &location : locations.toList()) - rangesInDocument[location.uri().toFilePath().toString()] + rangesInDocument[location.uri().toFilePath()] << ItemData{SymbolSupport::convertRange(location.range()), {}}; return generateSearchResultItems(rangesInDocument); } @@ -336,17 +336,17 @@ QList generateReplaceItems(const WorkspaceEdit &edits) return ItemData{SymbolSupport::convertRange(edit.range()), QVariant(edit)}; }); }; - QMap> rangesInDocument; + QMap> rangesInDocument; auto documentChanges = edits.documentChanges().value_or(QList()); if (!documentChanges.isEmpty()) { for (const TextDocumentEdit &documentChange : qAsConst(documentChanges)) { - rangesInDocument[documentChange.textDocument().uri().toFilePath().toString()] = convertEdits( + rangesInDocument[documentChange.textDocument().uri().toFilePath()] = convertEdits( documentChange.edits()); } } else { auto changes = edits.changes().value_or(WorkspaceEdit::Changes()); for (auto it = changes.begin(), end = changes.end(); it != end; ++it) - rangesInDocument[it.key().toFilePath().toString()] = convertEdits(it.value()); + rangesInDocument[it.key().toFilePath()] = convertEdits(it.value()); } return generateSearchResultItems(rangesInDocument); } diff --git a/src/plugins/languageclient/languageclientsymbolsupport.h b/src/plugins/languageclient/languageclientsymbolsupport.h index 7b6374df712..6ba376a1d51 100644 --- a/src/plugins/languageclient/languageclientsymbolsupport.h +++ b/src/plugins/languageclient/languageclientsymbolsupport.h @@ -64,7 +64,7 @@ public: void renameSymbol(TextEditor::TextDocument *document, const QTextCursor &cursor); static Core::Search::TextRange convertRange(const LanguageServerProtocol::Range &range); - static QStringList getFileContents(const QString &filePath); + static QStringList getFileContents(const Utils::FilePath &filePath); private: void handleFindReferencesResponse(