Fix #2295 - Respond to initialize with an initialized message

This commit is contained in:
w0rp
2019-02-15 08:54:53 +00:00
parent 89039187da
commit 0d10653a7c
10 changed files with 132 additions and 57 deletions

View File

@@ -21,7 +21,6 @@ function! ale#lsp#Register(executable_or_address, project, init_options) abort
" init_options: Options to send to the server.
" config: Configuration settings to send to the server.
" callback_list: A list of callbacks for handling LSP responses.
" message_queue: Messages queued for sending to callbacks.
" capabilities_queue: The list of callbacks to call with capabilities.
" capabilities: Features the server supports.
let s:connections[l:conn_id] = {
@@ -35,7 +34,6 @@ function! ale#lsp#Register(executable_or_address, project, init_options) abort
\ 'init_options': a:init_options,
\ 'config': {},
\ 'callback_list': [],
\ 'message_queue': [],
\ 'init_queue': [],
\ 'capabilities': {
\ 'hover': 0,
@@ -250,13 +248,8 @@ function! ale#lsp#HandleInitResponse(conn, response) abort
return
endif
" After the server starts, send messages we had queued previously.
for l:message_data in a:conn.message_queue
call s:SendMessageData(a:conn, l:message_data)
endfor
" Remove the messages now.
let a:conn.message_queue = []
" The initialized message must be sent before everything else.
call ale#lsp#Send(a:conn.id, ale#lsp#message#Initialized())
" Call capabilities callbacks queued for the project.
for l:Callback in a:conn.init_queue
@@ -317,14 +310,23 @@ function! ale#lsp#MarkConnectionAsTsserver(conn_id) abort
let l:conn.capabilities.symbol_search = 1
endfunction
function! s:SendInitMessage(conn) abort
let [l:init_id, l:init_data] = ale#lsp#CreateMessageData(
\ ale#lsp#message#Initialize(a:conn.root, a:conn.init_options),
\)
let a:conn.init_request_id = l:init_id
call s:SendMessageData(a:conn, l:init_data)
endfunction
" Start a program for LSP servers.
"
" 1 will be returned if the program is running, or 0 if the program could
" not be started.
function! ale#lsp#StartProgram(conn_id, executable, command) abort
let l:conn = s:connections[a:conn_id]
let l:started = 0
if !has_key(l:conn, 'job_id') || !ale#job#IsRunning(l:conn.job_id)
if !has_key(l:conn, 'job_id') || !ale#job#HasOpenChannel(l:conn.job_id)
let l:options = {
\ 'mode': 'raw',
\ 'out_cb': {_, message -> ale#lsp#HandleMessage(a:conn_id, message)},
@@ -335,6 +337,8 @@ function! ale#lsp#StartProgram(conn_id, executable, command) abort
else
let l:job_id = ale#job#Start(a:command, l:options)
endif
let l:started = 1
else
let l:job_id = l:conn.job_id
endif
@@ -343,6 +347,10 @@ function! ale#lsp#StartProgram(conn_id, executable, command) abort
let l:conn.job_id = l:job_id
endif
if l:started
call s:SendInitMessage(l:conn)
endif
return l:job_id > 0
endfunction
@@ -352,11 +360,14 @@ endfunction
" not be opened.
function! ale#lsp#ConnectToAddress(conn_id, address) abort
let l:conn = s:connections[a:conn_id]
let l:started = 0
if !has_key(l:conn, 'channel_id') || !ale#socket#IsOpen(l:conn.channel_id)
let l:channel_id = ale#socket#Open(a:address, {
\ 'callback': {_, mess -> ale#lsp#HandleMessage(a:conn_id, mess)},
\})
let l:started = 1
else
let l:channel_id = l:conn.channel_id
endif
@@ -365,6 +376,10 @@ function! ale#lsp#ConnectToAddress(conn_id, address) abort
let l:conn.channel_id = l:channel_id
endif
if l:started
call s:SendInitMessage(l:conn)
endif
return l:channel_id >= 0
endfunction
@@ -429,26 +444,12 @@ function! ale#lsp#Send(conn_id, message) abort
return 0
endif
" If we haven't initialized the server yet, then send the message for it.
if !l:conn.initialized && !l:conn.init_request_id
let [l:init_id, l:init_data] = ale#lsp#CreateMessageData(
\ ale#lsp#message#Initialize(l:conn.root, l:conn.init_options),
\)
let l:conn.init_request_id = l:init_id
call s:SendMessageData(l:conn, l:init_data)
if !l:conn.initialized
throw 'LSP server not initialized yet!'
endif
let [l:id, l:data] = ale#lsp#CreateMessageData(a:message)
if l:conn.initialized
" Send the message now.
call s:SendMessageData(l:conn, l:data)
else
" Add the message we wanted to send to a List to send later.
call add(l:conn.message_queue, l:data)
endif
call s:SendMessageData(l:conn, l:data)
return l:id == 0 ? -1 : l:id
endfunction