Merge pull request #17666 from zeertzjq/api-statusline-fillchar-relax

feat(api): relax statusline fillchar width check
This commit is contained in:
zeertzjq 2022-03-10 10:39:14 +08:00 committed by GitHub
commit d6fe06b0a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 13 deletions

View File

@ -2241,7 +2241,7 @@ Array nvim_get_mark(String name, Dictionary opts, Error *err)
/// - winid: (number) |window-ID| of the window to use as context for statusline. /// - winid: (number) |window-ID| of the window to use as context for statusline.
/// - maxwidth: (number) Maximum width of statusline. /// - maxwidth: (number) Maximum width of statusline.
/// - fillchar: (string) Character to fill blank spaces in the statusline (see /// - fillchar: (string) Character to fill blank spaces in the statusline (see
/// 'fillchars'). /// 'fillchars'). Treated as single-width even if it isn't.
/// - highlights: (boolean) Return highlight information. /// - highlights: (boolean) Return highlight information.
/// - use_tabline: (boolean) Evaluate tabline instead of statusline. When |TRUE|, {winid} /// - use_tabline: (boolean) Evaluate tabline instead of statusline. When |TRUE|, {winid}
/// is ignored. /// is ignored.
@ -2277,11 +2277,12 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error *
if (HAS_KEY(opts->fillchar)) { if (HAS_KEY(opts->fillchar)) {
if (opts->fillchar.type != kObjectTypeString || opts->fillchar.data.string.size == 0 if (opts->fillchar.type != kObjectTypeString || opts->fillchar.data.string.size == 0
|| char2cells(fillchar = utf_ptr2char((char_u *)opts->fillchar.data.string.data)) != 1 || ((size_t)utf_ptr2len((char_u *)opts->fillchar.data.string.data)
|| (size_t)utf_char2len(fillchar) != opts->fillchar.data.string.size) { != opts->fillchar.data.string.size)) {
api_set_error(err, kErrorTypeValidation, "fillchar must be a single-width character"); api_set_error(err, kErrorTypeValidation, "fillchar must be a single character");
return result; return result;
} }
fillchar = utf_ptr2char((char_u *)opts->fillchar.data.string.data);
} }
if (HAS_KEY(opts->highlights)) { if (HAS_KEY(opts->highlights)) {

View File

@ -2621,24 +2621,24 @@ describe('API', function()
eq({ str = 'a━━━b', width = 5 }, eq({ str = 'a━━━b', width = 5 },
meths.eval_statusline('a%=b', { fillchar = '', maxwidth = 5 })) meths.eval_statusline('a%=b', { fillchar = '', maxwidth = 5 }))
end) end)
it('rejects double-width fillchar', function() it('treats double-width fillchar as single-width', function()
eq('fillchar must be a single-width character', eq({ str = 'a哦哦哦b', width = 5 },
pcall_err(meths.eval_statusline, '', { fillchar = '' })) meths.eval_statusline('a%=b', { fillchar = '', maxwidth = 5 }))
end) end)
it('rejects control character fillchar', function() it('treats control character fillchar as single-width', function()
eq('fillchar must be a single-width character', eq({ str = 'a\031\031\031b', width = 5 },
pcall_err(meths.eval_statusline, '', { fillchar = '\a' })) meths.eval_statusline('a%=b', { fillchar = '\031', maxwidth = 5 }))
end) end)
it('rejects multiple-character fillchar', function() it('rejects multiple-character fillchar', function()
eq('fillchar must be a single-width character', eq('fillchar must be a single character',
pcall_err(meths.eval_statusline, '', { fillchar = 'aa' })) pcall_err(meths.eval_statusline, '', { fillchar = 'aa' }))
end) end)
it('rejects empty string fillchar', function() it('rejects empty string fillchar', function()
eq('fillchar must be a single-width character', eq('fillchar must be a single character',
pcall_err(meths.eval_statusline, '', { fillchar = '' })) pcall_err(meths.eval_statusline, '', { fillchar = '' }))
end) end)
it('rejects non-string fillchar', function() it('rejects non-string fillchar', function()
eq('fillchar must be a single-width character', eq('fillchar must be a single character',
pcall_err(meths.eval_statusline, '', { fillchar = 1 })) pcall_err(meths.eval_statusline, '', { fillchar = 1 }))
end) end)
describe('highlight parsing', function() describe('highlight parsing', function()