#517 Implement some LSP message handling

This commit is contained in:
w0rp 2017-05-07 23:54:14 +01:00
parent 32f21751f4
commit cd79ced839
3 changed files with 412 additions and 0 deletions

93
autoload/ale/lsp.vim Normal file
View File

@ -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

View File

@ -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

View File

@ -0,0 +1,254 @@
Before:
let g:ale_lsp_next_message_id = 1
function CheckMessage(message, expected_method_name, ...) abort
if a:0 > 1
throw 'Too many arguments!'
endif
let l:match = matchlist(a:message, '\v^Content-Length: (\d+)' . "\r\n\r\n" . '(.+)$')
if empty(l:match)
Assert 0, 'Invalid message format: ' . a:message
endif
if strlen(l:match[2]) < str2nr(l:match[1])
Assert 0, 'Invalid Content-Length (' . l:match[1] . ') :' . a:message
endif
let l:expected_json = {
\ 'id': g:ale_lsp_next_message_id - 1,
\ 'jsonrpc': '2.0',
\ 'method': a:expected_method_name,
\}
if a:0 > 0
let l:expected_json.params = a:1
endif
AssertEqual l:expected_json, json_decode(l:match[2])
endfunction
function Range(start_line, start_char, end_line, end_char) abort
return {
\ 'start': {'line': a:start_line, 'character': a:start_char},
\ 'end': {'line': a:end_line, 'character': a:end_char},
\}
endfunction
After:
delfunction CheckMessage
delfunction Range
Execute(GetNextMessageID() should increment appropriately):
" We should get the initial ID, and increment a bit.
AssertEqual 1, ale#lsp#GetNextMessageID()
AssertEqual 2, ale#lsp#GetNextMessageID()
AssertEqual 3, ale#lsp#GetNextMessageID()
" Set the maximum ID.
let g:ale_lsp_next_message_id = 9223372036854775807
" When we hit the maximum ID, the next ID afterwards should be 1.
AssertEqual 9223372036854775807, ale#lsp#GetNextMessageID()
AssertEqual 1, ale#lsp#GetNextMessageID()
Execute(ale#lsp#CreateMessage() should create an appropriate message):
" 71 is the size in bytes for UTF-8, not the number of characters.
AssertEqual
\ "Content-Length: 71\r\n\r\n"
\ . '{"id":1,"jsonrpc":"2.0","method":"someMethod","params":{"foo":"barÜ"}}',
\ ale#lsp#CreateMessage('someMethod', {'foo': 'barÜ'})
" Check again to ensure that we use the next ID.
AssertEqual
\ "Content-Length: 71\r\n\r\n"
\ . '{"id":2,"jsonrpc":"2.0","method":"someMethod","params":{"foo":"barÜ"}}',
\ ale#lsp#CreateMessage('someMethod', {'foo': 'barÜ'})
Execute(ale#lsp#ReadMessage() should read messages correctly):
AssertEqual
\ {'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}},
\ ale#lsp#ReadMessage(
\ "Content-Length: 49\r\n\r\n"
\ . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}'
\ )
Execute(ale#lsp#message#Initialize() should return correct messages):
call CheckMessage(
\ ale#lsp#message#Initialize(123, '/foo/bar'),
\ 'initialize',
\ {
\ 'processId': 123,
\ 'rootUri': '/foo/bar',
\ 'capabilities': {},
\ }
\)
Execute(ale#lsp#message#Initialized() should return correct messages):
call CheckMessage(ale#lsp#message#Initialized(), 'initialized')
Execute(ale#lsp#message#Shutdown() should return correct messages):
call CheckMessage(ale#lsp#message#Shutdown(), 'shutdown')
Execute(ale#lsp#message#Exit() should return correct messages):
call CheckMessage(ale#lsp#message#Exit(), 'exit')
Execute(ale#lsp#message#DidOpen() should return correct messages):
call CheckMessage(
\ ale#lsp#message#DidOpen('/foo/bar', 'typescript', 123, 'foobar'),
\ 'textDocument/didOpen',
\ {
\ 'textDocument': {
\ 'uri': '/foo/bar',
\ 'languageId': 'typescript',
\ 'version': 123,
\ 'text': 'foobar',
\ },
\ }
\)
Execute(ale#lsp#message#DidChange() should return correct messages):
call CheckMessage(
\ ale#lsp#message#DidChange('/foo/bar', 123, 'foobar'),
\ 'textDocument/didChange',
\ {
\ 'textDocument': {
\ 'uri': '/foo/bar',
\ 'version': 123,
\ },
\ 'contentChanges': [{'text': 'foobar'}]
\ }
\)
Execute(ale#lsp#message#DidSave() should return correct messages):
call CheckMessage(
\ ale#lsp#message#DidSave('/foo/bar'),
\ 'textDocument/didSave',
\ {
\ 'textDocument': {
\ 'uri': '/foo/bar',
\ },
\ }
\)
Execute(ale#lsp#message#DidClose() should return correct messages):
call CheckMessage(
\ ale#lsp#message#DidClose('/foo/bar'),
\ 'textDocument/didClose',
\ {
\ 'textDocument': {
\ 'uri': '/foo/bar',
\ },
\ }
\)
Execute(ale#lsp#ReadDiagnostics() should handle errors):
AssertEqual ['filename.ts', [
\ {
\ 'type': 'E',
\ 'message': 'Something went wrong!',
\ 'lnum': 3,
\ 'col': 11,
\ 'end_lnum': 5,
\ 'end_col': 16,
\ 'nr': 'some-error',
\ }
\ ]],
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
\ {
\ 'severity': 1,
\ 'range': Range(2, 10, 4, 15),
\ 'code': 'some-error',
\ 'message': 'Something went wrong!',
\ },
\ ]})
Execute(ale#lsp#ReadDiagnostics() should handle warnings):
AssertEqual ['filename.ts', [
\ {
\ 'type': 'W',
\ 'message': 'Something went wrong!',
\ 'lnum': 2,
\ 'col': 4,
\ 'end_lnum': 2,
\ 'end_col': 4,
\ 'nr': 'some-warning',
\ }
\ ]],
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
\ {
\ 'severity': 2,
\ 'range': Range(1, 3, 1, 3),
\ 'code': 'some-warning',
\ 'message': 'Something went wrong!',
\ },
\ ]})
Execute(ale#lsp#ReadDiagnostics() should treat messages with missing severity as errors):
AssertEqual ['filename.ts', [
\ {
\ 'type': 'E',
\ 'message': 'Something went wrong!',
\ 'lnum': 3,
\ 'col': 11,
\ 'end_lnum': 5,
\ 'end_col': 16,
\ 'nr': 'some-error',
\ }
\ ]],
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
\ {
\ 'range': Range(2, 10, 4, 15),
\ 'code': 'some-error',
\ 'message': 'Something went wrong!',
\ },
\ ]})
Execute(ale#lsp#ReadDiagnostics() should handle messages without codes):
AssertEqual ['filename.ts', [
\ {
\ 'type': 'E',
\ 'message': 'Something went wrong!',
\ 'lnum': 3,
\ 'col': 11,
\ 'end_lnum': 5,
\ 'end_col': 16,
\ }
\ ]],
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
\ {
\ 'range': Range(2, 10, 4, 15),
\ 'message': 'Something went wrong!',
\ },
\ ]})
Execute(ale#lsp#ReadDiagnostics() should handle multiple messages):
AssertEqual ['filename.ts', [
\ {
\ 'type': 'E',
\ 'message': 'Something went wrong!',
\ 'lnum': 1,
\ 'col': 3,
\ 'end_lnum': 1,
\ 'end_col': 3,
\ },
\ {
\ 'type': 'W',
\ 'message': 'A warning',
\ 'lnum': 2,
\ 'col': 5,
\ 'end_lnum': 2,
\ 'end_col': 5,
\ },
\ ]],
\ ale#lsp#ReadDiagnostics({'uri': 'filename.ts', 'diagnostics': [
\ {
\ 'range': Range(0, 2, 0, 2),
\ 'message': 'Something went wrong!',
\ },
\ {
\ 'severity': 2,
\ 'range': Range(1, 4, 1, 4),
\ 'message': 'A warning',
\ },
\ ]})