Merge pull request #9183 from bfredl/offset2

api: make nvim_buf_get_offset independent on platform option
This commit is contained in:
Björn Linse 2018-11-02 08:36:38 +01:00 committed by GitHub
commit 48398d61e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 39 deletions

View File

@ -493,11 +493,10 @@ end:
/// Return the byte offset for a line. /// Return the byte offset for a line.
// //
/// This includes the end-of-line character, depending on the 'fileformat' /// The first line returns 0. UTF-8 bytes are counted, and EOL counts as one
/// option for the current buffer. The first line returns 0. UTF-8 encoding is /// byte. 'fileformat' and 'fileencoding' are ignored. Sending in the index
/// used, 'fileencoding' is ignored. Sending in the index just below the last /// just below the last line gives the total byte count for the entire buffer.
/// line gives the total byte count for the entire file. A final newline is /// A final EOL is included if it would be written, see 'eol'.
/// included if it would be written, see 'eol'.
/// ///
/// Unlike |line2byte()|, throws error for out-of-bounds indexing. /// Unlike |line2byte()|, throws error for out-of-bounds indexing.
/// Returns -1 for unloaded buffer. /// Returns -1 for unloaded buffer.
@ -506,7 +505,7 @@ end:
/// @param index Line index /// @param index Line index
/// @param[out] err Error details, if any /// @param[out] err Error details, if any
/// @return Integer Byte offset /// @return Integer Byte offset
Integer nvim_buf_get_offset_for_line(Buffer buffer, Integer index, Error *err) Integer nvim_buf_get_offset(Buffer buffer, Integer index, Error *err)
FUNC_API_SINCE(5) FUNC_API_SINCE(5)
{ {
buf_T *buf = find_buffer_by_handle(buffer, err); buf_T *buf = find_buffer_by_handle(buffer, err);
@ -524,7 +523,7 @@ Integer nvim_buf_get_offset_for_line(Buffer buffer, Integer index, Error *err)
return 0; return 0;
} }
return ml_find_line_or_offset(buf, (int)index+1, NULL); return ml_find_line_or_offset(buf, (int)index+1, NULL, true);
} }
/// Gets a buffer-scoped (b:) variable. /// Gets a buffer-scoped (b:) variable.

View File

@ -3734,7 +3734,8 @@ int build_stl_str_hl(
FALLTHROUGH; FALLTHROUGH;
case STL_OFFSET: case STL_OFFSET:
{ {
long l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL); long l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL,
false);
num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ? num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
0L : l + 1 + (!(State & INSERT) && empty_line ? 0L : l + 1 + (!(State & INSERT) && empty_line ?
0 : (int)wp->w_cursor.col); 0 : (int)wp->w_cursor.col);

View File

@ -7232,7 +7232,7 @@ static void f_byte2line(typval_T *argvars, typval_T *rettv, FunPtr fptr)
rettv->vval.v_number = -1; rettv->vval.v_number = -1;
} else { } else {
rettv->vval.v_number = (varnumber_T)ml_find_line_or_offset(curbuf, 0, rettv->vval.v_number = (varnumber_T)ml_find_line_or_offset(curbuf, 0,
&boff); &boff, false);
} }
} }
@ -12084,7 +12084,7 @@ static void f_line2byte(typval_T *argvars, typval_T *rettv, FunPtr fptr)
if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) { if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1) {
rettv->vval.v_number = -1; rettv->vval.v_number = -1;
} else { } else {
rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL); rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL, false);
} }
if (rettv->vval.v_number >= 0) { if (rettv->vval.v_number >= 0) {
rettv->vval.v_number++; rettv->vval.v_number++;

View File

@ -3850,13 +3850,17 @@ static void ml_updatechunk(buf_T *buf, linenr_T line, long len, int updtype)
ml_upd_lastcurix = curix; ml_upd_lastcurix = curix;
} }
/* /// Find offset for line or line with offset.
* Find offset for line or line with offset. ///
* Find line with offset if "lnum" is 0; return remaining offset in offp /// @param buf buffer to use
* Find offset of line if "lnum" > 0 /// @param lnum if > 0, find offset of lnum, store offset in offp
* return -1 if information is not available /// if == 0, return line with offset *offp
*/ /// @param offp Location where offset of line is stored, or to read offset to
long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp) /// use to find line. In the later case, store remaining offset.
/// @param no_ff ignore 'fileformat' option, always use one byte for NL.
///
/// @return -1 if information is not available
long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp, bool no_ff)
{ {
linenr_T curline; linenr_T curline;
int curix; int curix;
@ -3869,7 +3873,7 @@ long ml_find_line_or_offset(buf_T *buf, linenr_T lnum, long *offp)
int text_end; int text_end;
long offset; long offset;
int len; int len;
int ffdos = (get_fileformat(buf) == EOL_DOS); int ffdos = !no_ff && (get_fileformat(buf) == EOL_DOS);
int extra = 0; int extra = 0;
/* take care of cached line first */ /* take care of cached line first */
@ -3983,7 +3987,7 @@ void goto_byte(long cnt)
if (boff) { if (boff) {
boff--; boff--;
} }
lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff); lnum = ml_find_line_or_offset(curbuf, (linenr_T)0, &boff, false);
if (lnum < 1) { // past the end if (lnum < 1) { // past the end
curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
curwin->w_curswant = MAXCOL; curwin->w_curswant = MAXCOL;

View File

@ -301,37 +301,40 @@ describe('api/buf', function()
end) end)
end) end)
describe('get_offset_for_line', function() describe('get_offset', function()
local get_offset_for_line = curbufmeths.get_offset_for_line local get_offset = curbufmeths.get_offset
it('works', function() it('works', function()
curbufmeths.set_lines(0,-1,true,{'Some','exa\000mple', '', 'text'}) curbufmeths.set_lines(0,-1,true,{'Some\r','exa\000mple', '', 'buf\rfer', 'text'})
eq(4, curbufmeths.line_count()) eq(5, curbufmeths.line_count())
eq(0, get_offset_for_line(0)) eq(0, get_offset(0))
eq(5, get_offset_for_line(1)) eq(6, get_offset(1))
eq(14, get_offset_for_line(2)) eq(15, get_offset(2))
eq(15, get_offset_for_line(3)) eq(16, get_offset(3))
eq(20, get_offset_for_line(4)) eq(24, get_offset(4))
eq({false,'Index out of bounds'}, meth_pcall(get_offset_for_line, 5)) eq(29, get_offset(5))
eq({false,'Index out of bounds'}, meth_pcall(get_offset_for_line, -1)) eq({false,'Index out of bounds'}, meth_pcall(get_offset, 6))
eq({false,'Index out of bounds'}, meth_pcall(get_offset, -1))
curbufmeths.set_option('eol', false) curbufmeths.set_option('eol', false)
curbufmeths.set_option('fixeol', false) curbufmeths.set_option('fixeol', false)
eq(19, get_offset_for_line(4)) eq(28, get_offset(5))
-- fileformat is ignored
curbufmeths.set_option('fileformat', 'dos') curbufmeths.set_option('fileformat', 'dos')
eq(0, get_offset_for_line(0)) eq(0, get_offset(0))
eq(6, get_offset_for_line(1)) eq(6, get_offset(1))
eq(16, get_offset_for_line(2)) eq(15, get_offset(2))
eq(18, get_offset_for_line(3)) eq(16, get_offset(3))
eq(22, get_offset_for_line(4)) eq(24, get_offset(4))
eq(28, get_offset(5))
curbufmeths.set_option('eol', true) curbufmeths.set_option('eol', true)
eq(24, get_offset_for_line(4)) eq(29, get_offset(5))
command("set hidden") command("set hidden")
command("enew") command("enew")
eq(6, bufmeths.get_offset_for_line(1,1)) eq(6, bufmeths.get_offset(1,1))
command("bunload! 1") command("bunload! 1")
eq(-1, bufmeths.get_offset_for_line(1,1)) eq(-1, bufmeths.get_offset(1,1))
end) end)
end) end)