From f2e9d636190569b8110b4f7ed18f696e6fc7d417 Mon Sep 17 00:00:00 2001 From: max ulidtko Date: Fri, 23 Oct 2015 18:58:23 +0300 Subject: [PATCH 1/2] Add support for double-width characters Fix SublimeText/ElasticTabstops#14 --- elastic_tabstops.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/elastic_tabstops.py b/elastic_tabstops.py index 114f38d..11e100c 100644 --- a/elastic_tabstops.py +++ b/elastic_tabstops.py @@ -20,6 +20,22 @@ from ElasticTabstops.edit import Edit from itertools import zip_longest +# works the same in Py2 and Py3 (thanks jesus) +from unicodedata import east_asian_width + +def unicode_char_width(c): + """ Wide chars are Chinese ideographs, Japanese kanji and alike. + They get two columns of space to render. + """ + return { + 'Na': 1, 'N': 1, 'H': 1, + 'W': 2, 'F': 2 + } [east_asian_width(c)] + +def column_width(s): + """ Calculate string width in columns, accounting for wide chars """ + return sum(map(unicode_char_width, s)) + def lines_in_buffer(view): row, col = view.rowcol(view.size()) #"row" is the index of the last row; need to add 1 to get number of rows @@ -67,7 +83,7 @@ def cell_widths_for_row(view, row): right_edge = tabs[i+1] rightmost_selection = rightmost_selection_in_cell(selection_columns, right_edge) cell = line[left_edge:right_edge] - widths[i] = max(len(cell.rstrip()), rightmost_selection - left_edge) + widths[i] = max(column_width(cell.rstrip()), rightmost_selection - left_edge) return widths def find_cell_widths_for_block(view, row): @@ -106,13 +122,19 @@ def adjust_row(view, glued, row, widths): location += 1 + w it += bias difference = location - it - if difference == 0: - continue end_tab_point = view.text_point(row, it) partial_line = view.substr(view.line(end_tab_point))[0:it] + + columns = column_width(partial_line) + difference = location - columns + + if difference == 0: + continue + stripped_partial_line = partial_line.rstrip() ispaces = len(partial_line) - len(stripped_partial_line) + if difference > 0: view.run_command("maybe_mark_undo_groups_for_gluing") glued = True From 39adb52c506f10561c113f2738237f5b4b890370 Mon Sep 17 00:00:00 2001 From: max ulidtko Date: Mon, 9 Nov 2015 15:57:04 +0200 Subject: [PATCH 2/2] simplify comment --- elastic_tabstops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elastic_tabstops.py b/elastic_tabstops.py index 11e100c..31d37c5 100644 --- a/elastic_tabstops.py +++ b/elastic_tabstops.py @@ -20,7 +20,7 @@ from ElasticTabstops.edit import Edit from itertools import zip_longest -# works the same in Py2 and Py3 (thanks jesus) +# works the same in Py2 and Py3 from unicodedata import east_asian_width def unicode_char_width(c):