]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/lsp/message.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/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / lsp / message.vim
1 " Author: w0rp <devw0rp@gmail.com>
2 " Description: Language Server Protocol message implementations
3 "
4 " Messages in this movie will be returned in the format
5 " [is_notification, method_name, params?]
6 "
7 " All functions which accept line and column arguments expect them to be 1-based
8 " (the same format as being returned by getpos() and friends), those then
9 " will be converted to 0-based as specified by LSP.
10 let g:ale_lsp_next_version_id = 1
11
12 " The LSP protocols demands that we send every change to a document, including
13 " undo, with incrementing version numbers, so we'll just use one incrementing
14 " ID for everything.
15 function! ale#lsp#message#GetNextVersionID() abort
16     " Use the current ID
17     let l:id = g:ale_lsp_next_version_id
18
19     " Increment the ID variable.
20     let g:ale_lsp_next_version_id += 1
21
22     " When the ID overflows, reset it to 1. By the time we hit the initial ID
23     " again, the messages will be long gone.
24     if g:ale_lsp_next_version_id < 1
25         let g:ale_lsp_next_version_id = 1
26     endif
27
28     return l:id
29 endfunction
30
31 function! ale#lsp#message#Initialize(root_path, options, capabilities) abort
32     " NOTE: rootPath is deprecated in favour of rootUri
33     return [0, 'initialize', {
34     \   'processId': getpid(),
35     \   'rootPath': a:root_path,
36     \   'capabilities': a:capabilities,
37     \   'initializationOptions': a:options,
38     \   'rootUri': ale#util#ToURI(a:root_path),
39     \}]
40 endfunction
41
42 function! ale#lsp#message#Initialized() abort
43     return [1, 'initialized', {}]
44 endfunction
45
46 function! ale#lsp#message#Shutdown() abort
47     return [0, 'shutdown']
48 endfunction
49
50 function! ale#lsp#message#Exit() abort
51     return [1, 'exit']
52 endfunction
53
54 function! ale#lsp#message#DidOpen(buffer, language_id) abort
55     return [1, 'textDocument/didOpen', {
56     \   'textDocument': {
57     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
58     \       'languageId': a:language_id,
59     \       'version': ale#lsp#message#GetNextVersionID(),
60     \       'text': ale#util#GetBufferContents(a:buffer),
61     \   },
62     \}]
63 endfunction
64
65 function! ale#lsp#message#DidChange(buffer) abort
66     " For changes, we simply send the full text of the document to the server.
67     return [1, 'textDocument/didChange', {
68     \   'textDocument': {
69     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
70     \       'version': ale#lsp#message#GetNextVersionID(),
71     \   },
72     \   'contentChanges': [{'text': ale#util#GetBufferContents(a:buffer)}]
73     \}]
74 endfunction
75
76 function! ale#lsp#message#DidSave(buffer, include_text) abort
77     let l:response = [1, 'textDocument/didSave', {
78     \   'textDocument': {
79     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
80     \   },
81     \}]
82
83     if a:include_text
84         let l:response[2].textDocument.version = ale#lsp#message#GetNextVersionID()
85         let l:response[2].text = ale#util#GetBufferContents(a:buffer)
86     endif
87
88     return l:response
89 endfunction
90
91 function! ale#lsp#message#DidClose(buffer) abort
92     return [1, 'textDocument/didClose', {
93     \   'textDocument': {
94     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
95     \   },
96     \}]
97 endfunction
98
99 let s:COMPLETION_TRIGGER_INVOKED = 1
100 let s:COMPLETION_TRIGGER_CHARACTER = 2
101
102 function! ale#lsp#message#Completion(buffer, line, column, trigger_character) abort
103     let l:message = [0, 'textDocument/completion', {
104     \   'textDocument': {
105     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
106     \   },
107     \   'position': {'line': a:line - 1, 'character': a:column - 1},
108     \}]
109
110     if !empty(a:trigger_character)
111         let l:message[2].context = {
112         \   'triggerKind': s:COMPLETION_TRIGGER_CHARACTER,
113         \   'triggerCharacter': a:trigger_character,
114         \}
115     endif
116
117     return l:message
118 endfunction
119
120 function! ale#lsp#message#Definition(buffer, line, column) abort
121     return [0, 'textDocument/definition', {
122     \   'textDocument': {
123     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
124     \   },
125     \   'position': {'line': a:line - 1, 'character': a:column - 1},
126     \}]
127 endfunction
128
129 function! ale#lsp#message#TypeDefinition(buffer, line, column) abort
130     return [0, 'textDocument/typeDefinition', {
131     \   'textDocument': {
132     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
133     \   },
134     \   'position': {'line': a:line - 1, 'character': a:column - 1},
135     \}]
136 endfunction
137
138 function! ale#lsp#message#Implementation(buffer, line, column) abort
139     return [0, 'textDocument/implementation', {
140     \   'textDocument': {
141     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
142     \   },
143     \   'position': {'line': a:line - 1, 'character': a:column - 1},
144     \}]
145 endfunction
146
147 function! ale#lsp#message#References(buffer, line, column) abort
148     return [0, 'textDocument/references', {
149     \   'textDocument': {
150     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
151     \   },
152     \   'position': {'line': a:line - 1, 'character': a:column - 1},
153     \   'context': {'includeDeclaration': v:false},
154     \}]
155 endfunction
156
157 function! ale#lsp#message#Symbol(query) abort
158     return [0, 'workspace/symbol', {
159     \   'query': a:query,
160     \}]
161 endfunction
162
163 function! ale#lsp#message#Hover(buffer, line, column) abort
164     return [0, 'textDocument/hover', {
165     \   'textDocument': {
166     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
167     \   },
168     \   'position': {'line': a:line - 1, 'character': a:column - 1},
169     \}]
170 endfunction
171
172 function! ale#lsp#message#DidChangeConfiguration(buffer, config) abort
173     return [1, 'workspace/didChangeConfiguration', {
174     \   'settings': a:config,
175     \}]
176 endfunction
177
178 function! ale#lsp#message#Rename(buffer, line, column, new_name) abort
179     return [0, 'textDocument/rename', {
180     \   'textDocument': {
181     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
182     \   },
183     \   'position': {'line': a:line - 1, 'character': a:column - 1},
184     \   'newName': a:new_name,
185     \}]
186 endfunction
187
188 function! ale#lsp#message#CodeAction(buffer, line, column, end_line, end_column, diagnostics) abort
189     return [0, 'textDocument/codeAction', {
190     \   'textDocument': {
191     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
192     \   },
193     \   'range': {
194     \       'start': {'line': a:line - 1, 'character': a:column - 1},
195     \       'end': {'line': a:end_line - 1, 'character': a:end_column},
196     \   },
197     \   'context': {
198     \       'diagnostics': a:diagnostics
199     \   },
200     \}]
201 endfunction
202
203 function! ale#lsp#message#Diagnostic(buffer) abort
204     return [0, 'textDocument/diagnostic', {
205     \   'textDocument': {
206     \       'uri': ale#util#ToURI(expand('#' . a:buffer . ':p')),
207     \   },
208     \}]
209 endfunction
210
211 function! ale#lsp#message#ExecuteCommand(command, arguments) abort
212     return [0, 'workspace/executeCommand', {
213     \   'command': a:command,
214     \   'arguments': a:arguments,
215     \}]
216 endfunction