aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-01-31 23:43:46 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-01-31 23:43:46 +0000
commit5bb0758567e953f15335d284f31ae14947339c7a (patch)
treedb0c781532d9a05140ebbd159b7b006433afab09
parenta2e3d22928e127385277c472fef6aff96d238ff2 (diff)
downloadrneovim-5bb0758567e953f15335d284f31ae14947339c7a.tar.gz
rneovim-5bb0758567e953f15335d284f31ae14947339c7a.tar.bz2
rneovim-5bb0758567e953f15335d284f31ae14947339c7a.zip
feat(colorcolchar): make colorcol qsort() stable by including index
-rw-r--r--src/nvim/window.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/nvim/window.c b/src/nvim/window.c
index cf5d0013a3..5e99cec015 100644
--- a/src/nvim/window.c
+++ b/src/nvim/window.c
@@ -7410,7 +7410,17 @@ static bool frame_check_width(const frame_T *topfrp, int width)
/// Compares the column numbers
static int colorcol_cmp(const void *a, const void *b)
{
- return ((const colorcol_T *)a)->col - ((const colorcol_T *)b)->col;
+ colorcol_T *ac = (colorcol_T*) a;
+ colorcol_T *bc = (colorcol_T*) b;
+ int ret = ac->col - bc->col;
+ if (ret == 0) {
+ // qsort() is not inherently stable, so to make it stable,
+ // the syn_attr field temporarily contains the original index.
+ // Comparing these will enforce stability.
+ return ac->syn_attr - bc->syn_attr;
+ } else {
+ return ret;
+ }
}
/// Handle setting 'colorcolumn' or 'textwidth' in window "wp".
@@ -7495,13 +7505,14 @@ char *check_colorcolumn(win_T *wp)
}
if (!do_skip) {
- color_cols[count++] = (colorcol_T) {
- .col = col - 1, // 1-based to 0-based
- .syn_attr = 0,
+ color_cols[count] = (colorcol_T) {
+ .col = col - 1, // 1-based to 0-based
+ .syn_attr = (int) count, // Temporarily use this for stable sorting.
.ch = ch,
.syn_name = syn_name[0] == 0 ? NULL : xstrdup(syn_name),
.flags = flags,
};
+ count ++;
}
if (*s == NUL) {
@@ -7528,7 +7539,9 @@ char *check_colorcolumn(win_T *wp)
for (unsigned int i = 0; i < count; i++) {
// skip duplicates
if (j == 0 || wp->w_p_cc_cols[j - 1].col != color_cols[i].col) {
- wp->w_p_cc_cols[j++] = color_cols[i];
+ wp->w_p_cc_cols[j] = color_cols[i];
+ // Clear syn_attr, which was used for stable sorting.
+ wp->w_p_cc_cols[j ++].syn_attr = 0;
}
}
memset(&wp->w_p_cc_cols[j], 0, sizeof(wp->w_p_cc_cols[j]));