-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Markdown: fix table formatting alignment for CJK and full-width characters #3478
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
Open
sxin0
wants to merge
2
commits into
JetBrains:master
Choose a base branch
from
sxin0:fix/markdown-cjk-table-alignment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...markdown/core/src/org/intellij/plugins/markdown/editor/MarkdownCharacterGridCustomizer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
| package org.intellij.plugins.markdown.editor | ||
|
|
||
| import com.intellij.openapi.editor.impl.EditorImpl | ||
| import com.intellij.openapi.editor.impl.view.DoubleWidthCharacterStrategy | ||
| import com.intellij.openapi.fileEditor.TextEditor | ||
| import com.intellij.openapi.fileEditor.impl.text.TextEditorCustomizer | ||
| import org.intellij.plugins.markdown.editor.tables.TableCharacterWidthUtils | ||
| import org.intellij.plugins.markdown.lang.hasMarkdownType | ||
|
|
||
| internal class MarkdownCharacterGridCustomizer : TextEditorCustomizer { | ||
| override suspend fun execute(textEditor: TextEditor) { | ||
| if (!textEditor.file.hasMarkdownType()) return | ||
|
|
||
| val editor = textEditor.editor as? EditorImpl ?: return | ||
| editor.settings.characterGridWidthMultiplier = 1.0f | ||
|
|
||
| val grid = editor.characterGrid ?: return | ||
| grid.doubleWidthCharacterStrategy = DoubleWidthCharacterStrategy { codePoint -> | ||
| TableCharacterWidthUtils.isFullWidthCharacter(codePoint) | ||
| } | ||
| } | ||
| } |
112 changes: 112 additions & 0 deletions
112
...markdown/core/src/org/intellij/plugins/markdown/editor/tables/TableCharacterWidthUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. | ||
| package org.intellij.plugins.markdown.editor.tables | ||
|
|
||
| /** | ||
| * Utility class for calculating character display width in Markdown tables. | ||
| * Handles proper width calculation for CJK and other full-width characters. | ||
| */ | ||
| internal object TableCharacterWidthUtils { | ||
|
|
||
| /** | ||
| * Calculates the display width of a string, considering different character widths. | ||
| * Full-width characters (CJK, etc.) are counted as 2 units, | ||
| * while half-width characters (ASCII, etc.) are counted as 1 unit. | ||
| * | ||
| * @param text The text to measure | ||
| * @return The display width in character units | ||
| */ | ||
| fun calculateDisplayWidth(text: String): Int { | ||
| if (text.isEmpty()) return 0 | ||
|
|
||
| var width = 0 | ||
| var i = 0 | ||
| while (i < text.length) { | ||
| val codePoint = text.codePointAt(i) | ||
| width += getCharacterWidth(codePoint) | ||
| i += Character.charCount(codePoint) | ||
| } | ||
| return width | ||
| } | ||
|
|
||
| private fun getCharacterWidth(codePoint: Int): Int { | ||
| return when { | ||
| // ASCII printable characters (half-width) | ||
| codePoint in 0x20..0x7E -> 1 | ||
|
|
||
| // Control characters | ||
| codePoint < 0x20 -> 0 | ||
|
|
||
| // Full-width characters (CJK, emoji, etc.) | ||
| isFullWidthCharacter(codePoint) -> 2 | ||
|
|
||
| // Default to 1 for other characters (Latin Extended, Cyrillic, etc.) | ||
| else -> 1 | ||
| } | ||
| } | ||
|
|
||
| internal fun isFullWidthCharacter(codePoint: Int): Boolean { | ||
| return when { | ||
| // CJK Unified Ideographs | ||
| codePoint in 0x4E00..0x9FFF -> true | ||
|
|
||
| // CJK Extension A | ||
| codePoint in 0x3400..0x4DBF -> true | ||
|
|
||
| // CJK Extension B | ||
| codePoint in 0x20000..0x2A6DF -> true | ||
|
|
||
| // CJK Extension C | ||
| codePoint in 0x2A700..0x2B73F -> true | ||
|
|
||
| // CJK Extension D | ||
| codePoint in 0x2B740..0x2B81F -> true | ||
|
|
||
| // CJK Extension E | ||
| codePoint in 0x2B820..0x2CEAF -> true | ||
|
|
||
| // CJK Extension F | ||
| codePoint in 0x2CEB0..0x2EBEF -> true | ||
|
|
||
| // CJK Compatibility Ideographs | ||
| codePoint in 0xF900..0xFAFF -> true | ||
|
|
||
| // CJK Compatibility Ideographs Supplement | ||
| codePoint in 0x2F800..0x2FA1F -> true | ||
|
|
||
| // CJK Symbols and Punctuation (ideographic space, 、。「」〈〉 etc.) | ||
| codePoint in 0x3000..0x303F -> true | ||
|
|
||
| // Hiragana | ||
| codePoint in 0x3040..0x309F -> true | ||
|
|
||
| // Katakana | ||
| codePoint in 0x30A0..0x30FF -> true | ||
|
|
||
| // Katakana Phonetic Extensions | ||
| codePoint in 0x31F0..0x31FF -> true | ||
|
|
||
| // Hangul Syllables | ||
| codePoint in 0xAC00..0xD7AF -> true | ||
|
|
||
| // Hangul Jamo (only leading consonants are Wide per Unicode EAW) | ||
| codePoint in 0x1100..0x115F -> true | ||
|
|
||
| // Hangul Jamo Extended-A | ||
| codePoint in 0xA960..0xA97F -> true | ||
|
|
||
| // Fullwidth ASCII variants (FF01-FF60) and Fullwidth brackets (FF5F-FF60) | ||
| codePoint in 0xFF01..0xFF60 -> true | ||
|
|
||
| // Fullwidth signs (FFE0-FFE6) | ||
| codePoint in 0xFFE0..0xFFE6 -> true | ||
|
|
||
| // Emoji | ||
| codePoint in 0x1F600..0x1F64F -> true // Emoticons | ||
| codePoint in 0x1F300..0x1F5FF -> true // Misc Symbols and Pictographs | ||
| codePoint in 0x1F680..0x1F6FF -> true // Transport and Map | ||
| codePoint in 0x1F1E0..0x1F1FF -> true // Regional Indicator Symbols | ||
|
|
||
| else -> false | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
plugins/markdown/test/data/editor/tables/format/chinese_table_test.after.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | 名字 | 年龄 | | ||
| |----------|------| | ||
| | 投放账号 | 3 . * | |
3 changes: 3 additions & 0 deletions
3
plugins/markdown/test/data/editor/tables/format/chinese_table_test.before.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | 名字 | 年龄 | | ||
| |------|-------| | ||
| | 投放账号 | 3 . * | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...down/test/src/org/intellij/plugins/markdown/editor/tables/TableCharacterWidthUtilsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package org.intellij.plugins.markdown.editor.tables | ||
|
|
||
| import org.junit.Test | ||
| import org.junit.Assert.* | ||
|
|
||
| class TableCharacterWidthUtilsTest { | ||
|
|
||
| @Test | ||
| fun `test ASCII characters width calculation`() { | ||
| assertEquals(1, TableCharacterWidthUtils.calculateDisplayWidth("a")) | ||
| assertEquals(1, TableCharacterWidthUtils.calculateDisplayWidth("A")) | ||
| assertEquals(1, TableCharacterWidthUtils.calculateDisplayWidth("1")) | ||
| assertEquals(1, TableCharacterWidthUtils.calculateDisplayWidth("!")) | ||
| assertEquals(5, TableCharacterWidthUtils.calculateDisplayWidth("hello")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Chinese characters width calculation`() { | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u4E2D")) | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u6587")) | ||
| assertEquals(4, TableCharacterWidthUtils.calculateDisplayWidth("\u4E2D\u6587")) | ||
| assertEquals(8, TableCharacterWidthUtils.calculateDisplayWidth("\u6295\u653E\u8D26\u53F7")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test mixed content width calculation`() { | ||
| assertEquals(3, TableCharacterWidthUtils.calculateDisplayWidth("a\u4E2D")) | ||
| assertEquals(3, TableCharacterWidthUtils.calculateDisplayWidth("\u4E2Da")) | ||
| assertEquals(7, TableCharacterWidthUtils.calculateDisplayWidth("hello\u4E2D")) | ||
| assertEquals(7, TableCharacterWidthUtils.calculateDisplayWidth("\u4E2Dhello")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test empty string width calculation`() { | ||
| assertEquals(0, TableCharacterWidthUtils.calculateDisplayWidth("")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test control characters width calculation`() { | ||
| assertEquals(0, TableCharacterWidthUtils.calculateDisplayWidth("\t")) | ||
| assertEquals(0, TableCharacterWidthUtils.calculateDisplayWidth("\n")) | ||
| assertEquals(0, TableCharacterWidthUtils.calculateDisplayWidth("\r")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test emoji width calculation`() { | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\uD83D\uDE00")) | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\uD83D\uDE80")) | ||
| assertEquals(4, TableCharacterWidthUtils.calculateDisplayWidth("\uD83D\uDE00\uD83D\uDE80")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Japanese characters width calculation`() { | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u3042")) // Hiragana a | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u30A2")) // Katakana a | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u6F22")) // Kanji | ||
| } | ||
|
|
||
| @Test | ||
| fun `test Korean characters width calculation`() { | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\uD55C")) // Hangul | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\uAE00")) | ||
| } | ||
|
|
||
| @Test | ||
| fun `test CJK symbols and punctuation width calculation`() { | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u3000")) // Ideographic space | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u3001")) // 、 | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u3002")) // 。 | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u300C")) // 「 | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\u300D")) // 」 | ||
| } | ||
|
|
||
| @Test | ||
| fun `test full-width ASCII variants width calculation`() { | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\uFF21")) // Fullwidth A | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\uFF11")) // Fullwidth 1 | ||
| assertEquals(2, TableCharacterWidthUtils.calculateDisplayWidth("\uFF01")) // Fullwidth ! | ||
| } | ||
|
|
||
| @Test | ||
| fun `test table cell content examples`() { | ||
| // Test cases from the user's problem | ||
| assertEquals(4, TableCharacterWidthUtils.calculateDisplayWidth("\u540D\u5B57")) // 名字 | ||
| assertEquals(4, TableCharacterWidthUtils.calculateDisplayWidth("\u5E74\u9F84")) // 年龄 | ||
| assertEquals(8, TableCharacterWidthUtils.calculateDisplayWidth("\u6295\u653E\u8D26\u53F7")) // 投放账号 | ||
| assertEquals(4, TableCharacterWidthUtils.calculateDisplayWidth("name")) | ||
| assertEquals(3, TableCharacterWidthUtils.calculateDisplayWidth("age")) | ||
| assertEquals(4, TableCharacterWidthUtils.calculateDisplayWidth("demo")) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing CJK Symbols and Punctuation full-width range
Medium Severity
isFullWidthCharacteris missing the CJK Symbols and Punctuation range (U+3000–U+303F), which contains frequently-used East Asian Wide characters like ideographic space (U+3000), 、(U+3001), 。(U+3002), 「」brackets, and 〈〉angle brackets. These fall through togetCharacterWidth'selse -> 1default instead of returning width 2. Tables containing common CJK punctuation will still have misaligned columns. Since this function also drives the editor'sDoubleWidthCharacterStrategy, the mismatch would compound in rendering.