Skip to content

Commit 2dd91f1

Browse files
committed
Linebreak should not apply for leading whitespace
gh-issue 13228, linebreak only applies after first non-whitespce
1 parent 317468a commit 2dd91f1

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

src/charset.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,8 +1121,10 @@ win_lbr_chartabsize(
11211121
int size;
11221122
int mb_added = 0;
11231123
int n;
1124+
int need_lbr = FALSE;
11241125
char_u *sbr;
11251126
int no_sbr = FALSE;
1127+
colnr_T vcol_start = 0; // start from where to consider linebreak
11261128
#endif
11271129

11281130
#if defined(FEAT_PROP_POPUP)
@@ -1344,7 +1346,14 @@ win_lbr_chartabsize(
13441346
* If 'linebreak' set check at a blank before a non-blank if the line
13451347
* needs a break here
13461348
*/
1347-
if (wp->w_p_lbr
1349+
if (wp->w_p_lbr && wp->w_p_wrap && wp->w_width != 0)
1350+
{
1351+
char_u *t = cts->cts_line;
1352+
while (VIM_ISBREAK((int)*t))
1353+
t++;
1354+
vcol_start = t - cts->cts_line;
1355+
}
1356+
if (wp->w_p_lbr && vcol_start <= vcol
13481357
&& VIM_ISBREAK((int)s[0])
13491358
&& !VIM_ISBREAK((int)s[1])
13501359
&& wp->w_p_wrap

src/drawline.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ typedef struct {
171171
#ifdef FEAT_SIGNS
172172
sign_attrs_T sattr;
173173
#endif
174+
#ifdef FEAT_LINEBREAK
175+
// do consider wrapping in linebreak mode only after encountering
176+
// a non whitespace char
177+
int need_lbr;
178+
#endif
174179
} winlinevars_T;
175180

176181
// draw_state values for items that are drawn in sequence:
@@ -968,6 +973,9 @@ win_line_start(win_T *wp UNUSED, winlinevars_T *wlv, int save_extra)
968973
{
969974
wlv->col = 0;
970975
wlv->off = (unsigned)(current_ScreenLine - ScreenLines);
976+
#ifdef FEAT_LINEBREAK
977+
wlv->need_lbr = FALSE;
978+
#endif
971979

972980
#ifdef FEAT_RIGHTLEFT
973981
if (wp->w_p_rl)
@@ -994,6 +1002,9 @@ win_line_start(win_T *wp UNUSED, winlinevars_T *wlv, int save_extra)
9941002
wlv->saved_extra_for_textprop = wlv->extra_for_textprop;
9951003
wlv->saved_c_extra = wlv->c_extra;
9961004
wlv->saved_c_final = wlv->c_final;
1005+
#ifdef FEAT_LINEBREAK
1006+
wlv->need_lbr = TRUE;
1007+
#endif
9971008
#ifdef FEAT_SYN_HL
9981009
if (!(wlv->cul_screenline
9991010
# ifdef FEAT_DIFF
@@ -2904,9 +2915,14 @@ win_line(
29042915
wlv.char_attr);
29052916
}
29062917
#endif
2918+
#ifdef FEAT_LINEBREAK
2919+
if ( wp->w_p_lbr && !wlv.need_lbr && c != NUL &&
2920+
!VIM_ISBREAK((int)*ptr))
2921+
wlv.need_lbr = TRUE;
2922+
#endif
29072923
#ifdef FEAT_LINEBREAK
29082924
// Found last space before word: check for line break.
2909-
if (wp->w_p_lbr && c0 == c
2925+
if (wp->w_p_lbr && c0 == c && wlv.need_lbr
29102926
&& VIM_ISBREAK(c) && !VIM_ISBREAK((int)*ptr))
29112927
{
29122928
int mb_off = has_mbyte ? (*mb_head_off)(line, ptr - 1)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
|1+0&#ffffff0| @73
2+
>2| @73
3+
|3| @73
4+
|4| @10|t+0#0000001#ffd7ff255|e|x|t| +0#0000000#ffffff0@58
5+
|h|e|r|e| |i|s| |s|o|m|e| |t|e|x|t| |t|o| |h|o|v|e|r| |o|v|e|r| @43
6+
|6| @73
7+
|7| @73
8+
|8| @73
9+
|9| @73
10+
|:|c|a|l@1| |M|o|v|e|A|w|a|y|(|)| @40|2|,|1| @10|T|o|p|

src/testdir/rtp/syntax/Xexport.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vim9script
2+
export var That = 'yes'

src/testdir/rtp/syntax/vim.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vim9script
2+
import './Xexport.vim' as exp
3+
echo exp.That

src/testdir/test_listlbr.vim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,19 @@ func Test_ctrl_char_on_wrap_column()
372372
call s:close_windows()
373373
endfunc
374374

375+
func Test_linebreak_no_break_after_whitespace_only()
376+
call s:test_windows('setl ts=4 linebreak wrap')
377+
call setline(1, "\tabcdefghijklmnopqrstuvwxyz" ..
378+
\ "abcdefghijklmnopqrstuvwxyz")
379+
let lines = s:screen_lines([1, 4], winwidth(0))
380+
let expect = [
381+
\ " abcdefghijklmnop",
382+
\ "qrstuvwxyzabcdefghij",
383+
\ "klmnopqrstuvwxyz ",
384+
\ "~ ",
385+
\ ]
386+
call s:compare_lines(expect, lines)
387+
call s:close_windows()
388+
endfunc
389+
375390
" vim: shiftwidth=2 sts=2 expandtab

0 commit comments

Comments
 (0)