vim-patch:8.2.4702: C++ scope labels are hard-coded

Problem:    C++ scope labels are hard-coded.
Solution:   Add 'cinscopedecls' to define the labels. (Tom Praschan,
            closes vim/vim#10109)
3506cf34c1
This commit is contained in:
Tom Praschan
2022-04-07 16:14:02 +02:00
parent 233014f92b
commit 45f62464d3
11 changed files with 100 additions and 20 deletions

View File

@ -38,11 +38,12 @@ is not a C compiler: it does not recognize all syntax. One requirement is
that toplevel functions have a '{' in the first column. Otherwise they are
easily confused with declarations.
These four options control C program indenting:
These five options control C program indenting:
'cindent' Enables Vim to perform C program indenting automatically.
'cinkeys' Specifies which keys trigger reindenting in insert mode.
'cinoptions' Sets your preferred indent style.
'cinwords' Defines keywords that start an extra indent in the next line.
'cinscopedecls' Defines strings that are recognized as a C++ scope declaration.
If 'lisp' is not on and 'equalprg' is empty, the "=" operator indents using
Vim's built-in algorithm rather than calling an external program.
@ -289,8 +290,9 @@ The examples below assume a 'shiftwidth' of 4.
<
*cino-g*
gN Place C++ scope declarations N characters from the indent of the
block they are in. (default 'shiftwidth'). A scope declaration
can be "public:", "protected:" or "private:".
block they are in. (default 'shiftwidth'). By default, a scope
declaration is "public:", "protected:" or "private:". This can
be adjusted with the 'cinscopedecls' option.
cino= cino=g0 >
{ {

View File

@ -1288,10 +1288,20 @@ A jump table for the options with a short description can be found at |Q_op|.
matter, include the keyword both the uppercase and lowercase:
"if,If,IF".
*'clipboard'* *'cb'*
*'cinscopedecls'* *'cinsd'*
'cinscopedecls' 'cinsd' string (default "public,protected,private")
local to buffer
{not available when compiled without the |+cindent|
feature}
Keywords that are interpreted as a C++ scope declaration by |cino-g|.
Useful e.g. for working with the Qt framework that defines additional
scope declarations "signals", "public slots" and "private slots": >
set cinscopedecls+=signals,public\ slots,private\ slots
< *'clipboard'* *'cb'*
'clipboard' 'cb' string (default "")
global
This option is a list of comma separated names.
This option is a list of comma-separated names.
These names are recognized:
*clipboard-unnamed*

View File

@ -638,6 +638,7 @@ Short explanation of each option: *option-list*
'cinkeys' 'cink' keys that trigger indent when 'cindent' is set
'cinoptions' 'cino' how to do indenting when 'cindent' is set
'cinwords' 'cinw' words where 'si' and 'cin' add an indent
'cinscopedecls' 'cinsd' words that are recognized by 'cino-g'
'clipboard' 'cb' use the clipboard as the unnamed register
'cmdheight' 'ch' number of lines to use for the command-line
'cmdwinheight' 'cwh' height of the command-line window

View File

@ -870,6 +870,9 @@ if has("cindent")
call append("$", "cinwords\tlist of words that cause more C-indent")
call append("$", "\t(local to buffer)")
call <SID>OptionL("cinw")
call append("$", "cinscopedecls\tlist of scope declaration names used by cino-g")
call append("$", "\t(local to buffer)")
call <SID>OptionL("cinsd")
call append("$", "indentexpr\texpression used to obtain the indent of a line")
call append("$", "\t(local to buffer)")
call <SID>OptionL("inde")

View File

@ -1938,6 +1938,7 @@ void free_buf_options(buf_T *buf, int free_p_ff)
clear_string_option(&buf->b_p_cink);
clear_string_option(&buf->b_p_cino);
clear_string_option(&buf->b_p_cinw);
clear_string_option(&buf->b_p_cinsd);
clear_string_option(&buf->b_p_cpt);
clear_string_option(&buf->b_p_cfu);
clear_string_option(&buf->b_p_ofu);

View File

@ -696,6 +696,7 @@ struct file_buffer {
char_u *b_p_cino; ///< 'cinoptions'
char_u *b_p_cink; ///< 'cinkeys'
char_u *b_p_cinw; ///< 'cinwords'
char_u *b_p_cinsd; ///< 'cinscopedecls'
char_u *b_p_com; ///< 'comments'
char_u *b_p_cms; ///< 'commentstring'
char_u *b_p_cpt; ///< 'complete'

View File

@ -512,24 +512,27 @@ static int cin_isdefault(const char_u *s)
&& s[1] != ':';
}
/*
* Recognize a "public/private/protected" scope declaration label.
*/
bool cin_isscopedecl(const char_u *s)
/// Recognize a scope declaration label set in 'cinscopedecls'.
bool cin_isscopedecl(const char_u *p)
{
int i;
const char_u *s = cin_skipcomment(p);
s = cin_skipcomment(s);
if (STRNCMP(s, "public", 6) == 0) {
i = 6;
} else if (STRNCMP(s, "protected", 9) == 0) {
i = 9;
} else if (STRNCMP(s, "private", 7) == 0) {
i = 7;
} else {
return false;
const size_t cinsd_len = STRLEN(curbuf->b_p_cinsd) + 1;
char_u *cinsd_buf = xmalloc(cinsd_len);
for (char_u *cinsd = curbuf->b_p_cinsd; *cinsd; ) {
const size_t len = copy_option_part(&cinsd, cinsd_buf, cinsd_len, ",");
if (STRNCMP(s, cinsd_buf, len) == 0) {
const char_u *skip = cin_skipcomment(s + len);
if (*skip == ':' && skip[1] != ':') {
return true;
}
}
}
return *(s = cin_skipcomment(s + i)) == ':' && s[1] != ':';
xfree(cinsd_buf);
return false;
}
// Maximum number of lines to search back for a "namespace" line.

View File

@ -133,6 +133,7 @@ static int p_cin;
static char_u *p_cink;
static char_u *p_cino;
static char_u *p_cinw;
static char_u *p_cinsd;
static char_u *p_com;
static char_u *p_cms;
static char_u *p_cpt;
@ -2060,6 +2061,7 @@ void check_buf_options(buf_T *buf)
parse_cino(buf);
check_string_option(&buf->b_p_ft);
check_string_option(&buf->b_p_cinw);
check_string_option(&buf->b_p_cinsd);
check_string_option(&buf->b_p_cpt);
check_string_option(&buf->b_p_cfu);
check_string_option(&buf->b_p_ofu);
@ -6058,6 +6060,8 @@ static char_u *get_varp(vimoption_T *p)
return (char_u *)&(curbuf->b_p_cink);
case PV_CINO:
return (char_u *)&(curbuf->b_p_cino);
case PV_CINSD:
return (char_u *)&(curbuf->b_p_cinsd);
case PV_CINW:
return (char_u *)&(curbuf->b_p_cinw);
case PV_COM:
@ -6505,6 +6509,8 @@ void buf_copy_options(buf_T *buf, int flags)
COPY_OPT_SCTX(buf, BV_CINK);
buf->b_p_cino = vim_strsave(p_cino);
COPY_OPT_SCTX(buf, BV_CINO);
buf->b_p_cinsd = vim_strsave(p_cinsd);
COPY_OPT_SCTX(buf, BV_CINSD);
// Don't copy 'filetype', it must be detected
buf->b_p_ft = empty_option;
buf->b_p_pi = p_pi;

View File

@ -773,6 +773,7 @@ enum {
BV_CINK,
BV_CINO,
BV_CINW,
BV_CINSD,
BV_CM,
BV_CMS,
BV_COM,

View File

@ -350,6 +350,15 @@ return {
varname='p_cinw',
defaults={if_true="if,else,while,do,for,switch"}
},
{
full_name='cinscopedecls', abbreviation='cinsd',
short_desc=N_("words that are recognized by 'cino-g'"),
type='string', list='onecomma', scope={'buffer'},
deny_duplicates=true,
alloced=true,
varname='p_cinsd',
defaults={if_true="public,protected,private"}
},
{
full_name='clipboard', abbreviation='cb',
short_desc=N_("use the clipboard as the unnamed register"),

View File

@ -5311,6 +5311,49 @@ func Test_cindent_case()
bwipe!
endfunc
func Test_cindent_scopedecls()
new
setl cindent ts=4 sw=4
setl cino=g0
setl cinsd+=public\ slots,signals
let code =<< trim [CODE]
class Foo
{
public:
virtual void foo() = 0;
public slots:
void onBar();
signals:
void baz();
private:
int x;
};
[CODE]
call append(0, code)
normal gg
normal ]]=][
let expected =<< trim [CODE]
class Foo
{
public:
virtual void foo() = 0;
public slots:
void onBar();
signals:
void baz();
private:
int x;
};
[CODE]
call assert_equal(expected, getline(1, '$'))
enew! | close
endfunc
func Test_cindent_pragma()
new
setl cindent ts=4 sw=4