]> git.madduck.net Git - etc/vim.git/blob - autoload/lsp/utils/position.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/vim-lsp/' content from commit 04428c92
[etc/vim.git] / autoload / lsp / utils / position.vim
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)
8     if l:lines == []
9         if type(a:expr) != v:t_string || !filereadable(a:expr)
10             " invalid a:expr
11             return a:char + 1
12         endif
13         " a:expr is a file that is not yet loaded as a buffer
14         let l:lines = readfile(a:expr, '', a:lnum)
15         if l:lines == []
16             " when the file is empty. a:char should be 0 in the case
17             return a:char + 1
18         endif
19     endif
20     let l:linestr = l:lines[-1]
21     return strlen(strcharpart(l:linestr, 0, a:char)) + 1
22 endfunction
23
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)
28     if l:lines == []
29         if type(a:expr) != v:t_string || !filereadable(a:expr)
30             " invalid a:expr
31             return a:col - 1
32         endif
33         " a:expr is a file that is not yet loaded as a buffer
34         let l:lines = readfile(a:expr, '', a:lnum)
35     endif
36     let l:linestr = l:lines[-1]
37     return strchars(strpart(l:linestr, 0, a:col - 1))
38 endfunction
39
40 " @param expr = see :help bufname()
41 " @param position = {
42 "   'line': 1,
43 "   'character': 1
44 " }
45 " @returns [
46 "   line,
47 "   col
48 " ]
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]
53 endfunction
54
55 " @param expr = see :help bufname()
56 " @param position = {
57 "   'line': 1,
58 "   'character': 1
59 " }
60 " @returns
61 "   line
62 function! lsp#utils#position#lsp_line_to_vim(expr, position) abort
63     return a:position['line'] + 1
64 endfunction
65
66 " @param expr = see :help bufname()
67 " @param position = {
68 "   'line': 1,
69 "   'character': 1
70 " }
71 " @returns
72 "   line
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)
77 endfunction
78
79 " @param expr = :help bufname()
80 " @param pos = [lnum, col]
81 " @returns {
82 "   'line': line,
83 "   'character': character
84 " }
85 function! lsp#utils#position#vim_to_lsp(expr, pos) abort
86     return {
87          \   'line': a:pos[0] - 1,
88          \   'character': s:to_char(a:expr, a:pos[0], a:pos[1])
89          \ }
90 endfunction
91