diff --git a/autoload/ale/lsp.vim b/autoload/ale/lsp.vim index 29759f66..b7f8c542 100644 --- a/autoload/ale/lsp.vim +++ b/autoload/ale/lsp.vim @@ -241,9 +241,8 @@ function! ale#lsp#HandleMessage(conn, message) abort endfor endfunction -function! s:HandleChannelMessage(channel, message) abort - let l:info = ch_info(a:channel) - let l:address = l:info.hostname . l:info.address +function! s:HandleChannelMessage(channel_id, message) abort + let l:address = ale#socket#GetAddress(a:channel_id) let l:conn = s:FindConnection('id', l:address) call ale#lsp#HandleMessage(l:conn, a:message) @@ -319,15 +318,13 @@ function! ale#lsp#ConnectToAddress(address, project_root, callback, initializati " Get the current connection or a new one. let l:conn = !empty(l:conn) ? l:conn : ale#lsp#NewConnection(a:initialization_options) - if !has_key(l:conn, 'channel') || ch_status(l:conn.channel) isnot# 'open' - let l:conn.channnel = ch_open(a:address, { - \ 'mode': 'raw', - \ 'waittime': 0, + if !has_key(l:conn, 'channel_id') || !ale#socket#IsOpen(l:conn.channel_id) + let l:conn.channnel_id = ale#socket#Open(a:address, { \ 'callback': function('s:HandleChannelMessage'), \}) endif - if ch_status(l:conn.channnel) is# 'fail' + if l:conn.channnel_id < 0 return 0 endif @@ -343,8 +340,8 @@ endfunction " queued messages. function! ale#lsp#StopAll() abort for l:conn in s:connections - if has_key(l:conn, 'channel') - call ch_close(l:conn.channel) + if has_key(l:conn, 'channel_id') + call ale#socket#Close(l:conn.channel_id) else call ale#job#Stop(l:conn.id) endif @@ -356,9 +353,9 @@ 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) is# 'open' + elseif has_key(a:conn, 'channel_id') && ale#socket#IsOpen(a:conn.channel_id) " Send the message to the server - call ch_sendraw(a:conn.channel, a:data) + call ale#socket#Send(a:conn.channel_id, a:data) else return 0 endif diff --git a/autoload/ale/socket.vim b/autoload/ale/socket.vim index 78eba737..0ca4dea6 100644 --- a/autoload/ale/socket.vim +++ b/autoload/ale/socket.vim @@ -9,6 +9,7 @@ " ale#socket#IsOpen(channel_id) -> 1 if open, 0 otherwise " ale#socket#Close(channel_id) " ale#socket#Send(channel_id, data) +" ale#socket#GetAddress(channel_id) -> Return the address for a job let s:channel_map = get(s:, 'channel_map', {}) @@ -47,6 +48,7 @@ function! ale#socket#Open(address, options) abort let l:Callback = a:options.callback let l:channel_info = { + \ 'address': a:address, \ 'mode': l:mode, \ 'callback': a:options.callback, \} @@ -135,3 +137,8 @@ function! ale#socket#Send(channel_id, data) abort call ch_sendraw(l:channel, a:data) endif endfunction + +" Get an address for a channel, or an empty string. +function! ale#socket#GetAddress(channel_id) abort + return get(get(s:channel_map, a:channel_id, {}), 'address', '') +endfunction diff --git a/test/test_socket_connections.vader b/test/test_socket_connections.vader index 71a1728b..6837fe51 100644 --- a/test/test_socket_connections.vader +++ b/test/test_socket_connections.vader @@ -75,10 +75,12 @@ Execute(Sending and receiving connections to sockets should work): AssertEqual g:channel_id, g:channel_id_received AssertEqual 'hello world', g:data_received + AssertEqual '127.0.0.1:' . g:port, ale#socket#GetAddress(g:channel_id) call ale#socket#Close(g:channel_id) AssertEqual 0, ale#socket#IsOpen(g:channel_id) + AssertEqual '', ale#socket#GetAddress(g:channel_id) endif " NeoVim versions which can't connect to sockets should just fail.