#517 - Implement LSP chunked message parsing, sending messages to sockets, and callbacks

This commit is contained in:
w0rp
2017-05-08 22:18:28 +01:00
parent cd79ced839
commit 28c6ec9cad
7 changed files with 492 additions and 334 deletions
+21 -22
View File
@@ -1,65 +1,64 @@
" Author: w0rp <devw0rp@gmail.com>
" Description: Language Server Protocol message implementations
"
" Messages in this movie will be returned in the format
" [is_notification, method_name, params?]
function! ale#lsp#message#CancelRequest(id) abort
return ale#lsp#CreateMessage('$/cancelRequest', {'id': a:id})
endfunction
function! ale#lsp#message#Initialize(processId, rootUri) abort
function! ale#lsp#message#Initialize(root_uri) abort
" TODO: Define needed capabilities.
return ale#lsp#CreateMessage('initialize', {
\ 'processId': a:processId,
\ 'rootUri': a:rootUri,
return [0, 'initialize', {
\ 'processId': getpid(),
\ 'rootUri': a:root_uri,
\ 'capabilities': {},
\})
\}]
endfunction
function! ale#lsp#message#Initialized() abort
return ale#lsp#CreateMessage('initialized')
return [1, 'initialized']
endfunction
function! ale#lsp#message#Shutdown() abort
return ale#lsp#CreateMessage('shutdown')
return [0, 'shutdown']
endfunction
function! ale#lsp#message#Exit() abort
return ale#lsp#CreateMessage('exit')
return [1, 'exit']
endfunction
function! ale#lsp#message#DidOpen(uri, languageId, version, text) abort
return ale#lsp#CreateMessage('textDocument/didOpen', {
function! ale#lsp#message#DidOpen(uri, language_id, version, text) abort
return [1, 'textDocument/didOpen', {
\ 'textDocument': {
\ 'uri': a:uri,
\ 'languageId': a:languageId,
\ 'languageId': a:language_id,
\ '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', {
return [1, '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', {
return [1, 'textDocument/didSave', {
\ 'textDocument': {
\ 'uri': a:uri,
\ },
\})
\}]
endfunction
function! ale#lsp#message#DidClose(uri) abort
return ale#lsp#CreateMessage('textDocument/didClose', {
return [1, 'textDocument/didClose', {
\ 'textDocument': {
\ 'uri': a:uri,
\ },
\})
\}]
endfunction
+44
View File
@@ -0,0 +1,44 @@
" Author: w0rp <devw0rp@gmail.com>
" Description: Parsing and transforming of LSP server responses.
" 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#response#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