Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include <qqmlhover_p.h>
5
7
8Q_LOGGING_CATEGORY(hoverLog, "qt.languageserver.hover")
9
10QQmlHover::QQmlHover(QmlLsp::QQmlCodeModel *codeModel)
11 : QQmlBaseModule(codeModel)
12{
13}
14
16{
17 return u"QQmlHover"_s;
18}
19
20void QQmlHover::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol)
21{
22 protocol->registerHoverRequestHandler(getRequestHandler());
23}
24
26 const QLspSpecification::InitializeParams &,
27 QLspSpecification::InitializeResult &serverCapabilities)
28{
29 serverCapabilities.capabilities.hoverProvider = true;
30}
31
32void QQmlHover::process(RequestPointerArgument request)
33{
34 using namespace QQmlJS::Dom;
35 QLspSpecification::Hover result;
36 ResponseScopeGuard guard(result, request->m_response);
37
38 if (!request) {
39 qCWarning(hoverLog) << "No hover information is available!";
40 return;
41 }
42
43 const auto textDocument = request->m_parameters.textDocument;
44 const auto [hoveredLine, hoveredCharacter] = request->m_parameters.position;
45 qCDebug(hoverLog) << QStringLiteral("Hovered (line, col): (%1,%2)").arg(hoveredLine).arg(hoveredCharacter);
46
47 const auto doc = m_codeModel->openDocumentByUrl(
48 QQmlLSUtils::lspUriToQmlUrl(textDocument.uri));
49
50 DomItem file = doc.snapshot.doc.fileObject(GoTo::MostLikely);
51 if (!file) {
53 0, u"Could not find the file %1"_s.arg(doc.snapshot.doc.canonicalFilePath()) });
54 return;
55 }
56
57 // TODO: Fetch the actual documentation or other possible infos to be shown when hovered.
58 // Early return if hovered element is not identifier kind (for example, don't perform anything on paranthesis)
59
60 const auto documentation = QQmlLSUtils::getDocumentationFromLocation(
61 file, { hoveredLine + 1, hoveredCharacter + 1 });
62 if (documentation.isEmpty()) {
63 qCDebug(hoverLog)
65 "No documentation hints found for the item at (line, col): (%1,%2)")
66 .arg(hoveredLine)
67 .arg(hoveredCharacter);
68 return;
69 }
70
71 QLspSpecification::MarkupContent content;
72
73 // TODO: This should eventually be a Markdown kind.
74 // We will do post-formatting what we fetch from documentation.
75 content.kind = QLspSpecification::MarkupKind::PlainText;
76 content.value = documentation;
77
78 result.contents = content;
79}
80
Implements a server for the language server protocol.
void registerHandlers(QLanguageServer *server, QLanguageServerProtocol *protocol) override
Definition qqmlhover.cpp:20
void process(RequestPointerArgument req) override
Definition qqmlhover.cpp:32
void setupCapabilities(const QLspSpecification::InitializeParams &clientInfo, QLspSpecification::InitializeResult &) override
Definition qqmlhover.cpp:25
QString name() const override
Definition qqmlhover.cpp:15
static QByteArray lspUriToQmlUrl(const QByteArray &uri)
static QByteArray getDocumentationFromLocation(const DomItem &file, const QQmlLSUtilsTextPosition &position)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
OpenDocument openDocumentByUrl(const QByteArray &url)
Combined button and popup list for selecting options.
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
#define qCDebug(category,...)
GLuint64EXT * result
[6]
#define QStringLiteral(str)
QFile file
[0]
QNetworkRequest request(url)
QmlLsp::QQmlCodeModel * m_codeModel
This class sends a result or an error when going out of scope.
void setError(const QQmlLSUtilsErrorMessage &error)