All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " This function can be error prone if the caller forgets to use +1 to vim line
2 " so use lsp#utils#position#lsp_to_vim instead
3 " Convert a character-index (0-based) to byte-index (1-based)
4 " This function requires a buffer specifier (expr, see :help bufname()),
5 " a line number (lnum, 1-based), and a character-index (char, 0-based).
6 function! s:to_col(expr, lnum, char) abort
7 let l:lines = getbufline(a:expr, a:lnum)
9 if type(a:expr) != v:t_string || !filereadable(a:expr)
13 " a:expr is a file that is not yet loaded as a buffer
14 let l:lines = readfile(a:expr, '', a:lnum)
16 " when the file is empty. a:char should be 0 in the case
20 let l:linestr = l:lines[-1]
21 return strlen(strcharpart(l:linestr, 0, a:char)) + 1
24 " The inverse version of `s:to_col`.
25 " Convert [lnum, col] to LSP's `Position`.
26 function! s:to_char(expr, lnum, col) abort
27 let l:lines = getbufline(a:expr, a:lnum)
29 if type(a:expr) != v:t_string || !filereadable(a:expr)
33 " a:expr is a file that is not yet loaded as a buffer
34 let l:lines = readfile(a:expr, '', a:lnum)
36 let l:linestr = l:lines[-1]
37 return strchars(strpart(l:linestr, 0, a:col - 1))
40 " @param expr = see :help bufname()
49 function! lsp#utils#position#lsp_to_vim(expr, position) abort
50 let l:line = lsp#utils#position#lsp_line_to_vim(a:expr, a:position)
51 let l:col = lsp#utils#position#lsp_character_to_vim(a:expr, a:position)
52 return [l:line, l:col]
55 " @param expr = see :help bufname()
62 function! lsp#utils#position#lsp_line_to_vim(expr, position) abort
63 return a:position['line'] + 1
66 " @param expr = see :help bufname()
73 function! lsp#utils#position#lsp_character_to_vim(expr, position) abort
74 let l:line = a:position['line'] + 1 " optimize function overhead by not calling lsp_line_to_vim
75 let l:char = a:position['character']
76 return s:to_col(a:expr, l:line, l:char)
79 " @param expr = :help bufname()
80 " @param pos = [lnum, col]
83 " 'character': character
85 function! lsp#utils#position#vim_to_lsp(expr, pos) abort
87 \ 'line': a:pos[0] - 1,
88 \ 'character': s:to_char(a:expr, a:pos[0], a:pos[1])