Merge pull request #13807 from spywhere/min-size-auto-sign

Auto sign column with minimum size support
This commit is contained in:
Matthieu Coudron
2021-01-23 22:46:29 +01:00
committed by GitHub
3 changed files with 147 additions and 3 deletions

View File

@@ -5536,6 +5536,12 @@ A jump table for the options with a short description can be found at |Q_op|.
"auto" only when there is a sign to display
"auto:[1-9]" resize to accommodate multiple signs up to the
given number (maximum 9), e.g. "auto:4"
"auto:[1-8]-[2-9]"
resize to accommodate multiple signs up to the
given maximum number (maximum 9) while keeping
at least the given minimum (maximum 8) fixed
space. The minimum number should always be less
than the maximum number, e.g. "auto:2-5"
"no" never
"yes" always
"yes:[1-9]" always, with fixed space for signs up to the given

View File

@@ -2913,7 +2913,7 @@ ambw_end:
#endif
} else if (varp == &curwin->w_p_scl) {
// 'signcolumn'
if (check_opt_strings(*varp, p_scl_values, false) != OK) {
if (check_signcolumn(*varp) != OK) {
errmsg = e_invarg;
}
// When changing the 'signcolumn' to or from 'number', recompute the
@@ -3232,6 +3232,34 @@ static int int_cmp(const void *a, const void *b)
return *(const int *)a - *(const int *)b;
}
/// Handle setting 'signcolumn' for value 'val'
///
/// @return OK when the value is valid, FAIL otherwise
int check_signcolumn(char_u *val)
{
// check for basic match
if (check_opt_strings(val, p_scl_values, false) == OK) {
return OK;
}
// check for 'auto:<NUMBER>-<NUMBER>'
if (STRLEN(val) == 8
&& !STRNCMP(val, "auto:", 5)
&& ascii_isdigit(val[5])
&& val[6] == '-'
&& ascii_isdigit(val[7])
) {
int min = val[5] - '0';
int max = val[7] - '0';
if (min < 1 || max < 2 || min > 8 || max > 9 || min >= max) {
return FAIL;
}
return OK;
}
return FAIL;
}
/// Handle setting 'colorcolumn' or 'textwidth' in window "wp".
///
/// @return error message, NULL if it's OK.
@@ -7095,7 +7123,7 @@ int csh_like_shell(void)
/// buffer signs and on user configuration.
int win_signcol_count(win_T *wp)
{
int maximum = 1, needed_signcols;
int minimum = 0, maximum = 1, needed_signcols;
const char *scl = (const char *)wp->w_p_scl;
// Note: It checks "no" or "number" in 'signcolumn' option
@@ -7119,9 +7147,14 @@ int win_signcol_count(win_T *wp)
if (!strncmp(scl, "auto:", 5)) {
// Variable depending on a configuration
maximum = scl[5] - '0';
// auto:<NUM>-<NUM>
if (strlen(scl) == 8 && *(scl + 6) == '-') {
minimum = maximum;
maximum = scl[7] - '0';
}
}
return MIN(maximum, needed_signcols);
return MAX(minimum, MIN(maximum, needed_signcols));
}
/// Get window or buffer local options

View File

@@ -266,6 +266,111 @@ describe('Signs', function()
]]}
end)
it('auto-resize sign column with minimum size (#13783)', function()
feed('ia<cr>b<cr>c<cr><esc>')
command('set number')
-- sign column should always accommodate at the minimum size
command('set signcolumn=auto:1-3')
screen:expect([[
{2: }{6: 1 }a |
{2: }{6: 2 }b |
{2: }{6: 3 }c |
{2: }{6: 4 }^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
-- should support up to 8 signs at minimum
command('set signcolumn=auto:8-9')
screen:expect([[
{2: }{6: 1 }a |
{2: }{6: 2 }b |
{2: }{6: 3 }c |
{2: }{6: 4 }^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
-- should keep the same sign size when signs are not exceeding
-- the minimum
command('set signcolumn=auto:2-5')
command('sign define pietSearch text=>> texthl=Search')
command('sign place 1 line=1 name=pietSearch buffer=1')
screen:expect([[
{1:>>}{2: }{6: 1 }a |
{2: }{6: 2 }b |
{2: }{6: 3 }c |
{2: }{6: 4 }^ |
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
{0:~ }|
|
]])
-- should resize itself when signs are exceeding minimum but
-- not over the maximum
command('sign place 2 line=1 name=pietSearch buffer=1')
command('sign place 3 line=1 name=pietSearch buffer=1')
command('sign place 4 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:~ }|
|
]])
-- should keep the column at maximum size when signs are
-- exceeding the maximum
command('sign place 5 line=1 name=pietSearch buffer=1')
command('sign place 6 line=1 name=pietSearch buffer=1')
command('sign place 7 line=1 name=pietSearch buffer=1')
command('sign place 8 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('ignores signs with no icon and text when calculting the signcolumn width', function()
feed('ia<cr>b<cr>c<cr><esc>')
command('set number')