//===--- BreakableToken.h - Format C++ code ---------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// \file /// Declares BreakableToken, BreakableStringLiteral, BreakableComment, /// BreakableBlockComment and BreakableLineCommentSection classes, that contain /// token type-specific logic to break long lines in tokens and reflow content /// between tokens. /// //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H #define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H #include "Encoding.h" #include "WhitespaceManager.h" #include "llvm/ADT/StringSet.h" namespace clang { namespace format { /// Checks if \p Token switches formatting, like /* clang-format off */. /// \p Token must be a comment. bool switchesFormatting(const FormatToken &Token); struct FormatStyle; /// Base class for tokens / ranges of tokens that can allow breaking /// within the tokens - for example, to avoid whitespace beyond the column /// limit, or to reflow text. /// /// Generally, a breakable token consists of logical lines, addressed by a line /// index. For example, in a sequence of line comments, each line comment is its /// own logical line; similarly, for a block comment, each line in the block /// comment is on its own logical line. /// /// There are two methods to compute the layout of the token: /// - getRangeLength measures the number of columns needed for a range of text /// within a logical line, and /// - getContentStartColumn returns the start column at which we want the /// content of a logical line to start (potentially after introducing a line /// break). /// /// The mechanism to adapt the layout of the breakable token is organised /// around the concept of a \c Split, which is a whitespace range that signifies /// a position of the content of a token where a reformatting might be done. /// /// Operating with splits is divided into two operations: /// - getSplit, for finding a split starting at a position, /// - insertBreak, for executing the split using a whitespace manager. /// /// There is a pair of operations that are used to compress a long whitespace /// range with a single space if that will bring the line length under the /// column limit: /// - getLineLengthAfterCompression, for calculating the size in columns of the /// line after a whitespace range has been compressed, and /// - compressWhitespace, for executing the whitespace compression using a /// whitespace manager; note that the compressed whitespace may be in the /// middle of the original line and of the reformatted line. /// /// For tokens where the whitespace before each line needs to be also /// reformatted, for example for tokens supporting reflow, there are analogous /// operations that might be executed before the main line breaking occurs: /// - getReflowSplit, for finding a split such that the content preceding it /// needs to be specially reflown, /// - reflow, for executing the split using a whitespace manager, /// - introducesBreakBefore, for checking if reformatting the beginning /// of the content introduces a line break before it, /// - adaptStartOfLine, for executing the reflow using a whitespace /// manager. /// /// For tokens that require the whitespace after the last line to be /// reformatted, for example in multiline jsdoc comments that require the /// trailing '*/' to be on a line of itself, there are analogous operations /// that might be executed after the last line has been reformatted: /// - getSplitAfterLastLine, for finding a split after the last line that needs /// to be reflown, /// - replaceWhitespaceAfterLastLine, for executing the reflow using a /// whitespace manager. /// class BreakableToken { … }; class BreakableStringLiteral : public BreakableToken { … }; class BreakableStringLiteralUsingOperators : public BreakableStringLiteral { … }; class BreakableComment : public BreakableToken { … }; class BreakableBlockComment : public BreakableComment { … }; class BreakableLineCommentSection : public BreakableComment { … }; } // namespace format } // namespace clang #endif