Git Product home page Git Product logo

Comments (6)

boppreh avatar boppreh commented on May 21, 2024 1

There's a workaround if your parser doesn't support comments the way you want: transform the comments into strings with a unique random prefix, run it through your pretty-printer, then transform any strings that have the prefix back into comments.

It's incredibly dirty (you'll need to escape quotes, for example), but matches well with programmers' intuition about comments and ASTs, and is quite easy to implement.

from black.

ambv avatar ambv commented on May 21, 2024

Comments don't have a place in the grammar. The AST I'm using, lib2to3, is placing them as whitespace prefixes on the next instruction. An explicit goal of Black is to be deterministic by never consulting existing formatting on the file.

Those two things combined together make it very hard for me to support one particular use case: "suffix" comments on blocks. Prefix comments are easy and this is what most code does: standalone comments describe the line behind them, not before them.

I'm not closing this as this might have a solution in the future, but this is not easy to fix.

from black.

njsmith avatar njsmith commented on May 21, 2024

If lib2to3 turns out to be too limited, I think you could also hack comment support into a regular parser by doing a second pass scanning for comments (they're included in tokenize output) and then matching that up with your AST using the line/offset numbers. I think prettier might use this way?

Comment handling is super hard so I sympathize. But I'm really skeptical about whether a real-world formatter can get away with handling comments properly without taking their original spacing into account. For another much simpler example: the way black mangles the vertical spacing around section break comments in this file is pretty annoying. I'm not happy that that file is so complicated that it needs those section breaks, but since I'm reimplementing the huge socket module interface I can't really change the file, and those comments help make it more manageable.

#22 is another tricky comment handling issue -- I hope it's just a minor bug, but I wouldn't be shocked if it turns out to be unfixable while sticking to lib2to3's grammar.

from black.

carljm avatar carljm commented on May 21, 2024

The approach suggested by @boppreh seems promising. In Python we could use triple-quoted strings for the temporary replacement, which would dramatically reduce the frequency of needing quoting.

from black.

boppreh avatar boppreh commented on May 21, 2024

Here's an example of the replace-twice approach, in this case to avoid processing comments and strings by replacing them identifiers: https://github.com/boppreh/altpython/blob/master/altpython.py

from black.

hugovk avatar hugovk commented on May 21, 2024

Here's an example of changing indents of commented code. Subsequently uncommenting for debugging would not work as originally intended.

@@ -22,7 +25,8 @@ class TestBmpReference(PillowTestCase):
                 im.load()
             except Exception:  # as msg:
                 pass
-                # print("Bad Image %s: %s" %(f,msg))
+
+    # print("Bad Image %s: %s" %(f,msg))

     def test_questionable(self):
         """ These shouldn't crash/dos, but it's not well defined that these
@@ -47,35 +51,38 @@ class TestBmpReference(PillowTestCase):
             except Exception:  # as msg:
                 if os.path.basename(f) in supported:
                     raise
-                # print("Bad Image %s: %s" %(f,msg))
+
+    # print("Bad Image %s: %s" %(f,msg))

     def test_good(self):

from black.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.