mirror of
https://github.com/dense-analysis/ale.git
synced 2026-02-08 10:51:06 +08:00
#517 Add more code LSP support which makes the tssserver linter behave more like the LSP linters
This commit is contained in:
@@ -11,10 +11,13 @@ function! s:NewConnection() abort
|
||||
" data: The message data received so far.
|
||||
" executable: An executable only set for program connections.
|
||||
" open_documents: A list of buffers we told the server we opened.
|
||||
" callback_list: A list of callbacks for handling LSP responses.
|
||||
let l:conn = {
|
||||
\ 'id': '',
|
||||
\ 'data': '',
|
||||
\ 'projects': {},
|
||||
\ 'open_documents': [],
|
||||
\ 'callback_list': [],
|
||||
\}
|
||||
|
||||
call add(s:connections, l:conn)
|
||||
@@ -141,6 +144,35 @@ function! ale#lsp#ReadMessageData(data) abort
|
||||
return [l:remainder, l:response_list]
|
||||
endfunction
|
||||
|
||||
function! s:FindProjectWithInitRequestID(conn, init_request_id) abort
|
||||
for l:project_root in keys(a:conn.projects)
|
||||
let l:project = a:conn.projects[l:project_root]
|
||||
|
||||
if l:project.init_request_id == a:init_request_id
|
||||
return l:project
|
||||
endif
|
||||
endfor
|
||||
|
||||
return {}
|
||||
endfunction
|
||||
|
||||
function! s:HandleInitializeResponse(conn, response) abort
|
||||
let l:request_id = a:response.request_id
|
||||
let l:project = s:FindProjectWithInitRequestID(a:conn, l:request_id)
|
||||
|
||||
if empty(l:project)
|
||||
return
|
||||
endif
|
||||
|
||||
" After the server starts, send messages we had queued previously.
|
||||
for l:message_data in l:project.message_queue
|
||||
call s:SendMessageData(a:conn, l:message_data)
|
||||
endfor
|
||||
|
||||
" Remove the messages now.
|
||||
let a:conn.message_queue = []
|
||||
endfunction
|
||||
|
||||
function! ale#lsp#HandleMessage(conn, message) abort
|
||||
let a:conn.data .= a:message
|
||||
|
||||
@@ -149,8 +181,13 @@ function! ale#lsp#HandleMessage(conn, message) abort
|
||||
|
||||
" Call our callbacks.
|
||||
for l:response in l:response_list
|
||||
if has_key(a:conn, 'callback')
|
||||
call ale#util#GetFunction(a:conn.callback)(l:response)
|
||||
if get(l:response, 'method', '') ==# 'initialize'
|
||||
call s:HandleInitializeResponse(a:conn, l:response)
|
||||
else
|
||||
" Call all of the registered handlers with the response.
|
||||
for l:Callback in a:conn.callback_list
|
||||
call ale#util#GetFunction(l:Callback)(l:response)
|
||||
endfor
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
@@ -169,11 +206,22 @@ function! s:HandleCommandMessage(job_id, message) abort
|
||||
call ale#lsp#HandleMessage(l:conn, a:message)
|
||||
endfunction
|
||||
|
||||
function! s:RegisterProject(conn, project_root) abort
|
||||
if !has_key(a:conn, a:project_root)
|
||||
" Tools without project roots are ready right away, like tsserver.
|
||||
let a:conn.projects[a:project_root] = {
|
||||
\ 'initialized': empty(a:project_root),
|
||||
\ 'init_messsage_id': 0,
|
||||
\ 'message_queue': [],
|
||||
\}
|
||||
endif
|
||||
endfunction
|
||||
|
||||
" Start a program for LSP servers which run with executables.
|
||||
"
|
||||
" The job ID will be returned for for the program if it ran, otherwise
|
||||
" 0 will be returned.
|
||||
function! ale#lsp#StartProgram(executable, command, callback) abort
|
||||
function! ale#lsp#StartProgram(executable, command, project_root, callback) abort
|
||||
if !executable(a:executable)
|
||||
return 0
|
||||
endif
|
||||
@@ -199,13 +247,15 @@ function! ale#lsp#StartProgram(executable, command, callback) abort
|
||||
endif
|
||||
|
||||
let l:conn.id = l:job_id
|
||||
let l:conn.callback = a:callback
|
||||
" Add the callback to the List if it's not there already.
|
||||
call uniq(sort(add(l:conn.callback_list, a:callback)))
|
||||
call s:RegisterProject(l:conn, a:project_root)
|
||||
|
||||
return l:job_id
|
||||
endfunction
|
||||
|
||||
" Connect to an address and set up a callback for handling responses.
|
||||
function! ale#lsp#ConnectToAddress(address, callback) abort
|
||||
function! ale#lsp#ConnectToAddress(address, project_root, callback) abort
|
||||
let l:conn = s:FindConnection('id', a:address)
|
||||
" Get the current connection or a new one.
|
||||
let l:conn = !empty(l:conn) ? l:conn : s:NewConnection()
|
||||
@@ -223,7 +273,22 @@ function! ale#lsp#ConnectToAddress(address, callback) abort
|
||||
endif
|
||||
|
||||
let l:conn.id = a:address
|
||||
let l:conn.callback = a:callback
|
||||
" Add the callback to the List if it's not there already.
|
||||
call uniq(sort(add(l:conn.callback_list, a:callback)))
|
||||
call s:RegisterProject(l:conn, a:project_root)
|
||||
|
||||
return 1
|
||||
endfunction
|
||||
|
||||
function! s:SendMessageData(conn, data) abort
|
||||
if has_key(a:conn, 'executable')
|
||||
call ale#job#SendRaw(a:conn.id, a:data)
|
||||
elseif has_key(a:conn, 'channel') && ch_status(a:conn.channnel) ==# 'open'
|
||||
" Send the message to the server
|
||||
call ch_sendraw(a:conn.channel, a:data)
|
||||
else
|
||||
return 0
|
||||
endif
|
||||
|
||||
return 1
|
||||
endfunction
|
||||
@@ -234,28 +299,60 @@ endfunction
|
||||
" Returns -1 when a message is sent, but no response is expected
|
||||
" 0 when the message is not sent and
|
||||
" >= 1 with the message ID when a response is expected.
|
||||
function! ale#lsp#Send(conn_id, message) abort
|
||||
function! ale#lsp#Send(conn_id, message, ...) abort
|
||||
let l:project_root = get(a:000, 0, '')
|
||||
|
||||
let l:conn = s:FindConnection('id', a:conn_id)
|
||||
|
||||
if empty(l:conn)
|
||||
return 0
|
||||
endif
|
||||
|
||||
let l:project = get(l:conn.projects, l:project_root, {})
|
||||
|
||||
if empty(l:project)
|
||||
return 0
|
||||
endif
|
||||
|
||||
" If we haven't initialized the server yet, then send the message for it.
|
||||
if !l:project.initialized
|
||||
" Only send the init message once.
|
||||
if !l:project.init_request_id
|
||||
let [l:init_id, l:init_data] = ale#lsp#CreateMessageData(
|
||||
\ ale#lsp#message#Initialize(l:conn.project_root),
|
||||
\)
|
||||
|
||||
let l:project.init_request_id = l:init_id
|
||||
|
||||
call s:SendMessageData(l:conn, l:init_data)
|
||||
endif
|
||||
endif
|
||||
|
||||
let [l:id, l:data] = ale#lsp#CreateMessageData(a:message)
|
||||
|
||||
if has_key(l:conn, 'executable')
|
||||
call ale#job#SendRaw(l:conn.id, l:data)
|
||||
elseif has_key(l:conn, 'channel') && ch_status(l:conn.channnel) ==# 'open'
|
||||
" Send the message to the server
|
||||
call ch_sendraw(l:conn.channel, l:data)
|
||||
if l:project.initialized
|
||||
" Send the message now.
|
||||
call s:SendMessageData(l:conn, l:data)
|
||||
else
|
||||
return 0
|
||||
" Add the message we wanted to send to a List to send later.
|
||||
call add(l:project.message_queue, l:data)
|
||||
endif
|
||||
|
||||
return l:id == 0 ? -1 : l:id
|
||||
endfunction
|
||||
|
||||
function! ale#lsp#OpenTSServerDocumentIfNeeded(conn_id, buffer) abort
|
||||
function! ale#lsp#OpenDocumentIfNeeded(conn_id, buffer, project_root, language_id) abort
|
||||
let l:conn = s:FindConnection('id', a:conn_id)
|
||||
let l:opened = 0
|
||||
|
||||
if !empty(l:conn) && index(l:conn.open_documents, a:buffer) < 0
|
||||
call ale#lsp#Send(a:conn_id, ale#lsp#tsserver_message#Open(a:buffer))
|
||||
if empty(a:language_id)
|
||||
let l:message = ale#lsp#tsserver_message#Open(a:buffer)
|
||||
else
|
||||
let l:message = ale#lsp#message#DidOpen(a:buffer, a:language_id)
|
||||
endif
|
||||
|
||||
call ale#lsp#Send(a:conn_id, l:message, a:project_root)
|
||||
call add(l:conn.open_documents, a:buffer)
|
||||
let l:opened = 1
|
||||
endif
|
||||
|
||||
Reference in New Issue
Block a user