FormatInternal.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===--- FormatInternal.h - Format C++ code ---------------------*- 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. /// \file
  10. /// This file declares Format APIs to be used internally by the
  11. /// formatting library implementation.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_FORMAT_FORMATINTERNAL_H
  15. #define LLVM_CLANG_LIB_FORMAT_FORMATINTERNAL_H
  16. #include "BreakableToken.h"
  17. #include <utility>
  18. namespace clang {
  19. namespace format {
  20. namespace internal {
  21. /// Reformats the given \p Ranges in the code fragment \p Code.
  22. ///
  23. /// A fragment of code could conceptually be surrounded by other code that might
  24. /// constrain how that fragment is laid out.
  25. /// For example, consider the fragment of code between 'R"(' and ')"',
  26. /// exclusive, in the following code:
  27. ///
  28. /// void outer(int x) {
  29. /// string inner = R"(name: data
  30. /// ^ FirstStartColumn
  31. /// value: {
  32. /// x: 1
  33. /// ^ NextStartColumn
  34. /// }
  35. /// )";
  36. /// ^ LastStartColumn
  37. /// }
  38. ///
  39. /// The outer code can influence the inner fragment as follows:
  40. /// * \p FirstStartColumn specifies the column at which \p Code starts.
  41. /// * \p NextStartColumn specifies the additional indent dictated by the
  42. /// surrounding code. It is applied to the rest of the lines of \p Code.
  43. /// * \p LastStartColumn specifies the column at which the last line of
  44. /// \p Code should end, in case the last line is an empty line.
  45. ///
  46. /// In the case where the last line of the fragment contains content,
  47. /// the fragment ends at the end of that content and \p LastStartColumn is
  48. /// not taken into account, for example in:
  49. ///
  50. /// void block() {
  51. /// string inner = R"(name: value)";
  52. /// }
  53. ///
  54. /// Each range is extended on either end to its next bigger logic unit, i.e.
  55. /// everything that might influence its formatting or might be influenced by its
  56. /// formatting.
  57. ///
  58. /// Returns a pair P, where:
  59. /// * P.first are the ``Replacements`` necessary to make all \p Ranges comply
  60. /// with \p Style.
  61. /// * P.second is the penalty induced by formatting the fragment \p Code.
  62. /// If the formatting of the fragment doesn't have a notion of penalty,
  63. /// returns 0.
  64. ///
  65. /// If ``Status`` is non-null, its value will be populated with the status of
  66. /// this formatting attempt. See \c FormattingAttemptStatus.
  67. std::pair<tooling::Replacements, unsigned>
  68. reformat(const FormatStyle &Style, StringRef Code,
  69. ArrayRef<tooling::Range> Ranges, unsigned FirstStartColumn,
  70. unsigned NextStartColumn, unsigned LastStartColumn, StringRef FileName,
  71. FormattingAttemptStatus *Status);
  72. } // namespace internal
  73. } // namespace format
  74. } // namespace clang
  75. #endif