ClangCodeModel: Special rendering for deprecated completion items

We add the attribute textually and show a warning icon.

Fixes: QTCREATORBUG-2325
Change-Id: Icc0305a703e26c84095167087b30fa3456f97614
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-06-17 09:48:54 +02:00
parent 79b8e5397d
commit 0fa349237d
6 changed files with 53 additions and 0 deletions

View File

@@ -49,6 +49,16 @@ Utils::optional<CompletionItem::InsertTextFormat> CompletionItem::insertTextForm
return Utils::nullopt;
}
Utils::optional<QList<CompletionItem::CompletionItemTag>> CompletionItem::tags() const
{
if (const auto value = optionalValue<QJsonArray>(tagsKey)) {
QList<CompletionItemTag> tags;
for (auto it = value->cbegin(); it != value->cend(); ++it)
tags << static_cast<CompletionItemTag>(it->toInt());
return tags;
}
return {};
}
CompletionItemResolveRequest::CompletionItemResolveRequest(const CompletionItem &params)
: Request(methodName, params)

View File

@@ -214,6 +214,27 @@ public:
void setData(const QJsonValue &data) { insert(dataKey, data); }
void clearData() { remove(dataKey); }
/**
* Completion item tags are extra annotations that tweak the rendering of a
* completion item.
* @since 3.15.0
*/
enum CompletionItemTag {
Deprecated = 1,
};
/**
* Tags for this completion item.
* @since 3.15.0
*/
Utils::optional<QList<CompletionItemTag>> tags() const;
/**
* Indicates if this item is deprecated.
* @deprecated Use `tags` instead if supported.
*/
Utils::optional<bool> deprecated() const { return optionalValue<bool>(deprecatedKey); }
bool isValid() const override { return contains(labelKey); }
};

View File

@@ -209,6 +209,7 @@ constexpr char symbolKindKey[] = "symbolKind";
constexpr char syncKindKey[] = "syncKind";
constexpr char synchronizationKey[] = "synchronization";
constexpr char tabSizeKey[] = "tabSize";
constexpr char tagsKey[] = "tags";
constexpr char targetKey[] = "target";
constexpr char textDocumentKey[] = "textDocument";
constexpr char textDocumentSyncKey[] = "textDocumentSync";

View File

@@ -741,6 +741,7 @@ public:
private:
QIcon icon() const override;
QString text() const override;
};
class ClangdClient::ClangdCompletionAssistProcessor : public LanguageClientCompletionAssistProcessor
@@ -2551,6 +2552,8 @@ ClangdCompletionItem::SpecialQtType ClangdCompletionItem::getQtType(const Comple
QIcon ClangdCompletionItem::icon() const
{
if (isDeprecated())
return Utils::Icons::WARNING.icon();
const SpecialQtType qtType = getQtType(item());
switch (qtType) {
case SpecialQtType::Signal:
@@ -2566,6 +2569,14 @@ QIcon ClangdCompletionItem::icon() const
return LanguageClientCompletionItem::icon();
}
QString ClangdCompletionItem::text() const
{
const QString clangdValue = LanguageClientCompletionItem::text();
if (isDeprecated())
return "[[deprecated]]" + clangdValue;
return clangdValue;
}
MessageId ClangdClient::Private::getAndHandleAst(const TextDocOrFile &doc,
const AstHandler &astHandler,
AstCallbackMode callbackMode, const Range &range)

View File

@@ -227,6 +227,15 @@ bool LanguageClientCompletionItem::isPerfectMatch(int pos, QTextDocument *doc) c
return textToInsert == textAt(QTextCursor(doc), pos - length, length);
}
bool LanguageClientCompletionItem::isDeprecated() const
{
if (const auto tags = m_item.tags(); tags && tags->contains(CompletionItem::Deprecated))
return true;
if (const auto deprecated = m_item.deprecated())
return *deprecated;
return false;
}
class LanguageClientCompletionModel : public GenericProposalModel
{
public:

View File

@@ -135,6 +135,7 @@ public:
bool operator <(const LanguageClientCompletionItem &other) const;
bool isPerfectMatch(int pos, QTextDocument *doc) const;
bool isDeprecated() const;
private:
LanguageServerProtocol::CompletionItem m_item;