From c2810f2c1655593a48791327e9563417caf2e261 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Tue, 31 Aug 2021 03:42:16 -0400 Subject: [PATCH] [clangd] Omit type hints that are too long Differential Revision: https://reviews.llvm.org/D108972 --- clang-tools-extra/clangd/InlayHints.cpp | 7 +++++-- clang-tools-extra/clangd/unittests/InlayHintTests.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp index 7c3c6a2421d8..bea5306daeb7 100644 --- a/clang-tools-extra/clangd/InlayHints.cpp +++ b/clang-tools-extra/clangd/InlayHints.cpp @@ -331,8 +331,9 @@ private: if (!T.getTypePtrOrNull()) return; - addInlayHint(R, InlayHintKind::TypeHint, - std::string(Prefix) + T.getAsString(TypeHintPolicy)); + std::string TypeName = T.getAsString(TypeHintPolicy); + if (TypeName.length() < TypeNameLimit) + addInlayHint(R, InlayHintKind::TypeHint, std::string(Prefix) + TypeName); } std::vector &Results; @@ -341,6 +342,8 @@ private: StringRef MainFileBuf; const HeuristicResolver *Resolver; PrintingPolicy TypeHintPolicy; + + static const size_t TypeNameLimit = 32; }; std::vector inlayHints(ParsedAST &AST) { diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp index 6796a8ce70ff..62930d1f42b1 100644 --- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp +++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp @@ -591,6 +591,17 @@ TEST(TypeHints, DependentType) { )cpp"); } +TEST(TypeHints, LongTypeName) { + assertTypeHints(R"cpp( + template + struct A {}; + struct MultipleWords {}; + A foo(); + // Omit type hint past a certain length (currently 32) + auto var = foo(); + )cpp"); +} + // FIXME: Low-hanging fruit where we could omit a type hint: // - auto x = TypeName(...); // - auto x = (TypeName) (...);