1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 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 #pragma once
27 
28 #include "languageclient_global.h"
29 
30 #include <coreplugin/find/searchresultitem.h>
31 #include <texteditor/textdocument.h>
32 
33 #include <languageserverprotocol/languagefeatures.h>
34 
35 namespace Core {
36 class SearchResult;
37 class SearchResultItem;
38 }
39 
40 namespace LanguageServerProtocol { class MessageId; }
41 
42 namespace LanguageClient {
43 
44 class Client;
45 
46 class LANGUAGECLIENT_EXPORT SymbolSupport
47 {
48     Q_DECLARE_TR_FUNCTIONS(SymbolSupport)
49 public:
50     explicit SymbolSupport(Client *client);
51 
52     void findLinkAt(TextEditor::TextDocument *document,
53                     const QTextCursor &cursor,
54                     Utils::ProcessLinkCallback callback,
55                     const bool resolveTarget);
56 
57     using ResultHandler = std::function<void(const QList<LanguageServerProtocol::Location> &)>;
58     Utils::optional<LanguageServerProtocol::MessageId> findUsages(
59             TextEditor::TextDocument *document,
60             const QTextCursor &cursor,
61             const ResultHandler &handler = {});
62 
63     bool supportsRename(TextEditor::TextDocument *document);
64     void renameSymbol(TextEditor::TextDocument *document, const QTextCursor &cursor);
65 
66     static Core::Search::TextRange convertRange(const LanguageServerProtocol::Range &range);
67     static QStringList getFileContents(const Utils::FilePath &filePath);
68 
69 private:
70     void handleFindReferencesResponse(
71         const LanguageServerProtocol::FindReferencesRequest::Response &response,
72         const QString &wordUnderCursor,
73         const ResultHandler &handler);
74 
75     void requestPrepareRename(const LanguageServerProtocol::TextDocumentPositionParams &params,
76                               const QString &placeholder);
77     void requestRename(const LanguageServerProtocol::TextDocumentPositionParams &positionParams,
78                        const QString &newName, Core::SearchResult *search);
79     void startRenameSymbol(const LanguageServerProtocol::TextDocumentPositionParams &params,
80                            const QString &placeholder);
81     void handleRenameResponse(Core::SearchResult *search,
82                               const LanguageServerProtocol::RenameRequest::Response &response);
83     void applyRename(const QList<Core::SearchResultItem> &checkedItems);
84 
85     Client *m_client = nullptr;
86 };
87 
88 } // namespace LanguageClient
89