refactor(diff.c): reduce scope of variables (#20781)

Co-authored-by: zeertzjq <zeertzjq@outlook.com>
This commit is contained in:
Lewis Russell
2022-10-23 00:57:51 +01:00
committed by GitHub
parent 2f9b94a268
commit d70193c326

View File

@@ -309,8 +309,6 @@ static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T
diff_T *dp = tp->tp_first_diff; diff_T *dp = tp->tp_first_diff;
linenr_T lnum_deleted = line1; // lnum of remaining deletion linenr_T lnum_deleted = line1; // lnum of remaining deletion
linenr_T n;
linenr_T off;
for (;;) { for (;;) {
// If the change is after the previous diff block and before the next // If the change is after the previous diff block and before the next
// diff block, thus not touching an existing change, create a new diff // diff block, thus not touching an existing change, create a new diff
@@ -374,7 +372,8 @@ static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T
// 2. 3. 4. 5.: inserted/deleted lines touching this diff. // 2. 3. 4. 5.: inserted/deleted lines touching this diff.
if (deleted > 0) { if (deleted > 0) {
off = 0; linenr_T n;
linenr_T off = 0;
if (dp->df_lnum[idx] >= line1) { if (dp->df_lnum[idx] >= line1) {
if (last <= line2) { if (last <= line2) {
// 4. delete all lines of diff // 4. delete all lines of diff
@@ -1537,14 +1536,8 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
int line_idx = 0; int line_idx = 0;
diff_T *dprev = NULL; diff_T *dprev = NULL;
diff_T *dp = curtab->tp_first_diff; diff_T *dp = curtab->tp_first_diff;
diff_T *dn, *dpl;
diffout_T *dout = &dio->dio_diff; diffout_T *dout = &dio->dio_diff;
char linebuf[LBUFLEN]; // only need to hold the diff line
char *line;
linenr_T off;
int i;
int notset = true; // block "*dp" not set yet int notset = true; // block "*dp" not set yet
diffhunk_T *hunk = NULL; // init to avoid gcc warning
enum { enum {
DIFF_ED, DIFF_ED,
DIFF_UNIFIED, DIFF_UNIFIED,
@@ -1561,6 +1554,8 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
} }
} }
diffhunk_T *hunk = NULL;
if (!dio->dio_internal) { if (!dio->dio_internal) {
hunk = xmalloc(sizeof(*hunk)); hunk = xmalloc(sizeof(*hunk));
} }
@@ -1572,6 +1567,8 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
} }
hunk = ((diffhunk_T **)dout->dout_ga.ga_data)[line_idx++]; hunk = ((diffhunk_T **)dout->dout_ga.ga_data)[line_idx++];
} else { } else {
char *line;
char linebuf[LBUFLEN]; // only need to hold the diff line
if (fd == NULL) { if (fd == NULL) {
if (line_idx >= dout->dout_ga.ga_len) { if (line_idx >= dout->dout_ga.ga_len) {
break; // did last line break; // did last line
@@ -1647,6 +1644,7 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
&& (hunk->lnum_orig + hunk->count_orig >= dp->df_lnum[idx_orig])) { && (hunk->lnum_orig + hunk->count_orig >= dp->df_lnum[idx_orig])) {
// New block overlaps with existing block(s). // New block overlaps with existing block(s).
// First find last block that overlaps. // First find last block that overlaps.
diff_T *dpl;
for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next) { for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next) {
if (hunk->lnum_orig + hunk->count_orig < dpl->df_next->df_lnum[idx_orig]) { if (hunk->lnum_orig + hunk->count_orig < dpl->df_next->df_lnum[idx_orig]) {
break; break;
@@ -1655,10 +1653,10 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
// If the newly found block starts before the old one, set the // If the newly found block starts before the old one, set the
// start back a number of lines. // start back a number of lines.
off = dp->df_lnum[idx_orig] - hunk->lnum_orig; linenr_T off = dp->df_lnum[idx_orig] - hunk->lnum_orig;
if (off > 0) { if (off > 0) {
for (i = idx_orig; i < idx_new; i++) { for (int i = idx_orig; i < idx_new; i++) {
if (curtab->tp_diffbuf[i] != NULL) { if (curtab->tp_diffbuf[i] != NULL) {
dp->df_lnum[i] -= off; dp->df_lnum[i] -= off;
} }
@@ -1692,7 +1690,7 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
off = 0; off = 0;
} }
for (i = idx_orig; i < idx_new; i++) { for (int i = idx_orig; i < idx_new; i++) {
if (curtab->tp_diffbuf[i] != NULL) { if (curtab->tp_diffbuf[i] != NULL) {
dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i] dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
- dp->df_lnum[i] + off; - dp->df_lnum[i] + off;
@@ -1700,7 +1698,7 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
} }
// Delete the diff blocks that have been merged into one. // Delete the diff blocks that have been merged into one.
dn = dp->df_next; diff_T *dn = dp->df_next;
dp->df_next = dpl->df_next; dp->df_next = dpl->df_next;
while (dn != dp->df_next) { while (dn != dp->df_next) {
@@ -1720,7 +1718,7 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio)
// Set values for other buffers, these must be equal to the // Set values for other buffers, these must be equal to the
// original buffer, otherwise there would have been a change // original buffer, otherwise there would have been a change
// already. // already.
for (i = idx_orig + 1; i < idx_new; i++) { for (int i = idx_orig + 1; i < idx_new; i++) {
if (curtab->tp_diffbuf[i] != NULL) { if (curtab->tp_diffbuf[i] != NULL) {
diff_copy_entry(dprev, dp, idx_orig, i); diff_copy_entry(dprev, dp, idx_orig, i);
} }
@@ -1797,7 +1795,6 @@ void diff_clear(tabpage_T *tp)
/// @return diff status. /// @return diff status.
int diff_check(win_T *wp, linenr_T lnum) int diff_check(win_T *wp, linenr_T lnum)
{ {
diff_T *dp;
buf_T *buf = wp->w_buffer; buf_T *buf = wp->w_buffer;
if (curtab->tp_diff_invalid) { if (curtab->tp_diff_invalid) {
@@ -1828,6 +1825,7 @@ int diff_check(win_T *wp, linenr_T lnum)
} }
// search for a change that includes "lnum" in the list of diffblocks. // search for a change that includes "lnum" in the list of diffblocks.
diff_T *dp;
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) { for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) {
if (lnum <= dp->df_lnum[idx] + dp->df_count[idx]) { if (lnum <= dp->df_lnum[idx] + dp->df_count[idx]) {
break; break;
@@ -2023,7 +2021,6 @@ void diff_set_topline(win_T *fromwin, win_T *towin)
{ {
buf_T *frombuf = fromwin->w_buffer; buf_T *frombuf = fromwin->w_buffer;
linenr_T lnum = fromwin->w_topline; linenr_T lnum = fromwin->w_topline;
diff_T *dp;
int fromidx = diff_buf_idx(frombuf); int fromidx = diff_buf_idx(frombuf);
if (fromidx == DB_COUNT) { if (fromidx == DB_COUNT) {
@@ -2038,6 +2035,7 @@ void diff_set_topline(win_T *fromwin, win_T *towin)
towin->w_topfill = 0; towin->w_topfill = 0;
// search for a change that includes "lnum" in the list of diffblocks. // search for a change that includes "lnum" in the list of diffblocks.
diff_T *dp;
for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) { for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) {
if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx]) { if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx]) {
break; break;
@@ -2281,14 +2279,6 @@ bool diffopt_filler(void)
bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{ {
char *line_new;
int si_org;
int si_new;
int ei_org;
int ei_new;
bool added = true;
int l;
// Make a copy of the line, the next ml_get() will invalidate it. // Make a copy of the line, the next ml_get() will invalidate it.
char *line_org = xstrdup(ml_get_buf(wp->w_buffer, lnum, false)); char *line_org = xstrdup(ml_get_buf(wp->w_buffer, lnum, false));
@@ -2313,6 +2303,12 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
return false; return false;
} }
int si_org;
int si_new;
int ei_org;
int ei_new;
bool added = true;
linenr_T off = lnum - dp->df_lnum[idx]; linenr_T off = lnum - dp->df_lnum[idx];
int i; int i;
for (i = 0; i < DB_COUNT; i++) { for (i = 0; i < DB_COUNT; i++) {
@@ -2322,7 +2318,7 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
continue; continue;
} }
added = false; added = false;
line_new = ml_get_buf(curtab->tp_diffbuf[i], dp->df_lnum[i] + off, false); char *line_new = ml_get_buf(curtab->tp_diffbuf[i], dp->df_lnum[i] + off, false);
// Search for start of difference // Search for start of difference
si_org = si_new = 0; si_org = si_new = 0;
@@ -2337,6 +2333,7 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
si_org = (int)(skipwhite(line_org + si_org) - line_org); si_org = (int)(skipwhite(line_org + si_org) - line_org);
si_new = (int)(skipwhite(line_new + si_new) - line_new); si_new = (int)(skipwhite(line_new + si_new) - line_new);
} else { } else {
int l;
if (!diff_equal_char((char_u *)line_org + si_org, (char_u *)line_new + si_new, &l)) { if (!diff_equal_char((char_u *)line_org + si_org, (char_u *)line_new + si_new, &l)) {
break; break;
} }
@@ -2383,6 +2380,7 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
p1 -= utf_head_off(line_org, (char *)p1); p1 -= utf_head_off(line_org, (char *)p1);
p2 -= utf_head_off(line_new, (char *)p2); p2 -= utf_head_off(line_new, (char *)p2);
int l;
if (!diff_equal_char(p1, p2, &l)) { if (!diff_equal_char(p1, p2, &l)) {
break; break;
} }
@@ -2412,16 +2410,14 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
bool diff_infold(win_T *wp, linenr_T lnum) bool diff_infold(win_T *wp, linenr_T lnum)
FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1)
{ {
bool other = false;
// Return if 'diff' isn't set. // Return if 'diff' isn't set.
if (!wp->w_p_diff) { if (!wp->w_p_diff) {
return false; return false;
} }
int idx = -1; int idx = -1;
int i; bool other = false;
for (i = 0; i < DB_COUNT; i++) { for (int i = 0; i < DB_COUNT; i++) {
if (curtab->tp_diffbuf[i] == wp->w_buffer) { if (curtab->tp_diffbuf[i] == wp->w_buffer) {
idx = i; idx = i;
} else if (curtab->tp_diffbuf[i] != NULL) { } else if (curtab->tp_diffbuf[i] != NULL) {
@@ -2461,13 +2457,13 @@ bool diff_infold(win_T *wp, linenr_T lnum)
/// "dp" and "do" commands. /// "dp" and "do" commands.
void nv_diffgetput(bool put, size_t count) void nv_diffgetput(bool put, size_t count)
{ {
exarg_T ea;
char buf[30];
if (bt_prompt(curbuf)) { if (bt_prompt(curbuf)) {
vim_beep(BO_OPER); vim_beep(BO_OPER);
return; return;
} }
exarg_T ea;
char buf[30];
if (count == 0) { if (count == 0) {
ea.arg = ""; ea.arg = "";
} else { } else {
@@ -2503,24 +2499,9 @@ static bool valid_diff(diff_T *diff)
/// @param eap /// @param eap
void ex_diffgetput(exarg_T *eap) void ex_diffgetput(exarg_T *eap)
{ {
linenr_T lnum;
linenr_T count;
linenr_T off = 0; linenr_T off = 0;
diff_T *dp;
diff_T *dfree;
int i;
int added;
char *p;
aco_save_T aco; aco_save_T aco;
buf_T *buf;
linenr_T start_skip;
linenr_T end_skip;
linenr_T new_count;
int buf_empty;
int found_not_ma = false;
int idx_other; int idx_other;
int idx_from;
int idx_to;
// Find the current buffer in the list of diff buffers. // Find the current buffer in the list of diff buffers.
int idx_cur = diff_buf_idx(curbuf); int idx_cur = diff_buf_idx(curbuf);
@@ -2530,6 +2511,7 @@ void ex_diffgetput(exarg_T *eap)
} }
if (*eap->arg == NUL) { if (*eap->arg == NUL) {
int found_not_ma = false;
// No argument: Find the other buffer in the list of diff buffers. // No argument: Find the other buffer in the list of diff buffers.
for (idx_other = 0; idx_other < DB_COUNT; idx_other++) { for (idx_other = 0; idx_other < DB_COUNT; idx_other++) {
if ((curtab->tp_diffbuf[idx_other] != curbuf) if ((curtab->tp_diffbuf[idx_other] != curbuf)
@@ -2552,7 +2534,7 @@ void ex_diffgetput(exarg_T *eap)
} }
// Check that there isn't a third buffer in the list // Check that there isn't a third buffer in the list
for (i = idx_other + 1; i < DB_COUNT; i++) { for (int i = idx_other + 1; i < DB_COUNT; i++) {
if ((curtab->tp_diffbuf[i] != curbuf) if ((curtab->tp_diffbuf[i] != curbuf)
&& (curtab->tp_diffbuf[i] != NULL) && (curtab->tp_diffbuf[i] != NULL)
&& ((eap->cmdidx != CMD_diffput) && ((eap->cmdidx != CMD_diffput)
@@ -2564,11 +2546,12 @@ void ex_diffgetput(exarg_T *eap)
} }
} else { } else {
// Buffer number or pattern given. Ignore trailing white space. // Buffer number or pattern given. Ignore trailing white space.
p = eap->arg + strlen(eap->arg); char *p = eap->arg + strlen(eap->arg);
while (p > eap->arg && ascii_iswhite(p[-1])) { while (p > eap->arg && ascii_iswhite(p[-1])) {
p--; p--;
} }
int i;
for (i = 0; ascii_isdigit(eap->arg[i]) && eap->arg + i < p; i++) {} for (i = 0; ascii_isdigit(eap->arg[i]) && eap->arg + i < p; i++) {}
if (eap->arg + i == p) { if (eap->arg + i == p) {
@@ -2582,7 +2565,7 @@ void ex_diffgetput(exarg_T *eap)
return; return;
} }
} }
buf = buflist_findnr(i); buf_T *buf = buflist_findnr(i);
if (buf == NULL) { if (buf == NULL) {
semsg(_("E102: Can't find buffer \"%s\""), eap->arg); semsg(_("E102: Can't find buffer \"%s\""), eap->arg);
@@ -2617,6 +2600,8 @@ void ex_diffgetput(exarg_T *eap)
} }
} }
int idx_from;
int idx_to;
if (eap->cmdidx == CMD_diffget) { if (eap->cmdidx == CMD_diffget) {
idx_from = idx_other; idx_from = idx_other;
idx_to = idx_cur; idx_to = idx_cur;
@@ -2644,20 +2629,20 @@ void ex_diffgetput(exarg_T *eap)
diff_T *dprev = NULL; diff_T *dprev = NULL;
for (dp = curtab->tp_first_diff; dp != NULL;) { for (diff_T *dp = curtab->tp_first_diff; dp != NULL;) {
if (dp->df_lnum[idx_cur] > eap->line2 + off) { if (dp->df_lnum[idx_cur] > eap->line2 + off) {
// past the range that was specified // past the range that was specified
break; break;
} }
dfree = NULL; diff_T *dfree = NULL;
lnum = dp->df_lnum[idx_to]; linenr_T lnum = dp->df_lnum[idx_to];
count = dp->df_count[idx_to]; linenr_T count = dp->df_count[idx_to];
if ((dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off) if ((dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off)
&& (u_save(lnum - 1, lnum + count) != FAIL)) { && (u_save(lnum - 1, lnum + count) != FAIL)) {
// Inside the specified range and saving for undo worked. // Inside the specified range and saving for undo worked.
start_skip = 0; linenr_T start_skip = 0;
end_skip = 0; linenr_T end_skip = 0;
if (eap->addr_count > 0) { if (eap->addr_count > 0) {
// A range was specified: check if lines need to be skipped. // A range was specified: check if lines need to be skipped.
@@ -2682,7 +2667,7 @@ void ex_diffgetput(exarg_T *eap)
// range ends above end of current/from diff block // range ends above end of current/from diff block
if (idx_cur == idx_from) { if (idx_cur == idx_from) {
// :diffput // :diffput
i = dp->df_count[idx_cur] - start_skip - end_skip; int i = dp->df_count[idx_cur] - start_skip - end_skip;
if (count > i) { if (count > i) {
count = i; count = i;
@@ -2701,10 +2686,10 @@ void ex_diffgetput(exarg_T *eap)
} }
} }
buf_empty = buf_is_empty(curbuf); bool buf_empty = buf_is_empty(curbuf);
added = 0; int added = 0;
for (i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
// remember deleting the last line of the buffer // remember deleting the last line of the buffer
buf_empty = curbuf->b_ml.ml_line_count == 1; buf_empty = curbuf->b_ml.ml_line_count == 1;
if (ml_delete(lnum, false) == OK) { if (ml_delete(lnum, false) == OK) {
@@ -2712,12 +2697,12 @@ void ex_diffgetput(exarg_T *eap)
} }
} }
for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; i++) { for (int i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; i++) {
linenr_T nr = dp->df_lnum[idx_from] + start_skip + i; linenr_T nr = dp->df_lnum[idx_from] + start_skip + i;
if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count) { if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count) {
break; break;
} }
p = xstrdup(ml_get_buf(curtab->tp_diffbuf[idx_from], nr, false)); char *p = xstrdup(ml_get_buf(curtab->tp_diffbuf[idx_from], nr, false));
ml_append(lnum + i - 1, p, 0, false); ml_append(lnum + i - 1, p, 0, false);
xfree(p); xfree(p);
added++; added++;
@@ -2728,12 +2713,13 @@ void ex_diffgetput(exarg_T *eap)
ml_delete((linenr_T)2, false); ml_delete((linenr_T)2, false);
} }
} }
new_count = dp->df_count[idx_to] + added; linenr_T new_count = dp->df_count[idx_to] + added;
dp->df_count[idx_to] = new_count; dp->df_count[idx_to] = new_count;
if ((start_skip == 0) && (end_skip == 0)) { if ((start_skip == 0) && (end_skip == 0)) {
// Check if there are any other buffers and if the diff is // Check if there are any other buffers and if the diff is
// equal in them. // equal in them.
int i;
for (i = 0; i < DB_COUNT; i++) { for (i = 0; i < DB_COUNT; i++) {
if ((curtab->tp_diffbuf[i] != NULL) if ((curtab->tp_diffbuf[i] != NULL)
&& (i != idx_from) && (i != idx_from)