Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatter calculates the width of a fullwidth character as 1 #11034

Open
yous opened this issue Jul 28, 2021 · 6 comments
Open

Formatter calculates the width of a fullwidth character as 1 #11034

yous opened this issue Jul 28, 2021 · 6 comments

Comments

@yous
Copy link
Contributor

yous commented Jul 28, 2021

Original file (Note that the third character is fullwidth):

require "spec"

describe ".ord" do
  it "runs for each character" do
    "ABA".chars.map(&.ord).should eq([65,
                                       66,
                                       65313])
  end
end

After running crystal tool format:

require "spec"

describe ".ord" do
  it "runs for each character" do
    "ABA".chars.map(&.ord).should eq([65,
                                      66,
                                      65313])
  end
end

This file passes crystal tool format:

require "spec"

describe ".ord" do
  it "runs for each character" do
    "ABC".chars.map(&.ord).should eq([65,
                                      66,
                                      67])
  end
end

You can replace with either or 😀 as they're also treated as halfwidth characters by formatter.

@yous yous added the kind:bug label Jul 28, 2021
@asterite
Copy link
Member

Sorry, I don't understand what's the issue.

Could you provide the description like this?

  • I have this code
  • When I format it I get this
  • But I was expecting it to format it like this

@yous
Copy link
Contributor Author

yous commented Jul 28, 2021

Okay, I have this code, and this is what I want after the formatting:

require "spec"

describe ".ord" do
  it "runs for each character" do
    "ABA".chars.map(&.ord).should eq([65,
                                       66,
                                       65313])
  end
end

But when I format it, I get this:

require "spec"

describe ".ord" do
  it "runs for each character" do
    "ABA".chars.map(&.ord).should eq([65,
                                      66,
                                      65313])
  end
end

Note that 66, line and 65313]) line are changed.

@asterite
Copy link
Member

What about the character ? It seems its width is bewteen 1 and 2:

require "spec"

describe ".ord" do
  it "runs for each character" do
    "AB你".chars.map(&.ord).should eq([65,
                                      66, # Not well aligned
                                      65313])
  end
end

Still not correctly aligned:

require "spec"

describe ".ord" do
  it "runs for each character" do
    "AB你".chars.map(&.ord).should eq([65,
                                       66,
                                       65313])
  end
end

There's no possible way it will look right.

The formatter considers each char to be width 1. If things don't look well like that, it's fine. They are small exceptions. And it's impossible to fix in the general case.

Just my opinion, though!

@asterite asterite reopened this Jul 28, 2021
@yous
Copy link
Contributor Author

yous commented Jul 28, 2021

What about terminal emulators? They should select exact 1 or 2 column to show a character.

This is from preferences in iTerm2:

Screenshot 2021-07-28 21 37 52

cat example.cr shows fullwidth characters with 2 columns:

Screenshot 2021-07-28 21 40 23

Of course there are ambiguous characters in Unicode, but I think we can usually use 1 for them.

@asterite
Copy link
Member

I see, that makes sense.

We can leave this open. We first need to have support in the standard library to compute the width of unicode characters.

@straight-shoota
Copy link
Member

Looks like a good use case for https://forum.crystal-lang.org/t/unicode-character-string-width/3286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Unicode
To do
Development

No branches or pull requests

3 participants