vscode-black-formatter: Black does not format correctly strings containing emojis

It seems that this extension does not handle correctly formatting strings containing emojis when formatting from vscode. Formatting the file with the black command via cli works correctly.

Examples:

before

s = '😊'
x = 'foo'

after

s = ""'
x = "foo"

or

s = '🀡 hi!'

after

s = "🀡 hi"'

Investigating a bit, it seems that the positions sent back from the language server to the client count the emojis as a single character while the visual studio code editor doesn’t, as for the first example the line edits relative to the line 0 are 0:4-0:5, new_text='"' and 0:6-0:7, new_text='"', but the second one seems to replace something other than the second '. Tampering with the server code and setting 0:7-0:8 to the second edit fixes the problem.

Tested on Windows 10.

About this issue

  • Original URL
  • State: closed
  • Created 7 months ago
  • Reactions: 4
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Verification steps:

  1. Install the latest pre-release
  2. Create something.py:
s = '😊'
  1. Trigger formatting with black using Format document with.
  2. It should update to:
s = "😊"