mirror of
https://github.com/neovim/neovim.git
synced 2025-09-11 13:58:18 +00:00
Remove code duplication in get_cursor_rel_lnum
This commit is contained in:

committed by
Justin M. Keyes

parent
a01f7948bc
commit
7e3681c32e
@@ -259,47 +259,34 @@ int dec_cursor(void)
|
|||||||
return dec(&curwin->w_cursor);
|
return dec(&curwin->w_cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Get the line number relative to the current cursor position, i.e. the
|
||||||
* Get the line number relative to the current cursor position, i.e. the
|
/// difference between line number and cursor position. Only look for lines that
|
||||||
* difference between line number and cursor position. Only look for lines that
|
/// can be visible, folded lines don't count.
|
||||||
* can be visible, folded lines don't count.
|
///
|
||||||
*/
|
/// @param lnum line number to get the result for
|
||||||
linenr_T get_cursor_rel_lnum(
|
linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum)
|
||||||
win_T *wp,
|
|
||||||
linenr_T lnum /* line number to get the result for */
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
linenr_T cursor = wp->w_cursor.lnum;
|
linenr_T cursor = wp->w_cursor.lnum;
|
||||||
|
if (lnum == cursor || !hasAnyFolding(wp)) {
|
||||||
|
return lnum - cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
linenr_T from_line = lnum < cursor ? lnum : cursor;
|
||||||
|
linenr_T to_line = lnum > cursor ? lnum : cursor;
|
||||||
linenr_T retval = 0;
|
linenr_T retval = 0;
|
||||||
|
|
||||||
if (hasAnyFolding(wp)) {
|
// Loop until we reach to_line, skipping folds.
|
||||||
if (lnum > cursor) {
|
for (; from_line < to_line; from_line++, retval++) {
|
||||||
while (lnum > cursor) {
|
// If from_line is in a fold, set it to the last line of that fold.
|
||||||
(void)hasFoldingWin(wp, lnum, &lnum, NULL, true, NULL);
|
hasFoldingWin(wp, from_line, NULL, &from_line, true, NULL);
|
||||||
/* if lnum and cursor are in the same fold,
|
|
||||||
* now lnum <= cursor */
|
|
||||||
if (lnum > cursor)
|
|
||||||
retval++;
|
|
||||||
lnum--;
|
|
||||||
}
|
|
||||||
} else if (lnum < cursor) {
|
|
||||||
while (lnum < cursor) {
|
|
||||||
(void)hasFoldingWin(wp, lnum, NULL, &lnum, true, NULL);
|
|
||||||
/* if lnum and cursor are in the same fold,
|
|
||||||
* now lnum >= cursor */
|
|
||||||
if (lnum < cursor)
|
|
||||||
retval--;
|
|
||||||
lnum++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* else if (lnum == cursor)
|
|
||||||
* retval = 0;
|
|
||||||
*/
|
|
||||||
} else {
|
|
||||||
retval = lnum - cursor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
// If to_line is in a closed fold, the line count is off by +1. Correct it.
|
||||||
|
if (from_line > to_line) {
|
||||||
|
retval--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (lnum < cursor) ? -retval : retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Reference in New Issue
Block a user