12#include "clang/Format/Format.h"
13#include "clang/Tooling/Core/Replacement.h"
14#include "llvm/Support/Error.h"
15#include "gmock/gmock.h"
16#include "gtest/gtest.h"
22std::string afterTyped(llvm::StringRef CodeWithCursor, llvm::StringRef Typed,
23 clang::format::FormatStyle Style) {
24 Annotations Code(CodeWithCursor);
25 unsigned Cursor = llvm::cantFail(
positionToOffset(Code.code(), Code.point()));
27 tooling::Replacements Merged;
29 if (llvm::Error
E = Merged.add(R))
30 ADD_FAILURE() << llvm::toString(std::move(
E));
31 auto NewCode = tooling::applyAllReplacements(Code.code(), Merged);
32 EXPECT_TRUE(
bool(NewCode))
33 <<
"Bad replacements: " << llvm::toString(NewCode.takeError());
39void expectAfterNewline(
const char *Before,
const char *After,
40 format::FormatStyle Style = format::getGoogleStyle(
41 format::FormatStyle::LK_Cpp)) {
42 EXPECT_EQ(After, afterTyped(Before,
"\n", Style)) << Before;
44void expectAfter(
const char *Typed,
const char *Before,
const char *After,
45 format::FormatStyle Style =
46 format::getGoogleStyle(format::FormatStyle::LK_Cpp)) {
47 EXPECT_EQ(After, afterTyped(Before, Typed, Style)) << Before;
50TEST(FormatIncremental, SplitComment) {
51 expectAfterNewline(R
"cpp(
60 expectAfterNewline(R"cpp(
61// trailing whitespace is not a split
65// trailing whitespace is not a split
69 expectAfterNewline(R"cpp(
80 expectAfterNewline(R"cpp(
89 expectAfterNewline(R"cpp(
98 expectAfterNewline(R
"cpp(
107 expectAfterNewline(R
"cpp(
117 expectAfterNewline(R"cpp(
128 expectAfterNewline(R
"cpp(
131 return; // All spelled tokens are accounted for.
132 // that takes two lines
139 return; // All spelled tokens are accounted for.
140 // that takes two lines
146 format::FormatStyle TabStyle =
147 format::getGoogleStyle(format::FormatStyle::LK_Cpp);
148 TabStyle.UseTab = format::FormatStyle::UT_Always;
149 TabStyle.TabWidth = 4;
150 TabStyle.IndentWidth = 4;
152 expectAfterNewline(
"void foo() {\n\t// this comment was\n^split\n}\n",
153 "void foo() {\n\t// this comment was\n\t// ^split\n}\n",
157TEST(FormatIncremental, Indentation) {
158 expectAfterNewline(R
"cpp(
169 expectAfterNewline(R"cpp(
180 expectAfterNewline(R"cpp(
190 expectAfterNewline(R"cpp(
202 expectAfterNewline(R
"cpp(
216 expectAfterNewline(R
"cpp(
230 expectAfterNewline(R
"cpp(
245TEST(FormatIncremental, FormatPreviousLine) {
246 expectAfterNewline(R"cpp(
259 expectAfterNewline(R"cpp(
261auto L = []{return;return;};
274TEST(FormatIncremental, Annoyances) {
276 expectAfterNewline(R
"cpp(
291 expectAfterNewline(R
"cpp(
306TEST(FormatIncremental, FormatBrace) {
307 expectAfter("}", R
"cpp(
314vector<int> x = {1, 2, 3}^
unsigned transformCursorPosition(unsigned Offset, const std::vector< tooling::Replacement > &Replacements)
Determine the new cursor position after applying Replacements.
std::vector< tooling::Replacement > formatIncremental(llvm::StringRef OriginalCode, unsigned OriginalCursor, llvm::StringRef InsertedText, format::FormatStyle Style)
Applies limited formatting around new InsertedText.
TEST(BackgroundQueueTest, Priority)
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//