Skip to content

Commit

Permalink
refactor: move legacy signs to extmark
Browse files Browse the repository at this point in the history
  • Loading branch information
luukvbaal committed Oct 19, 2023
1 parent bbc74d5 commit d6a1166
Show file tree
Hide file tree
Showing 18 changed files with 272 additions and 788 deletions.
4 changes: 2 additions & 2 deletions src/nvim/api/extmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Dictionary nvim_get_namespaces(void)
return retval;
}

const char *describe_ns(NS ns_id)
const char *describe_ns(NS ns_id, const char *unknown)
{
String name;
handle_T id;
Expand All @@ -93,7 +93,7 @@ const char *describe_ns(NS ns_id)
return name.data;
}
})
return "(UNKNOWN PLUGIN)";
return unknown;
}

// Is the Namespace in use?
Expand Down
3 changes: 1 addition & 2 deletions src/nvim/api/vim.c
Original file line number Diff line number Diff line change
Expand Up @@ -2200,8 +2200,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Error *
HlPriId cul = { 0 };
HlPriId num = { 0 };
linenr_T lnum = statuscol_lnum;
int num_signs = buf_get_signattrs(wp->w_buffer, lnum, sattrs, &num, &line, &cul);
decor_redraw_signs(wp->w_buffer, lnum - 1, &num_signs, sattrs, &num, &line, &cul);
decor_get_signs(wp->w_buffer, lnum - 1, sattrs, &num, &line, &cul);
wp->w_scwidth = win_signcol_count(wp);

statuscol.sattrs = sattrs;
Expand Down
111 changes: 33 additions & 78 deletions src/nvim/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,6 @@ static void free_buffer_stuff(buf_T *buf, int free_flags)
buf_init_changedtick(buf);
}
uc_clear(&buf->b_ucmds); // clear local user commands
buf_delete_signs(buf, "*"); // delete any signs
extmark_free_all(buf); // delete any extmarks
map_clear_mode(buf, MAP_ALL_MODES, true, false); // clear local mappings
map_clear_mode(buf, MAP_ALL_MODES, true, true); // clear local abbrevs
Expand Down Expand Up @@ -4046,55 +4045,12 @@ char *buf_spname(buf_T *buf)

static int buf_signcols_inner(buf_T *buf, int maximum)
{
sign_entry_T *sign; // a sign in the sign list
int signcols = 0;
int linesum = 0;
linenr_T curline = 0;

buf->b_signcols.sentinel = 0;

FOR_ALL_SIGNS_IN_BUF(buf, sign) {
if (sign->se_lnum > curline) {
// Counted all signs, now add extmark signs
if (curline > 0) {
linesum += decor_signcols(buf, &decor_state, (int)curline - 1, (int)curline - 1,
maximum - linesum);
}
curline = sign->se_lnum;
if (linesum > signcols) {
signcols = linesum;
buf->b_signcols.sentinel = curline;
if (signcols >= maximum) {
return maximum;
}
}
linesum = 0;
}
if (sign->se_has_text_or_icon) {
linesum++;
}
}

if (curline > 0) {
linesum += decor_signcols(buf, &decor_state, (int)curline - 1, (int)curline - 1,
maximum - linesum);
}
if (linesum > signcols) {
signcols = linesum;
if (signcols >= maximum) {
return maximum;
}
}
int signcols = decor_signcols(buf, &decor_state, 0, (int)buf->b_ml.ml_line_count - 1, maximum);

// Check extmarks between signs
linesum = decor_signcols(buf, &decor_state, 0, (int)buf->b_ml.ml_line_count - 1, maximum);

if (linesum > signcols) {
signcols = linesum;
buf->b_signcols.sentinel = curline;
if (signcols >= maximum) {
return maximum;
}
if (signcols >= maximum) {
return maximum;
}

return signcols;
Expand Down Expand Up @@ -4129,45 +4085,44 @@ void buf_signcols_del_check(buf_T *buf, linenr_T line1, linenr_T line2)
///
/// @param buf buffer to check
/// @param added sign being added
void buf_signcols_add_check(buf_T *buf, sign_entry_T *added)
void buf_signcols_add_check(buf_T *buf)
{
if (!buf->b_signcols.valid) {
return;
}

if (!added || !buf->b_signcols.sentinel) {
if (!buf->b_signcols.sentinel) {
buf->b_signcols.valid = false;
return;
}

if (added->se_lnum == buf->b_signcols.sentinel) {
if (buf->b_signcols.size == buf->b_signcols.max) {
buf->b_signcols.max++;
}
buf->b_signcols.size++;
redraw_buf_later(buf, UPD_NOT_VALID);
return;
}

sign_entry_T *s;

// Get first sign for added lnum
for (s = added; s->se_prev && s->se_lnum == s->se_prev->se_lnum; s = s->se_prev) {}

// Count signs for lnum
int linesum = 1;
for (; s->se_next && s->se_lnum == s->se_next->se_lnum; s = s->se_next) {
linesum++;
}
linesum += decor_signcols(buf, &decor_state, (int)s->se_lnum - 1, (int)s->se_lnum - 1,
SIGN_SHOW_MAX - linesum);

if (linesum > buf->b_signcols.size) {
buf->b_signcols.size = linesum;
buf->b_signcols.max = linesum;
buf->b_signcols.sentinel = added->se_lnum;
redraw_buf_later(buf, UPD_NOT_VALID);
}
// if (added->se_lnum == buf->b_signcols.sentinel) {
// if (buf->b_signcols.size == buf->b_signcols.max) {
// buf->b_signcols.max++;
// }
// buf->b_signcols.size++;
// redraw_buf_later(buf, UPD_NOT_VALID);
// return;
// }
//
// sign_entry_T *s;
//
// // Get first sign for added lnum
// for (s = added; s->se_prev && s->se_lnum == s->se_prev->se_lnum; s = s->se_prev) {}
//
// // Count signs for lnum
// int linesum = 1;
// for (; s->se_next && s->se_lnum == s->se_next->se_lnum; s = s->se_next) {
// linesum++;
// }
// linesum += decor_signcols(buf, &decor_state, (int)s->se_lnum - 1, (int)s->se_lnum - 1,
// SIGN_SHOW_MAX - linesum);
//
// if (linesum > buf->b_signcols.size) {
// buf->b_signcols.size = linesum;
// buf->b_signcols.max = linesum;
// buf->b_signcols.sentinel = added->se_lnum;
// redraw_buf_later(buf, UPD_NOT_VALID);
// }
}

int buf_signcols(buf_T *buf, int maximum)
Expand Down
1 change: 0 additions & 1 deletion src/nvim/buffer_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@ struct file_buffer {
// normally points to this, but some windows
// may use a different synblock_T.

sign_entry_T *b_signlist; // list of placed signs
struct {
int size; // last calculated number of sign columns
bool valid; // calculated sign columns is valid
Expand Down
30 changes: 21 additions & 9 deletions src/nvim/decoration.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void decor_clear(Decoration *decor)
}
kv_destroy(decor->virt_lines);
xfree(decor->sign_text);
xfree(decor->sign_name);
}

void decor_free(Decoration *decor)
Expand Down Expand Up @@ -408,19 +409,26 @@ int decor_redraw_col(win_T *wp, int col, int win_col, bool hidden, DecorState *s
return attr;
}

void decor_redraw_signs(buf_T *buf, int row, int *num_signs, SignTextAttrs sattrs[],
HlPriId *num_id, HlPriId *line_id, HlPriId *cul_id)
/// Return the attributes of all the signs placed on 'row' in buffer
/// 'buf'. Used when refreshing the screen. Returns the number of signs.
/// @param buf Buffer in which to search
/// @param row Row in which to search
/// @param sattrs Output array for attrs
/// @return Number of signs of which attrs were found
int decor_get_signs(buf_T *buf, int row, SignTextAttrs sattrs[], HlPriId *num_id, HlPriId *line_id,
HlPriId *cul_id)
{
if (!buf->b_signs) {
return;
return 0;
}

int num_signs = 0;
MarkTreeIter itr[1] = { 0 };
marktree_itr_get(buf->b_marktree, row, 0, itr);

// TODO(bfredl): integrate with main decor loop.
if (!marktree_itr_get_overlap(buf->b_marktree, row, 0, itr)) {
return;
return 0;
}

MTPair pair;
Expand All @@ -435,7 +443,7 @@ void decor_redraw_signs(buf_T *buf, int row, int *num_signs, SignTextAttrs sattr
continue;
}

decor_to_sign(decor, num_signs, sattrs, num_id, line_id, cul_id);
decor_to_sign(decor, &num_signs, sattrs, num_id, line_id, cul_id);
}

while (true) {
Expand All @@ -454,20 +462,23 @@ void decor_redraw_signs(buf_T *buf, int row, int *num_signs, SignTextAttrs sattr
goto next_mark;
}

decor_to_sign(decor, num_signs, sattrs, num_id, line_id, cul_id);
decor_to_sign(decor, &num_signs, sattrs, num_id, line_id, cul_id);

next_mark:
marktree_itr_next(buf->b_marktree, itr);
}

return num_signs;
}

static void decor_to_sign(Decoration *decor, int *num_signs, SignTextAttrs sattrs[],
HlPriId *num_id, HlPriId *line_id, HlPriId *cul_id)
{
if (decor->sign_text) {
int j;
for (j = (*num_signs); j > 0; j--) {
if (sattrs[j - 1].priority >= decor->priority) {
for (j = *num_signs; j > 0; j--) {
if (sattrs[j - 1].priority >= decor->priority
&& sattrs[j - 1].add_idx >= decor->sign_add_idx) {
break;
}
if (j < SIGN_SHOW_MAX) {
Expand All @@ -478,7 +489,8 @@ static void decor_to_sign(Decoration *decor, int *num_signs, SignTextAttrs sattr
sattrs[j] = (SignTextAttrs) {
.text = decor->sign_text,
.hl_id = decor->sign_hl_id,
.priority = decor->priority
.priority = decor->priority,
.add_idx = decor->sign_add_idx
};
(*num_signs)++;
}
Expand Down
4 changes: 3 additions & 1 deletion src/nvim/decoration.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ struct Decoration {
int col; // fixed col value, like win_col
int virt_text_width; // width of virt_text
char *sign_text;
char *sign_name;
int sign_add_idx;
int sign_hl_id;
int number_hl_id;
int line_hl_id;
Expand All @@ -72,7 +74,7 @@ struct Decoration {
};
#define DECORATION_INIT { KV_INITIAL_VALUE, KV_INITIAL_VALUE, 0, kVTEndOfLine, \
kHlModeUnknown, false, false, false, false, kNone, \
DECOR_PRIORITY_BASE, 0, 0, NULL, 0, 0, 0, 0, 0, false }
DECOR_PRIORITY_BASE, 0, 0, NULL, NULL, 0, 0, 0, 0, 0, 0, false }

typedef struct {
int start_row;
Expand Down
2 changes: 1 addition & 1 deletion src/nvim/decoration_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static kvec_t(DecorProvider) decor_providers = KV_INITIAL_VALUE;

static void decor_provider_error(DecorProvider *provider, const char *name, const char *msg)
{
const char *ns_name = describe_ns(provider->ns_id);
const char *ns_name = describe_ns(provider->ns_id, "(UNKNOWN PLUGIN)");
ELOG("error in provider %s.%s: %s", ns_name, name, msg);
msg_schedule_semsg_multiline("Error in decoration provider %s.%s:\n%s", ns_name, name, msg);
}
Expand Down
3 changes: 1 addition & 2 deletions src/nvim/drawline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1380,8 +1380,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool number_onl
HlPriId sign_cul = { 0 };
HlPriId sign_num = { 0 };
// TODO(bfredl, vigoux): line_attr should not take priority over decoration!
int num_signs = buf_get_signattrs(buf, wlv.lnum, wlv.sattrs, &sign_num, &line_id, &sign_cul);
decor_redraw_signs(buf, wlv.lnum - 1, &num_signs, wlv.sattrs, &sign_num, &line_id, &sign_cul);
int num_signs = decor_get_signs(buf, wlv.lnum - 1, wlv.sattrs, &sign_num, &line_id, &sign_cul);

int sign_cul_attr = 0;
int sign_num_attr = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/nvim/drawscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include "nvim/drawscreen.h"
#include "nvim/eval.h"
#include "nvim/ex_getln.h"
#include "nvim/extmark.h"
#include "nvim/extmark_defs.h"
#include "nvim/fold.h"
#include "nvim/getchar.h"
Expand Down Expand Up @@ -2635,7 +2636,8 @@ int number_width(win_T *wp)

// If 'signcolumn' is set to 'number' and there is a sign to display, then
// the minimal width for the number column is 2.
if (n < 2 && (wp->w_buffer->b_signlist != NULL)

if (n < 2 && (wp->w_buffer->b_signs_with_text)
&& (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u')) {
n = 2;
}
Expand Down
3 changes: 2 additions & 1 deletion src/nvim/eval/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "nvim/eval/funcs.h"
#include "nvim/eval/typval.h"
#include "nvim/eval/typval_defs.h"
#include "nvim/extmark.h"
#include "nvim/globals.h"
#include "nvim/macros.h"
#include "nvim/memline.h"
Expand Down Expand Up @@ -507,7 +508,7 @@ static dict_T *get_buffer_info(buf_T *buf)
}
tv_dict_add_list(dict, S_LEN("windows"), windows);

if (buf->b_signlist != NULL) {
if (buf->b_signs) {
// List of signs placed in this buffer
tv_dict_add_list(dict, S_LEN("signs"), get_buffer_signs(buf));
}
Expand Down
4 changes: 2 additions & 2 deletions src/nvim/extmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void extmark_set(buf_T *buf, uint32_t ns_id, uint32_t *idp, int row, colnr_T col
if (decor->sign_text) {
buf->b_signs_with_text++;
// TODO(lewis6991): smarter invalidation
buf_signcols_add_check(buf, NULL);
buf_signcols_add_check(buf);
}
decor_redraw(buf, row, end_row > -1 ? end_row : row, decor);
}
Expand Down Expand Up @@ -312,7 +312,7 @@ bool extmark_clear(buf_T *buf, uint32_t ns_id, int l_row, colnr_T l_col, int u_r
/// if upper_lnum or upper_col are negative the buffer
/// will be searched to the start, or end
/// dir can be set to control the order of the array
/// amount = amount of marks to find or -1 for all
/// amount = amount of marks to find or INT64_MAX for all
ExtmarkInfoArray extmark_get(buf_T *buf, uint32_t ns_id, int l_row, colnr_T l_col, int u_row,
colnr_T u_col, int64_t amount, bool reverse, ExtmarkType type_filter,
bool overlap)
Expand Down
4 changes: 0 additions & 4 deletions src/nvim/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,6 @@ EXTERN buf_T *curbuf INIT(= NULL); // currently active buffer
#define FOR_ALL_BUF_WININFO(buf, wip) \
for ((wip) = (buf)->b_wininfo; (wip) != NULL; (wip) = (wip)->wi_next) // NOLINT

// Iterate through all the signs placed in a buffer
#define FOR_ALL_SIGNS_IN_BUF(buf, sign) \
for ((sign) = (buf)->b_signlist; (sign) != NULL; (sign) = (sign)->se_next) // NOLINT

// List of files being edited (global argument list). curwin->w_alist points
// to this when the window is using the global argument list.
EXTERN alist_T global_alist; // global argument list
Expand Down
2 changes: 0 additions & 2 deletions src/nvim/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ void early_init(mparm_T *paramp)
TIME_MSG("inits 1");

set_lang_var(); // set v:lang and v:ctype

init_signs();
}

#ifdef MAKE_LIB
Expand Down
2 changes: 0 additions & 2 deletions src/nvim/mark.c
Original file line number Diff line number Diff line change
Expand Up @@ -1193,8 +1193,6 @@ void mark_adjust_buf(buf_T *buf, linenr_T line1, linenr_T line2, linenr_T amount
if (!found_one) {
buf->b_has_qf_entry &= ~BUF_HAS_LL_ENTRY;
}

sign_mark_adjust(buf, line1, line2, amount, amount_after);
}

if (op != kExtmarkNOOP) {
Expand Down
Loading

0 comments on commit d6a1166

Please sign in to comment.