feat(sign):Allow signs to be 0 width (#13290)

Adds support for signs to be 0 cells wide. If all signs of the same
group have no width, the signcolumn will not be rendered for that group.
This commit is contained in:
Lukas Reineke
2020-12-25 01:33:52 +09:00
committed by GitHub
parent 6c28bddfad
commit 88ae03bcdb
5 changed files with 133 additions and 31 deletions

View File

@@ -5479,7 +5479,9 @@ int buf_signcols(buf_T *buf)
curline = sign->lnum; curline = sign->lnum;
linesum = 0; linesum = 0;
} }
linesum++; if (sign->has_text_or_icon) {
linesum++;
}
} }
if (linesum > buf->b_signcols_max) { if (linesum > buf->b_signcols_max) {
buf->b_signcols_max = linesum; buf->b_signcols_max = linesum;

View File

@@ -173,13 +173,15 @@ static void insert_sign(
const char_u *group, // sign group; NULL for global group const char_u *group, // sign group; NULL for global group
int prio, // sign priority int prio, // sign priority
linenr_T lnum, // line number which gets the mark linenr_T lnum, // line number which gets the mark
int typenr // typenr of sign we are adding int typenr, // typenr of sign we are adding
bool has_text_or_icon // sign has text or icon
) )
{ {
signlist_T *newsign = xmalloc(sizeof(signlist_T)); signlist_T *newsign = xmalloc(sizeof(signlist_T));
newsign->id = id; newsign->id = id;
newsign->lnum = lnum; newsign->lnum = lnum;
newsign->typenr = typenr; newsign->typenr = typenr;
newsign->has_text_or_icon = has_text_or_icon;
if (group != NULL) { if (group != NULL) {
newsign->group = sign_group_ref(group); newsign->group = sign_group_ref(group);
} else { } else {
@@ -210,13 +212,14 @@ static void insert_sign(
/// Insert a new sign sorted by line number and sign priority. /// Insert a new sign sorted by line number and sign priority.
static void insert_sign_by_lnum_prio( static void insert_sign_by_lnum_prio(
buf_T *buf, // buffer to store sign in buf_T *buf, // buffer to store sign in
signlist_T *prev, // previous sign entry signlist_T *prev, // previous sign entry
int id, // sign ID int id, // sign ID
const char_u *group, // sign group; NULL for global group const char_u *group, // sign group; NULL for global group
int prio, // sign priority int prio, // sign priority
linenr_T lnum, // line number which gets the mark linenr_T lnum, // line number which gets the mark
int typenr // typenr of sign we are adding int typenr, // typenr of sign we are adding
bool has_text_or_icon // sign has text or icon
) )
{ {
signlist_T *sign; signlist_T *sign;
@@ -234,7 +237,7 @@ static void insert_sign_by_lnum_prio(
sign = prev->next; sign = prev->next;
} }
insert_sign(buf, prev, sign, id, group, prio, lnum, typenr); insert_sign(buf, prev, sign, id, group, prio, lnum, typenr, has_text_or_icon);
} }
/// Get the name of a sign by its typenr. /// Get the name of a sign by its typenr.
@@ -342,12 +345,13 @@ static void sign_sort_by_prio_on_line(buf_T *buf, signlist_T *sign)
/// Add the sign into the signlist. Find the right spot to do it though. /// Add the sign into the signlist. Find the right spot to do it though.
void buf_addsign( void buf_addsign(
buf_T *buf, // buffer to store sign in buf_T *buf, // buffer to store sign in
int id, // sign ID int id, // sign ID
const char_u *groupname, // sign group const char_u *groupname, // sign group
int prio, // sign priority int prio, // sign priority
linenr_T lnum, // line number which gets the mark linenr_T lnum, // line number which gets the mark
int typenr // typenr of sign we are adding int typenr, // typenr of sign we are adding
bool has_text_or_icon // sign has text or icon
) )
{ {
signlist_T *sign; // a sign in the signlist signlist_T *sign; // a sign in the signlist
@@ -363,13 +367,29 @@ void buf_addsign(
sign_sort_by_prio_on_line(buf, sign); sign_sort_by_prio_on_line(buf, sign);
return; return;
} else if (lnum < sign->lnum) { } else if (lnum < sign->lnum) {
insert_sign_by_lnum_prio(buf, prev, id, groupname, prio, lnum, typenr); insert_sign_by_lnum_prio(
buf,
prev,
id,
groupname,
prio,
lnum,
typenr,
has_text_or_icon);
return; return;
} }
prev = sign; prev = sign;
} }
insert_sign_by_lnum_prio(buf, prev, id, groupname, prio, lnum, typenr); insert_sign_by_lnum_prio(
buf,
prev,
id,
groupname,
prio,
lnum,
typenr,
has_text_or_icon);
} }
// For an existing, placed sign "markId" change the type to "typenr". // For an existing, placed sign "markId" change the type to "typenr".
@@ -786,11 +806,15 @@ static int sign_define_init_text(sign_T *sp, char_u *text)
} }
cells += utf_ptr2cells(s); cells += utf_ptr2cells(s);
} }
// Currently must be one or two display cells // Currently must be empty, one or two display cells
if (s != endp || cells < 1 || cells > 2) { if (s != endp || cells > 2) {
EMSG2(_("E239: Invalid sign text: %s"), text); EMSG2(_("E239: Invalid sign text: %s"), text);
return FAIL; return FAIL;
} }
if (cells < 1) {
sp->sn_text = NULL;
return OK;
}
xfree(sp->sn_text); xfree(sp->sn_text);
// Allocate one byte more if we need to pad up // Allocate one byte more if we need to pad up
@@ -939,7 +963,15 @@ int sign_place(
if (lnum > 0) { if (lnum > 0) {
// ":sign place {id} line={lnum} name={name} file={fname}": // ":sign place {id} line={lnum} name={name} file={fname}":
// place a sign // place a sign
buf_addsign(buf, *sign_id, sign_group, prio, lnum, sp->sn_typenr); bool has_text_or_icon = sp->sn_text != NULL || sp->sn_icon != NULL;
buf_addsign(
buf,
*sign_id,
sign_group,
prio,
lnum,
sp->sn_typenr,
has_text_or_icon);
} else { } else {
// ":sign place {id} file={fname}": change sign type // ":sign place {id} file={fname}": change sign type
lnum = buf_change_sign_type(buf, *sign_id, sign_group, sp->sn_typenr); lnum = buf_change_sign_type(buf, *sign_id, sign_group, sp->sn_typenr);

View File

@@ -1,6 +1,7 @@
#ifndef NVIM_SIGN_DEFS_H #ifndef NVIM_SIGN_DEFS_H
#define NVIM_SIGN_DEFS_H #define NVIM_SIGN_DEFS_H
#include <stdbool.h>
#include "nvim/pos.h" #include "nvim/pos.h"
#include "nvim/types.h" #include "nvim/types.h"
@@ -22,13 +23,14 @@ typedef struct signlist signlist_T;
struct signlist struct signlist
{ {
int id; // unique identifier for each placed sign int id; // unique identifier for each placed sign
linenr_T lnum; // line number which has this sign linenr_T lnum; // line number which has this sign
int typenr; // typenr of sign int typenr; // typenr of sign
signgroup_T *group; // sign group bool has_text_or_icon; // has text or icon
int priority; // priority for highlighting signgroup_T *group; // sign group
signlist_T *next; // next signlist entry int priority; // priority for highlighting
signlist_T *prev; // previous entry -- for easy reordering signlist_T *next; // next signlist entry
signlist_T *prev; // previous entry -- for easy reordering
}; };
// Default sign priority for highlighting // Default sign priority for highlighting

View File

@@ -122,9 +122,9 @@ func Test_sign()
call assert_fails("sign define Sign4 text=a\e linehl=Comment", 'E239:') call assert_fails("sign define Sign4 text=a\e linehl=Comment", 'E239:')
call assert_fails("sign define Sign4 text=\ea linehl=Comment", 'E239:') call assert_fails("sign define Sign4 text=\ea linehl=Comment", 'E239:')
" Only 1 or 2 character text is allowed " Only 0, 1 or 2 character text is allowed
call assert_fails("sign define Sign4 text=abc linehl=Comment", 'E239:') call assert_fails("sign define Sign4 text=abc linehl=Comment", 'E239:')
call assert_fails("sign define Sign4 text= linehl=Comment", 'E239:') " call assert_fails("sign define Sign4 text= linehl=Comment", 'E239:')
call assert_fails("sign define Sign4 text=\\ ab linehl=Comment", 'E239:') call assert_fails("sign define Sign4 text=\\ ab linehl=Comment", 'E239:')
" define sign with whitespace " define sign with whitespace
@@ -306,7 +306,7 @@ func Test_sign_invalid_commands()
call assert_fails('sign jump 1 name=', 'E474:') call assert_fails('sign jump 1 name=', 'E474:')
call assert_fails('sign jump 1 name=Sign1', 'E474:') call assert_fails('sign jump 1 name=Sign1', 'E474:')
call assert_fails('sign jump 1 line=100', '474:') call assert_fails('sign jump 1 line=100', '474:')
call assert_fails('sign define Sign2 text=', 'E239:') " call assert_fails('sign define Sign2 text=', 'E239:')
" Non-numeric identifier for :sign place " Non-numeric identifier for :sign place
call assert_fails("sign place abc line=3 name=Sign1 buffer=" . bufnr(''), call assert_fails("sign place abc line=3 name=Sign1 buffer=" . bufnr(''),
\ 'E474:') \ 'E474:')
@@ -415,7 +415,7 @@ func Test_sign_funcs()
" Tests for invalid arguments to sign_define() " Tests for invalid arguments to sign_define()
call assert_fails('call sign_define("sign4", {"text" : "===>"})', 'E239:') call assert_fails('call sign_define("sign4", {"text" : "===>"})', 'E239:')
call assert_fails('call sign_define("sign5", {"text" : ""})', 'E239:') " call assert_fails('call sign_define("sign5", {"text" : ""})', 'E239:')
call assert_fails('call sign_define([])', 'E730:') call assert_fails('call sign_define([])', 'E730:')
call assert_fails('call sign_define("sign6", [])', 'E715:') call assert_fails('call sign_define("sign6", [])', 'E715:')

View File

@@ -76,6 +76,28 @@ describe('Signs', function()
]]) ]])
end) end)
it('allows signs with no text', function()
feed('ia<cr>b<cr><esc>')
command('sign define piet1 text= texthl=Search')
command('sign place 1 line=1 name=piet1 buffer=1')
screen:expect([[
a |
b |
^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
end)
it('can be called right after :split', function() it('can be called right after :split', function()
feed('ia<cr>b<cr>c<cr><esc>gg') feed('ia<cr>b<cr>c<cr><esc>gg')
-- This used to cause a crash due to :sign using a special redraw -- This used to cause a crash due to :sign using a special redraw
@@ -244,6 +266,50 @@ describe('Signs', function()
]]} ]]}
end) end)
it('ignores signs with no icon and text when calculting the signcolumn width', function()
feed('ia<cr>b<cr>c<cr><esc>')
command('set number')
command('set signcolumn=auto:2')
command('sign define pietSearch text=>> texthl=Search')
command('sign define pietError text= texthl=Error')
command('sign place 2 line=1 name=pietError buffer=1')
-- no signcolumn with only empty sign
screen:expect([[
{6: 1 }a |
{6: 2 }b |
{6: 3 }c |
{6: 4 }^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
-- single column with 1 sign with text and one sign without
command('sign place 1 line=1 name=pietSearch buffer=1')
screen:expect([[
{1:>>}{6: 1 }a |
{2: }{6: 2 }b |
{2: }{6: 3 }c |
{2: }{6: ^4 } |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
end)
it('can have 32bit sign IDs', function() it('can have 32bit sign IDs', function()
command('sign define piet text=>> texthl=Search') command('sign define piet text=>> texthl=Search')
command('sign place 100000 line=1 name=piet buffer=1') command('sign place 100000 line=1 name=piet buffer=1')