Commit c5449795 authored by Christoph Cullmann's avatar Christoph Cullmann

improve hover support

support all three styles of hover response:

contents: MarkedString | MarkedString[] | MarkupContent;

=> this is needed to have functional hover for Rust rls

in addition: cut too long tool tips until we have some better way to show stuff
parent bf54ad69
......@@ -77,12 +77,28 @@ public:
QPointer<KTextEditor::View> v(view);
auto h = [this,v,position] (const LSPHover & info)
{
if (!v || info.contents.value.isEmpty()) {
if (!v || info.contents.isEmpty()) {
return;
}
auto localCoordinates = v->cursorToCoordinate(position);
QToolTip::showText(v->mapToGlobal(localCoordinates), info.contents.value);
// combine contents elements to one string
QString finalTooltip;
for (auto &element : info.contents) {
if (!finalTooltip.isEmpty()) {
finalTooltip.append(QLatin1Char('\n'));
}
finalTooltip.append(element.value);
}
// we need to cut this a bit if too long until we have
// something more sophisticated than a tool tip for it
if (finalTooltip.size() > 512) {
finalTooltip.resize(512);
finalTooltip.append(QStringLiteral("..."));
}
// show tool tip: think about a better way for "large" stuff
QToolTip::showText(v->mapToGlobal(v->cursorToCoordinate(position)), finalTooltip);
};
m_handle.cancel() = m_server->documentHover(view->document()->url(), position, this, h);
......
......@@ -61,6 +61,8 @@ LSPClientPlugin::LSPClientPlugin(QObject *parent, const QList<QVariant> &)
m_debugMode = (qgetenv("LSPCLIENT_DEBUG") == QByteArray("1"));
if (!m_debugMode) {
QLoggingCategory::setFilterRules(QStringLiteral("katelspclientplugin.debug=false\nkatelspclientplugin.info=false"));
} else {
QLoggingCategory::setFilterRules(QStringLiteral("katelspclientplugin.debug=true\nkatelspclientplugin.info=true"));
}
readConfig();
......
......@@ -150,7 +150,9 @@ struct LSPDocumentHighlight
struct LSPHover
{
LSPMarkupContent contents;
// vector for contents to support all three variants: MarkedString | MarkedString[] | MarkupContent
// vector variant is still in use e.g. by Rust rls
QVector<LSPMarkupContent> contents;
LSPRange range;
};
......
......@@ -964,29 +964,46 @@ parseDocumentHighlightList(const QJsonValue & result)
return ret;
}
static LSPHover
parseHover(const QJsonValue & result)
static LSPMarkupContent
parseHoverContentElement(const QJsonValue & contents)
{
LSPHover ret;
auto hover = result.toObject();
// normalize content which can be of many forms
ret.range = parseRange(hover.value(MEMBER_RANGE).toObject());
auto contents = hover.value(QStringLiteral("contents"));
LSPMarkupContent result;
if (contents.isString()) {
ret.contents.value = contents.toString();
result.value = contents.toString();
} else {
// should be object, pretend so
auto cont = contents.toObject();
auto text = cont.value(QStringLiteral("value")).toString();
if (text.isEmpty()) {
// nothing to lose, try markdown
ret.contents = parseMarkupContent(contents);
result = parseMarkupContent(contents);
} else {
ret.contents.value = text;
result.value = text;
}
}
if (result.value.length())
result.kind = LSPMarkupKind::PlainText;
return result;
}
static LSPHover
parseHover(const QJsonValue & result)
{
LSPHover ret;
auto hover = result.toObject();
// normalize content which can be of many forms
ret.range = parseRange(hover.value(MEMBER_RANGE).toObject());
auto contents = hover.value(QStringLiteral("contents"));
// support the deprecated MarkedString[] variant, used by e.g. Rust rls
if (contents.isArray()) {
for (const auto & c : contents.toArray()) {
ret.contents.push_back(parseHoverContentElement(c));
}
} else {
ret.contents.push_back(parseHoverContentElement(contents));
}
if (ret.contents.value.length())
ret.contents.kind = LSPMarkupKind::PlainText;
return ret;
}
......
......@@ -96,7 +96,9 @@ int main(int argc, char ** argv)
q.exec();
auto hover_h = [&q] (const LSPHover & hover) {
std::cout << "hover: " << hover.contents.value.toStdString() << std::endl;
for (auto &element : hover.contents) {
std::cout << "hover: " << element.value.toStdString() << std::endl;
}
q.quit();
};
lsp.documentHover(document, {position[0].toInt(), position[1].toInt()}, &app, hover_h);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment