[vim/vim] how to get column number ignoring virtual-text ? (Discussion #12833)

80 views
Skip to first unread message

Violin

unread,
Aug 17, 2023, 11:32:50 PM8/17/23
to vim/vim, Subscribed

I very like vim9 newly introduced "virtual text" feature . It makes inlay hints possible in programming.
But something bothering me is that when I set ruler in status line (airline plugin, actually), I always get column number of cursor position which is sum of "real text" and "virtual text" .
When I move cursor from left side of virtual text to right side, column number jumps -- that's ok.
When I have a max width of code line, I can't judge whether my code really exceeding width limit -- that's terrible.
Is there a way to get column number excluding virtual texts width, and get "real text" part of column ?


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/12833@github.com>

Adam Tao

unread,
Aug 28, 2023, 2:42:57 AM8/28/23
to vim/vim, Subscribed

I'm facing the same issue too. I really like the text properties feature which makes some LSP features (such as code lens and inlay hints) possible. On the other side, however, it introduces some inconvenience to calculate column number.

For files use spaces as indentation, it's very easy to handle. %c in statusline or col() will get the column number I want.

But for files use tabs as indentation (like files in Linux kernel), I don't find a easy way to calculate column number. %c in statusline or col() will calculate the width of tab as 1, which I want it to be 8. %v in statusline or virtcol() will count the length of virtual text (like inlay hint and code lens).

For example, %c will show the column number as 45 (byte offset) while %v will show the column number as 77 (screen column number) for the following line (linux/virt/kvm/kvm_main.c, line 333):
image
%c will show the column number as 45.
image
%v will show the column number as 77.

However, what I really want is 59, which can be calcute by counting the width of tab as 8 and not counting any length of virtual text.

After patch 1762, I think it can be calculated via virtcol() and prop_list(). But I just wonder whether there is any builtin function can do this?


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/12833/comments/6839031@github.com>

Adam Tao

unread,
Aug 28, 2023, 10:00:35 AM8/28/23
to vim/vim, Subscribed

Emacs will show the column number I want:
image
(It show 58 instead of 59 since the column in emacs starts from 0)


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/12833/comments/6842893@github.com>

Adam Tao

unread,
Oct 19, 2023, 12:23:11 AM10/19/23
to vim/vim, Subscribed

I have written a function which can get the column number of "real part":

function tcx4c70Vim#utils#GetColumnNumber() abort
  let l:vcol = virtcol('.')
  " Only after 9.0.1762 we can filter out above virtual text via text_align property.
  if !has('patch-9.0.1762')
    return l:vcol
  endif

  let l:text_columns = winwidth(0) - (float2nr(log10(line('$'))) + 2)
  if has('folding')
    let l:text_columns -= &foldcolumn
  endif
  if has('signs')
    if !empty(filter(sign_getplaced(bufnr(), {'group': '*'}), '!empty(v:val["signs"])'))
      let l:text_columns -= 2
    endif
  endif

  let l:col = col('.')
  let l:virt_texts = filter(prop_list(line('.')), 'has_key(v:val, "text") && (v:val["col"] != 0 || get(v:val, "text_align", "after") == "above")')
  " l:virt_texts has already been sorted by col
  for l:virt_text in l:virt_texts
    if l:virt_text['col'] == 0 && l:vcol > l:text_columns
      let l:vcol -= l:text_columns
    elseif l:col >= l:virt_text['col']
      let l:vcol -= len(l:virt_text['text'])
    else
      break
    endif
  endfor
  return l:vcol
endfunction

However, it depends on path 9.0.1762, folding, and signs.


Reply to this email directly, view it on GitHub,.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/repo-discussions/12833/comments/7322121@github.com>

Reply all
Reply to author
Forward
0 new messages