refactor(extmark): redundant ExtmarkInfo delenda est, use MTPair instead

This commit is contained in:
bfredl
2023-11-18 20:35:12 +01:00
parent bec2ebebda
commit ec283e6b4b
5 changed files with 44 additions and 84 deletions

View File

@@ -142,40 +142,42 @@ Array virt_text_to_array(VirtText vt, bool hl_name)
return chunks; return chunks;
} }
static Array extmark_to_array(const ExtmarkInfo *extmark, bool id, bool add_dict, bool hl_name) static Array extmark_to_array(MTPair extmark, bool id, bool add_dict, bool hl_name)
{ {
MTKey start = extmark.start;
Array rv = ARRAY_DICT_INIT; Array rv = ARRAY_DICT_INIT;
if (id) { if (id) {
ADD(rv, INTEGER_OBJ((Integer)extmark->mark_id)); ADD(rv, INTEGER_OBJ((Integer)start.id));
} }
ADD(rv, INTEGER_OBJ(extmark->row)); ADD(rv, INTEGER_OBJ(start.pos.row));
ADD(rv, INTEGER_OBJ(extmark->col)); ADD(rv, INTEGER_OBJ(start.pos.col));
if (add_dict) { if (add_dict) {
Dictionary dict = ARRAY_DICT_INIT; Dictionary dict = ARRAY_DICT_INIT;
PUT(dict, "ns_id", INTEGER_OBJ((Integer)extmark->ns_id)); PUT(dict, "ns_id", INTEGER_OBJ((Integer)start.ns));
PUT(dict, "right_gravity", BOOLEAN_OBJ(extmark->right_gravity)); PUT(dict, "right_gravity", BOOLEAN_OBJ(mt_right(start)));
if (extmark->end_row >= 0) { if (extmark.end_pos.row >= 0) {
PUT(dict, "end_row", INTEGER_OBJ(extmark->end_row)); PUT(dict, "end_row", INTEGER_OBJ(extmark.end_pos.row));
PUT(dict, "end_col", INTEGER_OBJ(extmark->end_col)); PUT(dict, "end_col", INTEGER_OBJ(extmark.end_pos.col));
PUT(dict, "end_right_gravity", BOOLEAN_OBJ(extmark->end_right_gravity)); PUT(dict, "end_right_gravity", BOOLEAN_OBJ(extmark.end_right_gravity));
} }
if (extmark->no_undo) { if (mt_no_undo(start)) {
PUT(dict, "undo_restore", BOOLEAN_OBJ(false)); PUT(dict, "undo_restore", BOOLEAN_OBJ(false));
} }
if (extmark->invalidate) { if (mt_invalidate(start)) {
PUT(dict, "invalidate", BOOLEAN_OBJ(true)); PUT(dict, "invalidate", BOOLEAN_OBJ(true));
} }
if (extmark->invalid) { if (mt_invalid(start)) {
PUT(dict, "invalid", BOOLEAN_OBJ(true)); PUT(dict, "invalid", BOOLEAN_OBJ(true));
} }
const Decoration *decor = &extmark->decor; // pretend this is a pointer for a short while, Decoration will be factored away very soon
const Decoration decor[1] = { get_decor(start) };
if (decor->hl_id) { if (decor->hl_id) {
PUT(dict, "hl_group", hl_group_name(decor->hl_id, hl_name)); PUT(dict, "hl_group", hl_group_name(decor->hl_id, hl_name));
PUT(dict, "hl_eol", BOOLEAN_OBJ(decor->hl_eol)); PUT(dict, "hl_eol", BOOLEAN_OBJ(decor->hl_eol));
@@ -308,11 +310,11 @@ ArrayOf(Integer) nvim_buf_get_extmark_by_id(Buffer buffer, Integer ns_id,
} }
} }
ExtmarkInfo extmark = extmark_from_id(buf, (uint32_t)ns_id, (uint32_t)id); MTPair extmark = extmark_from_id(buf, (uint32_t)ns_id, (uint32_t)id);
if (extmark.row < 0) { if (extmark.start.pos.row < 0) {
return rv; return rv;
} }
return extmark_to_array(&extmark, false, details, hl_name); return extmark_to_array(extmark, false, details, hl_name);
} }
/// Gets |extmarks| (including |signs|) in "traversal order" from a |charwise| /// Gets |extmarks| (including |signs|) in "traversal order" from a |charwise|
@@ -432,7 +434,7 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e
u_col, (int64_t)limit, reverse, type, opts->overlap); u_col, (int64_t)limit, reverse, type, opts->overlap);
for (size_t i = 0; i < kv_size(marks); i++) { for (size_t i = 0; i < kv_size(marks); i++) {
ADD(rv, ARRAY_OBJ(extmark_to_array(&kv_A(marks, i), true, details, hl_name))); ADD(rv, ARRAY_OBJ(extmark_to_array(kv_A(marks, i), true, details, hl_name)));
} }
kv_destroy(marks); kv_destroy(marks);
@@ -1121,13 +1123,13 @@ static bool extmark_get_index_from_obj(buf_T *buf, Integer ns_id, Object obj, in
}); });
} }
ExtmarkInfo extmark = extmark_from_id(buf, (uint32_t)ns_id, (uint32_t)id); MTPair extmark = extmark_from_id(buf, (uint32_t)ns_id, (uint32_t)id);
VALIDATE_INT((extmark.row >= 0), "mark id (not found)", id, { VALIDATE_INT((extmark.start.pos.row >= 0), "mark id (not found)", id, {
return false; return false;
}); });
*row = extmark.row; *row = extmark.start.pos.row;
*col = extmark.col; *col = extmark.start.pos.col;
return true; return true;
// Check if it is a position // Check if it is a position

View File

@@ -257,7 +257,7 @@ ExtmarkInfoArray extmark_get(buf_T *buf, uint32_t ns_id, int l_row, colnr_T l_co
MTPair pair; MTPair pair;
while (marktree_itr_step_overlap(buf->b_marktree, itr, &pair)) { while (marktree_itr_step_overlap(buf->b_marktree, itr, &pair)) {
push_mark(&array, ns_id, type_filter, pair.start, pair.end_pos, pair.end_right_gravity); push_mark(&array, ns_id, type_filter, pair);
} }
} else { } else {
// Find all the marks beginning with the start position // Find all the marks beginning with the start position
@@ -278,7 +278,7 @@ ExtmarkInfoArray extmark_get(buf_T *buf, uint32_t ns_id, int l_row, colnr_T l_co
} }
MTKey end = marktree_get_alt(buf->b_marktree, mark, NULL); MTKey end = marktree_get_alt(buf->b_marktree, mark, NULL);
push_mark(&array, ns_id, type_filter, mark, end.pos, mt_right(end)); push_mark(&array, ns_id, type_filter, mtpair_from(mark, end));
next_mark: next_mark:
if (reverse) { if (reverse) {
marktree_itr_prev(buf->b_marktree, itr); marktree_itr_prev(buf->b_marktree, itr);
@@ -289,15 +289,14 @@ next_mark:
return array; return array;
} }
static void push_mark(ExtmarkInfoArray *array, uint32_t ns_id, ExtmarkType type_filter, MTKey mark, static void push_mark(ExtmarkInfoArray *array, uint32_t ns_id, ExtmarkType type_filter, MTPair mark)
MTPos end_pos, bool end_right)
{ {
if (!(ns_id == UINT32_MAX || mark.ns == ns_id)) { if (!(ns_id == UINT32_MAX || mark.start.ns == ns_id)) {
return; return;
} }
uint16_t type_flags = kExtmarkNone; uint16_t type_flags = kExtmarkNone;
if (type_filter != kExtmarkNone) { if (type_filter != kExtmarkNone) {
Decoration *decor = mark.decor_full; Decoration *decor = mark.start.decor_full;
if (decor && (decor->sign_text || decor->number_hl_id)) { if (decor && (decor->sign_text || decor->number_hl_id)) {
type_flags |= (kExtmarkSignHL|kExtmarkSign); type_flags |= (kExtmarkSignHL|kExtmarkSign);
} }
@@ -310,7 +309,7 @@ static void push_mark(ExtmarkInfoArray *array, uint32_t ns_id, ExtmarkType type_
if (decor && decor->virt_lines.size) { if (decor && decor->virt_lines.size) {
type_flags |= kExtmarkVirtLines; type_flags |= kExtmarkVirtLines;
} }
if (mark.hl_id) { if (mark.start.hl_id) {
type_flags |= kExtmarkHighlight; type_flags |= kExtmarkHighlight;
} }
@@ -319,44 +318,20 @@ static void push_mark(ExtmarkInfoArray *array, uint32_t ns_id, ExtmarkType type_
} }
} }
kv_push(*array, ((ExtmarkInfo) { .ns_id = mark.ns, kv_push(*array, mark);
.mark_id = mark.id,
.row = mark.pos.row, .col = mark.pos.col,
.end_row = end_pos.row,
.end_col = end_pos.col,
.invalidate = mt_invalidate(mark),
.invalid = mt_invalid(mark),
.right_gravity = mt_right(mark),
.end_right_gravity = end_right,
.no_undo = mt_no_undo(mark),
.decor = get_decor(mark) }));
} }
/// Lookup an extmark by id /// Lookup an extmark by id
ExtmarkInfo extmark_from_id(buf_T *buf, uint32_t ns_id, uint32_t id) MTPair extmark_from_id(buf_T *buf, uint32_t ns_id, uint32_t id)
{ {
ExtmarkInfo ret = EXTMARKINFO_INIT;
MTKey mark = marktree_lookup_ns(buf->b_marktree, ns_id, id, false, NULL); MTKey mark = marktree_lookup_ns(buf->b_marktree, ns_id, id, false, NULL);
if (!mark.id) { if (!mark.id) {
return ret; return mtpair_from(mark, mark); // invalid
} }
assert(mark.pos.row >= 0); assert(mark.pos.row >= 0);
MTKey end = marktree_get_alt(buf->b_marktree, mark, NULL); MTKey end = marktree_get_alt(buf->b_marktree, mark, NULL);
ret.ns_id = ns_id; return mtpair_from(mark, end);
ret.mark_id = id;
ret.row = mark.pos.row;
ret.col = mark.pos.col;
ret.end_row = end.pos.row;
ret.end_col = end.pos.col;
ret.right_gravity = mt_right(mark);
ret.end_right_gravity = mt_right(end);
ret.no_undo = mt_no_undo(mark);
ret.invalidate = mt_invalidate(mark);
ret.invalid = mt_invalid(mark);
ret.decor = get_decor(mark);
return ret;
} }
/// free extmarks from the buffer /// free extmarks from the buffer

View File

@@ -15,24 +15,7 @@
EXTERN int extmark_splice_pending INIT( = 0); EXTERN int extmark_splice_pending INIT( = 0);
typedef struct { typedef kvec_t(MTPair) ExtmarkInfoArray;
uint64_t ns_id;
uint64_t mark_id;
int row;
colnr_T col;
int end_row;
colnr_T end_col;
bool right_gravity;
bool end_right_gravity;
bool invalidate;
bool invalid;
bool no_undo;
Decoration decor; // TODO(bfredl): CHONKY
} ExtmarkInfo;
#define EXTMARKINFO_INIT { 0, 0, -1, -1, -1, -1, false, false, false, false, false, \
DECORATION_INIT };
typedef kvec_t(ExtmarkInfo) ExtmarkInfoArray;
// TODO(bfredl): good enough name for now. // TODO(bfredl): good enough name for now.
typedef ptrdiff_t bcount_t; typedef ptrdiff_t bcount_t;

View File

@@ -1503,11 +1503,6 @@ bool marktree_itr_get_overlap(MarkTree *b, int row, int col, MarkTreeIter *itr)
return true; return true;
} }
static inline MTPair pair_from(MTKey start, MTKey end)
{
return (MTPair){ .start = start, .end_pos = end.pos, .end_right_gravity = mt_right(end) };
}
/// Step through all overlapping pairs at a position. /// Step through all overlapping pairs at a position.
/// ///
/// This function must only be used with an iterator from |marktree_itr_step_overlap| /// This function must only be used with an iterator from |marktree_itr_step_overlap|
@@ -1526,7 +1521,7 @@ bool marktree_itr_step_overlap(MarkTree *b, MarkTreeIter *itr, MTPair *pair)
while (itr->i == -1) { while (itr->i == -1) {
if (itr->intersect_idx < kv_size(itr->x->intersect)) { if (itr->intersect_idx < kv_size(itr->x->intersect)) {
uint64_t id = kv_A(itr->x->intersect, itr->intersect_idx++); uint64_t id = kv_A(itr->x->intersect, itr->intersect_idx++);
*pair = pair_from(marktree_lookup(b, id, NULL), *pair = mtpair_from(marktree_lookup(b, id, NULL),
marktree_lookup(b, id|MARKTREE_END_FLAG, NULL)); marktree_lookup(b, id|MARKTREE_END_FLAG, NULL));
return true; return true;
} }
@@ -1564,7 +1559,7 @@ bool marktree_itr_step_overlap(MarkTree *b, MarkTreeIter *itr, MTPair *pair)
} }
unrelative(itr->pos, &k.pos); unrelative(itr->pos, &k.pos);
*pair = pair_from(k, end); *pair = mtpair_from(k, end);
return true; // it's a start! return true; // it's a start!
} }
} }
@@ -1583,7 +1578,7 @@ bool marktree_itr_step_overlap(MarkTree *b, MarkTreeIter *itr, MTPair *pair)
if (pos_less(itr->intersect_pos, start.pos)) { if (pos_less(itr->intersect_pos, start.pos)) {
continue; continue;
} }
*pair = pair_from(start, k); *pair = mtpair_from(start, k);
return true; // end of a range which began before us! return true; // end of a range which began before us!
} }
} }

View File

@@ -159,6 +159,11 @@ static inline uint16_t mt_flags(bool right_gravity, bool hl_eol, bool no_undo, b
| (decor_level << MT_FLAG_DECOR_OFFSET)); | (decor_level << MT_FLAG_DECOR_OFFSET));
} }
static inline MTPair mtpair_from(MTKey start, MTKey end)
{
return (MTPair){ .start = start, .end_pos = end.pos, .end_right_gravity = mt_right(end) };
}
typedef kvec_withinit_t(uint64_t, 4) Intersection; typedef kvec_withinit_t(uint64_t, 4) Intersection;
struct mtnode_s { struct mtnode_s {