Skip to content

Mismatch between clang-format and vscode.json-language-features #10

Description

@Mishio-Rusterholz

Issue

Pre-commitのclang-formatはJSONファイルにも適用されるが,VSCodeビルトインのvscode.json-language-featuresとフォーマットが一致しない.

例:以下はVSCodeのフォーマット結果だが,clang-formatをかけると変わる(2行になる)

{
  "postCreateCommand": "mkdir -p .hist .cache/vcpkg/archives .cache/vcpkg/registries && git submodule update --init --recursive && pixi install && pixi run setup && pixi run pre-commit_setup",
}

結果,両フォーマッタのいたちごっこが発生しうる.特にVSCode設定で"editor.formatOnSave": true,に設定している場合,JSONファイルの編集保存後にclang-formatと食い違う.

Possible solution

VSCodeのJSONフォーマッタは拡張機能ではなくビルトインであるため,変更できない.

→clang-format の対象からJSONを外し,ファイルタイプを限定する.

     hooks:
       - id: clang-format
+        # デフォルトの types_or には json も含まれるが、clang-format の JSON 整形は
+        # VSCode の JSON/JSONC フォーマッタ(vscode.json-language-features)と食い違う
+        # (長い文字列の折り返し、コメント付き配列要素の誤整形など)。json を除外する。
+        types_or: [c++, c, c#, cuda, java, javascript, objective-c, proto, textproto]
         args: [--style=file, -i]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions