| 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 | #include <QtQmlLS/private/qqmllshelputils_p.h> | 
| 6 |  | 
| 7 | QT_BEGIN_NAMESPACE | 
| 8 |  | 
| 9 | Q_LOGGING_CATEGORY(hoverLog, "qt.languageserver.hover" ) | 
| 10 |  | 
| 11 | QQmlHover::QQmlHover(QmlLsp::QQmlCodeModel *codeModel) | 
| 12 |     : QQmlBaseModule(codeModel), m_helpManager(std::make_unique<HelpManager>()) | 
| 13 | { | 
| 14 |     // if set thorugh the commandline | 
| 15 |     if (!codeModel->documentationRootPath().isEmpty()) | 
| 16 |         m_helpManager->setDocumentationRootPath(codeModel->documentationRootPath()); | 
| 17 |  | 
| 18 |     connect(sender: codeModel, signal: &QmlLsp::QQmlCodeModel::documentationRootPathChanged, context: this, slot: [this](const QString &path) { | 
| 19 |         m_helpManager->setDocumentationRootPath(path); | 
| 20 |     }); | 
| 21 | } | 
| 22 |  | 
| 23 | QQmlHover::~QQmlHover() = default; | 
| 24 |  | 
| 25 | QString QQmlHover::name() const | 
| 26 | { | 
| 27 |     return u"QQmlHover"_s ; | 
| 28 | } | 
| 29 |  | 
| 30 | void QQmlHover::registerHandlers(QLanguageServer *, QLanguageServerProtocol *protocol) | 
| 31 | { | 
| 32 |     protocol->registerHoverRequestHandler(handler: getRequestHandler()); | 
| 33 | } | 
| 34 |  | 
| 35 | void QQmlHover::setupCapabilities( | 
| 36 |         const QLspSpecification::InitializeParams &, | 
| 37 |         QLspSpecification::InitializeResult &serverCapabilities) | 
| 38 | { | 
| 39 |     serverCapabilities.capabilities.hoverProvider = true; | 
| 40 | } | 
| 41 |  | 
| 42 | void QQmlHover::process(RequestPointerArgument request) | 
| 43 | { | 
| 44 |     if (!m_helpManager) { | 
| 45 |         qCWarning(hoverLog) | 
| 46 |                 << "No help manager is available, documentation hints will not function!" ; | 
| 47 |         return; | 
| 48 |     } | 
| 49 |     using namespace QQmlJS::Dom; | 
| 50 |     QLspSpecification::Hover result; | 
| 51 |     ResponseScopeGuard guard(result, request->m_response); | 
| 52 |     if (!request) { | 
| 53 |         qCWarning(hoverLog) << "No hover information is available!" ; | 
| 54 |         return; | 
| 55 |     } | 
| 56 |     const auto textDocument = request->m_parameters.textDocument; | 
| 57 |     const auto position = request->m_parameters.position; | 
| 58 |     const auto doc = m_codeModel->openDocumentByUrl(url: QQmlLSUtils::lspUriToQmlUrl(uri: textDocument.uri)); | 
| 59 |     DomItem file = doc.snapshot.doc.fileObject(option: GoTo::MostLikely); | 
| 60 |     if (!file) { | 
| 61 |         guard.setError(QQmlLSUtils::ErrorMessage{ | 
| 62 |                 .code: 0, .message: u"Could not find the file %1"_s .arg(a: doc.snapshot.doc.canonicalFilePath()) }); | 
| 63 |         return; | 
| 64 |     } | 
| 65 |  | 
| 66 |     const auto documentation = m_helpManager->documentationForItem(file, position); | 
| 67 |     if (!documentation.has_value()) { | 
| 68 |         qCDebug(hoverLog) | 
| 69 |                 << QStringLiteral( | 
| 70 |                            "No documentation hints found for the item at (line, col): (%1,%2)" ) | 
| 71 |                            .arg(a: position.line) | 
| 72 |                            .arg(a: position.character); | 
| 73 |         return; | 
| 74 |     } | 
| 75 |     QLspSpecification::MarkupContent content; | 
| 76 |     // TODO: We need to do post-formatting what we fetch from documentation. | 
| 77 |     content.kind = QLspSpecification::MarkupKind::Markdown; | 
| 78 |     content.value = documentation.value(); | 
| 79 |     result.contents = std::move(content); | 
| 80 | } | 
| 81 |  | 
| 82 | QT_END_NAMESPACE | 
| 83 |  |