robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

luukvbaalfeat(column): per row click handlers for 'statuscolumn' (#40265)69160854c5

master
6.5 KiB242 linesraw
1#pragma once
2
3#include <stdbool.h>
4#include <stdint.h>
5#include <stdio.h>
6
7#include "nvim/api/private/defs.h"
8#include "nvim/assert_defs.h"
9#include "nvim/highlight_defs.h"
10#include "nvim/statusline_defs.h"
11#include "nvim/types_defs.h"
12
13#ifdef __NetBSD__
14# undef uint64_t
15# define uint64_t uint64_t
16#endif
17
18typedef const char *cstr_t;
19typedef const char *path_t;
20typedef void *ptr_t;
21
22// when used as a key, String doesn't need to be NUL terminated,
23// and can also contain embedded NUL:s as part of the data.
24static inline uint32_t hash_String(String s)
25{
26  uint32_t h = 0;
27  for (size_t i = 0; i < s.size; i++) {
28    h = (h << 5) - h + (uint8_t)s.data[i];
29  }
30  return h;
31}
32
33static inline bool equal_String(String a, String b)
34{
35  if (a.size != b.size) {
36    return false;
37  }
38
39  return (a.size == 0) || (memcmp(a.data, b.data, a.size) == 0);
40}
41
42#define Set(type) Set_##type
43#define Map(T, U) Map_##T##_##U
44#define PMap(T) Map(T, ptr_t)
45
46static const int value_init_int = 0;
47static const ptr_t value_init_ptr_t = NULL;
48static const uint32_t value_init_uint32_t = 0;
49static const int64_t value_init_int64_t = 0;
50static const String value_init_String = STRING_INIT;
51static const ColorItem value_init_ColorItem = COLOR_ITEM_INITIALIZER;
52static const MTDamagePair value_init_MTDamagePair = MTDAMAGE_PAIR_INIT;
53static const StcClick value_init_StcClick = { 0 };
54
55// layer 0: type non-specific code
56
57typedef struct {
58  uint32_t n_buckets;
59  uint32_t size;
60  uint32_t n_occupied;
61  uint32_t upper_bound;
62  uint32_t n_keys;  // this is almost always "size", but keys[] could contain ded items..
63  uint32_t keys_capacity;
64  uint32_t *hash;
65} MapHash;
66
67#define MAPHASH_INIT { 0, 0, 0, 0, 0, 0, NULL }
68#define SET_INIT { MAPHASH_INIT, NULL }
69#define MAP_INIT { SET_INIT, NULL }
70
71#define MH_TOMBSTONE UINT32_MAX
72
73#define mh_is_empty(h, i) ((h)->hash[i] == 0)
74#define mh_is_del(h, i) ((h)->hash[i] == MH_TOMBSTONE)
75#define mh_is_either(h, i) ((uint32_t)((h)->hash[i] + 1U) <= 1U)
76
77typedef enum {
78  kMHExisting = 0,
79  kMHNewKeyDidFit,
80  kMHNewKeyRealloc,
81} MHPutStatus;
82
83void mh_clear(MapHash *h);
84void mh_realloc(MapHash *h, uint32_t n_min_buckets);
85
86// layer 1: key type specific defs
87// This is all need for sets.
88
89#define MH_DECLS(T, K, K_query) \
90  typedef struct { \
91    MapHash h; \
92    K *keys; \
93  } Set(T); \
94 \
95  uint32_t mh_find_bucket_##T(Set(T) *set, K_query key, bool put); \
96  uint32_t mh_get_##T(Set(T) *set, K_query key); \
97  void mh_rehash_##T(Set(T) *set); \
98  uint32_t mh_put_##T(Set(T) *set, K_query key, MHPutStatus *new); \
99
100#define KEY_DECLS(T) \
101  MH_DECLS(T, T, T) \
102  uint32_t mh_delete_##T(Set(T) *set, T *key); \
103  static inline bool set_put_##T(Set(T) *set, T key, T **key_alloc) { \
104    MHPutStatus status; \
105    uint32_t k = mh_put_##T(set, key, &status); \
106    if (key_alloc) { \
107      *key_alloc = &set->keys[k]; \
108    } \
109    return status != kMHExisting; \
110  } \
111  static inline T set_del_##T(Set(T) *set, T key) \
112  { \
113    mh_delete_##T(set, &key); \
114    return key; \
115  } \
116  static inline bool set_has_##T(Set(T) *set, T key) { \
117    return mh_get_##T(set, key) != MH_TOMBSTONE; \
118  } \
119
120// layer 2: key+value specific defs
121// now we finally get Maps
122
123#define MAP_DECLS(T, U) \
124  typedef struct { \
125    Set(T) set; \
126    U *values; \
127  } Map(T, U); \
128  static inline U map_get_##T##_##U(Map(T, U) *map, T key) \
129  { \
130    uint32_t k = mh_get_##T(&map->set, key); \
131    return k == MH_TOMBSTONE ? value_init_##U : map->values[k]; \
132  } \
133  U *map_ref_##T##_##U(Map(T, U) *map, T key, T **key_alloc); \
134  U *map_put_ref_##T##_##U(Map(T, U) *map, T key, T **key_alloc, bool *new_item); \
135  static inline void map_put_##T##_##U(Map(T, U) *map, T key, U value) \
136  { \
137    U *val = map_put_ref_##T##_##U(map, key, NULL, NULL); \
138    *val = value; \
139  } \
140  U map_del_##T##_##U(Map(T, U) *map, T key, T *key_alloc); \
141
142// NOTE: Keys AND values must be allocated! Map and Set does not make a copy.
143
144#define quasiquote(x, y) x##y
145
146MH_DECLS(glyph, char, String)
147KEY_DECLS(int)
148KEY_DECLS(cstr_t)
149KEY_DECLS(path_t)
150KEY_DECLS(ptr_t)
151KEY_DECLS(uint64_t)
152KEY_DECLS(int64_t)
153KEY_DECLS(uint32_t)
154KEY_DECLS(String)
155KEY_DECLS(HlEntry)
156KEY_DECLS(ColorKey)
157
158MAP_DECLS(int, ptr_t)
159MAP_DECLS(cstr_t, ptr_t)
160MAP_DECLS(cstr_t, int)
161MAP_DECLS(ptr_t, ptr_t)
162MAP_DECLS(uint32_t, ptr_t)
163MAP_DECLS(uint64_t, ptr_t)
164MAP_DECLS(uint64_t, int)
165MAP_DECLS(int64_t, int64_t)
166MAP_DECLS(int64_t, ptr_t)
167MAP_DECLS(uint32_t, uint32_t)
168MAP_DECLS(String, int)
169MAP_DECLS(int, String)
170MAP_DECLS(ColorKey, ColorItem)
171MAP_DECLS(uint64_t, MTDamagePair)
172MAP_DECLS(int, StcClick)
173typedef Map(int, StcClick) StcClicks;
174static const StcClicks value_init_StcClicks = { 0 };
175MAP_DECLS(int, StcClicks)
176
177#define set_has(T, set, key) set_has_##T(set, key)
178#define set_put(T, set, key) set_put_##T(set, key, NULL)
179#define set_put_ref(T, set, key, key_alloc) set_put_##T(set, key, key_alloc)
180#define set_put_idx(T, set, key, status) mh_put_##T(set, key, status)
181#define set_del(T, set, key) set_del_##T(set, key)
182#define set_size(set) ((set)->h.size)
183#define set_clear(T, set) mh_clear(&(set)->h)
184#define set_destroy(T, set) \
185  do { \
186    xfree((set)->keys); \
187    xfree((set)->h.hash); \
188    *(set) = (Set(T)) SET_INIT; \
189  } while (0)
190
191#define map_get(T, U) map_get_##T##_##U
192#define map_has(T, map, key) set_has(T, &(map)->set, key)
193#define map_put(T, U) map_put_##T##_##U
194#define map_ref(T, U) map_ref_##T##_##U
195#define map_put_ref(T, U) map_put_ref_##T##_##U
196#define map_del(T, U) map_del_##T##_##U
197#define map_size(map) set_size(&(map)->set)
198#define map_clear(T, map) set_clear(T, &(map)->set)
199#define map_destroy(T, map) \
200  do { \
201    set_destroy(T, &(map)->set); \
202    XFREE_CLEAR((map)->values); \
203  } while (0)
204
205#define pmap_get(T) map_get(T, ptr_t)
206#define pmap_put(T) map_put(T, ptr_t)
207#define pmap_ref(T) map_ref(T, ptr_t)
208#define pmap_put_ref(T) map_put_ref(T, ptr_t)
209/// @see pmap_del2
210#define pmap_del(T) map_del(T, ptr_t)
211
212#define set_foreach(set, key, block) \
213  { \
214    uint32_t __i; \
215    for (__i = 0; __i < (set)->h.n_keys; __i++) { \
216      (key) = (set)->keys[__i]; \
217      block; \
218    } \
219  }
220
221#define map_foreach_key(map, key, block) set_foreach(&(map)->set, key, block)
222
223#define map_foreach(map, key, value, code) \
224  { \
225    uint32_t __i; \
226    for (__i = 0; __i < (map)->set.h.n_keys; __i++) { \
227      (key) = (map)->set.keys[__i]; \
228      (value) = (map)->values[__i]; \
229      code; \
230    } \
231  }
232
233#define map_foreach_value(map, value, code) \
234  { \
235    uint32_t __i; \
236    for (__i = 0; __i < (map)->set.h.n_keys; __i++) { \
237      (value) = (map)->values[__i]; \
238      code; \
239    } \
240  }
241
242void pmap_del2(PMap(cstr_t) *map, const char *key);