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 "Protocol.h"
17 #include "clang/Format/Format.h"
18 #include "clang/Tooling/Core/Replacement.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace clang {
22 namespace clangd {
23 
24 /// Applies limited formatting around new \p InsertedText.
25 /// The \p Code already contains the updated text before \p Cursor, and may have
26 /// had additional / characters (such as indentation) inserted by the editor.
27 ///
28 /// Example breaking a line (^ is the cursor):
29 /// === before newline is typed ===
30 /// if(1){^}
31 /// === after newline is typed and editor indents ===
32 /// if(1){
33 ///   ^}
34 /// === after formatIncremental(InsertedText="\n") ===
35 /// if (1) {
36 ///   ^
37 /// }
38 ///
39 /// We return sorted vector<tooling::Replacement>, not tooling::Replacements!
40 /// We may insert text both before and after the cursor. tooling::Replacements
41 /// would merge these, and thus lose information about cursor position.
42 std::vector<tooling::Replacement>
43 formatIncremental(llvm::StringRef Code, unsigned Cursor,
44                   llvm::StringRef InsertedText, format::FormatStyle Style);
45 
46 /// Determine the new cursor position after applying \p Replacements.
47 /// Analogue of tooling::Replacements::getShiftedCodePosition().
48 unsigned
49 transformCursorPosition(unsigned Offset,
50                         const std::vector<tooling::Replacement> &Replacements);
51 
52 } // namespace clangd
53 } // namespace clang
54 
55 #endif
56 
57