67

clang-formatツールが改行を削除しないようにする設定を探しています。

たとえば、私はColumnLimit120 に設定しており、サンプル コードを再フォーマットすると次のようになります。

前:

#include <vector>
#include <string>

std::vector<std::string> get_vec()
{
   return std::vector<std::string> {
      "this is a test",
      "some of the lines are longer",
      "than other, but I would like",
      "to keep them on separate lines"
   };
}

int main()
{
   auto vec = get_vec();
}

後:

#include <vector>
#include <string>

std::vector<std::string> get_vec()
{
   return std::vector<std::string>{"this is a test", "some of the lines are longer", "than other, but I would like",
         "to keep them on separate lines"};
}

int main()
{
   auto vec = get_vec();
}

私が望むのは、ツールが 120 文字を超える行を分割することですが、120 文字未満であるという理由だけで行を結合することを決定しないことです。

そのようなオプションはありますか?ドキュメントには何も目立ちませんでした。

4

5 に答える 5

18

clang-format で正確にやりたいことを実行できるかどうかはわかりませんが、clang-format にコードのセクションをそのままにしておくように指示することは可能です。私はこれを、まさにあなたが話しているようなシナリオ、つまり非常に特定の書式設定によって読みやすくするコードのブロックに使用します。

std::vector<std::string> get_vec()
{
   // clang-format off
   return std::vector<std::string> {
      "this is a test",
      "some of the lines are longer",
      "than other, but I would like",
      "to keep them on separate lines"
   };
   // clang-format on
}

参照: http://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code

于 2015-12-18T10:12:52.400 に答える