1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25
26 #include "documentsymbolcache.h"
27
28 #include "client.h"
29
30 #include <coreplugin/editormanager/editormanager.h>
31 #include <texteditor/textdocument.h>
32
33 using namespace LanguageServerProtocol;
34
35 namespace LanguageClient {
36
DocumentSymbolCache(Client * client)37 DocumentSymbolCache::DocumentSymbolCache(Client *client)
38 : QObject(client)
39 , m_client(client)
40 {
41 auto connectDocument = [this](Core::IDocument *document) {
42 connect(document, &Core::IDocument::contentsChanged, this, [document, this]() {
43 m_cache.remove(DocumentUri::fromFilePath(document->filePath()));
44 });
45 };
46
47 for (Core::IDocument *document : Core::DocumentModel::openedDocuments())
48 connectDocument(document);
49 connect(Core::EditorManager::instance(),
50 &Core::EditorManager::documentOpened,
51 this,
52 connectDocument);
53 m_compressionTimer.setSingleShot(true);
54 connect(&m_compressionTimer, &QTimer::timeout, this, &DocumentSymbolCache::requestSymbolsImpl);
55 }
56
requestSymbols(const DocumentUri & uri)57 void DocumentSymbolCache::requestSymbols(const DocumentUri &uri)
58 {
59 m_compressedUris.insert(uri);
60 m_compressionTimer.start(200);
61 }
62
requestSymbolsImpl()63 void DocumentSymbolCache::requestSymbolsImpl()
64 {
65 if (!m_client->reachable()) {
66 m_compressionTimer.start(200);
67 return;
68 }
69 for (const DocumentUri &uri : qAsConst(m_compressedUris)) {
70 auto entry = m_cache.find(uri);
71 if (entry != m_cache.end()) {
72 emit gotSymbols(uri, entry.value());
73 continue;
74 }
75
76 const DocumentSymbolParams params((TextDocumentIdentifier(uri)));
77 DocumentSymbolsRequest request(params);
78 request.setResponseCallback([uri, self = QPointer<DocumentSymbolCache>(this)](
79 const DocumentSymbolsRequest::Response &response) {
80 if (self)
81 self->handleResponse(uri, response);
82 });
83 m_client->sendContent(request);
84 }
85 m_compressedUris.clear();
86 }
87
handleResponse(const DocumentUri & uri,const DocumentSymbolsRequest::Response & response)88 void DocumentSymbolCache::handleResponse(const DocumentUri &uri,
89 const DocumentSymbolsRequest::Response &response)
90 {
91 if (Utils::optional<DocumentSymbolsRequest::Response::Error> error = response.error()) {
92 if (m_client)
93 m_client->log(error.value());
94 }
95 const DocumentSymbolsResult &symbols = response.result().value_or(DocumentSymbolsResult());
96 m_cache[uri] = symbols;
97 emit gotSymbols(uri, symbols);
98 }
99
100 } // namespace LanguageClient
101