[PATCH] D93528: [clang-format] Add basic support for formatting JSON

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 17 08:56:38 PDT 2021


MyDeveloperDay updated this revision to Diff 352744.
MyDeveloperDay added a comment.

I have reworked this patch to utilise the fact that clang-format can format a JSON object in javascript reasonably well.

This technique adds for JSON files only a replacement to the front of the JSON transforming:

  {
      "A": 1,
      "B": [ 2,3 ]
  }

into

  x = {
      "A": 1,
      "B": [ 2,3 ]
  }

This is then parsed by clang-format the a more traditional sense, allowing a similar technique as would be used when formatting the code, (but with some JSON specific rules, to make it follow primarily the style used by https://github.com/kapilratnani/JSON-Viewer (Notepad++ plugin, which at least I've used extensively)

After formatter, a subsequent replacement is merged to remove the x = from the front of the json, rendering it back to its original form.

  {
      "A": 1,
      "B": [ 2,3 ]
  }

I prefer to use this approach rather than our Support/JSON libraries which aren't really setup for formatting options, this give us greater flexibility later on if users want custom rules from brace wrapping.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D93528/new/

https://reviews.llvm.org/D93528

Files:
  clang/docs/ClangFormat.rst
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/tools/clang-format/ClangFormat.cpp
  clang/unittests/Format/CMakeLists.txt

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93528.352744.patch
Type: text/x-patch
Size: 7650 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210617/db230556/attachment-0001.bin>


More information about the cfe-commits mailing list