aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/fold.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-11-19 22:57:13 +0000
committerJosh Rahm <joshuarahm@gmail.com>2024-11-19 22:57:13 +0000
commit9be89f131f87608f224f0ee06d199fcd09d32176 (patch)
tree11022dcfa9e08cb4ac5581b16734196128688d48 /src/nvim/fold.c
parentff7ed8f586589d620a806c3758fac4a47a8e7e15 (diff)
parent88085c2e80a7e3ac29aabb6b5420377eed99b8b6 (diff)
downloadrneovim-9be89f131f87608f224f0ee06d199fcd09d32176.tar.gz
rneovim-9be89f131f87608f224f0ee06d199fcd09d32176.tar.bz2
rneovim-9be89f131f87608f224f0ee06d199fcd09d32176.zip
Merge remote-tracking branch 'upstream/master' into mix_20240309
Diffstat (limited to 'src/nvim/fold.c')
-rw-r--r--src/nvim/fold.c139
1 files changed, 38 insertions, 101 deletions
diff --git a/src/nvim/fold.c b/src/nvim/fold.c
index 59a4dc6aad..e7231c31ab 100644
--- a/src/nvim/fold.c
+++ b/src/nvim/fold.c
@@ -22,6 +22,7 @@
#include "nvim/decoration.h"
#include "nvim/diff.h"
#include "nvim/drawscreen.h"
+#include "nvim/errors.h"
#include "nvim/eval.h"
#include "nvim/eval/typval.h"
#include "nvim/ex_session.h"
@@ -247,9 +248,7 @@ bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp
return false;
}
- if (last > win->w_buffer->b_ml.ml_line_count) {
- last = win->w_buffer->b_ml.ml_line_count;
- }
+ last = MIN(last, win->w_buffer->b_ml.ml_line_count);
if (lastp != NULL) {
*lastp = last;
}
@@ -617,15 +616,11 @@ void foldCreate(win_T *wp, pos_T start, pos_T end)
ga_grow(&fold_ga, cont);
// If the first fold starts before the new fold, let the new fold
// start there. Otherwise the existing fold would change.
- if (start_rel.lnum > fp->fd_top) {
- start_rel.lnum = fp->fd_top;
- }
+ start_rel.lnum = MIN(start_rel.lnum, fp->fd_top);
// When last contained fold isn't completely contained, adjust end
// of new fold.
- if (end_rel.lnum < fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1) {
- end_rel.lnum = fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1;
- }
+ end_rel.lnum = MAX(end_rel.lnum, fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1);
// Move contained folds to inside new fold
memmove(fold_ga.ga_data, fp, sizeof(fold_T) * (size_t)cont);
fold_ga.ga_len += cont;
@@ -721,12 +716,8 @@ void deleteFold(win_T *const wp, const linenr_T start, const linenr_T end, const
(int)(found_fp - (fold_T *)found_ga->ga_data),
recursive);
} else {
- if (first_lnum > found_fp->fd_top + found_off) {
- first_lnum = found_fp->fd_top + found_off;
- }
- if (last_lnum < lnum) {
- last_lnum = lnum;
- }
+ first_lnum = MIN(first_lnum, found_fp->fd_top + found_off);
+ last_lnum = MAX(last_lnum, lnum);
if (!did_one) {
parseMarker(wp);
}
@@ -787,14 +778,10 @@ void foldUpdate(win_T *wp, linenr_T top, linenr_T bot)
}
if (wp->w_folds.ga_len > 0) {
- linenr_T maybe_small_start = top;
- linenr_T maybe_small_end = bot;
-
// Mark all folds from top to bot (or bot to top) as maybe-small.
- if (top > bot) {
- maybe_small_start = bot;
- maybe_small_end = top;
- }
+ linenr_T maybe_small_start = MIN(top, bot);
+ linenr_T maybe_small_end = MAX(top, bot);
+
fold_T *fp;
foldFind(&wp->w_folds, maybe_small_start, &fp);
while (fp < (fold_T *)wp->w_folds.ga_data + wp->w_folds.ga_len
@@ -1224,11 +1211,7 @@ static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, bool opening, bool re
// Change from level-dependent folding to manual.
if (use_level || fp->fd_flags == FD_LEVEL) {
use_level = true;
- if (level >= wp->w_p_fdl) {
- fp->fd_flags = FD_CLOSED;
- } else {
- fp->fd_flags = FD_OPEN;
- }
+ fp->fd_flags = level >= wp->w_p_fdl ? FD_CLOSED : FD_OPEN;
fp2 = (fold_T *)fp->fd_nested.ga_data;
for (int j = 0; j < fp->fd_nested.ga_len; j++) {
fp2[j].fd_flags = FD_LEVEL;
@@ -1353,6 +1336,9 @@ void deleteFoldRecurse(buf_T *bp, garray_T *gap)
// foldMarkAdjust() {{{2
/// Update line numbers of folds for inserted/deleted lines.
+///
+/// We are adjusting the folds in the range from line1 til line2,
+/// make sure that line2 does not get smaller than line1
void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, linenr_T amount,
linenr_T amount_after)
{
@@ -1361,6 +1347,9 @@ void foldMarkAdjust(win_T *wp, linenr_T line1, linenr_T line2, linenr_T amount,
if (amount == MAXLNUM && line2 >= line1 && line2 - line1 >= -amount_after) {
line2 = line1 - amount_after - 1;
}
+ if (line2 < line1) {
+ line2 = line1;
+ }
// If appending a line in Insert mode, it should be included in the fold
// just above the line.
if ((State & MODE_INSERT) && amount == 1 && line2 == MAXLNUM) {
@@ -1377,15 +1366,11 @@ static void foldMarkAdjustRecurse(win_T *wp, garray_T *gap, linenr_T line1, line
return;
}
- linenr_T top;
-
// In Insert mode an inserted line at the top of a fold is considered part
// of the fold, otherwise it isn't.
- if ((State & MODE_INSERT) && amount == 1 && line2 == MAXLNUM) {
- top = line1 + 1;
- } else {
- top = line1;
- }
+ linenr_T top = ((State & MODE_INSERT) && amount == 1 && line2 == MAXLNUM)
+ ? line1 + 1
+ : line1;
// Find the fold containing or just below "line1".
fold_T *fp;
@@ -1479,9 +1464,7 @@ static int getDeepestNestingRecurse(garray_T *gap)
fold_T *fp = (fold_T *)gap->ga_data;
for (int i = 0; i < gap->ga_len; i++) {
int level = getDeepestNestingRecurse(&fp[i].fd_nested) + 1;
- if (level > maxlevel) {
- maxlevel = level;
- }
+ maxlevel = MAX(maxlevel, level);
}
return maxlevel;
@@ -1597,7 +1580,6 @@ static void foldCreateMarkers(win_T *wp, pos_T start, pos_T end)
static void foldAddMarker(buf_T *buf, pos_T pos, const char *marker, size_t markerlen)
{
char *cms = buf->b_p_cms;
- char *newline;
char *p = strstr(buf->b_p_cms, "%s");
bool line_is_comment = false;
linenr_T lnum = pos.lnum;
@@ -1613,7 +1595,7 @@ static void foldAddMarker(buf_T *buf, pos_T pos, const char *marker, size_t mark
// Check if the line ends with an unclosed comment
skip_comment(line, false, false, &line_is_comment);
- newline = xmalloc(line_len + markerlen + strlen(cms) + 1);
+ char *newline = xmalloc(line_len + markerlen + strlen(cms) + 1);
STRCPY(newline, line);
// Append the marker to the end of the line
if (p == NULL || line_is_comment) {
@@ -1736,10 +1718,7 @@ char *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T foldinfo
// Set "v:folddashes" to a string of "level" dashes.
// Set "v:foldlevel" to "level".
- int level = foldinfo.fi_level;
- if (level > (int)sizeof(dashes) - 1) {
- level = (int)sizeof(dashes) - 1;
- }
+ int level = MIN(foldinfo.fi_level, (int)sizeof(dashes) - 1);
memset(dashes, '-', (size_t)level);
dashes[level] = NUL;
set_vim_var_string(VV_FOLDDASHES, dashes, -1);
@@ -1936,9 +1915,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
// When deleting lines at the end of the buffer "top" can be past the end
// of the buffer.
- if (top > wp->w_buffer->b_ml.ml_line_count) {
- top = wp->w_buffer->b_ml.ml_line_count;
- }
+ top = MIN(top, wp->w_buffer->b_ml.ml_line_count);
fline_T fline;
@@ -2046,9 +2023,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
if (fpn != NULL && current_fdl == fline.lvl) {
linenr_T fold_end_lnum = fold_start_lnum + fpn->fd_len;
- if (fold_end_lnum > bot) {
- bot = fold_end_lnum;
- }
+ bot = MAX(bot, fold_end_lnum);
}
}
@@ -2126,9 +2101,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot)
if (wp->w_redraw_top == 0 || wp->w_redraw_top > top) {
wp->w_redraw_top = top;
}
- if (wp->w_redraw_bot < end) {
- wp->w_redraw_bot = end;
- }
+ wp->w_redraw_bot = MAX(wp->w_redraw_bot, end);
}
invalid_top = 0;
@@ -2204,10 +2177,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
// and after the first line of the fold, set the level to zero to
// force the fold to end. Do the same when had_end is set: Previous
// line was marked as end of a fold.
- lvl = flp->lvl;
- if (lvl > MAX_LEVEL) {
- lvl = MAX_LEVEL;
- }
+ lvl = MIN(flp->lvl, MAX_LEVEL);
if (flp->lnum > firstlnum
&& (level > lvl - flp->start || level >= flp->had_end)) {
lvl = 0;
@@ -2262,12 +2232,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
while (!got_int) {
// set concat to 1 if it's allowed to concatenate this fold
// with a previous one that touches it.
- int concat;
- if (flp->start != 0 || flp->had_end <= MAX_LEVEL) {
- concat = 0;
- } else {
- concat = 1;
- }
+ int concat = (flp->start != 0 || flp->had_end <= MAX_LEVEL) ? 0 : 1;
// Find an existing fold to re-use. Preferably one that
// includes startlnum, otherwise one that ends just before
@@ -2423,9 +2388,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
if (lvl > level && fp != NULL) {
// There is a nested fold, handle it recursively.
// At least do one line (can happen when finish is true).
- if (bot < flp->lnum) {
- bot = flp->lnum;
- }
+ bot = MAX(bot, flp->lnum);
// Line numbers in the nested fold are relative to the start of
// this fold.
@@ -2555,9 +2518,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level,
// Need to redraw the lines we inspected, which might be further down than
// was asked for.
- if (bot < flp->lnum - 1) {
- bot = flp->lnum - 1;
- }
+ bot = MAX(bot, flp->lnum - 1);
return bot;
}
@@ -2897,17 +2858,11 @@ static void foldlevelIndent(fline_T *flp)
// depends on surrounding lines
if (*s == NUL || vim_strchr(flp->wp->w_p_fdi, (uint8_t)(*s)) != NULL) {
// first and last line can't be undefined, use level 0
- if (lnum == 1 || lnum == buf->b_ml.ml_line_count) {
- flp->lvl = 0;
- } else {
- flp->lvl = -1;
- }
+ flp->lvl = (lnum == 1 || lnum == buf->b_ml.ml_line_count) ? 0 : -1;
} else {
flp->lvl = get_indent_buf(buf, lnum) / get_sw_value(buf);
}
- if (flp->lvl > flp->wp->w_p_fdn) {
- flp->lvl = (int)MAX(0, flp->wp->w_p_fdn);
- }
+ flp->lvl = MIN(flp->lvl, (int)MAX(0, flp->wp->w_p_fdn));
}
// foldlevelDiff() {{{2
@@ -2915,11 +2870,7 @@ static void foldlevelIndent(fline_T *flp)
/// Doesn't use any caching.
static void foldlevelDiff(fline_T *flp)
{
- if (diff_infold(flp->wp, flp->lnum + flp->off)) {
- flp->lvl = 1;
- } else {
- flp->lvl = 0;
- }
+ flp->lvl = (diff_infold(flp->wp, flp->lnum + flp->off)) ? 1 : 0;
}
// foldlevelExpr() {{{2
@@ -3066,11 +3017,7 @@ static void foldlevelMarker(fline_T *flp)
if (n > 0) {
flp->lvl = n;
flp->lvl_next = n;
- if (n <= start_lvl) {
- flp->start = 1;
- } else {
- flp->start = n - start_lvl;
- }
+ flp->start = MAX(n - start_lvl, 1);
}
} else {
flp->lvl++;
@@ -3087,9 +3034,7 @@ static void foldlevelMarker(fline_T *flp)
flp->lvl = n;
flp->lvl_next = n - 1;
// never start a fold with an end marker
- if (flp->lvl_next > start_lvl) {
- flp->lvl_next = start_lvl;
- }
+ flp->lvl_next = MIN(flp->lvl_next, start_lvl);
}
} else {
flp->lvl_next--;
@@ -3100,9 +3045,7 @@ static void foldlevelMarker(fline_T *flp)
}
// The level can't go negative, must be missing a start marker.
- if (flp->lvl_next < 0) {
- flp->lvl_next = 0;
- }
+ flp->lvl_next = MAX(flp->lvl_next, 0);
}
// foldlevelSyntax() {{{2
@@ -3243,11 +3186,7 @@ static void foldclosed_both(typval_T *argvars, typval_T *rettv, bool end)
linenr_T first;
linenr_T last;
if (hasFoldingWin(curwin, lnum, &first, &last, false, NULL)) {
- if (end) {
- rettv->vval.v_number = (varnumber_T)last;
- } else {
- rettv->vval.v_number = (varnumber_T)first;
- }
+ rettv->vval.v_number = (varnumber_T)(end ? last : first);
return;
}
}
@@ -3314,7 +3253,7 @@ void f_foldtext(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
char *r = xmalloc(len);
snprintf(r, len, txt, dashes, count);
len = strlen(r);
- STRCAT(r, s);
+ strcat(r, s);
// remove 'foldmarker' and 'commentstring'
foldtext_cleanup(r + len);
rettv->vval.v_string = r;
@@ -3335,9 +3274,7 @@ void f_foldtextresult(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
entered = true;
linenr_T lnum = tv_get_lnum(argvars);
// Treat illegal types and illegal string values for {lnum} the same.
- if (lnum < 0) {
- lnum = 0;
- }
+ lnum = MAX(lnum, 0);
foldinfo_T info = fold_info(curwin, lnum);
if (info.fi_lines > 0) {