Skip to content

Commit 2ae1aa9

Browse files
zhangyi1357hokein
authored andcommitted
[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: [[ clangd/clangd#1357 | Make the type hint length limit configurable ]] Reviewed By: hokein Differential Revision: https://reviews.llvm.org/D147395
1 parent ea60ffc commit 2ae1aa9

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

Diff for: clang-tools-extra/clangd/Config.h

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ struct Config {
147147
bool Parameters = true;
148148
bool DeducedTypes = true;
149149
bool Designators = true;
150+
// Limit the length of type names in inlay hints. (0 means no limit)
151+
uint32_t TypeNameLimit = 32;
150152
} InlayHints;
151153
};
152154

Diff for: clang-tools-extra/clangd/ConfigCompile.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,11 @@ struct FragmentCompiler {
611611
Out.Apply.push_back([Value(**F.Designators)](const Params &, Config &C) {
612612
C.InlayHints.Designators = Value;
613613
});
614+
if (F.TypeNameLimit)
615+
Out.Apply.push_back(
616+
[Value(**F.TypeNameLimit)](const Params &, Config &C) {
617+
C.InlayHints.TypeNameLimit = Value;
618+
});
614619
}
615620

616621
constexpr static llvm::SourceMgr::DiagKind Error = llvm::SourceMgr::DK_Error;

Diff for: clang-tools-extra/clangd/ConfigFragment.h

+2
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ struct Fragment {
322322
std::optional<Located<bool>> DeducedTypes;
323323
/// Show designators in aggregate initialization.
324324
std::optional<Located<bool>> Designators;
325+
/// Limit the length of type name hints. (0 means no limit)
326+
std::optional<Located<uint32_t>> TypeNameLimit;
325327
};
326328
InlayHintsBlock InlayHints;
327329
};

Diff for: clang-tools-extra/clangd/ConfigYAML.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ class Parser {
254254
if (auto Value = boolValue(N, "Designators"))
255255
F.Designators = *Value;
256256
});
257+
Dict.handle("TypeNameLimit", [&](Node &N) {
258+
if (auto Value = uint32Value(N, "TypeNameLimit"))
259+
F.TypeNameLimit = *Value;
260+
});
257261
Dict.parse(N);
258262
}
259263

@@ -375,6 +379,17 @@ class Parser {
375379
return std::nullopt;
376380
}
377381

382+
std::optional<Located<uint32_t>> uint32Value(Node &N, llvm::StringRef Desc) {
383+
if (auto Scalar = scalarValue(N, Desc)) {
384+
unsigned long long Num;
385+
if (!llvm::getAsUnsignedInteger(**Scalar, 0, Num)) {
386+
return Located<uint32_t>(Num, Scalar->Range);
387+
}
388+
}
389+
warning(Desc + " invalid number", N);
390+
return std::nullopt;
391+
}
392+
378393
// Try to parse a list of single scalar values, or just a single value.
379394
std::optional<std::vector<Located<std::string>>> scalarValues(Node &N) {
380395
std::vector<Located<std::string>> Result;

Diff for: clang-tools-extra/clangd/InlayHints.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
688688
return;
689689

690690
std::string TypeName = T.getAsString(Policy);
691-
if (TypeName.length() < TypeNameLimit)
691+
if (Cfg.InlayHints.TypeNameLimit == 0 ||
692+
TypeName.length() < Cfg.InlayHints.TypeNameLimit)
692693
addInlayHint(R, HintSide::Right, InlayHintKind::Type, Prefix, TypeName,
693694
/*Suffix=*/"");
694695
}
@@ -714,8 +715,6 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
714715
// the policies are initialized for more details.)
715716
PrintingPolicy TypeHintPolicy;
716717
PrintingPolicy StructuredBindingPolicy;
717-
718-
static const size_t TypeNameLimit = 32;
719718
};
720719

721720
} // namespace

Diff for: clang-tools-extra/clangd/unittests/InlayHintTests.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,21 @@ TEST(TypeHints, LongTypeName) {
13241324
// Omit type hint past a certain length (currently 32)
13251325
auto var = foo();
13261326
)cpp");
1327+
1328+
Config Cfg;
1329+
Cfg.InlayHints.TypeNameLimit = 0;
1330+
WithContextValue WithCfg(Config::Key, std::move(Cfg));
1331+
1332+
assertTypeHints(
1333+
R"cpp(
1334+
template <typename, typename, typename>
1335+
struct A {};
1336+
struct MultipleWords {};
1337+
A<MultipleWords, MultipleWords, MultipleWords> foo();
1338+
// Should have type hint with TypeNameLimit = 0
1339+
auto $var[[var]] = foo();
1340+
)cpp",
1341+
ExpectedHint{": A<MultipleWords, MultipleWords, MultipleWords>", "var"});
13271342
}
13281343

13291344
TEST(TypeHints, DefaultTemplateArgs) {

0 commit comments

Comments
 (0)