black/tests
Hong Minhee (洪 民憙) ef6e079901
Let string splitters respect East_Asian_Width property (#3445)
This patch changes the preview style so that string splitters respect
Unicode East Asian Width[^1] property.  If you are not familiar to CJK
languages it is not clear immediately.  Let me elaborate with some
examples.

Traditionally, East Asian characters (including punctuation) have
taken up space twice than European letters and stops when they are
rendered in monospace typeset.  Compare the following characters:

```
abcdefg.
글、字。
```

The characters at the first line are half-width, and the second line
are full-width.  (Also note that the last character with a small
circle, the East Asian period, is also full-width.)  Therefore, if we
want to prevent those full-width characters to exceed the maximum
columns per line, we need to count their *width* rather than the number
of characters.  Again, the following characters:

```
글、字。
```

These are just 4 characters, but their total width is 8.

Suppose we want to maintain up to 4 columns per line with the following
text:

```
abcdefg.
글、字。
```

How should it be then?  We want it to look like:

```
abcd
efg.
글、
字。
```

However, Black currently turns it into like this:

```
abcd
efg.
글、字。
```

It's because Black currently counts the number of characters in the line
instead of measuring their width. So, how could we measure the width?
How can we tell if a character is full- or half-width? What if half-width
characters and full-width ones are mixed in a line? That's why Unicode
defined an attribute named `East_Asian_Width`. Unicode grouped every
single character according to their width in fixed-width typeset.

This partially addresses #1197, but only for string splitters. The other
parts need to be fixed as well in future patches.

This was implemented by copying rich's own approach to handling wide
characters: generate a table using wcwidth, check it into source
control, and use in to drive helper functions in Black's logic. This
gets us the best of both worlds: accuracy and performance (and let's us
update as per our stability policy too!).

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2023-03-19 19:09:57 -04:00
..
data Let string splitters respect East_Asian_Width property (#3445) 2023-03-19 19:09:57 -04:00
__init__.py Remove deprecated use of 'setup.py test' (#1275) 2020-05-08 06:23:50 -07:00
conftest.py Use optional tests for "no_python2" to simplify local testing (#2203) 2021-05-07 15:03:13 +02:00
empty.toml Add --target-version option to allow users to choose targeted Python versions (#618) 2019-02-06 18:43:50 -08:00
optional.py Use strict mypy checking (#3222) 2022-08-30 20:46:46 -07:00
test_black.py Infer target version based on project metadata (#3219) 2023-01-31 18:00:17 -08:00
test_blackd.py Add option to skip the first line of source code (#3299) 2022-10-06 15:17:32 -07:00
test_format.py Support files with type comment syntax errors (#3594) 2023-03-19 18:52:06 -04:00
test_ipynb.py Reformat codebase with isort 2022-07-27 17:19:28 -04:00
test_no_ipynb.py make black[jupyter] installation cross-shell (#3394) 2022-12-10 11:49:33 -08:00
test_trans.py Reformat codebase with isort 2022-07-27 17:19:28 -04:00
test.toml Add support for custom python cell magics (#2744) 2022-01-20 16:45:28 -08:00
util.py Check stability for both preview and non-preview styles (#3423) 2022-12-17 10:31:47 -08:00