cursor.vim 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. scriptencoding utf-8
  2. " Position of cursor relative to screen cell
  3. function! coc#cursor#screen_pos() abort
  4. let nr = winnr()
  5. let [row, col] = win_screenpos(nr)
  6. return [row + winline() - 2, col + wincol() - 2]
  7. endfunction
  8. function! coc#cursor#move_by_col(delta)
  9. let pos = getcurpos()
  10. call cursor(pos[1], pos[2] + a:delta)
  11. endfunction
  12. " Get cursor position.
  13. function! coc#cursor#position()
  14. return [line('.') - 1, strchars(strpart(getline('.'), 0, col('.') - 1))]
  15. endfunction
  16. " Move cursor to position.
  17. function! coc#cursor#move_to(line, character) abort
  18. let content = getline(a:line + 1)
  19. let pre = strcharpart(content, 0, a:character)
  20. let col = strlen(pre) + 1
  21. call cursor(a:line + 1, col)
  22. endfunction
  23. " Character offset of current cursor, vim provide bytes offset only.
  24. function! coc#cursor#char_offset() abort
  25. let offset = 0
  26. let lnum = line('.')
  27. for i in range(1, lnum)
  28. if i == lnum
  29. let offset += strchars(strpart(getline('.'), 0, col('.')-1))
  30. else
  31. let offset += strchars(getline(i)) + 1
  32. endif
  33. endfor
  34. return offset
  35. endfunction
  36. " Returns latest selection range
  37. function! coc#cursor#get_selection(char) abort
  38. let m = a:char ? 'char' : visualmode()
  39. if empty(m)
  40. return v:null
  41. endif
  42. let [_, sl, sc, soff] = getpos(m ==# 'char' ? "'[" : "'<")
  43. let [_, el, ec, eoff] = getpos(m ==# 'char' ? "']" : "'>")
  44. let start_idx = coc#string#get_character(getline(sl), sc)
  45. if m ==# 'V'
  46. return [sl - 1, start_idx, el, 0]
  47. endif
  48. let line = getline(el)
  49. let end_idx = coc#string#get_character(line, ec)
  50. if m !=# 'char'
  51. let end_idx = end_idx == strchars(line) ? end_idx : end_idx + 1
  52. endif
  53. return [sl - 1, start_idx, el - 1, end_idx]
  54. endfunction