Skip to content

[clangd] Improve BlockEnd inlayhint presentation #136106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions clang-tools-extra/clangd/InlayHints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ std::string summarizeExpr(const Expr *E) {
return getSimpleName(*E->getFoundDecl()).str();
}
std::string VisitCallExpr(const CallExpr *E) {
return Visit(E->getCallee());
std::string Result = Visit(E->getCallee());
Result += E->getNumArgs() == 0 ? "()" : "(...)";
return Result;
}
std::string
VisitCXXDependentScopeMemberExpr(const CXXDependentScopeMemberExpr *E) {
Expand Down Expand Up @@ -147,6 +149,9 @@ std::string summarizeExpr(const Expr *E) {
}

// Literals are just printed
std::string VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
return "nullptr";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a small test case (or extend an existing one) that exercises this change? e.g. something like:

void foo(char *s) {
  if (s != nullptr) {
  }  // if s != nullptr
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added it to TEST(BlockEndHints, If)

}
std::string VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
return E->getValue() ? "true" : "false";
}
Expand All @@ -165,12 +170,14 @@ std::string summarizeExpr(const Expr *E) {
std::string Result = "\"";
if (E->containsNonAscii()) {
Result += "...";
} else if (E->getLength() > 10) {
Result += E->getString().take_front(7);
Result += "...";
} else {
llvm::raw_string_ostream OS(Result);
llvm::printEscapedString(E->getString(), OS);
if (E->getLength() > 10) {
llvm::printEscapedString(E->getString().take_front(7), OS);
Result += "...";
} else {
llvm::printEscapedString(E->getString(), OS);
}
}
Result.push_back('"');
return Result;
Expand Down Expand Up @@ -408,12 +415,14 @@ struct Callee {
class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
public:
InlayHintVisitor(std::vector<InlayHint> &Results, ParsedAST &AST,
const Config &Cfg, std::optional<Range> RestrictRange)
const Config &Cfg, std::optional<Range> RestrictRange,
InlayHintOptions HintOptions)
: Results(Results), AST(AST.getASTContext()), Tokens(AST.getTokens()),
Cfg(Cfg), RestrictRange(std::move(RestrictRange)),
MainFileID(AST.getSourceManager().getMainFileID()),
Resolver(AST.getHeuristicResolver()),
TypeHintPolicy(this->AST.getPrintingPolicy()) {
TypeHintPolicy(this->AST.getPrintingPolicy()),
HintOptions(HintOptions) {
bool Invalid = false;
llvm::StringRef Buf =
AST.getSourceManager().getBufferData(MainFileID, &Invalid);
Expand Down Expand Up @@ -1120,7 +1129,6 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
// Otherwise, the hint shouldn't be shown.
std::optional<Range> computeBlockEndHintRange(SourceRange BraceRange,
StringRef OptionalPunctuation) {
constexpr unsigned HintMinLineLimit = 2;

auto &SM = AST.getSourceManager();
auto [BlockBeginFileId, BlockBeginOffset] =
Expand Down Expand Up @@ -1148,7 +1156,7 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
auto RBraceLine = SM.getLineNumber(RBraceFileId, RBraceOffset);

// Don't show hint on trivial blocks like `class X {};`
if (BlockBeginLine + HintMinLineLimit - 1 > RBraceLine)
if (BlockBeginLine + HintOptions.HintMinLineLimit - 1 > RBraceLine)
return std::nullopt;

// This is what we attach the hint to, usually "}" or "};".
Expand Down Expand Up @@ -1178,17 +1186,20 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
StringRef MainFileBuf;
const HeuristicResolver *Resolver;
PrintingPolicy TypeHintPolicy;
InlayHintOptions HintOptions;
};

} // namespace

std::vector<InlayHint> inlayHints(ParsedAST &AST,
std::optional<Range> RestrictRange) {
std::optional<Range> RestrictRange,
InlayHintOptions HintOptions) {
std::vector<InlayHint> Results;
const auto &Cfg = Config::current();
if (!Cfg.InlayHints.Enabled)
return Results;
InlayHintVisitor Visitor(Results, AST, Cfg, std::move(RestrictRange));
InlayHintVisitor Visitor(Results, AST, Cfg, std::move(RestrictRange),
HintOptions);
Visitor.TraverseAST(AST.getASTContext());

// De-duplicate hints. Duplicates can sometimes occur due to e.g. explicit
Expand Down
9 changes: 8 additions & 1 deletion clang-tools-extra/clangd/InlayHints.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ namespace clang {
namespace clangd {
class ParsedAST;

struct InlayHintOptions {
// Minimum height of a code block in lines for a BlockEnd hint to be shown
// Includes the lines containing the braces
int HintMinLineLimit = 10;
};

/// Compute and return inlay hints for a file.
/// If RestrictRange is set, return only hints whose location is in that range.
std::vector<InlayHint> inlayHints(ParsedAST &AST,
std::optional<Range> RestrictRange);
std::optional<Range> RestrictRange,
InlayHintOptions HintOptions = {});

} // namespace clangd
} // namespace clang
Expand Down
Loading
Loading