Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlhover.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qqmlhover_p.h"
6#include <QtQmlLS/private/qqmllshelputils_p.h>
7
9
10Q_STATIC_LOGGING_CATEGORY(hoverLog, "qt.languageserver.hover")
11
12QQmlHover::QQmlHover(QmlLsp::QQmlCodeModelManager *codeModelManager)
13 : QQmlBaseModule(codeModelManager)
14{
15}
16
17QQmlHover::~QQmlHover() = default;
18
19void QQmlHover::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
20{
21 protocol->registerHoverRequestHandler(getRequestHandler());
22}
23
24void QQmlHover::setupCapabilities(QLspSpecification::ServerCapabilities &caps)
25{
26 caps.hoverProvider = true;
27}
28
29void QQmlHover::process(RequestPointerArgument request)
30{
31 HelpManager *helpManager =
32 m_codeModelManager->helpManagerForUrl(request->m_parameters.textDocument.uri);
33
34 if (!helpManager) {
35 qCWarning(hoverLog)
36 << "No help manager is available, documentation hints will not function!";
37 return;
38 }
39 using namespace QQmlJS::Dom;
40 QLspSpecification::Hover result;
41 ResponseScopeGuard guard(result, request->m_response);
42 if (!request) {
43 qCWarning(hoverLog) << "No hover information is available!";
44 return;
45 }
46 const auto textDocument = request->m_parameters.textDocument;
47 const auto position = request->m_parameters.position;
48 const auto doc =
49 m_codeModelManager->openDocumentByUrl(QQmlLSUtils::lspUriToQmlUrl(textDocument.uri));
50 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
51 if (!file) {
52 guard.setError(QQmlLSUtils::ErrorMessage{
53 0, u"Could not find the file %1"_s.arg(doc.snapshot.doc.canonicalFilePath()) });
54 return;
55 }
56
57 const auto documentation = helpManager->documentationForItem(file, position);
58 if (!documentation.has_value()) {
59 qCDebug(hoverLog)
60 << QStringLiteral(
61 "No documentation hints found for the item at (line, col): (%1,%2)")
62 .arg(position.line)
63 .arg(position.character);
64 return;
65 }
66 QLspSpecification::MarkupContent content;
67 // TODO: We need to do post-formatting what we fetch from documentation.
68 content.kind = QLspSpecification::MarkupKind::Markdown;
69 content.value = documentation.value();
70 result.contents = std::move(content);
71}
72
73QT_END_NAMESPACE
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
Definition qqmlhover.cpp:19
void process(RequestPointerArgument req) override
Definition qqmlhover.cpp:29
void setupCapabilities(QLspSpecification::ServerCapabilities &caps) override
Definition qqmlhover.cpp:24
~QQmlHover() override
Combined button and popup list for selecting options.