Skip to content

Commit 767a7df

Browse files
committed
Fix wrap interaction with show-whitespaces and test word wrapping more
Consider atom boundaries as word boundaries, which should be correct du to passes ordering. Fixes #5350
1 parent ee23c00 commit 767a7df

File tree

9 files changed

+31
-8
lines changed

9 files changed

+31
-8
lines changed

src/highlighters.cc

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ struct WrapHighlighter : Highlighter
676676
bool next_split_pos(SplitPos& pos, DisplayLine::iterator line_end,
677677
ColumnCount wrap_column, ColumnCount prefix_len) const
678678
{
679-
SplitPos last_word_boundary = pos;
680-
SplitPos last_WORD_boundary = pos;
679+
SplitPos last_word_boundary{};
680+
SplitPos last_WORD_boundary{};
681681

682682
auto update_word_boundaries = [&](Codepoint cp) {
683683
if (m_word_wrap and not is_word<Word>(cp))
@@ -704,6 +704,9 @@ struct WrapHighlighter : Highlighter
704704
{
705705
++pos.atom_it;
706706
pos.byte = 0;
707+
// Thanks to the pass ordering, atom boundary should always be reasonable word split points
708+
last_word_boundary = pos;
709+
last_WORD_boundary = pos;
707710
}
708711
}
709712
if (pos.atom_it == line_end)
@@ -713,24 +716,27 @@ struct WrapHighlighter : Highlighter
713716
if (m_word_wrap and pos.byte < content.length())
714717
{
715718
auto find_split_pos = [&](SplitPos start_pos, auto is_word) {
716-
if (start_pos.byte == 0)
719+
if (start_pos.column == 0)
717720
return false;
718721
const char* it = &content[pos.byte];
719722
// split at current position if is a word boundary
720723
if (not is_word(utf8::codepoint(it, content.end()), {'_'}))
721724
return true;
722725
// split at last word boundary if the word is shorter than our wrapping width
723726
ColumnCount word_length = pos.column - start_pos.column;
724-
while (it != content.end() and word_length <= (wrap_column - prefix_len))
727+
ColumnCount max_word_length = wrap_column - prefix_len;
728+
while (it != content.end() and word_length <= max_word_length)
725729
{
726730
const Codepoint cp = utf8::read_codepoint(it, content.end());
727731
if (not is_word(cp, {'_'}))
728-
{
729-
pos = start_pos;
730-
return true;
731-
}
732+
break;
732733
word_length += codepoint_width(cp);
733734
}
735+
if (word_length <= max_word_length)
736+
{
737+
pos = start_pos;
738+
return true;
739+
}
734740
return false;
735741
};
736742
if (find_split_pos(last_WORD_boundary, is_word<WORD>) or
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo bar
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add-highlighter window/ wrap -word -width 5
2+
add-highlighter window/ show-whitespaces
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ui_out '{ "jsonrpc": "2.0", "method": "set_ui_options", "params": [{}] }'
2+
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "f" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "oo" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg"] }, "contents": "·" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "bar" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": ["final_fg"] }, "contents": "¬" }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'
3+
ui_out -until '{ "jsonrpc": "2.0", "method": "refresh", "params": [true] }'
4+

test/highlight/wrap/word/cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

test/highlight/wrap/word/in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
123456789 wrap
2+
123456 wrap
3+
123456 wrap 1234 wrap 123456789012 wrap

test/highlight/wrap/word/rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add-highlighter window/ wrap -word -width 10

test/highlight/wrap/word/script

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ui_out '{ "jsonrpc": "2.0", "method": "set_ui_options", "params": [{}] }'
2+
ui_out '{ "jsonrpc": "2.0", "method": "draw", "params": [[[{ "face": { "fg": "black", "bg": "white", "underline": "default", "attributes": [] }, "contents": "1" }, { "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "23456789 " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "wrap\u000a" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "123456 " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "wrap\u000a" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "123456 " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "wrap 1234 " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "wrap 12345" }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "6789012 " }], [{ "face": { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, "contents": "wrap\u000a" }]], { "fg": "default", "bg": "default", "underline": "default", "attributes": [] }, { "fg": "blue", "bg": "default", "underline": "default", "attributes": [] }] }'
3+
ui_out -until '{ "jsonrpc": "2.0", "method": "refresh", "params": [true] }'
4+

0 commit comments

Comments
 (0)