1 //===--- Format.h - automatic code formatting ---------------*- C++-*------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Clangd uses clang-format for formatting operations.
10 // This file adapts it to support new scenarios like format-on-type.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FORMAT_H
14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FORMAT_H
16 #include "clang/Format/Format.h"
17 #include "clang/Tooling/Core/Replacement.h"
18 #include "llvm/ADT/StringRef.h"
23 /// Applies limited formatting around new \p InsertedText.
24 /// The \p Code already contains the updated text before \p Cursor, and may have
25 /// had additional / characters (such as indentation) inserted by the editor.
27 /// Example breaking a line (^ is the cursor):
28 /// === before newline is typed ===
30 /// === after newline is typed and editor indents ===
33 /// === after formatIncremental(InsertedText="\n") ===
38 /// We return sorted vector<tooling::Replacement>, not tooling::Replacements!
39 /// We may insert text both before and after the cursor. tooling::Replacements
40 /// would merge these, and thus lose information about cursor position.
41 std::vector
<tooling::Replacement
>
42 formatIncremental(llvm::StringRef Code
, unsigned Cursor
,
43 llvm::StringRef InsertedText
, format::FormatStyle Style
);
45 /// Determine the new cursor position after applying \p Replacements.
46 /// Analogue of tooling::Replacements::getShiftedCodePosition().
48 transformCursorPosition(unsigned Offset
,
49 const std::vector
<tooling::Replacement
> &Replacements
);