@@ -263,6 +263,9 @@ static void draw_virt_text(win_T *wp, buf_T *buf, int col_off, int *end_col, int
263263 int * const indices = state -> ranges_i .items ;
264264 DecorRangeSlot * const slots = state -> slots .items ;
265265
266+ /// Total width of all virtual text with "eol_right_align" alignment
267+ int totalWidthOfEolRightAlignedVirtText = 0 ;
268+
266269 for (int i = 0 ; i < end ; i ++ ) {
267270 DecorRange * item = & slots [indices [i ]].range ;
268271 if (!(item -> start_row == state -> row && decor_virt_pos (item ))) {
@@ -277,7 +280,44 @@ static void draw_virt_text(win_T *wp, buf_T *buf, int col_off, int *end_col, int
277280 if (decor_virt_pos (item ) && item -> draw_col == -1 ) {
278281 bool updated = true;
279282 VirtTextPos pos = decor_virt_pos_kind (item );
280- if (pos == kVPosRightAlign ) {
283+
284+ if (do_eol && pos == kVPosEndOfLineRightAlign ) {
285+ int eolOffset = 0 ;
286+ if (totalWidthOfEolRightAlignedVirtText == 0 ) {
287+ // Look ahead to the remaining decor items
288+ for (int j = i ; j < end ; j ++ ) {
289+ /// A future decor to be handled in this function's call
290+ DecorRange * lookaheadItem = & slots [indices [j ]].range ;
291+
292+ if (lookaheadItem -> start_row != state -> row
293+ || !decor_virt_pos (lookaheadItem )
294+ || lookaheadItem -> draw_col != -1 ) {
295+ continue ;
296+ }
297+
298+ /// The Virtual Text of the decor item we're looking ahead to
299+ DecorVirtText * lookaheadVt = NULL ;
300+ if (item -> kind == kDecorKindVirtText ) {
301+ assert (item -> data .vt );
302+ lookaheadVt = item -> data .vt ;
303+ }
304+
305+ if (decor_virt_pos_kind (lookaheadItem ) == kVPosEndOfLineRightAlign ) {
306+ // An extra space is added for single character spacing in EOL alignment
307+ totalWidthOfEolRightAlignedVirtText += (lookaheadVt -> width + 1 );
308+ }
309+ }
310+
311+ // Remove one space from the total width since there's no single space after the last entry
312+ totalWidthOfEolRightAlignedVirtText -- ;
313+
314+ if (totalWidthOfEolRightAlignedVirtText <= (right_pos - state -> eol_col )) {
315+ eolOffset = right_pos - totalWidthOfEolRightAlignedVirtText - state -> eol_col ;
316+ }
317+ }
318+
319+ item -> draw_col = state -> eol_col + eolOffset ;
320+ } else if (pos == kVPosRightAlign ) {
281321 right_pos -= vt -> width ;
282322 item -> draw_col = right_pos ;
283323 } else if (pos == kVPosEndOfLine && do_eol ) {
@@ -304,7 +344,7 @@ static void draw_virt_text(win_T *wp, buf_T *buf, int col_off, int *end_col, int
304344 int vcol = item -> draw_col - col_off ;
305345 int col = draw_virt_text_item (buf , item -> draw_col , vt -> data .virt_text ,
306346 vt -> hl_mode , max_col , vcol );
307- if (vt -> pos == kVPosEndOfLine && do_eol ) {
347+ if (do_eol && (( vt -> pos == kVPosEndOfLine ) || ( vt -> pos == kVPosEndOfLineRightAlign )) ) {
308348 state -> eol_col = col + 1 ;
309349 }
310350 * end_col = MAX (* end_col , col );
0 commit comments