[clang-tools-extra] 2ae1aa9 - [Clangd] Make the type hint length limit configurable

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 20 06:31:12 PDT 2023


Author: zhangyi1357
Date: 2023-04-20T15:21:48+02:00
New Revision: 2ae1aa9da7882f9a0707c4cea8d76bced44dd7fb

URL: https://github.com/llvm/llvm-project/commit/2ae1aa9da7882f9a0707c4cea8d76bced44dd7fb
DIFF: https://github.com/llvm/llvm-project/commit/2ae1aa9da7882f9a0707c4cea8d76bced44dd7fb.diff

LOG: [Clangd] Make the type hint length limit configurable

This commit is about clangd's type name hint length limit. The past behavior was 32 characters fixed limit. It is now configurable.

Projects can now add the following config fragment to their .clangd:

```
InlayHints:
  TypeNameLimit: 34
```

Ref: [[ https://github.com/clangd/clangd/issues/1357  | Make the type hint length limit configurable ]]

Reviewed By: hokein

Differential Revision: https://reviews.llvm.org/D147395

Added: 
    

Modified: 
    clang-tools-extra/clangd/Config.h
    clang-tools-extra/clangd/ConfigCompile.cpp
    clang-tools-extra/clangd/ConfigFragment.h
    clang-tools-extra/clangd/ConfigYAML.cpp
    clang-tools-extra/clangd/InlayHints.cpp
    clang-tools-extra/clangd/unittests/InlayHintTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/Config.h b/clang-tools-extra/clangd/Config.h
index daadf0ee3d3c..15b4b7ef06fe 100644
--- a/clang-tools-extra/clangd/Config.h
+++ b/clang-tools-extra/clangd/Config.h
@@ -147,6 +147,8 @@ struct Config {
     bool Parameters = true;
     bool DeducedTypes = true;
     bool Designators = true;
+    // Limit the length of type names in inlay hints. (0 means no limit)
+    uint32_t TypeNameLimit = 32;
   } InlayHints;
 };
 

diff  --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp
index fb6c6e86c1ac..9bd067666f5f 100644
--- a/clang-tools-extra/clangd/ConfigCompile.cpp
+++ b/clang-tools-extra/clangd/ConfigCompile.cpp
@@ -611,6 +611,11 @@ struct FragmentCompiler {
       Out.Apply.push_back([Value(**F.Designators)](const Params &, Config &C) {
         C.InlayHints.Designators = Value;
       });
+    if (F.TypeNameLimit)
+      Out.Apply.push_back(
+          [Value(**F.TypeNameLimit)](const Params &, Config &C) {
+            C.InlayHints.TypeNameLimit = Value;
+          });
   }
 
   constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;

diff  --git a/clang-tools-extra/clangd/ConfigFragment.h b/clang-tools-extra/clangd/ConfigFragment.h
index a56e919cbaf7..cfce4429532b 100644
--- a/clang-tools-extra/clangd/ConfigFragment.h
+++ b/clang-tools-extra/clangd/ConfigFragment.h
@@ -322,6 +322,8 @@ struct Fragment {
     std::optional<Located<bool>> DeducedTypes;
     /// Show designators in aggregate initialization.
     std::optional<Located<bool>> Designators;
+    /// Limit the length of type name hints. (0 means no limit)
+    std::optional<Located<uint32_t>> TypeNameLimit;
   };
   InlayHintsBlock InlayHints;
 };

diff  --git a/clang-tools-extra/clangd/ConfigYAML.cpp b/clang-tools-extra/clangd/ConfigYAML.cpp
index 84559f5c44f8..d16860a1ccf4 100644
--- a/clang-tools-extra/clangd/ConfigYAML.cpp
+++ b/clang-tools-extra/clangd/ConfigYAML.cpp
@@ -254,6 +254,10 @@ class Parser {
       if (auto Value = boolValue(N, "Designators"))
         F.Designators = *Value;
     });
+    Dict.handle("TypeNameLimit", [&](Node &N) {
+      if (auto Value = uint32Value(N, "TypeNameLimit"))
+        F.TypeNameLimit = *Value;
+    });
     Dict.parse(N);
   }
 
@@ -375,6 +379,17 @@ class Parser {
     return std::nullopt;
   }
 
+  std::optional<Located<uint32_t>> uint32Value(Node &N, llvm::StringRef Desc) {
+    if (auto Scalar = scalarValue(N, Desc)) {
+      unsigned long long Num;
+      if (!llvm::getAsUnsignedInteger(**Scalar, 0, Num)) {
+        return Located<uint32_t>(Num, Scalar->Range);
+      }
+    }
+    warning(Desc + " invalid number", N);
+    return std::nullopt;
+  }
+
   // Try to parse a list of single scalar values, or just a single value.
   std::optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
     std::vector<Located<std::string>> Result;

diff  --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index aa85551b1ced..50d4cb374385 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -688,7 +688,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
       return;
 
     std::string TypeName = T.getAsString(Policy);
-    if (TypeName.length() < TypeNameLimit)
+    if (Cfg.InlayHints.TypeNameLimit == 0 ||
+        TypeName.length() < Cfg.InlayHints.TypeNameLimit)
       addInlayHint(R, HintSide::Right, InlayHintKind::Type, Prefix, TypeName,
                    /*Suffix=*/"");
   }
@@ -714,8 +715,6 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
   // the policies are initialized for more details.)
   PrintingPolicy TypeHintPolicy;
   PrintingPolicy StructuredBindingPolicy;
-
-  static const size_t TypeNameLimit = 32;
 };
 
 } // namespace

diff  --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index cda86f30c3da..f400148e4d98 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1324,6 +1324,21 @@ TEST(TypeHints, LongTypeName) {
     // Omit type hint past a certain length (currently 32)
     auto var = foo();
   )cpp");
+
+  Config Cfg;
+  Cfg.InlayHints.TypeNameLimit = 0;
+  WithContextValue WithCfg(Config::Key, std::move(Cfg));
+
+  assertTypeHints(
+      R"cpp(
+    template <typename, typename, typename>
+    struct A {};
+    struct MultipleWords {};
+    A<MultipleWords, MultipleWords, MultipleWords> foo();
+    // Should have type hint with TypeNameLimit = 0
+    auto $var[[var]] = foo();
+  )cpp",
+      ExpectedHint{": A<MultipleWords, MultipleWords, MultipleWords>", "var"});
 }
 
 TEST(TypeHints, DefaultTemplateArgs) {


        


More information about the cfe-commits mailing list