]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

add preview option support for blackd (#3217)
authorAlexandr Artemyev <mogost@gmail.com>
Sat, 13 Aug 2022 03:23:02 +0000 (09:23 +0600)
committerGitHub <noreply@github.com>
Sat, 13 Aug 2022 03:23:02 +0000 (20:23 -0700)
Fixes #3195

Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
AUTHORS.md
CHANGES.md
docs/usage_and_configuration/black_as_a_server.md
src/blackd/__init__.py
tests/test_blackd.py

index faa2b05840f0c8090cf5f26a2428a9528280b8b4..f30cd55a08bee18ae9d888c5633fb219aba3ace8 100644 (file)
@@ -20,6 +20,7 @@ Multiple contributions by:
 - [Adam Johnson](mailto:me@adamj.eu)
 - [Adam Williamson](mailto:adamw@happyassassin.net)
 - [Alexander Huynh](mailto:github@grande.coffee)
+- [Alexandr Artemyev](mailto:mogost@gmail.com)
 - [Alex Vandiver](mailto:github@chmrr.net)
 - [Allan Simon](mailto:allan.simon@supinfo.com)
 - Anders-Petter Ljungquist
index 1fc8c65d6d8c643482cfd8d90e11451aa7343133..79f4ce591872ff2802302a56f775efb4afdd6b3f 100644 (file)
@@ -29,6 +29,8 @@
 
 <!-- Changes to blackd -->
 
+- `blackd` now supports preview style via `X-Preview` header (#3217)
+
 ### Configuration
 
 <!-- Changes to how Black can be configured -->
index fc9d1cab7168e155a7369d6d1a2a813718533467..a2d4252109a8595590edc7a18f6fe1ba0cedc556 100644 (file)
@@ -54,8 +54,11 @@ The headers controlling how source code is formatted are:
   command line flag. If present and its value is not the empty string, no string
   normalization will be performed.
 - `X-Skip-Magic-Trailing-Comma`: corresponds to the `--skip-magic-trailing-comma`
-  command line flag. If present and its value is not the empty string, trailing commas
+  command line flag. If present and its value is not an empty string, trailing commas
   will not be used as a reason to split lines.
+- `X-Preview`: corresponds to the `--preview` command line flag. If present and its
+  value is not an empty string, experimental and potentially disruptive style changes
+  will be used.
 - `X-Fast-Or-Safe`: if set to `fast`, `blackd` will act as _Black_ does when passed the
   `--fast` command line flag.
 - `X-Python-Variant`: if set to `pyi`, `blackd` will act as _Black_ does when passed the
index a6de79fbeaa4c98300a78442077255d77ba882f0..e52a9917cf38a1da2a2cbce68a896f926245f1fe 100644 (file)
@@ -32,6 +32,7 @@ LINE_LENGTH_HEADER = "X-Line-Length"
 PYTHON_VARIANT_HEADER = "X-Python-Variant"
 SKIP_STRING_NORMALIZATION_HEADER = "X-Skip-String-Normalization"
 SKIP_MAGIC_TRAILING_COMMA = "X-Skip-Magic-Trailing-Comma"
+PREVIEW = "X-Preview"
 FAST_OR_SAFE_HEADER = "X-Fast-Or-Safe"
 DIFF_HEADER = "X-Diff"
 
@@ -41,6 +42,7 @@ BLACK_HEADERS = [
     PYTHON_VARIANT_HEADER,
     SKIP_STRING_NORMALIZATION_HEADER,
     SKIP_MAGIC_TRAILING_COMMA,
+    PREVIEW,
     FAST_OR_SAFE_HEADER,
     DIFF_HEADER,
 ]
@@ -109,6 +111,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
         skip_magic_trailing_comma = bool(
             request.headers.get(SKIP_MAGIC_TRAILING_COMMA, False)
         )
+        preview = bool(request.headers.get(PREVIEW, False))
         fast = False
         if request.headers.get(FAST_OR_SAFE_HEADER, "safe") == "fast":
             fast = True
@@ -118,6 +121,7 @@ async def handle(request: web.Request, executor: Executor) -> web.Response:
             line_length=line_length,
             string_normalization=not skip_string_normalization,
             magic_trailing_comma=not skip_magic_trailing_comma,
+            preview=preview,
         )
         req_bytes = await request.content.read()
         charset = request.charset if request.charset is not None else "utf8"
index 18b2c98ac1f93ccadc57254584b93d72190cabe7..1d12113a3f383edac32a38b5c4e9dc89e79f15e6 100644 (file)
@@ -167,6 +167,13 @@ class BlackDTestCase(AioHTTPTestCase):
         )
         self.assertEqual(response.status, 400)
 
+    @unittest_run_loop
+    async def test_blackd_preview(self) -> None:
+        response = await self.client.post(
+            "/", data=b'print("hello")\n', headers={blackd.PREVIEW: "true"}
+        )
+        self.assertEqual(response.status, 204)
+
     @unittest_run_loop
     async def test_blackd_response_black_version_header(self) -> None:
         response = await self.client.post("/")