Skip to content

Commit 40891ba

Browse files
glepnirchrisbra
authored andcommitted
patch 9.1.1101: insexpand.c hard to read
Problem: insexpand.c hard to read Solution: refactor slightly to make it better readable (glepnir) Problem: - Complex while loops with nested conditions - Redundant if branches - Hard to understand and maintain Solution: - Restructure using while(true) with clear break conditions - Using ternary to replace some if conditions - Add descriptive comments for each step closes: #16600 Signed-off-by: glepnir <glephunter@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 35e02af commit 40891ba

2 files changed

Lines changed: 48 additions & 30 deletions

File tree

src/insexpand.c

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -818,9 +818,7 @@ ins_compl_add(
818818
match = ALLOC_CLEAR_ONE(compl_T);
819819
if (match == NULL)
820820
return FAIL;
821-
match->cp_number = -1;
822-
if (flags & CP_ORIGINAL_TEXT)
823-
match->cp_number = 0;
821+
match->cp_number = flags & CP_ORIGINAL_TEXT ? 0 : -1;
824822
if ((match->cp_str.string = vim_strnsave(str, len)) == NULL)
825823
{
826824
vim_free(match);
@@ -1250,10 +1248,7 @@ trigger_complete_changed_event(int cur)
12501248
if (recursive)
12511249
return;
12521250

1253-
if (cur < 0)
1254-
item = dict_alloc();
1255-
else
1256-
item = ins_compl_dict_alloc(compl_curr_match);
1251+
item = cur < 0 ? dict_alloc() : ins_compl_dict_alloc(compl_curr_match);
12571252
if (item == NULL)
12581253
return;
12591254
v_event = get_v_event(&save_v_event);
@@ -2128,10 +2123,7 @@ ins_compl_new_leader(void)
21282123
get_compl_len(void)
21292124
{
21302125
int off = (int)curwin->w_cursor.col - (int)compl_col;
2131-
2132-
if (off < 0)
2133-
return 0;
2134-
return off;
2126+
return MAX(0, off);
21352127
}
21362128

21372129
/*
@@ -2453,7 +2445,7 @@ ins_compl_stop(int c, int prev_mode, int retval)
24532445
// memory that was used, and make sure we can redo the insert.
24542446
if (compl_curr_match != NULL || compl_leader.string != NULL || c == Ctrl_E)
24552447
{
2456-
char_u *ptr;
2448+
char_u *ptr = NULL;
24572449

24582450
// If any of the original typed text has been changed, eg when
24592451
// ignorecase is set, we must add back-spaces to the redo
@@ -2463,8 +2455,6 @@ ins_compl_stop(int c, int prev_mode, int retval)
24632455
// CTRL-E then don't use the current match.
24642456
if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
24652457
ptr = compl_curr_match->cp_str.string;
2466-
else
2467-
ptr = NULL;
24682458
ins_compl_fixRedoBufForLeader(ptr);
24692459
}
24702460

@@ -2666,10 +2656,7 @@ ins_compl_prep(int c)
26662656
// We're already in CTRL-X mode, do we stay in it?
26672657
if (!vim_is_ctrl_x_key(c))
26682658
{
2669-
if (ctrl_x_mode_scroll())
2670-
ctrl_x_mode = CTRL_X_NORMAL;
2671-
else
2672-
ctrl_x_mode = CTRL_X_FINISHED;
2659+
ctrl_x_mode = ctrl_x_mode_scroll() ? CTRL_X_NORMAL : CTRL_X_FINISHED;
26732660
edit_submode = NULL;
26742661
}
26752662
showmode();
@@ -2726,10 +2713,14 @@ ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
27262713
if (compl_orig_text.string != NULL)
27272714
{
27282715
p = compl_orig_text.string;
2729-
for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
2730-
;
2716+
// Find length of common prefix between original text and new completion
2717+
while (p[len] != NUL && p[len] == ptr[len])
2718+
len++;
2719+
// Adjust length to not break inside a multi-byte character
27312720
if (len > 0)
27322721
len -= (*mb_head_off)(p, p + len);
2722+
// Add backspace characters for each remaining character in
2723+
// original text
27332724
for (p += len; *p != NUL; MB_PTR_ADV(p))
27342725
AppendCharToRedobuff(K_BS);
27352726
}
@@ -2749,28 +2740,49 @@ ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
27492740
ins_compl_next_buf(buf_T *buf, int flag)
27502741
{
27512742
static win_T *wp = NULL;
2743+
int skip_buffer;
27522744

27532745
if (flag == 'w') // just windows
27542746
{
27552747
if (buf == curbuf || !win_valid(wp))
27562748
// first call for this flag/expansion or window was closed
27572749
wp = curwin;
2758-
while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
2759-
&& wp->w_buffer->b_scanned)
2760-
;
2750+
2751+
while (TRUE)
2752+
{
2753+
// Move to next window (wrap to first window if at the end)
2754+
wp = (wp->w_next != NULL) ? wp->w_next : firstwin;
2755+
// Break if we're back at start or found an unscanned buffer
2756+
if (wp == curwin || !wp->w_buffer->b_scanned)
2757+
break;
2758+
}
27612759
buf = wp->w_buffer;
27622760
}
27632761
else
2762+
{
27642763
// 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
27652764
// (unlisted buffers)
27662765
// When completing whole lines skip unloaded buffers.
2767-
while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
2768-
&& ((flag == 'U'
2769-
? buf->b_p_bl
2770-
: (!buf->b_p_bl
2771-
|| (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
2772-
|| buf->b_scanned))
2773-
;
2766+
while (TRUE)
2767+
{
2768+
// Move to next buffer (wrap to first buffer if at the end)
2769+
buf = (buf->b_next != NULL) ? buf->b_next : firstbuf;
2770+
// Break if we're back at start buffer
2771+
if (buf == curbuf)
2772+
break;
2773+
2774+
// Check buffer conditions based on flag
2775+
if (flag == 'U')
2776+
skip_buffer = buf->b_p_bl;
2777+
else
2778+
skip_buffer = !buf->b_p_bl ||
2779+
(buf->b_ml.ml_mfp == NULL) != (flag == 'u');
2780+
2781+
// Break if we found a buffer that matches our criteria
2782+
if (!skip_buffer && !buf->b_scanned)
2783+
break;
2784+
}
2785+
}
27742786
return buf;
27752787
}
27762788

@@ -3313,17 +3325,21 @@ ins_compl_update_sequence_numbers(void)
33133325
// first loop cycle, so it's fast!
33143326
for (match = compl_curr_match->cp_next; match != NULL
33153327
&& !is_first_match(match); match = match->cp_next)
3328+
{
33163329
if (match->cp_number != -1)
33173330
{
33183331
number = match->cp_number;
33193332
break;
33203333
}
3334+
}
33213335
if (match != NULL)
3336+
{
33223337
// go down and assign all numbers which are not assigned yet
33233338
for (match = match->cp_prev; match
33243339
&& match->cp_number == -1;
33253340
match = match->cp_prev)
33263341
match->cp_number = ++number;
3342+
}
33273343
}
33283344
}
33293345

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
1101,
707709
/**/
708710
1100,
709711
/**/

0 commit comments

Comments
 (0)