ext_cmdline: add support for highlighting

This commit is contained in:
Björn Linse 2017-08-16 12:19:29 +02:00
parent 5ad591ef2d
commit 22402fb99d
3 changed files with 114 additions and 11 deletions

View File

@ -703,6 +703,23 @@ String cstr_to_string(const char *str)
}; };
} }
/// Copies buffer to an allocated String.
/// The resulting string is also NUL-terminated, to facilitate interoperating
/// with code using C strings.
///
/// @param buf the buffer to copy
/// @param size length of the buffer
/// @return the resulting String, if the input string was NULL, an
/// empty String is returned
String cbuf_to_string(const char *buf, size_t size)
FUNC_ATTR_NONNULL_ALL
{
return (String) {
.data = xmemdupz(buf, size),
.size = size
};
}
/// Creates a String using the given C string. Unlike /// Creates a String using the given C string. Unlike
/// cstr_to_string this function DOES NOT copy the C string. /// cstr_to_string this function DOES NOT copy the C string.
/// ///

View File

@ -2372,6 +2372,7 @@ static bool color_cmdline(const CmdlineInfo *const colored_ccline,
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{ {
bool printed_errmsg = false; bool printed_errmsg = false;
#define PRINT_ERRMSG(...) \ #define PRINT_ERRMSG(...) \
do { \ do { \
msg_putchar('\n'); \ msg_putchar('\n'); \
@ -2405,7 +2406,7 @@ static bool color_cmdline(const CmdlineInfo *const colored_ccline,
static unsigned prev_prompt_id = UINT_MAX; static unsigned prev_prompt_id = UINT_MAX;
static int prev_prompt_errors = 0; static int prev_prompt_errors = 0;
Callback color_cb = { .type = kCallbackNone }; Callback color_cb = CALLBACK_NONE;
bool can_free_cb = false; bool can_free_cb = false;
TryState tstate; TryState tstate;
Error err = ERROR_INIT; Error err = ERROR_INIT;
@ -2722,10 +2723,30 @@ draw_cmdline_no_arabicshape:
void ui_ext_cmdline_show(void) void ui_ext_cmdline_show(void)
{ {
Array content = ARRAY_DICT_INIT; Array content = ARRAY_DICT_INIT;
Array text = ARRAY_DICT_INIT; if (kv_size(last_ccline_colors.colors)) {
ADD(text, STRING_OBJ(cstr_to_string("Normal"))); for (size_t i = 0; i < kv_size(last_ccline_colors.colors); i++) {
ADD(text, STRING_OBJ(cstr_to_string((char *)(ccline.cmdbuff)))); CmdlineColorChunk chunk = kv_A(last_ccline_colors.colors, i);
ADD(content, ARRAY_OBJ(text)); Array item = ARRAY_DICT_INIT;
if (chunk.attr) {
attrentry_T *aep = syn_cterm_attr2entry(chunk.attr);
// TODO(bfredl): this desicion could be delayed by making attr_code a
// recognized type
HlAttrs rgb_attrs = attrentry2hlattrs(aep, true);
ADD(item, DICTIONARY_OBJ(hlattrs2dict(rgb_attrs)));
} else {
ADD(item, DICTIONARY_OBJ((Dictionary)ARRAY_DICT_INIT));
}
ADD(item, STRING_OBJ(cbuf_to_string((char *)ccline.cmdbuff + chunk.start,
chunk.end-chunk.start)));
ADD(content, ARRAY_OBJ(item));
}
} else {
Array item = ARRAY_DICT_INIT;
ADD(item, DICTIONARY_OBJ((Dictionary)ARRAY_DICT_INIT));
ADD(item, STRING_OBJ(cstr_to_string((char *)(ccline.cmdbuff))));
ADD(content, ARRAY_OBJ(item));
}
ui_call_cmdline_show(content, ccline.cmdpos, ui_call_cmdline_show(content, ccline.cmdpos,
cchar_to_string((char)ccline.cmdfirstc), cchar_to_string((char)ccline.cmdfirstc),
cstr_to_string((char *)(ccline.cmdprompt)), cstr_to_string((char *)(ccline.cmdprompt)),

View File

@ -1,6 +1,7 @@
local helpers = require('test.functional.helpers')(after_each) local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen') local Screen = require('test.functional.ui.screen')
local clear, feed, eq = helpers.clear, helpers.feed, helpers.eq local clear, feed, eq = helpers.clear, helpers.feed, helpers.eq
local source = helpers.source
if helpers.pending_win32(pending) then return end if helpers.pending_win32(pending) then return end
@ -36,6 +37,22 @@ describe('External command line completion', function()
screen:detach() screen:detach()
end) end)
function expect_cmdline(expected)
local attr_ids = screen._default_attr_ids
local attr_ignore = screen._default_attr_ignore
local actual = ''
for _, chunk in ipairs(content or {}) do
local attrs, text = chunk[1], chunk[2]
if screen:_equal_attrs(attrs, {}) then
actual = actual..text
else
local attr_id = screen:_get_attr_id(attr_ids, attr_ignore, attrs)
actual = actual..'{' .. attr_id .. ':' .. text .. '}'
end
end
eq(expected, actual)
end
describe("'cmdline'", function() describe("'cmdline'", function()
it(':sign', function() it(':sign', function()
feed(':') feed(':')
@ -58,7 +75,7 @@ describe('External command line completion', function()
~ | ~ |
| |
]], nil, nil, function() ]], nil, nil, function()
eq({{'Normal', 'sign'}}, content) eq({{{}, 'sign'}}, content)
eq(4, pos) eq(4, pos)
end) end)
@ -70,7 +87,7 @@ describe('External command line completion', function()
~ | ~ |
| |
]], nil, nil, function() ]], nil, nil, function()
eq({{'Normal', 'sign'}}, content) eq({{{}, 'sign'}}, content)
eq(true, shown) eq(true, shown)
eq(3, pos) eq(3, pos)
end) end)
@ -83,7 +100,7 @@ describe('External command line completion', function()
~ | ~ |
| |
]], nil, nil, function() ]], nil, nil, function()
eq({{'Normal', 'sin'}}, content) eq({{{}, 'sin'}}, content)
eq(true, shown) eq(true, shown)
eq(2, pos) eq(2, pos)
end) end)
@ -109,7 +126,7 @@ describe('External command line completion', function()
]], nil, nil, function() ]], nil, nil, function()
eq(true, shown) eq(true, shown)
eq("input", prompt) eq("input", prompt)
eq({{'Normal', 'default'}}, content) eq({{{}, 'default'}}, content)
end) end)
feed('<cr>') feed('<cr>')
@ -132,7 +149,7 @@ describe('External command line completion', function()
~ | ~ |
| |
]], nil, nil, function() ]], nil, nil, function()
eq({{'Normal', '1+2'}}, content) eq({{{}, '1+2'}}, content)
eq("\"", char) eq("\"", char)
eq(1, shift) eq(1, shift)
eq(2, level) eq(2, level)
@ -146,7 +163,7 @@ describe('External command line completion', function()
~ | ~ |
| |
]], nil, nil, function() ]], nil, nil, function()
eq({{'Normal', '3'}}, content) eq({{{}, '3'}}, content)
eq(2, current_hide_level) eq(2, current_hide_level)
eq(1, level) eq(1, level)
end) end)
@ -210,4 +227,52 @@ describe('External command line completion', function()
end) end)
end) end)
it('works with highlighted cmdline', function()
source([[
highlight RBP1 guibg=Red
highlight RBP2 guibg=Yellow
highlight RBP3 guibg=Green
highlight RBP4 guibg=Blue
let g:NUM_LVLS = 4
function RainBowParens(cmdline)
let ret = []
let i = 0
let lvl = 0
while i < len(a:cmdline)
if a:cmdline[i] is# '('
call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
let lvl += 1
elseif a:cmdline[i] is# ')'
let lvl -= 1
call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)])
endif
let i += 1
endwhile
return ret
endfunction
map <f5> :let x = input({'prompt':'>','highlight':'RainBowParens'})<cr>
"map <f5> :let x = input({'prompt':'>'})<cr>
]])
screen:set_default_attr_ids({
RBP1={background = Screen.colors.Red},
RBP2={background = Screen.colors.Yellow},
RBP3={background = Screen.colors.Green},
RBP4={background = Screen.colors.Blue},
EOB={bold = true, foreground = Screen.colors.Blue1},
ERR={foreground = Screen.colors.Grey100, background = Screen.colors.Red},
SK={foreground = Screen.colors.Blue},
PE={bold = true, foreground = Screen.colors.SeaGreen4}
})
feed('<f5>(a(b)a)')
screen:expect([[
^ |
{EOB:~ }|
{EOB:~ }|
{EOB:~ }|
|
]], nil, nil, function()
expect_cmdline('{RBP1:(}a{RBP2:(}b{RBP2:)}a{RBP1:)}')
end)
end)
end) end)