clang-tools  14.0.0git
Format.h
Go to the documentation of this file.
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
50  const std::vector<tooling::Replacement> &Replacements);
51 
52 } // namespace clangd
53 } // namespace clang
54 
55 #endif
56 
FormatStyle
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"( Style for formatting code around applied fixes: - 'none' (default) turns off formatting - 'file' (literally 'file', not a placeholder) uses .clang-format file in the closest parent directory - '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. This option overrides the 'FormatStyle` option in .clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory))
Protocol.h
Offset
size_t Offset
Definition: CodeComplete.cpp:1103
Code
std::string Code
Definition: FindTargetTests.cpp:67
clang::clangd::transformCursorPosition
unsigned transformCursorPosition(unsigned Offset, const std::vector< tooling::Replacement > &Replacements)
Determine the new cursor position after applying Replacements.
Definition: Format.cpp:354
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::formatIncremental
std::vector< tooling::Replacement > formatIncremental(llvm::StringRef OriginalCode, unsigned OriginalCursor, llvm::StringRef InsertedText, format::FormatStyle Style)
Applies limited formatting around new InsertedText.
Definition: Format.cpp:248