1fe68088dSSam McCall //===--- InsertionPoint.h - Where should we add new code? --------*- C++-*-===// 2fe68088dSSam McCall // 3fe68088dSSam McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe68088dSSam McCall // See https://llvm.org/LICENSE.txt for license information. 5fe68088dSSam McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe68088dSSam McCall // 7fe68088dSSam McCall //===----------------------------------------------------------------------===// 8fe68088dSSam McCall 9*c1871824SHaojian Wu #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_INSERTIONPOINT_H 10*c1871824SHaojian Wu #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_REFACTOR_INSERTIONPOINT_H 11*c1871824SHaojian Wu 12fe68088dSSam McCall #include "clang/AST/DeclCXX.h" 13fe68088dSSam McCall #include "clang/Basic/Specifiers.h" 14fe68088dSSam McCall #include "clang/Tooling/Core/Replacement.h" 15fe68088dSSam McCall 16fe68088dSSam McCall namespace clang { 17fe68088dSSam McCall namespace clangd { 18fe68088dSSam McCall 19fe68088dSSam McCall // An anchor describes where to insert code into a decl sequence. 20fe68088dSSam McCall // 21fe68088dSSam McCall // It allows inserting above or below a block of decls matching some criterion. 22fe68088dSSam McCall // For example, "insert after existing constructors". 23fe68088dSSam McCall struct Anchor { 24fe68088dSSam McCall // A predicate describing which decls are considered part of a block. 25fe68088dSSam McCall // Match need not handle TemplateDecls, which are unwrapped before matching. 26fe68088dSSam McCall std::function<bool(const Decl *)> Match; 27fe68088dSSam McCall // Whether the insertion point should be before or after the matching block. 28fe68088dSSam McCall enum Dir { Above, Below } Direction = Below; 29fe68088dSSam McCall }; 30fe68088dSSam McCall 31fe68088dSSam McCall // Returns the point to insert a declaration according to Anchors. 32fe68088dSSam McCall // Anchors are tried in order. For each, the first matching location is chosen. 33fe68088dSSam McCall SourceLocation insertionPoint(const DeclContext &Ctx, 34fe68088dSSam McCall llvm::ArrayRef<Anchor> Anchors); 35fe68088dSSam McCall 36fe68088dSSam McCall // Returns an edit inserting Code inside Ctx. 37fe68088dSSam McCall // Location is chosen according to Anchors, falling back to the end of Ctx. 38fe68088dSSam McCall // Fails if the chosen insertion point is in a different file than Ctx itself. 39fe68088dSSam McCall llvm::Expected<tooling::Replacement> insertDecl(llvm::StringRef Code, 40fe68088dSSam McCall const DeclContext &Ctx, 41fe68088dSSam McCall llvm::ArrayRef<Anchor> Anchors); 42fe68088dSSam McCall 43fe68088dSSam McCall // Variant for C++ classes that ensures the right access control. 44fe68088dSSam McCall SourceLocation insertionPoint(const CXXRecordDecl &InClass, 45fe68088dSSam McCall std::vector<Anchor> Anchors, 46fe68088dSSam McCall AccessSpecifier Protection); 47fe68088dSSam McCall 48fe68088dSSam McCall // Variant for C++ classes that ensures the right access control. 49fe68088dSSam McCall // May insert a new access specifier if needed. 50fe68088dSSam McCall llvm::Expected<tooling::Replacement> insertDecl(llvm::StringRef Code, 51fe68088dSSam McCall const CXXRecordDecl &InClass, 52fe68088dSSam McCall std::vector<Anchor> Anchors, 53fe68088dSSam McCall AccessSpecifier Protection); 54fe68088dSSam McCall 55fe68088dSSam McCall } // namespace clangd 56fe68088dSSam McCall } // namespace clang 57*c1871824SHaojian Wu 58*c1871824SHaojian Wu #endif 59