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
20{
21 return u"QQmlHover"_s;
22}
23
24void QQmlHover::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
25{
26 protocol->registerHoverRequestHandler(getRequestHandler());
27}
28
30 const QLspSpecification::InitializeParams &,
31 QLspSpecification::InitializeResult &serverCapabilities)
32{
33 serverCapabilities.capabilities.hoverProvider = true;
34}
35
36void QQmlHover::process(RequestPointerArgument request)
37{
38 HelpManager *helpManager =
39 m_codeModelManager->helpManagerForUrl(request->m_parameters.textDocument.uri);
40
41 if (!helpManager) {
42 qCWarning(hoverLog)
43 << "No help manager is available, documentation hints will not function!";
44 return;
45 }
46 using namespace QQmlJS::Dom;
47 QLspSpecification::Hover result;
48 ResponseScopeGuard guard(result, request->m_response);
49 if (!request) {
50 qCWarning(hoverLog) << "No hover information is available!";
51 return;
52 }
53 const auto textDocument = request->m_parameters.textDocument;
54 const auto position = request->m_parameters.position;
55 const auto doc =
56 m_codeModelManager->openDocumentByUrl(QQmlLSUtils::lspUriToQmlUrl(textDocument.uri));
57 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
58 if (!file) {
59 guard.setError(QQmlLSUtils::ErrorMessage{
60 0, u"Could not find the file %1"_s.arg(doc.snapshot.doc.canonicalFilePath()) });
61 return;
62 }
63
64 const auto documentation = helpManager->documentationForItem(file, position);
65 if (!documentation.has_value()) {
66 qCDebug(hoverLog)
67 << QStringLiteral(
68 "No documentation hints found for the item at (line, col): (%1,%2)")
69 .arg(position.line)
70 .arg(position.character);
71 return;
72 }
73 QLspSpecification::MarkupContent content;
74 // TODO: We need to do post-formatting what we fetch from documentation.
75 content.kind = QLspSpecification::MarkupKind::Markdown;
76 content.value = documentation.value();
77 result.contents = std::move(content);
78}
79
80QT_END_NAMESPACE
Implements a server for the language server protocol.
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
Definition qqmlhover.cpp:24
void process(RequestPointerArgument req) override
Definition qqmlhover.cpp:36
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
Definition qqmlhover.cpp:29
~QQmlHover() override
QString name() const override
Definition qqmlhover.cpp:19
This class sends a result or an error when going out of scope.