xref: /llvm-project/clang-tools-extra/clangd/Format.h (revision 4d006520b8c0cc3a52913b4665bf741c737e5592)
1 //===--- Format.h - automatic code formatting ---------------*- C++-*------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Clangd uses clang-format for formatting operations.
10 // This file adapts it to support new scenarios like format-on-type.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FORMAT_H
14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FORMAT_H
15 
16 #include "clang/Format/Format.h"
17 #include "clang/Tooling/Core/Replacement.h"
18 #include "llvm/ADT/StringRef.h"
19 
20 namespace clang {
21 namespace clangd {
22 
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.
26 ///
27 /// Example breaking a line (^ is the cursor):
28 /// === before newline is typed ===
29 /// if(1){^}
30 /// === after newline is typed and editor indents ===
31 /// if(1){
32 ///   ^}
33 /// === after formatIncremental(InsertedText="\n") ===
34 /// if (1) {
35 ///   ^
36 /// }
37 ///
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);
44 
45 /// Determine the new cursor position after applying \p Replacements.
46 /// Analogue of tooling::Replacements::getShiftedCodePosition().
47 unsigned
48 transformCursorPosition(unsigned Offset,
49                         const std::vector<tooling::Replacement> &Replacements);
50 
51 } // namespace clangd
52 } // namespace clang
53 
54 #endif
55 
56