2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
2019-04-04 14:36:28 +02:00
|
|
|
|
|
|
|
|
#include "documentsymbolcache.h"
|
|
|
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <texteditor/textdocument.h>
|
|
|
|
|
|
|
|
|
|
using namespace LanguageServerProtocol;
|
|
|
|
|
|
|
|
|
|
namespace LanguageClient {
|
|
|
|
|
|
|
|
|
|
DocumentSymbolCache::DocumentSymbolCache(Client *client)
|
|
|
|
|
: QObject(client)
|
|
|
|
|
, m_client(client)
|
|
|
|
|
{
|
2020-01-29 10:06:41 +01:00
|
|
|
auto connectDocument = [this](Core::IDocument *document) {
|
|
|
|
|
connect(document, &Core::IDocument::contentsChanged, this, [document, this]() {
|
2022-02-07 08:07:12 +01:00
|
|
|
const auto uri = DocumentUri::fromFilePath(document->filePath());
|
2020-01-29 10:06:41 +01:00
|
|
|
m_cache.remove(DocumentUri::fromFilePath(document->filePath()));
|
2022-02-07 08:07:12 +01:00
|
|
|
auto requestIdIt = m_runningRequests.find(uri);
|
|
|
|
|
if (requestIdIt != m_runningRequests.end()) {
|
|
|
|
|
m_client->cancelRequest(requestIdIt.value());
|
|
|
|
|
m_runningRequests.erase(requestIdIt);
|
|
|
|
|
}
|
2020-01-29 10:06:41 +01:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (Core::IDocument *document : Core::DocumentModel::openedDocuments())
|
|
|
|
|
connectDocument(document);
|
2019-04-04 14:36:28 +02:00
|
|
|
connect(Core::EditorManager::instance(),
|
|
|
|
|
&Core::EditorManager::documentOpened,
|
|
|
|
|
this,
|
2020-01-29 10:06:41 +01:00
|
|
|
connectDocument);
|
2020-01-29 11:17:47 +01:00
|
|
|
m_compressionTimer.setSingleShot(true);
|
|
|
|
|
connect(&m_compressionTimer, &QTimer::timeout, this, &DocumentSymbolCache::requestSymbolsImpl);
|
2019-04-04 14:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-14 17:00:16 +02:00
|
|
|
void DocumentSymbolCache::requestSymbols(const DocumentUri &uri, Schedule schedule)
|
2019-04-04 14:36:28 +02:00
|
|
|
{
|
2020-01-29 11:17:47 +01:00
|
|
|
m_compressedUris.insert(uri);
|
2021-09-14 17:00:16 +02:00
|
|
|
switch (schedule) {
|
|
|
|
|
case Schedule::Now:
|
|
|
|
|
requestSymbolsImpl();
|
|
|
|
|
break;
|
|
|
|
|
case Schedule::Delayed:
|
|
|
|
|
m_compressionTimer.start(200);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-01-29 11:17:47 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-10 10:49:30 +01:00
|
|
|
bool clientSupportsDocumentSymbols(const Client *client, const DocumentUri &uri)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(client, return false);
|
|
|
|
|
const auto doc = TextEditor::TextDocument::textDocumentForFilePath(uri.toFilePath());
|
|
|
|
|
return client->supportsDocumentSymbols(doc);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 11:17:47 +01:00
|
|
|
void DocumentSymbolCache::requestSymbolsImpl()
|
|
|
|
|
{
|
2021-07-01 08:00:51 +02:00
|
|
|
if (!m_client->reachable()) {
|
|
|
|
|
m_compressionTimer.start(200);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-01-29 11:17:47 +01:00
|
|
|
for (const DocumentUri &uri : qAsConst(m_compressedUris)) {
|
|
|
|
|
auto entry = m_cache.find(uri);
|
|
|
|
|
if (entry != m_cache.end()) {
|
|
|
|
|
emit gotSymbols(uri, entry.value());
|
2020-02-28 14:45:40 +01:00
|
|
|
continue;
|
2020-01-29 11:17:47 +01:00
|
|
|
}
|
2019-04-04 14:36:28 +02:00
|
|
|
|
2021-12-10 10:49:30 +01:00
|
|
|
if (!LanguageClient::clientSupportsDocumentSymbols(m_client, uri)) {
|
|
|
|
|
emit gotSymbols(uri, nullptr);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 11:17:47 +01:00
|
|
|
const DocumentSymbolParams params((TextDocumentIdentifier(uri)));
|
|
|
|
|
DocumentSymbolsRequest request(params);
|
|
|
|
|
request.setResponseCallback([uri, self = QPointer<DocumentSymbolCache>(this)](
|
|
|
|
|
const DocumentSymbolsRequest::Response &response) {
|
|
|
|
|
if (self)
|
|
|
|
|
self->handleResponse(uri, response);
|
|
|
|
|
});
|
2022-02-07 08:07:12 +01:00
|
|
|
m_runningRequests[uri] = request.id();
|
2022-05-12 09:51:39 +02:00
|
|
|
m_client->sendMessage(request);
|
2020-01-29 11:17:47 +01:00
|
|
|
}
|
2020-02-28 14:45:13 +01:00
|
|
|
m_compressedUris.clear();
|
2019-04-04 14:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DocumentSymbolCache::handleResponse(const DocumentUri &uri,
|
|
|
|
|
const DocumentSymbolsRequest::Response &response)
|
|
|
|
|
{
|
2022-02-07 08:07:12 +01:00
|
|
|
m_runningRequests.remove(uri);
|
2019-04-04 14:36:28 +02:00
|
|
|
if (Utils::optional<DocumentSymbolsRequest::Response::Error> error = response.error()) {
|
|
|
|
|
if (m_client)
|
2022-02-24 09:38:59 +01:00
|
|
|
m_client->log(*error);
|
2019-04-04 14:36:28 +02:00
|
|
|
}
|
|
|
|
|
const DocumentSymbolsResult &symbols = response.result().value_or(DocumentSymbolsResult());
|
|
|
|
|
m_cache[uri] = symbols;
|
|
|
|
|
emit gotSymbols(uri, symbols);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace LanguageClient
|