2018-07-24 08:07:49 +08:00
|
|
|
//===--- BreakableToken.h - Format C++ code ---------------------*- C++ -*-===//
|
2013-04-15 22:28:00 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2013-04-15 22:28:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Declares BreakableToken, BreakableStringLiteral, BreakableComment,
|
2017-01-25 21:58:58 +08:00
|
|
|
/// BreakableBlockComment and BreakableLineCommentSection classes, that contain
|
|
|
|
/// token type-specific logic to break long lines in tokens and reflow content
|
|
|
|
/// between tokens.
|
2013-04-15 22:28:00 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#ifndef LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
|
|
|
|
#define LLVM_CLANG_LIB_FORMAT_BREAKABLETOKEN_H
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-06-05 22:09:10 +08:00
|
|
|
#include "Encoding.h"
|
2013-04-15 22:28:00 +08:00
|
|
|
#include "TokenAnnotator.h"
|
|
|
|
#include "WhitespaceManager.h"
|
2018-07-30 16:45:45 +08:00
|
|
|
#include "llvm/ADT/StringSet.h"
|
2017-02-02 23:32:19 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2013-04-15 22:28:00 +08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Checks if \p Token switches formatting, like /* clang-format off */.
|
2017-01-25 21:58:58 +08:00
|
|
|
/// \p Token must be a comment.
|
|
|
|
bool switchesFormatting(const FormatToken &Token);
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
struct FormatStyle;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Base class for tokens / ranges of tokens that can allow breaking
|
2017-11-29 22:29:43 +08:00
|
|
|
/// within the tokens - for example, to avoid whitespace beyond the column
|
|
|
|
/// limit, or to reflow text.
|
2013-05-27 23:23:34 +08:00
|
|
|
///
|
2017-11-29 22:29:43 +08:00
|
|
|
/// 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:
|
2017-01-25 21:58:58 +08:00
|
|
|
/// - 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
|
2017-11-29 22:29:43 +08:00
|
|
|
/// range with a single space if that will bring the line length under the
|
2017-01-25 21:58:58 +08:00
|
|
|
/// 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:
|
2017-11-29 22:29:43 +08:00
|
|
|
/// - getReflowSplit, for finding a split such that the content preceding it
|
2017-01-25 21:58:58 +08:00
|
|
|
/// needs to be specially reflown,
|
2017-11-29 22:29:43 +08:00
|
|
|
/// - reflow, for executing the split using a whitespace manager,
|
2017-10-16 17:08:53 +08:00
|
|
|
/// - introducesBreakBefore, for checking if reformatting the beginning
|
|
|
|
/// of the content introduces a line break before it,
|
2017-11-29 22:29:43 +08:00
|
|
|
/// - adaptStartOfLine, for executing the reflow using a whitespace
|
2017-01-25 21:58:58 +08:00
|
|
|
/// manager.
|
|
|
|
///
|
2017-07-21 06:29:39 +08:00
|
|
|
/// 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.
|
|
|
|
///
|
2013-04-15 22:28:00 +08:00
|
|
|
class BreakableToken {
|
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Contains starting character index and length of split.
|
2013-05-27 23:23:34 +08:00
|
|
|
typedef std::pair<StringRef::size_type, unsigned> Split;
|
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
virtual ~BreakableToken() {}
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns the number of lines in this token in the original code.
|
2013-04-15 22:28:00 +08:00
|
|
|
virtual unsigned getLineCount() const = 0;
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns the number of columns required to format the text in the
|
2017-11-29 22:29:43 +08:00
|
|
|
/// byte range [\p Offset, \p Offset \c + \p Length).
|
|
|
|
///
|
|
|
|
/// \p Offset is the byte offset from the start of the content of the line
|
|
|
|
/// at \p LineIndex.
|
|
|
|
///
|
|
|
|
/// \p StartColumn is the column at which the text starts in the formatted
|
|
|
|
/// file, needed to compute tab stops correctly.
|
|
|
|
virtual unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
|
|
|
|
StringRef::size_type Length,
|
|
|
|
unsigned StartColumn) const = 0;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns the number of columns required to format the text following
|
2017-11-29 22:29:43 +08:00
|
|
|
/// the byte \p Offset in the line \p LineIndex, including potentially
|
|
|
|
/// unbreakable sequences of tokens following after the end of the token.
|
|
|
|
///
|
|
|
|
/// \p Offset is the byte offset from the start of the content of the line
|
|
|
|
/// at \p LineIndex.
|
|
|
|
///
|
|
|
|
/// \p StartColumn is the column at which the text starts in the formatted
|
|
|
|
/// file, needed to compute tab stops correctly.
|
|
|
|
///
|
|
|
|
/// For breakable tokens that never use extra space at the end of a line, this
|
|
|
|
/// is equivalent to getRangeLength with a Length of StringRef::npos.
|
|
|
|
virtual unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
|
|
|
|
unsigned StartColumn) const {
|
|
|
|
return getRangeLength(LineIndex, Offset, StringRef::npos, StartColumn);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns the column at which content in line \p LineIndex starts,
|
2017-11-29 22:29:43 +08:00
|
|
|
/// assuming no reflow.
|
2013-05-27 23:23:34 +08:00
|
|
|
///
|
2017-11-29 22:29:43 +08:00
|
|
|
/// If \p Break is true, returns the column at which the line should start
|
|
|
|
/// after the line break.
|
|
|
|
/// If \p Break is false, returns the column at which the line itself will
|
|
|
|
/// start.
|
|
|
|
virtual unsigned getContentStartColumn(unsigned LineIndex,
|
|
|
|
bool Break) const = 0;
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2018-07-30 16:45:45 +08:00
|
|
|
/// Returns additional content indent required for the second line after the
|
|
|
|
/// content at line \p LineIndex is broken.
|
|
|
|
///
|
2018-07-30 20:22:41 +08:00
|
|
|
// (Next lines do not start with `///` since otherwise -Wdocumentation picks
|
|
|
|
// up the example annotations and generates warnings for them)
|
|
|
|
// For example, Javadoc @param annotations require and indent of 4 spaces and
|
|
|
|
// in this example getContentIndex(1) returns 4.
|
|
|
|
// /**
|
|
|
|
// * @param loooooooooooooong line
|
|
|
|
// * continuation
|
|
|
|
// */
|
2019-03-01 17:09:54 +08:00
|
|
|
virtual unsigned getContentIndent(unsigned LineIndex) const { return 0; }
|
2018-07-30 16:45:45 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a range (offset, length) at which to break the line at
|
2013-05-27 23:23:34 +08:00
|
|
|
/// \p LineIndex, if previously broken at \p TailOffset. If possible, do not
|
2017-11-29 22:29:43 +08:00
|
|
|
/// violate \p ColumnLimit, assuming the text starting at \p TailOffset in
|
|
|
|
/// the token is formatted starting at ContentStartColumn in the reformatted
|
|
|
|
/// file.
|
2013-04-15 22:28:00 +08:00
|
|
|
virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
|
2017-11-29 22:29:43 +08:00
|
|
|
unsigned ColumnLimit, unsigned ContentStartColumn,
|
2017-03-08 16:55:12 +08:00
|
|
|
llvm::Regex &CommentPragmasRegex) const = 0;
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Emits the previously retrieved \p Split via \p Whitespaces.
|
2013-04-15 22:28:00 +08:00
|
|
|
virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
|
2018-07-30 16:45:45 +08:00
|
|
|
unsigned ContentIndent,
|
2017-11-29 22:29:43 +08:00
|
|
|
WhitespaceManager &Whitespaces) const = 0;
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns the number of columns needed to format
|
2017-11-29 22:29:43 +08:00
|
|
|
/// \p RemainingTokenColumns, assuming that Split is within the range measured
|
|
|
|
/// by \p RemainingTokenColumns, and that the whitespace in Split is reduced
|
|
|
|
/// to a single space.
|
|
|
|
unsigned getLengthAfterCompression(unsigned RemainingTokenColumns,
|
|
|
|
Split Split) const;
|
2017-01-25 21:58:58 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Replaces the whitespace range described by \p Split with a single
|
2013-11-13 01:50:13 +08:00
|
|
|
/// space.
|
2017-01-25 21:58:58 +08:00
|
|
|
virtual void compressWhitespace(unsigned LineIndex, unsigned TailOffset,
|
|
|
|
Split Split,
|
2017-11-29 22:29:43 +08:00
|
|
|
WhitespaceManager &Whitespaces) const = 0;
|
2017-01-25 21:58:58 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns whether the token supports reflowing text.
|
2017-11-29 22:29:43 +08:00
|
|
|
virtual bool supportsReflow() const { return false; }
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a whitespace range (offset, length) of the content at \p
|
2017-11-29 22:29:43 +08:00
|
|
|
/// LineIndex such that the content of that line is reflown to the end of the
|
|
|
|
/// previous one.
|
2017-01-25 21:58:58 +08:00
|
|
|
///
|
2017-11-29 22:29:43 +08:00
|
|
|
/// Returning (StringRef::npos, 0) indicates reflowing is not possible.
|
2017-01-25 21:58:58 +08:00
|
|
|
///
|
2017-11-29 22:29:43 +08:00
|
|
|
/// The range will include any whitespace preceding the specified line's
|
|
|
|
/// content.
|
|
|
|
///
|
|
|
|
/// If the split is not contained within one token, for example when reflowing
|
|
|
|
/// line comments, returns (0, <length>).
|
|
|
|
virtual Split getReflowSplit(unsigned LineIndex,
|
2017-03-08 16:58:44 +08:00
|
|
|
llvm::Regex &CommentPragmasRegex) const {
|
2017-01-25 21:58:58 +08:00
|
|
|
return Split(StringRef::npos, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Reflows the current line into the end of the previous one.
|
2017-11-29 22:29:43 +08:00
|
|
|
virtual void reflow(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) const {}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns whether there will be a line break at the start of the
|
2017-11-17 19:17:15 +08:00
|
|
|
/// token.
|
2019-03-01 17:09:54 +08:00
|
|
|
virtual bool introducesBreakBeforeToken() const { return false; }
|
2017-10-16 17:08:53 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Replaces the whitespace between \p LineIndex-1 and \p LineIndex.
|
2017-11-29 22:29:43 +08:00
|
|
|
virtual void adaptStartOfLine(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) const {}
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Returns a whitespace range (offset, length) of the content at
|
2017-07-21 06:29:39 +08:00
|
|
|
/// the last line that needs to be reformatted after the last line has been
|
|
|
|
/// reformatted.
|
|
|
|
///
|
|
|
|
/// A result having offset == StringRef::npos means that no reformat is
|
|
|
|
/// necessary.
|
2017-11-29 22:29:43 +08:00
|
|
|
virtual Split getSplitAfterLastLine(unsigned TailOffset) const {
|
2017-07-21 06:29:39 +08:00
|
|
|
return Split(StringRef::npos, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Replaces the whitespace from \p SplitAfterLastLine on the last line
|
2017-07-21 06:29:39 +08:00
|
|
|
/// after the last line has been formatted by performing a reformatting.
|
2017-11-29 22:29:43 +08:00
|
|
|
void replaceWhitespaceAfterLastLine(unsigned TailOffset,
|
|
|
|
Split SplitAfterLastLine,
|
|
|
|
WhitespaceManager &Whitespaces) const {
|
2017-07-21 06:29:39 +08:00
|
|
|
insertBreak(getLineCount() - 1, TailOffset, SplitAfterLastLine,
|
2018-07-30 16:45:45 +08:00
|
|
|
/*ContentIndent=*/0, Whitespaces);
|
2017-07-21 06:29:39 +08:00
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Updates the next token of \p State to the next token after this
|
2017-01-25 21:58:58 +08:00
|
|
|
/// one. This can be used when this token manages a set of underlying tokens
|
|
|
|
/// as a unit and is responsible for the formatting of the them.
|
|
|
|
virtual void updateNextToken(LineState &State) const {}
|
|
|
|
|
2013-04-18 01:34:05 +08:00
|
|
|
protected:
|
2017-01-31 19:25:01 +08:00
|
|
|
BreakableToken(const FormatToken &Tok, bool InPPDirective,
|
|
|
|
encoding::Encoding Encoding, const FormatStyle &Style)
|
|
|
|
: Tok(Tok), InPPDirective(InPPDirective), Encoding(Encoding),
|
|
|
|
Style(Style) {}
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2013-04-18 01:34:05 +08:00
|
|
|
const FormatToken &Tok;
|
2013-06-14 19:46:10 +08:00
|
|
|
const bool InPPDirective;
|
|
|
|
const encoding::Encoding Encoding;
|
2013-09-05 22:08:34 +08:00
|
|
|
const FormatStyle &Style;
|
2013-04-15 22:28:00 +08:00
|
|
|
};
|
|
|
|
|
2017-11-29 22:29:43 +08:00
|
|
|
class BreakableStringLiteral : public BreakableToken {
|
2013-04-15 22:28:00 +08:00
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Creates a breakable token for a single line string literal.
|
2013-05-27 23:23:34 +08:00
|
|
|
///
|
|
|
|
/// \p StartColumn specifies the column in which the token will start
|
|
|
|
/// after formatting.
|
2017-01-31 19:25:01 +08:00
|
|
|
BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn,
|
|
|
|
StringRef Prefix, StringRef Postfix,
|
2018-01-23 19:26:19 +08:00
|
|
|
unsigned UnbreakableTailLength, bool InPPDirective,
|
|
|
|
encoding::Encoding Encoding, const FormatStyle &Style);
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2017-03-08 16:55:12 +08:00
|
|
|
Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
|
2018-04-23 18:02:59 +08:00
|
|
|
unsigned ContentStartColumn,
|
2017-03-08 16:55:12 +08:00
|
|
|
llvm::Regex &CommentPragmasRegex) const override;
|
2014-03-15 12:29:04 +08:00
|
|
|
void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
|
2018-07-30 16:45:45 +08:00
|
|
|
unsigned ContentIndent,
|
2017-11-29 22:29:43 +08:00
|
|
|
WhitespaceManager &Whitespaces) const override;
|
2017-01-25 21:58:58 +08:00
|
|
|
void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
|
2017-11-29 22:29:43 +08:00
|
|
|
WhitespaceManager &Whitespaces) const override {}
|
|
|
|
unsigned getLineCount() const override;
|
|
|
|
unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
|
|
|
|
StringRef::size_type Length,
|
|
|
|
unsigned StartColumn) const override;
|
|
|
|
unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
|
|
|
|
unsigned StartColumn) const override;
|
|
|
|
unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// The column in which the token starts.
|
|
|
|
unsigned StartColumn;
|
|
|
|
// The prefix a line needs after a break in the token.
|
|
|
|
StringRef Prefix;
|
|
|
|
// The postfix a line needs before introducing a break.
|
|
|
|
StringRef Postfix;
|
|
|
|
// The token text excluding the prefix and postfix.
|
|
|
|
StringRef Line;
|
|
|
|
// Length of the sequence of tokens after this string literal that cannot
|
|
|
|
// contain line breaks.
|
|
|
|
unsigned UnbreakableTailLength;
|
2013-04-15 22:28:00 +08:00
|
|
|
};
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
class BreakableComment : public BreakableToken {
|
|
|
|
protected:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Creates a breakable token for a comment.
|
2013-05-27 23:23:34 +08:00
|
|
|
///
|
2017-02-21 18:54:50 +08:00
|
|
|
/// \p StartColumn specifies the column in which the comment will start after
|
|
|
|
/// formatting.
|
2017-01-31 19:25:01 +08:00
|
|
|
BreakableComment(const FormatToken &Token, unsigned StartColumn,
|
|
|
|
bool InPPDirective, encoding::Encoding Encoding,
|
|
|
|
const FormatStyle &Style);
|
2013-04-18 01:34:05 +08:00
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
public:
|
2017-11-29 22:29:43 +08:00
|
|
|
bool supportsReflow() const override { return true; }
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned getLineCount() const override;
|
2017-03-08 16:55:12 +08:00
|
|
|
Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
|
2018-04-23 18:02:59 +08:00
|
|
|
unsigned ContentStartColumn,
|
2017-03-08 16:55:12 +08:00
|
|
|
llvm::Regex &CommentPragmasRegex) const override;
|
2017-01-25 21:58:58 +08:00
|
|
|
void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
|
2017-11-29 22:29:43 +08:00
|
|
|
WhitespaceManager &Whitespaces) const override;
|
2013-06-12 00:01:49 +08:00
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
protected:
|
|
|
|
// Returns the token containing the line at LineIndex.
|
|
|
|
const FormatToken &tokenAt(unsigned LineIndex) const;
|
|
|
|
|
|
|
|
// Checks if the content of line LineIndex may be reflown with the previous
|
|
|
|
// line.
|
2017-02-02 23:32:19 +08:00
|
|
|
virtual bool mayReflow(unsigned LineIndex,
|
|
|
|
llvm::Regex &CommentPragmasRegex) const = 0;
|
2017-01-25 21:58:58 +08:00
|
|
|
|
|
|
|
// Contains the original text of the lines of the block comment.
|
|
|
|
//
|
|
|
|
// In case of a block comments, excludes the leading /* in the first line and
|
|
|
|
// trailing */ in the last line. In case of line comments, excludes the
|
|
|
|
// leading // and spaces.
|
|
|
|
SmallVector<StringRef, 16> Lines;
|
|
|
|
|
|
|
|
// Contains the text of the lines excluding all leading and trailing
|
|
|
|
// whitespace between the lines. Note that the decoration (if present) is also
|
|
|
|
// not considered part of the text.
|
|
|
|
SmallVector<StringRef, 16> Content;
|
|
|
|
|
|
|
|
// Tokens[i] contains a reference to the token containing Lines[i] if the
|
|
|
|
// whitespace range before that token is managed by this block.
|
|
|
|
// Otherwise, Tokens[i] is a null pointer.
|
|
|
|
SmallVector<FormatToken *, 16> Tokens;
|
|
|
|
|
|
|
|
// ContentColumn[i] is the target column at which Content[i] should be.
|
|
|
|
// Note that this excludes a leading "* " or "*" in case of block comments
|
|
|
|
// where all lines have a "*" prefix, or the leading "// " or "//" in case of
|
|
|
|
// line comments.
|
|
|
|
//
|
|
|
|
// In block comments, the first line's target column is always positive. The
|
|
|
|
// remaining lines' target columns are relative to the first line to allow
|
|
|
|
// correct indentation of comments in \c WhitespaceManager. Thus they can be
|
|
|
|
// negative as well (in case the first line needs to be unindented more than
|
|
|
|
// there's actual whitespace in another line).
|
|
|
|
SmallVector<int, 16> ContentColumn;
|
|
|
|
|
|
|
|
// The intended start column of the first line of text from this section.
|
|
|
|
unsigned StartColumn;
|
|
|
|
|
|
|
|
// The prefix to use in front a line that has been reflown up.
|
|
|
|
// For example, when reflowing the second line after the first here:
|
|
|
|
// // comment 1
|
|
|
|
// // comment 2
|
|
|
|
// we expect:
|
|
|
|
// // comment 1 comment 2
|
|
|
|
// and not:
|
|
|
|
// // comment 1comment 2
|
|
|
|
StringRef ReflowPrefix = " ";
|
2013-05-27 23:23:34 +08:00
|
|
|
};
|
2013-04-18 01:34:05 +08:00
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
class BreakableBlockComment : public BreakableComment {
|
2013-05-27 23:23:34 +08:00
|
|
|
public:
|
2017-01-31 19:25:01 +08:00
|
|
|
BreakableBlockComment(const FormatToken &Token, unsigned StartColumn,
|
|
|
|
unsigned OriginalStartColumn, bool FirstInLine,
|
|
|
|
bool InPPDirective, encoding::Encoding Encoding,
|
2019-04-24 04:29:46 +08:00
|
|
|
const FormatStyle &Style, bool UseCRLF);
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2019-05-04 07:15:40 +08:00
|
|
|
Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
|
|
|
|
unsigned ContentStartColumn,
|
|
|
|
llvm::Regex &CommentPragmasRegex) const override;
|
2017-11-29 22:29:43 +08:00
|
|
|
unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
|
|
|
|
StringRef::size_type Length,
|
|
|
|
unsigned StartColumn) const override;
|
|
|
|
unsigned getRemainingLength(unsigned LineIndex, unsigned Offset,
|
|
|
|
unsigned StartColumn) const override;
|
|
|
|
unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
|
2018-07-30 16:45:45 +08:00
|
|
|
unsigned getContentIndent(unsigned LineIndex) const override;
|
2014-03-15 12:29:04 +08:00
|
|
|
void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
|
2018-07-30 16:45:45 +08:00
|
|
|
unsigned ContentIndent,
|
2017-11-29 22:29:43 +08:00
|
|
|
WhitespaceManager &Whitespaces) const override;
|
|
|
|
Split getReflowSplit(unsigned LineIndex,
|
2017-02-02 23:32:19 +08:00
|
|
|
llvm::Regex &CommentPragmasRegex) const override;
|
2017-11-29 22:29:43 +08:00
|
|
|
void reflow(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) const override;
|
2017-11-17 19:17:15 +08:00
|
|
|
bool introducesBreakBeforeToken() const override;
|
2017-11-29 22:29:43 +08:00
|
|
|
void adaptStartOfLine(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) const override;
|
|
|
|
Split getSplitAfterLastLine(unsigned TailOffset) const override;
|
2017-07-21 06:29:39 +08:00
|
|
|
|
2017-02-02 23:32:19 +08:00
|
|
|
bool mayReflow(unsigned LineIndex,
|
|
|
|
llvm::Regex &CommentPragmasRegex) const override;
|
2013-04-18 01:34:05 +08:00
|
|
|
|
2018-07-30 16:45:45 +08:00
|
|
|
// Contains Javadoc annotations that require additional indent when continued
|
|
|
|
// on multiple lines.
|
|
|
|
static const llvm::StringSet<> ContentIndentingJavadocAnnotations;
|
|
|
|
|
2013-04-18 01:34:05 +08:00
|
|
|
private:
|
2017-01-25 21:58:58 +08:00
|
|
|
// Rearranges the whitespace between Lines[LineIndex-1] and Lines[LineIndex].
|
2013-05-27 23:23:34 +08:00
|
|
|
//
|
2017-01-25 21:58:58 +08:00
|
|
|
// Updates Content[LineIndex-1] and Content[LineIndex] by stripping off
|
|
|
|
// leading and trailing whitespace.
|
|
|
|
//
|
|
|
|
// Sets ContentColumn to the intended column in which the text at
|
2013-05-27 23:23:34 +08:00
|
|
|
// Lines[LineIndex] starts (note that the decoration, if present, is not
|
|
|
|
// considered part of the text).
|
2013-09-05 22:08:34 +08:00
|
|
|
void adjustWhitespace(unsigned LineIndex, int IndentDelta);
|
2013-05-27 23:23:34 +08:00
|
|
|
|
|
|
|
// The column at which the text of a broken line should start.
|
|
|
|
// Note that an optional decoration would go before that column.
|
|
|
|
// IndentAtLineBreak is a uniform position for all lines in a block comment,
|
|
|
|
// regardless of their relative position.
|
|
|
|
// FIXME: Revisit the decision to do this; the main reason was to support
|
|
|
|
// patterns like
|
|
|
|
// /**************//**
|
|
|
|
// * Comment
|
|
|
|
// We could also support such patterns by special casing the first line
|
|
|
|
// instead.
|
|
|
|
unsigned IndentAtLineBreak;
|
2013-04-18 01:34:05 +08:00
|
|
|
|
2013-07-08 22:12:07 +08:00
|
|
|
// This is to distinguish between the case when the last line was empty and
|
|
|
|
// the case when it started with a decoration ("*" or "* ").
|
|
|
|
bool LastLineNeedsDecoration;
|
|
|
|
|
2013-05-27 23:23:34 +08:00
|
|
|
// Either "* " if all lines begin with a "*", or empty.
|
|
|
|
StringRef Decoration;
|
2017-02-16 20:39:31 +08:00
|
|
|
|
|
|
|
// If this block comment has decorations, this is the column of the start of
|
|
|
|
// the decorations.
|
|
|
|
unsigned DecorationColumn;
|
2017-07-21 06:29:39 +08:00
|
|
|
|
|
|
|
// If true, make sure that the opening '/**' and the closing '*/' ends on a
|
|
|
|
// line of itself. Styles like jsdoc require this for multiline comments.
|
|
|
|
bool DelimitersOnNewline;
|
2017-12-04 16:53:16 +08:00
|
|
|
|
|
|
|
// Length of the sequence of tokens after this string literal that cannot
|
|
|
|
// contain line breaks.
|
|
|
|
unsigned UnbreakableTailLength;
|
2013-04-18 01:34:05 +08:00
|
|
|
};
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
class BreakableLineCommentSection : public BreakableComment {
|
|
|
|
public:
|
2017-01-31 19:25:01 +08:00
|
|
|
BreakableLineCommentSection(const FormatToken &Token, unsigned StartColumn,
|
2017-01-25 21:58:58 +08:00
|
|
|
unsigned OriginalStartColumn, bool FirstInLine,
|
|
|
|
bool InPPDirective, encoding::Encoding Encoding,
|
|
|
|
const FormatStyle &Style);
|
|
|
|
|
2017-11-29 22:29:43 +08:00
|
|
|
unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
|
|
|
|
StringRef::size_type Length,
|
|
|
|
unsigned StartColumn) const override;
|
|
|
|
unsigned getContentStartColumn(unsigned LineIndex, bool Break) const override;
|
2017-01-25 21:58:58 +08:00
|
|
|
void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
|
2018-07-30 16:45:45 +08:00
|
|
|
unsigned ContentIndent,
|
2017-11-29 22:29:43 +08:00
|
|
|
WhitespaceManager &Whitespaces) const override;
|
|
|
|
Split getReflowSplit(unsigned LineIndex,
|
2017-02-02 23:32:19 +08:00
|
|
|
llvm::Regex &CommentPragmasRegex) const override;
|
2017-11-29 22:29:43 +08:00
|
|
|
void reflow(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) const override;
|
|
|
|
void adaptStartOfLine(unsigned LineIndex,
|
|
|
|
WhitespaceManager &Whitespaces) const override;
|
2017-03-08 16:58:44 +08:00
|
|
|
void updateNextToken(LineState &State) const override;
|
2017-02-02 23:32:19 +08:00
|
|
|
bool mayReflow(unsigned LineIndex,
|
|
|
|
llvm::Regex &CommentPragmasRegex) const override;
|
2017-01-25 21:58:58 +08:00
|
|
|
|
|
|
|
private:
|
2017-02-08 22:45:19 +08:00
|
|
|
// OriginalPrefix[i] contains the original prefix of line i, including
|
|
|
|
// trailing whitespace before the start of the content. The indentation
|
|
|
|
// preceding the prefix is not included.
|
|
|
|
// For example, if the line is:
|
|
|
|
// // content
|
|
|
|
// then the original prefix is "// ".
|
|
|
|
SmallVector<StringRef, 16> OriginalPrefix;
|
|
|
|
|
2017-01-25 21:58:58 +08:00
|
|
|
// Prefix[i] contains the intended leading "//" with trailing spaces to
|
|
|
|
// account for the indentation of content within the comment at line i after
|
|
|
|
// formatting. It can be different than the original prefix when the original
|
|
|
|
// line starts like this:
|
|
|
|
// //content
|
|
|
|
// Then the original prefix is "//", but the prefix is "// ".
|
|
|
|
SmallVector<StringRef, 16> Prefix;
|
|
|
|
|
|
|
|
SmallVector<unsigned, 16> OriginalContentColumn;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The token to which the last line of this breakable token belongs
|
2017-01-25 21:58:58 +08:00
|
|
|
/// to; nullptr if that token is the initial token.
|
|
|
|
///
|
|
|
|
/// The distinction is because if the token of the last line of this breakable
|
|
|
|
/// token is distinct from the initial token, this breakable token owns the
|
|
|
|
/// whitespace before the token of the last line, and the whitespace manager
|
|
|
|
/// must be able to modify it.
|
|
|
|
FormatToken *LastLineTok = nullptr;
|
|
|
|
};
|
2013-04-15 22:28:00 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|
|
|
|
|
2014-08-14 00:25:19 +08:00
|
|
|
#endif
|