diff options
author | Pavel Platto <hinidu@gmail.com> | 2014-05-20 11:41:49 +0300 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2014-05-28 10:42:06 -0400 |
commit | 7e3681c32e4fd1e19c01ffe7286ea29aafb2efc4 (patch) | |
tree | ed1d11dcde7c7eb64f9474ae1c8ef891757fd554 | |
parent | a01f7948bc28df20b5d96ed53f8d25b2db12a48d (diff) | |
download | rneovim-7e3681c32e4fd1e19c01ffe7286ea29aafb2efc4.tar.gz rneovim-7e3681c32e4fd1e19c01ffe7286ea29aafb2efc4.tar.bz2 rneovim-7e3681c32e4fd1e19c01ffe7286ea29aafb2efc4.zip |
Remove code duplication in get_cursor_rel_lnum
-rw-r--r-- | src/nvim/cursor.c | 57 |
1 files changed, 22 insertions, 35 deletions
diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index 308f09f264..8f8bc60510 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -259,47 +259,34 @@ int dec_cursor(void) return dec(&curwin->w_cursor); } -/* - * 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 - * can be visible, folded lines don't count. - */ -linenr_T get_cursor_rel_lnum( - win_T *wp, - linenr_T lnum /* line number to get the result for */ -) +/// 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 +/// can be visible, folded lines don't count. +/// +/// @param lnum line number to get the result for +linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T 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; - if (hasAnyFolding(wp)) { - if (lnum > cursor) { - while (lnum > cursor) { - (void)hasFoldingWin(wp, lnum, &lnum, NULL, 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; + // Loop until we reach to_line, skipping folds. + for (; from_line < to_line; from_line++, retval++) { + // If from_line is in a fold, set it to the last line of that fold. + hasFoldingWin(wp, from_line, NULL, &from_line, true, NULL); } - 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; } /* |