From ac2c487e2ec0eec6cd2ffb2ba6262c69ce04dd3e Mon Sep 17 00:00:00 2001 From: David Schulz Date: Thu, 25 Jun 2020 13:42:35 +0200 Subject: [PATCH] LSP: Use document contents to collect search result texts Otherwise search results in modified files will get wrong line texts. Change-Id: I7be4b27ebc5b250da3a3a0050de8646bcfcd010b Reviewed-by: Christian Stenger --- .../languageclientsymbolsupport.cpp | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/plugins/languageclient/languageclientsymbolsupport.cpp b/src/plugins/languageclient/languageclientsymbolsupport.cpp index efd019c2eab..8dc5f7bd516 100644 --- a/src/plugins/languageclient/languageclientsymbolsupport.cpp +++ b/src/plugins/languageclient/languageclientsymbolsupport.cpp @@ -139,23 +139,40 @@ struct ItemData QVariant userData; }; +static QStringList getFileContents(const QString &filePath) +{ + QString fileContent; + if (TextEditor::TextDocument *document = TextEditor::TextDocument::textDocumentForFilePath( + Utils::FilePath::fromString(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) + != Utils::TextFileFormat::ReadSuccess) { + qDebug() << "Failed to read file" << filePath << ":" << error; + } + } + return fileContent.split("\n"); +} + QList generateSearchResultItems( const QMap> &rangesInDocument) { QList result; for (auto it = rangesInDocument.begin(); it != rangesInDocument.end(); ++it) { const QString &fileName = it.key(); - QFile file(fileName); - file.open(QFile::ReadOnly); Core::SearchResultItem item; item.path = QStringList() << fileName; item.useTextEditorFont = true; - QStringList lines = QString::fromLocal8Bit(file.readAll()).split(QChar::LineFeed); + QStringList lines = getFileContents(fileName); for (const ItemData &data : it.value()) { item.mainRange = data.range; - if (file.isOpen() && data.range.begin.line > 0 && data.range.begin.line <= lines.size()) + if (data.range.begin.line > 0 && data.range.begin.line <= lines.size()) item.text = lines[data.range.begin.line - 1]; else item.text.clear();