Skip to content

Fix display of annotation for double width characters #46

New issue

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

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

Already on GitHub? # to your account

Merged
merged 4 commits into from
Sep 4, 2021

Conversation

magurotuna
Copy link
Contributor

Hi there.
We're using this crate in denoland/deno_lint to display pretty diagnostics of the linter. By chance, I found that this crate doesn't work well if the source includes double width characters like:

const a = "こんにちは";

Specifically, if we attempt to point to in this snippet by passing (15, 16) as range, annotate-snippets will give us:

  |
1 | const a = "こんにちは";
  |                ^
  |

where obviously ^ points to the wrong position.
These characters take up twice the size of ordinary characters like the alphabet, so displaying annotation for them requires special handling, which I implemented in this patch.

Comment on lines +638 to +644
let expected = r#" --> <current file>:1:1
|
1 | お寿司
| ^^^^^^ Sushi1
2 | 食べたい🍣
| ---- note: Sushi2
|"#;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With monospace fonts, this is displayed like:

image

Comment on lines +601 to +607
let expected = r#" --> <current file>:1:3
|
1 | おはよう
| _____^
2 | | ございます
| |______^ Good morning
|"#;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Comment on lines +573 to +577
let expected = r#" --> <current file>:1:7
|
1 | こんにちは、世界
| ^^^^ world
|"#;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Comment on lines +668 to +672
let expected = r#" --> <current file>:1:7
|
1 | こんにちは、新しいWorld!
| ^^^^^^^^^^^ New world
|"#;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@zbraniecki
Copy link
Contributor

Thank you so much for the patch! It reads very clear and not just fixes the bug, but also improves the code.

@zbraniecki zbraniecki merged commit fc99036 into rust-lang:master Sep 4, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants