-
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| } | ||
| } | ||
| } |
| 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 | ||
|
|
||
| // 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 | ||
| codePoint in 0x1100..0x11FF -> true | ||
|
|
||
| // Hangul Jamo Extended-A | ||
| codePoint in 0xA960..0xA97F -> true | ||
|
|
||
| // Hangul Jamo Extended-B | ||
| codePoint in 0xD7B0..0xD7FF -> 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 | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing CJK Symbols and Punctuation full-width rangeMedium Severity
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | 名字 | 年龄 | | ||
| |----------|------| | ||
| | 投放账号 | 3 . * | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| | 名字 | 年龄 | | ||
| |------|-------| | ||
| | 投放账号 | 3 . * | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| 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 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")) | ||
| } | ||
| } |


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.
Hangul Jamo range over-classifies narrow characters as wide
Low Severity
The Hangul Jamo range
0x1100..0x11FFis entirely classified as full-width, but per the Unicode East Asian Width property only0x1100..0x115F(leading consonants) are Wide — characters0x1160..0x11FF(vowels and trailing consonants) are Neutral/narrow. Similarly, Hangul Jamo Extended-B (0xD7B0..0xD7FF) is entirely Neutral but classified as full-width here. This over-counts the display width for these characters.