diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/buffer.c | 100 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 2 | ||||
-rw-r--r-- | src/nvim/sign_defs.h | 13 | ||||
-rw-r--r-- | src/nvim/testdir/test_signs.vim | 124 |
4 files changed, 167 insertions, 72 deletions
diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index da65682fbe..3ddd2a7163 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -64,6 +64,7 @@ #include "nvim/quickfix.h" #include "nvim/regexp.h" #include "nvim/screen.h" +#include "nvim/sign.h" #include "nvim/spell.h" #include "nvim/strings.h" #include "nvim/syntax.h" @@ -5255,6 +5256,70 @@ bool find_win_for_buf(buf_T *buf, win_T **wp, tabpage_T **tp) return false; } +static hashtab_T sg_table; // sign group (signgroup_T) hashtable + +/* + * A new sign in group 'groupname' is added. If the group is not present, + * create it. Otherwise reference the group. + */ +static signgroup_T * sign_group_ref(char_u *groupname) +{ + static int initialized = FALSE; + hash_T hash; + hashitem_T *hi; + signgroup_T *group; + + if (!initialized) { + initialized = TRUE; + hash_init(&sg_table); + } + + hash = hash_hash(groupname); + hi = hash_lookup(&sg_table, S_LEN(groupname), hash); + if (HASHITEM_EMPTY(hi)) + { + // new group + group = (signgroup_T *)xmalloc( + (unsigned)(sizeof(signgroup_T) + STRLEN(groupname))); + if (group == NULL) + return NULL; + STRCPY(group->sg_name, groupname); + group->refcount = 1; + hash_add_item(&sg_table, hi, group->sg_name, hash); + } + else + { + // existing group + group = HI2SG(hi); + group->refcount++; + } + + return group; +} + +/* + * A sign in group 'groupname' is removed. If all the signs in this group are + * removed, then remove the group. + */ +static void sign_group_unref(char_u *groupname) +{ + hashitem_T *hi; + signgroup_T *group; + + hi = hash_find(&sg_table, groupname); + if (!HASHITEM_EMPTY(hi)) + { + group = HI2SG(hi); + group->refcount--; + if (group->refcount == 0) + { + // All the signs in this group are removed + hash_remove(&sg_table, hi); + xfree(group); + } + } +} + /* * Insert a new sign into the signlist for buffer 'buf' between the 'prev' and * 'next' signs. @@ -5275,7 +5340,14 @@ static void insert_sign( newsign->lnum = lnum; newsign->typenr = typenr; if (group != NULL) - newsign->group = vim_strsave(group); + { + newsign->group = sign_group_ref(group); + if (newsign->group == NULL) + { + xfree(newsign); + return; + } + } else newsign->group = NULL; newsign->priority = prio; @@ -5431,7 +5503,7 @@ int sign_in_group(signlist_T *sign, char_u *group) return ((group != NULL && STRCMP(group, "*") == 0) || (group == NULL && sign->group == NULL) || (group != NULL && sign->group != NULL && - STRCMP(group, sign->group) == 0)); + STRCMP(group, sign->group->sg_name) == 0)); } /* @@ -5445,7 +5517,7 @@ dict_T * sign_get_info(signlist_T *sign) return NULL; } tv_dict_add_nr(d, S_LEN("id"), sign->id); - tv_dict_add_str(d, S_LEN("group"), (sign->group == NULL) ? (char_u *)"" : sign->group); + tv_dict_add_str(d, S_LEN("group"), (sign->group == NULL) ? (char_u *)"" : sign->group->sg_name); tv_dict_add_nr(d, S_LEN("lnum"), sign->lnum); tv_dict_add_str(d, S_LEN("name"), sign_typenr2name(sign->typenr)); tv_dict_add_nr(d, S_LEN("priority"), sign->priority); @@ -5459,7 +5531,7 @@ dict_T * sign_get_info(signlist_T *sign) void buf_addsign( buf_T *buf, // buffer to store sign in int id, // sign ID - char_u *group, // sign group + char_u *groupname, // sign group int prio, // sign priority linenr_T lnum, // line number which gets the mark int typenr // typenr of sign we are adding @@ -5472,19 +5544,19 @@ void buf_addsign( prev = NULL; FOR_ALL_SIGNS_IN_BUF(buf) { if (lnum == sign->lnum && id == sign->id && - sign_in_group(sign, group)) { + sign_in_group(sign, groupname)) { // Update an existing sign sign->typenr = typenr; return; } else if ((lnum == sign->lnum && id != sign->id) || (id < 0 && lnum < sign->lnum)) { - insert_sign_by_lnum_prio(buf, prev, id, group, prio, lnum, typenr); + insert_sign_by_lnum_prio(buf, prev, id, groupname, prio, lnum, typenr); return; } prev = sign; } - insert_sign_by_lnum_prio(buf, prev, id, group, prio, lnum, typenr); + insert_sign_by_lnum_prio(buf, prev, id, groupname, prio, lnum, typenr); // Having more than one sign with _the same type_ and on the _same line_ is // unwanted, let's prevent it. @@ -5604,7 +5676,8 @@ linenr_T buf_delsign( next->prev = sign->prev; } lnum = sign->lnum; - xfree(sign->group); + if (sign->group != NULL) + sign_group_unref(sign->group->sg_name); xfree(sign); // Check whether only one sign needs to be deleted if (group == NULL || (*group != '*' && id != 0)) @@ -5709,7 +5782,7 @@ int buf_findsign_id( /* * Delete signs in buffer "buf". */ -buf_delete_signs(buf_T *buf, char_u *group) +void buf_delete_signs(buf_T *buf, char_u *group) { signlist_T *sign; signlist_T **lastp; // pointer to pointer to current sign @@ -5730,7 +5803,9 @@ buf_delete_signs(buf_T *buf, char_u *group) if (next != NULL) { next->prev = sign->prev; } - xfree(sign->group); + if (sign->group != NULL) { + sign_group_unref(sign->group->sg_name); + } xfree(sign); } else { lastp = &sign->next; @@ -5775,11 +5850,14 @@ void sign_list_placed(buf_T *rbuf, char_u *sign_group) msg_putchar('\n'); } FOR_ALL_SIGNS_IN_BUF(buf) { + if (got_int) { + break; + } if (!sign_in_group(sign, sign_group)) { continue; } if (sign->group != NULL) { - vim_snprintf(group, BUFSIZ, " group=%s", sign->group); + vim_snprintf(group, BUFSIZ, " group=%s", sign->group->sg_name); } else { group[0] = '\0'; } diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 4a589ec6e5..82b16a721e 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -6072,7 +6072,7 @@ void ex_sign(exarg_T *eap) if (bufarg && buf == NULL) { EMSG2(_("E158: Invalid buffer name: %s"), arg); - } else if (id <= 0 && !(idx == SIGNCMD_UNPLACE && id == -2)) { + } else if (id <= 0 && idx == SIGNCMD_PLACE) { if ((group == NULL) && (lnum >= 0 || sign_name != NULL)) { EMSG(_(e_invarg)); } else { diff --git a/src/nvim/sign_defs.h b/src/nvim/sign_defs.h index 313fb330ed..e3028a80ba 100644 --- a/src/nvim/sign_defs.h +++ b/src/nvim/sign_defs.h @@ -5,6 +5,17 @@ // signs: line annotations +// Sign group +typedef struct signgroup_S +{ + uint16_t refcount; // number of signs in this group + char_u sg_name[1]; // sign group name +} signgroup_T; + +// Macros to get the sign group structure from the group name +#define SGN_KEY_OFF offsetof(signgroup_T, sg_name) +#define HI2SG(hi) ((signgroup_T *)((hi)->hi_key - SGN_KEY_OFF)) + typedef struct signlist signlist_T; struct signlist @@ -12,7 +23,7 @@ struct signlist int id; // unique identifier for each placed sign linenr_T lnum; // line number which has this sign int typenr; // typenr of sign - char_u *group; // sign group + signgroup_T *group; // sign group int priority; // priority for highlighting signlist_T *next; // next signlist entry signlist_T *prev; // previous entry -- for easy reordering diff --git a/src/nvim/testdir/test_signs.vim b/src/nvim/testdir/test_signs.vim index 5e2ac3fb94..a0acc54b5b 100644 --- a/src/nvim/testdir/test_signs.vim +++ b/src/nvim/testdir/test_signs.vim @@ -59,7 +59,7 @@ func Test_sign() redraw " Check that we can't change sign. - call assert_fails("exe 'sign place 40 name=Sign1 buffer=' . bufnr('%')", 'E885:') + call assert_fails("sign place 40 name=Sign1 buffer=" . bufnr('%'), 'E885:') " Check placed signs let a=execute('sign place') @@ -68,7 +68,7 @@ func Test_sign() " Unplace the sign and try jumping to it again should fail. sign unplace 41 1 - call assert_fails("exe 'sign jump 41 buffer=' . bufnr('%')", 'E157:') + call assert_fails("sign jump 41 buffer=" . bufnr('%'), 'E157:') call assert_equal('a', getline('.')) " Unplace sign on current line. @@ -132,17 +132,22 @@ func Test_sign() sign undefine Sign4 " Error cases - call assert_fails("exe 'sign place abc line=3 name=Sign1 buffer=' . bufnr('%')", 'E474:') - call assert_fails("exe 'sign unplace abc name=Sign1 buffer=' . bufnr('%')", 'E474:') - call assert_fails("exe 'sign place 1abc line=3 name=Sign1 buffer=' . bufnr('%')", 'E474:') - call assert_fails("exe 'sign unplace 2abc name=Sign1 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign place abc line=3 name=Sign1 buffer=" . + \ bufnr('%'), 'E474:') + call assert_fails("sign unplace abc name=Sign1 buffer=" . + \ bufnr('%'), 'E474:') + call assert_fails("sign place 1abc line=3 name=Sign1 buffer=" . + \ bufnr('%'), 'E474:') + call assert_fails("sign unplace 2abc name=Sign1 buffer=" . + \ bufnr('%'), 'E474:') call assert_fails("sign unplace 2 *", 'E474:') - call assert_fails("exe 'sign place 1 line=3 name=Sign1 buffer=' . bufnr('%') a", 'E488:') - call assert_fails("exe 'sign place name=Sign1 buffer=' . bufnr('%')", 'E474:') - call assert_fails("exe 'sign place line=10 buffer=' . bufnr('%')", 'E474:') - call assert_fails("exe 'sign unplace 2 line=10 buffer=' . bufnr('%')", 'E474:') - call assert_fails("exe 'sign unplace 2 name=Sign1 buffer=' . bufnr('%')", 'E474:') - call assert_fails("exe 'sign place 2 line=3 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign place 1 line=3 name=Sign1 buffer=" . + \ bufnr('%') . " a", 'E488:') + call assert_fails("sign place name=Sign1 buffer=" . bufnr('%'), 'E474:') + call assert_fails("sign place line=10 buffer=" . bufnr('%'), 'E474:') + call assert_fails("sign unplace 2 line=10 buffer=" . bufnr('%'), 'E474:') + call assert_fails("sign unplace 2 name=Sign1 buffer=" . bufnr('%'), 'E474:') + call assert_fails("sign place 2 line=3 buffer=" . bufnr('%'), 'E474:') call assert_fails("sign place 2", 'E474:') call assert_fails("sign place abc", 'E474:') call assert_fails("sign place 5 line=3", 'E474:') @@ -157,7 +162,8 @@ func Test_sign() sign undefine Sign1 sign undefine Sign2 sign undefine Sign3 - call assert_fails("exe 'sign place 41 line=3 name=Sign1 buffer=' . bufnr('%')", 'E155:') + call assert_fails("sign place 41 line=3 name=Sign1 buffer=" . + \ bufnr('%'), 'E155:') endfunc " Undefining placed sign is not recommended. @@ -236,33 +242,33 @@ func Test_sign_invalid_commands() call assert_fails('sign place 1 buffer=999', 'E158:') call assert_fails('sign define Sign2 text=', 'E239:') " Non-numeric identifier for :sign place - call assert_fails("exe 'sign place abc line=3 name=Sign1 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign place abc line=3 name=Sign1 buffer=" . bufnr('%'), 'E474:') " Non-numeric identifier for :sign unplace - call assert_fails("exe 'sign unplace abc name=Sign1 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign unplace abc name=Sign1 buffer=" . bufnr('%'), 'E474:') " Number followed by an alphabet as sign identifier for :sign place - call assert_fails("exe 'sign place 1abc line=3 name=Sign1 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign place 1abc line=3 name=Sign1 buffer=" . bufnr('%'), 'E474:') " Number followed by an alphabet as sign identifier for :sign unplace - call assert_fails("exe 'sign unplace 2abc name=Sign1 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign unplace 2abc name=Sign1 buffer=" . bufnr('%'), 'E474:') " Sign identifier and '*' for :sign unplace call assert_fails("sign unplace 2 *", 'E474:') " Trailing characters after buffer number for :sign place - call assert_fails("exe 'sign place 1 line=3 name=Sign1 buffer=' . bufnr('%') . 'xxx'", 'E488:') + call assert_fails("sign place 1 line=3 name=Sign1 buffer=" . bufnr('%') . 'xxx', 'E488:') " Trailing characters after buffer number for :sign unplace - call assert_fails("exe 'sign unplace 1 buffer=' . bufnr('%') . 'xxx'", 'E488:') - call assert_fails("exe 'sign unplace * buffer=' . bufnr('%') . 'xxx'", 'E488:') + call assert_fails("sign unplace 1 buffer=" . bufnr('%') . 'xxx', 'E488:') + call assert_fails("sign unplace * buffer=" . bufnr('%') . 'xxx', 'E488:') call assert_fails("sign unplace 1 xxx", 'E474:') call assert_fails("sign unplace * xxx", 'E474:') call assert_fails("sign unplace xxx", 'E474:') " Placing a sign without line number - call assert_fails("exe 'sign place name=Sign1 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign place name=Sign1 buffer=" . bufnr('%'), 'E474:') " Placing a sign without sign name - call assert_fails("exe 'sign place line=10 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign place line=10 buffer=" . bufnr('%'), 'E474:') " Unplacing a sign with line number - call assert_fails("exe 'sign unplace 2 line=10 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign unplace 2 line=10 buffer=" . bufnr('%'), 'E474:') " Unplacing a sign with sign name - call assert_fails("exe 'sign unplace 2 name=Sign1 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign unplace 2 name=Sign1 buffer=" . bufnr('%'), 'E474:') " Placing a sign without sign name - call assert_fails("exe 'sign place 2 line=3 buffer=' . bufnr('%')", 'E474:') + call assert_fails("sign place 2 line=3 buffer=" . bufnr('%'), 'E474:') " Placing a sign with only sign identifier call assert_fails("sign place 2", 'E474:') " Placing a sign with only a name @@ -574,24 +580,24 @@ func Test_sign_group() call sign_unplace('*') " Test for :sign command and groups - exe 'sign place 5 line=10 name=sign1 file=' . fname - exe 'sign place 5 group=g1 line=10 name=sign1 file=' . fname - exe 'sign place 5 group=g2 line=10 name=sign1 file=' . fname + sign place 5 line=10 name=sign1 file=Xsign + sign place 5 group=g1 line=10 name=sign1 file=Xsign + sign place 5 group=g2 line=10 name=sign1 file=Xsign " Test for :sign place group={group} file={fname} - let a = execute('sign place file=' . fname) + let a = execute('sign place file=Xsign') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n line=10 id=5 name=sign1 priority=10\n", a) - let a = execute('sign place group=g2 file=' . fname) + let a = execute('sign place group=g2 file=Xsign') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n line=10 id=5 group=g2 name=sign1 priority=10\n", a) - let a = execute('sign place group=* file=' . fname) + let a = execute('sign place group=* file=Xsign') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n" . \ " line=10 id=5 group=g2 name=sign1 priority=10\n" . \ " line=10 id=5 group=g1 name=sign1 priority=10\n" . \ " line=10 id=5 name=sign1 priority=10\n", a) - let a = execute('sign place group=xyz file=' . fname) + let a = execute('sign place group=xyz file=Xsign') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n", a) call sign_unplace('*') @@ -624,22 +630,22 @@ func Test_sign_group() \ " line=12 id=5 group=g2 name=sign1 priority=10\n", a) " Test for :sign unplace - exe 'sign unplace 5 group=g2 file=' . fname + sign unplace 5 group=g2 file=Xsign call assert_equal([], sign_getplaced(bnum, {'group' : 'g2'})[0].signs) exe 'sign unplace 5 group=g1 buffer=' . bnum call assert_equal([], sign_getplaced(bnum, {'group' : 'g1'})[0].signs) - exe 'sign unplace 5 group=xy file=' . fname + sign unplace 5 group=xy file=Xsign call assert_equal(1, len(sign_getplaced(bnum, {'group' : '*'})[0].signs)) " Test for removing all the signs. Place the signs again for this test - exe 'sign place 5 group=g1 line=11 name=sign1 file=' . fname - exe 'sign place 5 group=g2 line=12 name=sign1 file=' . fname - exe 'sign place 6 line=20 name=sign1 file=' . fname - exe 'sign place 6 group=g1 line=21 name=sign1 file=' . fname - exe 'sign place 6 group=g2 line=22 name=sign1 file=' . fname - exe 'sign unplace 5 group=* file=' . fname + sign place 5 group=g1 line=11 name=sign1 file=Xsign + sign place 5 group=g2 line=12 name=sign1 file=Xsign + sign place 6 line=20 name=sign1 file=Xsign + sign place 6 group=g1 line=21 name=sign1 file=Xsign + sign place 6 group=g2 line=22 name=sign1 file=Xsign + sign unplace 5 group=* file=Xsign let a = execute('sign place group=*') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n" . \ " line=20 id=6 name=sign1 priority=10\n" . @@ -647,17 +653,17 @@ func Test_sign_group() \ " line=22 id=6 group=g2 name=sign1 priority=10\n", a) " Remove all the signs from the global group - exe 'sign unplace * file=' . fname + sign unplace * file=Xsign let a = execute('sign place group=*') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n" . \ " line=21 id=6 group=g1 name=sign1 priority=10\n" . \ " line=22 id=6 group=g2 name=sign1 priority=10\n", a) " Remove all the signs from a particular group - exe 'sign place 5 line=10 name=sign1 file=' . fname - exe 'sign place 5 group=g1 line=11 name=sign1 file=' . fname - exe 'sign place 5 group=g2 line=12 name=sign1 file=' . fname - exe 'sign unplace * group=g1 file=' . fname + sign place 5 line=10 name=sign1 file=Xsign + sign place 5 group=g1 line=11 name=sign1 file=Xsign + sign place 5 group=g2 line=12 name=sign1 file=Xsign + sign unplace * group=g1 file=Xsign let a = execute('sign place group=*') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n" . \ " line=10 id=5 name=sign1 priority=10\n" . @@ -665,26 +671,26 @@ func Test_sign_group() \ " line=22 id=6 group=g2 name=sign1 priority=10\n", a) " Remove all the signs from all the groups in a file - exe 'sign place 5 group=g1 line=11 name=sign1 file=' . fname - exe 'sign place 6 line=20 name=sign1 file=' . fname - exe 'sign place 6 group=g1 line=21 name=sign1 file=' . fname - exe 'sign unplace * group=* file=' . fname + sign place 5 group=g1 line=11 name=sign1 file=Xsign + sign place 6 line=20 name=sign1 file=Xsign + sign place 6 group=g1 line=21 name=sign1 file=Xsign + sign unplace * group=* file=Xsign let a = execute('sign place group=*') call assert_equal("\n--- Signs ---\n", a) " Remove a particular sign id in a group from all the files - exe 'sign place 5 group=g1 line=11 name=sign1 file=' . fname + sign place 5 group=g1 line=11 name=sign1 file=Xsign sign unplace 5 group=g1 let a = execute('sign place group=*') call assert_equal("\n--- Signs ---\n", a) " Remove a particular sign id in all the groups from all the files - exe 'sign place 5 line=10 name=sign1 file=' . fname - exe 'sign place 5 group=g1 line=11 name=sign1 file=' . fname - exe 'sign place 5 group=g2 line=12 name=sign1 file=' . fname - exe 'sign place 6 line=20 name=sign1 file=' . fname - exe 'sign place 6 group=g1 line=21 name=sign1 file=' . fname - exe 'sign place 6 group=g2 line=22 name=sign1 file=' . fname + sign place 5 line=10 name=sign1 file=Xsign + sign place 5 group=g1 line=11 name=sign1 file=Xsign + sign place 5 group=g2 line=12 name=sign1 file=Xsign + sign place 6 line=20 name=sign1 file=Xsign + sign place 6 group=g1 line=21 name=sign1 file=Xsign + sign place 6 group=g2 line=22 name=sign1 file=Xsign sign unplace 5 group=* let a = execute('sign place group=*') call assert_equal("\n--- Signs ---\nSigns for Xsign:\n" . @@ -693,14 +699,14 @@ func Test_sign_group() \ " line=22 id=6 group=g2 name=sign1 priority=10\n", a) " Remove all the signs from all the groups in all the files - exe 'sign place 5 line=10 name=sign1 file=' . fname - exe 'sign place 5 group=g1 line=11 name=sign1 file=' . fname + sign place 5 line=10 name=sign1 file=Xsign + sign place 5 group=g1 line=11 name=sign1 file=Xsign sign unplace * group=* let a = execute('sign place group=*') call assert_equal("\n--- Signs ---\n", a) " Error cases - call assert_fails("exe 'sign place 3 group= name=sign1 buffer=' . bnum", 'E474:') + call assert_fails("sign place 3 group= name=sign1 buffer=" . bnum, 'E474:') call delete("Xsign") call sign_unplace('*') |