summaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2017-05-07 23:54:14 +0100
committerw0rp <devw0rp@gmail.com>2017-05-07 23:54:14 +0100
commitcd79ced839fa2a5c3fc407d7cbe0cdf6734d17da (patch)
treeb85184492377f12d2d9c9540e26eebe183ad58bd /autoload
parent32f21751f4722abdb10b9da3b6cf088240d46421 (diff)
downloadale-cd79ced839fa2a5c3fc407d7cbe0cdf6734d17da.zip
#517 Implement some LSP message handling
Diffstat (limited to 'autoload')
-rw-r--r--autoload/ale/lsp.vim93
-rw-r--r--autoload/ale/lsp/message.vim65
2 files changed, 158 insertions, 0 deletions
diff --git a/autoload/ale/lsp.vim b/autoload/ale/lsp.vim
new file mode 100644
index 00000000..e01e4eb6
--- /dev/null
+++ b/autoload/ale/lsp.vim
@@ -0,0 +1,93 @@
+" Author: w0rp <devw0rp@gmail.com>
+" Description: Language Server Protocol client code
+
+let g:ale_lsp_next_message_id = 1
+
+function! ale#lsp#GetNextMessageID() abort
+ " Use the current ID
+ let l:id = g:ale_lsp_next_message_id
+
+ " Increment the ID variable.
+ let g:ale_lsp_next_message_id += 1
+
+ " When the ID overflows, reset it to 1. By the time we hit the initial ID
+ " again, the messages will be long gone.
+ if g:ale_lsp_next_message_id < 1
+ let g:ale_lsp_next_message_id = 1
+ endif
+
+ return l:id
+endfunction
+
+" (method_name, params)
+function! ale#lsp#CreateMessage(method_name, ...) abort
+ if a:0 > 1
+ throw 'Too many arguments!'
+ endif
+
+ let l:obj = {
+ \ 'id': ale#lsp#GetNextMessageID(),
+ \ 'jsonrpc': '2.0',
+ \ 'method': a:method_name,
+ \}
+
+ if a:0 > 0
+ let l:obj.params = a:1
+ endif
+
+ let l:body = json_encode(l:obj)
+
+ return 'Content-Length: ' . strlen(l:body) . "\r\n\r\n" . l:body
+endfunction
+
+function! ale#lsp#ReadMessage(data) abort
+ let l:header_end_index = match(a:data, "\r\n\r\n")
+
+ if l:header_end_index < 0
+ throw 'Invalid messaage: ' . string(a:data)
+ endif
+
+ return json_decode(a:data[l:header_end_index + 4:])
+endfunction
+
+" Constants for message severity codes.
+let s:SEVERITY_ERROR = 1
+let s:SEVERITY_WARNING = 2
+let s:SEVERITY_INFORMATION = 3
+let s:SEVERITY_HINT = 4
+
+" Parse the message for textDocument/publishDiagnostics
+function! ale#lsp#ReadDiagnostics(params) abort
+ let l:filename = a:params.uri
+ let l:loclist = []
+
+ for l:diagnostic in a:params.diagnostics
+ let l:severity = get(l:diagnostic, 'severity', 0)
+ let l:loclist_item = {
+ \ 'message': l:diagnostic.message,
+ \ 'type': 'E',
+ \ 'lnum': l:diagnostic.range.start.line + 1,
+ \ 'col': l:diagnostic.range.start.character + 1,
+ \ 'end_lnum': l:diagnostic.range.end.line + 1,
+ \ 'end_col': l:diagnostic.range.end.character + 1,
+ \}
+
+ if l:severity == s:SEVERITY_WARNING
+ let l:loclist_item.type = 'W'
+ elseif l:severity == s:SEVERITY_INFORMATION
+ " TODO: Use 'I' here in future.
+ let l:loclist_item.type = 'W'
+ elseif l:severity == s:SEVERITY_HINT
+ " TODO: Use 'H' here in future
+ let l:loclist_item.type = 'W'
+ endif
+
+ if has_key(l:diagnostic, 'code')
+ let l:loclist_item.nr = l:diagnostic.code
+ endif
+
+ call add(l:loclist, l:loclist_item)
+ endfor
+
+ return [l:filename, l:loclist]
+endfunction
diff --git a/autoload/ale/lsp/message.vim b/autoload/ale/lsp/message.vim
new file mode 100644
index 00000000..d46e68ab
--- /dev/null
+++ b/autoload/ale/lsp/message.vim
@@ -0,0 +1,65 @@
+" Author: w0rp <devw0rp@gmail.com>
+" Description: Language Server Protocol message implementations
+
+function! ale#lsp#message#CancelRequest(id) abort
+ return ale#lsp#CreateMessage('$/cancelRequest', {'id': a:id})
+endfunction
+
+function! ale#lsp#message#Initialize(processId, rootUri) abort
+ " TODO: Define needed capabilities.
+ return ale#lsp#CreateMessage('initialize', {
+ \ 'processId': a:processId,
+ \ 'rootUri': a:rootUri,
+ \ 'capabilities': {},
+ \})
+endfunction
+
+function! ale#lsp#message#Initialized() abort
+ return ale#lsp#CreateMessage('initialized')
+endfunction
+
+function! ale#lsp#message#Shutdown() abort
+ return ale#lsp#CreateMessage('shutdown')
+endfunction
+
+function! ale#lsp#message#Exit() abort
+ return ale#lsp#CreateMessage('exit')
+endfunction
+
+function! ale#lsp#message#DidOpen(uri, languageId, version, text) abort
+ return ale#lsp#CreateMessage('textDocument/didOpen', {
+ \ 'textDocument': {
+ \ 'uri': a:uri,
+ \ 'languageId': a:languageId,
+ \ 'version': a:version,
+ \ 'text': a:text,
+ \ },
+ \})
+endfunction
+
+function! ale#lsp#message#DidChange(uri, version, text) abort
+ " For changes, we simply send the full text of the document to the server.
+ return ale#lsp#CreateMessage('textDocument/didChange', {
+ \ 'textDocument': {
+ \ 'uri': a:uri,
+ \ 'version': a:version,
+ \ },
+ \ 'contentChanges': [{'text': a:text}]
+ \})
+endfunction
+
+function! ale#lsp#message#DidSave(uri) abort
+ return ale#lsp#CreateMessage('textDocument/didSave', {
+ \ 'textDocument': {
+ \ 'uri': a:uri,
+ \ },
+ \})
+endfunction
+
+function! ale#lsp#message#DidClose(uri) abort
+ return ale#lsp#CreateMessage('textDocument/didClose', {
+ \ 'textDocument': {
+ \ 'uri': a:uri,
+ \ },
+ \})
+endfunction