FormatInternal.h 3.0 KB

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