forked from qt-creator/qt-creator
LanguageClient: Use Utils::FilePath to get file content
Change-Id: Iddb2eb5c02f3e6674f1f71bb61fb1f13dec22794 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -489,8 +489,7 @@ void ClangdClient::Private::handleFindUsagesResult(quint64 key, const QList<Loca
|
|||||||
for (const Location &loc : locations) // TODO: Can contain duplicates. Rather fix in clang than work around it here.
|
for (const Location &loc : locations) // TODO: Can contain duplicates. Rather fix in clang than work around it here.
|
||||||
refData->fileData[loc.uri()].rangesAndLineText << qMakePair(loc.range(), QString()); // TODO: Can we assume that locations for the same file are grouped?
|
refData->fileData[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) {
|
for (auto it = refData->fileData.begin(); it != refData->fileData.end(); ++it) {
|
||||||
const QStringList lines = SymbolSupport::getFileContents(
|
const QStringList lines = SymbolSupport::getFileContents(it.key().toFilePath());
|
||||||
it.key().toFilePath().toString());
|
|
||||||
it->fileContent = lines.join('\n');
|
it->fileContent = lines.join('\n');
|
||||||
for (auto &rangeWithText : it.value().rangesAndLineText) {
|
for (auto &rangeWithText : it.value().rangesAndLineText) {
|
||||||
const int lineNo = rangeWithText.first.start().line();
|
const int lineNo = rangeWithText.first.start().line();
|
||||||
|
@@ -135,18 +135,18 @@ struct ItemData
|
|||||||
QVariant userData;
|
QVariant userData;
|
||||||
};
|
};
|
||||||
|
|
||||||
QStringList SymbolSupport::getFileContents(const QString &filePath)
|
QStringList SymbolSupport::getFileContents(const Utils::FilePath &filePath)
|
||||||
{
|
{
|
||||||
QString fileContent;
|
QString fileContent;
|
||||||
if (TextEditor::TextDocument *document = TextEditor::TextDocument::textDocumentForFilePath(
|
if (TextEditor::TextDocument *document = TextEditor::TextDocument::textDocumentForFilePath(
|
||||||
Utils::FilePath::fromString(filePath))) {
|
filePath)) {
|
||||||
fileContent = document->plainText();
|
fileContent = document->plainText();
|
||||||
} else {
|
} else {
|
||||||
Utils::TextFileFormat format;
|
Utils::TextFileFormat format;
|
||||||
format.lineTerminationMode = Utils::TextFileFormat::LFLineTerminator;
|
format.lineTerminationMode = Utils::TextFileFormat::LFLineTerminator;
|
||||||
QString error;
|
QString error;
|
||||||
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
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) {
|
!= Utils::TextFileFormat::ReadSuccess) {
|
||||||
qDebug() << "Failed to read file" << filePath << ":" << error;
|
qDebug() << "Failed to read file" << filePath << ":" << error;
|
||||||
}
|
}
|
||||||
@@ -155,17 +155,17 @@ QStringList SymbolSupport::getFileContents(const QString &filePath)
|
|||||||
}
|
}
|
||||||
|
|
||||||
QList<Core::SearchResultItem> generateSearchResultItems(
|
QList<Core::SearchResultItem> generateSearchResultItems(
|
||||||
const QMap<QString, QList<ItemData>> &rangesInDocument)
|
const QMap<Utils::FilePath, QList<ItemData>> &rangesInDocument)
|
||||||
{
|
{
|
||||||
QList<Core::SearchResultItem> result;
|
QList<Core::SearchResultItem> result;
|
||||||
for (auto it = rangesInDocument.begin(); it != rangesInDocument.end(); ++it) {
|
for (auto it = rangesInDocument.begin(); it != rangesInDocument.end(); ++it) {
|
||||||
const QString &fileName = it.key();
|
const Utils::FilePath &filePath = it.key();
|
||||||
|
|
||||||
Core::SearchResultItem item;
|
Core::SearchResultItem item;
|
||||||
item.setFilePath(Utils::FilePath::fromString(fileName));
|
item.setFilePath(filePath);
|
||||||
item.setUseTextEditorFont(true);
|
item.setUseTextEditorFont(true);
|
||||||
|
|
||||||
QStringList lines = SymbolSupport::getFileContents(fileName);
|
QStringList lines = SymbolSupport::getFileContents(filePath);
|
||||||
for (const ItemData &data : it.value()) {
|
for (const ItemData &data : it.value()) {
|
||||||
item.setMainRange(data.range);
|
item.setMainRange(data.range);
|
||||||
if (data.range.begin.line > 0 && data.range.begin.line <= lines.size())
|
if (data.range.begin.line > 0 && data.range.begin.line <= lines.size())
|
||||||
@@ -182,9 +182,9 @@ QList<Core::SearchResultItem> generateSearchResultItems(
|
|||||||
{
|
{
|
||||||
if (locations.isNull())
|
if (locations.isNull())
|
||||||
return {};
|
return {};
|
||||||
QMap<QString, QList<ItemData>> rangesInDocument;
|
QMap<Utils::FilePath, QList<ItemData>> rangesInDocument;
|
||||||
for (const Location &location : locations.toList())
|
for (const Location &location : locations.toList())
|
||||||
rangesInDocument[location.uri().toFilePath().toString()]
|
rangesInDocument[location.uri().toFilePath()]
|
||||||
<< ItemData{SymbolSupport::convertRange(location.range()), {}};
|
<< ItemData{SymbolSupport::convertRange(location.range()), {}};
|
||||||
return generateSearchResultItems(rangesInDocument);
|
return generateSearchResultItems(rangesInDocument);
|
||||||
}
|
}
|
||||||
@@ -336,17 +336,17 @@ QList<Core::SearchResultItem> generateReplaceItems(const WorkspaceEdit &edits)
|
|||||||
return ItemData{SymbolSupport::convertRange(edit.range()), QVariant(edit)};
|
return ItemData{SymbolSupport::convertRange(edit.range()), QVariant(edit)};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
QMap<QString, QList<ItemData>> rangesInDocument;
|
QMap<Utils::FilePath, QList<ItemData>> rangesInDocument;
|
||||||
auto documentChanges = edits.documentChanges().value_or(QList<TextDocumentEdit>());
|
auto documentChanges = edits.documentChanges().value_or(QList<TextDocumentEdit>());
|
||||||
if (!documentChanges.isEmpty()) {
|
if (!documentChanges.isEmpty()) {
|
||||||
for (const TextDocumentEdit &documentChange : qAsConst(documentChanges)) {
|
for (const TextDocumentEdit &documentChange : qAsConst(documentChanges)) {
|
||||||
rangesInDocument[documentChange.textDocument().uri().toFilePath().toString()] = convertEdits(
|
rangesInDocument[documentChange.textDocument().uri().toFilePath()] = convertEdits(
|
||||||
documentChange.edits());
|
documentChange.edits());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
auto changes = edits.changes().value_or(WorkspaceEdit::Changes());
|
auto changes = edits.changes().value_or(WorkspaceEdit::Changes());
|
||||||
for (auto it = changes.begin(), end = changes.end(); it != end; ++it)
|
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);
|
return generateSearchResultItems(rangesInDocument);
|
||||||
}
|
}
|
||||||
|
@@ -64,7 +64,7 @@ public:
|
|||||||
void renameSymbol(TextEditor::TextDocument *document, const QTextCursor &cursor);
|
void renameSymbol(TextEditor::TextDocument *document, const QTextCursor &cursor);
|
||||||
|
|
||||||
static Core::Search::TextRange convertRange(const LanguageServerProtocol::Range &range);
|
static Core::Search::TextRange convertRange(const LanguageServerProtocol::Range &range);
|
||||||
static QStringList getFileContents(const QString &filePath);
|
static QStringList getFileContents(const Utils::FilePath &filePath);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handleFindReferencesResponse(
|
void handleFindReferencesResponse(
|
||||||
|
Reference in New Issue
Block a user