Use one LSP connection per project

This commit is contained in:
w0rp
2018-08-24 13:16:58 +01:00
parent 9d7c48038c
commit c4eca7c417
13 changed files with 286 additions and 467 deletions

View File

@@ -14,17 +14,17 @@ Before:
let g:message_list = []
let g:capability_checked = ''
let g:conn_id = v:null
let g:Callback = ''
let g:wait_callback_list = []
function! ale#lsp_linter#StartLSP(buffer, linter) abort
let l:conn = ale#lsp#NewConnection({})
let l:conn.id = 347
let l:conn.open_documents = {a:buffer : -1}
let g:conn_id = ale#lsp#Register('executable', '/foo/bar', {})
call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
return {
\ 'buffer': a:buffer,
\ 'connection_id': 347,
\ 'connection_id': g:conn_id,
\ 'project_root': '/foo/bar',
\ 'language_id': 'python',
\}
@@ -35,7 +35,7 @@ Before:
return 'i'
endfunction
function! ale#lsp#WaitForCapability(conn_id, project_root, capability, callback) abort
function! ale#lsp#WaitForCapability(conn_id, capability, callback) abort
let g:capability_checked = a:capability
call add(g:wait_callback_list, a:callback)
endfunction
@@ -45,7 +45,7 @@ Before:
endfunction
" Replace the Send function for LSP, so we can monitor calls to it.
function! ale#lsp#Send(conn_id, message, ...) abort
function! ale#lsp#Send(conn_id, message) abort
call add(g:message_list, a:message)
return 1
@@ -54,9 +54,14 @@ Before:
After:
Restore
if g:conn_id isnot v:null
call ale#lsp#RemoveConnectionWithID(g:conn_id)
endif
unlet! g:message_list
unlet! g:capability_checked
unlet! g:wait_callback_list
unlet! g:conn_id
unlet! g:Callback
unlet! b:ale_old_omnifunc
unlet! b:ale_old_completopt
@@ -72,7 +77,6 @@ After:
return call('mode', a:000)
endfunction
call ale#lsp#RemoveConnectionWithID(347)
call ale#test#RestoreDirectory()
call ale#linter#Reset()
@@ -102,7 +106,7 @@ Execute(The right message should be sent for the initial tsserver request):
AssertEqual 1, len(g:wait_callback_list)
AssertEqual 'completion', g:capability_checked
call map(g:wait_callback_list, 'v:val([347, ''/foo/bar''])')
call map(g:wait_callback_list, 'v:val([g:conn_id, ''/foo/bar''])')
" We should send the right callback.
AssertEqual
@@ -116,7 +120,7 @@ Execute(The right message should be sent for the initial tsserver request):
AssertEqual
\ {
\ 'line_length': 3,
\ 'conn_id': 347,
\ 'conn_id': g:conn_id,
\ 'column': 3,
\ 'request_id': 1,
\ 'line': 1,
@@ -189,7 +193,7 @@ Execute(The right message should be sent for the initial LSP request):
AssertEqual 1, len(g:wait_callback_list)
AssertEqual 'completion', g:capability_checked
call map(g:wait_callback_list, 'v:val([347, ''/foo/bar''])')
call map(g:wait_callback_list, 'v:val([g:conn_id, ''/foo/bar''])')
" We should send the right callback.
AssertEqual
@@ -219,7 +223,7 @@ Execute(The right message should be sent for the initial LSP request):
AssertEqual
\ {
\ 'line_length': 3,
\ 'conn_id': 347,
\ 'conn_id': g:conn_id,
\ 'column': 3,
\ 'request_id': 1,
\ 'line': 1,

View File

@@ -13,6 +13,7 @@ Before:
let b:ale_enabled = 1
let g:ale_lsp_next_message_id = 1
let g:ale_run_synchronously = 1
let g:conn_id = v:null
let g:message_list = []
function! LanguageCallback() abort
@@ -34,26 +35,29 @@ Before:
let g:ale_linters = {'foobar': ['dummy_linter']}
function! ale#lsp_linter#StartLSP(buffer, linter) abort
let l:conn = ale#lsp#NewConnection({})
let l:conn.id = 347
let l:conn.open_documents = {a:buffer : -1}
let g:conn_id = ale#lsp#Register('executable', '/foo/bar', {})
call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
return {
\ 'buffer': a:buffer,
\ 'connection_id': 347,
\ 'connection_id': g:conn_id,
\ 'project_root': '/foo/bar',
\ 'language_id': 'foobar',
\}
endfunction
" Replace the Send function for LSP, so we can monitor calls to it.
function! ale#lsp#Send(conn_id, message, ...) abort
function! ale#lsp#Send(conn_id, message) abort
call add(g:message_list, a:message)
endfunction
After:
Restore
if g:conn_id isnot v:null
call ale#lsp#RemoveConnectionWithID(g:conn_id)
endif
unlet! b:ale_enabled
unlet! b:ale_linters
unlet! g:message_list
@@ -61,7 +65,6 @@ After:
delfunction LanguageCallback
delfunction ProjectRootCallback
call ale#lsp#RemoveConnectionWithID(347)
call ale#test#RestoreDirectory()
call ale#linter#Reset()

View File

@@ -5,7 +5,7 @@ Before:
" Mock the StartProgram function so we can just capture the arguments.
function! ale#lsp#StartProgram(...) abort
let g:args = a:000
let g:args = a:000[1:]
endfunction
After:
@@ -27,10 +27,10 @@ Execute(Command formatting should be applied correctly for LSP linters):
if has('win32')
AssertEqual
\ ['cmd', 'cmd /s/c "cmd --foo"', {}],
\ ['cmd', 'cmd /s/c "cmd --foo"'],
\ g:args
else
AssertEqual
\ ['true', [&shell, '-c', '''true'' --foo'], {}],
\ ['true', [&shell, '-c', '''true'' --foo']],
\ g:args
endif

View File

@@ -225,57 +225,3 @@ Execute(ale#lsp#ReadMessageData() should handle a message with part of a second
\ . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}'
\ . b:data
\ )
Execute(Projects with regular project roots should be registered correctly):
let b:conn = ale#lsp#NewConnection({})
call ale#lsp#RegisterProject(b:conn.id, '/foo/bar')
AssertEqual
\ {
\ '/foo/bar': {
\ 'root': '/foo/bar',
\ 'initialized': 0,
\ 'message_queue': [],
\ 'capabilities_queue': [],
\ 'init_request_id': 0,
\ },
\ },
\ b:conn.projects
Execute(Projects with regular project roots should be fetched correctly):
let b:conn = {
\ 'projects': {
\ '/foo/bar': {'initialized': 0, 'message_queue': [], 'init_request_id': 0},
\ },
\}
AssertEqual
\ {'initialized': 0, 'message_queue': [], 'init_request_id': 0},
\ ale#lsp#GetProject(b:conn, '/foo/bar')
Execute(Projects with empty project roots should be registered correctly):
let b:conn = ale#lsp#NewConnection({})
call ale#lsp#RegisterProject(b:conn.id, '')
AssertEqual
\ {
\ '<<EMPTY>>': {
\ 'root': '',
\ 'initialized': 1,
\ 'message_queue': [],
\ 'capabilities_queue': [],
\ 'init_request_id': 0,
\ },
\ },
\ b:conn.projects
Execute(Projects with empty project roots should be fetched correctly):
let b:conn = {
\ 'projects': {
\ '<<EMPTY>>': {'initialized': 1, 'message_queue': [], 'init_request_id': 0},
\ },
\}
AssertEqual
\ {'initialized': 1, 'message_queue': [], 'init_request_id': 0},
\ ale#lsp#GetProject(b:conn, '')

View File

@@ -1,15 +1,15 @@
Before:
let b:project = {
let b:conn = {
\ 'is_tsserver': 0,
\ 'data': '',
\ 'root': '/foo/bar',
\ 'open_documents': {},
\ 'initialized': 0,
\ 'init_request_id': 3,
\ 'init_request_id': 0,
\ 'init_options': {},
\ 'callback_list': [],
\ 'message_queue': [],
\ 'capabilities_queue': [],
\}
let b:conn = {
\ 'projects': {
\ '/foo/bar': b:project,
\ },
\ 'capabilities': {
\ 'hover': 0,
\ 'references': 0,
@@ -20,64 +20,26 @@ Before:
\}
After:
unlet! b:project
unlet! b:conn
Execute(publishDiagnostics messages with files inside project directories should initialize projects):
" This is for some other file, ignore this one.
call ale#lsp#HandleOtherInitializeResponses(b:conn, {
\ 'method': 'textDocument/publishDiagnostics',
\ 'params': {'uri': 'file:///xyz/bar/baz.txt'},
\})
AssertEqual
\ {
\ 'initialized': 0,
\ 'init_request_id': 3,
\ 'message_queue': [],
\ 'capabilities_queue': [],
\ },
\ b:project
call ale#lsp#HandleOtherInitializeResponses(b:conn, {
\ 'method': 'textDocument/publishDiagnostics',
\ 'params': {'uri': 'file:///foo/bar/baz.txt'},
\})
AssertEqual
\ {
\ 'initialized': 1,
\ 'init_request_id': 3,
\ 'message_queue': [],
\ 'capabilities_queue': [],
\ },
\ b:project
Execute(Messages with no method and capabilities should initialize projects):
call ale#lsp#HandleOtherInitializeResponses(b:conn, {
call ale#lsp#HandleInitResponse(b:conn, {
\ 'result': {'capabilities': {}},
\})
AssertEqual
\ {
\ 'initialized': 1,
\ 'init_request_id': 3,
\ 'message_queue': [],
\ 'capabilities_queue': [],
\ },
\ b:project
AssertEqual 1, b:conn.initialized
Execute(Other messages should not initialize projects):
call ale#lsp#HandleOtherInitializeResponses(b:conn, {'method': 'lolwat'})
call ale#lsp#HandleInitResponse(b:conn, {'method': 'lolwat'})
AssertEqual 0, b:project.initialized
AssertEqual 0, b:conn.initialized
call ale#lsp#HandleOtherInitializeResponses(b:conn, {'result': {'x': {}}})
call ale#lsp#HandleInitResponse(b:conn, {'result': {'x': {}}})
AssertEqual 0, b:project.initialized
AssertEqual 0, b:conn.initialized
Execute(Capabilities should bet set up correctly):
call ale#lsp#HandleOtherInitializeResponses(b:conn, {
call ale#lsp#HandleInitResponse(b:conn, {
\ 'jsonrpc': '2.0',
\ 'id': 1,
\ 'result': {
@@ -110,29 +72,19 @@ Execute(Capabilities should bet set up correctly):
\ },
\})
AssertEqual 1, b:conn.initialized
AssertEqual
\ {
\ 'capabilities': {
\ 'completion_trigger_characters': ['.'],
\ 'completion': 1,
\ 'references': 1,
\ 'hover': 1,
\ 'definition': 1,
\ },
\ 'message_queue': [],
\ 'projects': {
\ '/foo/bar': {
\ 'initialized': 1,
\ 'message_queue': [],
\ 'capabilities_queue': [],
\ 'init_request_id': 3,
\ },
\ },
\ 'completion_trigger_characters': ['.'],
\ 'completion': 1,
\ 'references': 1,
\ 'hover': 1,
\ 'definition': 1,
\ },
\ b:conn
\ b:conn.capabilities
Execute(Disabled capabilities should be recognised correctly):
call ale#lsp#HandleOtherInitializeResponses(b:conn, {
call ale#lsp#HandleInitResponse(b:conn, {
\ 'jsonrpc': '2.0',
\ 'id': 1,
\ 'result': {
@@ -161,29 +113,19 @@ Execute(Disabled capabilities should be recognised correctly):
\ },
\})
AssertEqual 1, b:conn.initialized
AssertEqual
\ {
\ 'capabilities': {
\ 'completion_trigger_characters': [],
\ 'completion': 0,
\ 'references': 0,
\ 'hover': 0,
\ 'definition': 0,
\ },
\ 'message_queue': [],
\ 'projects': {
\ '/foo/bar': {
\ 'initialized': 1,
\ 'message_queue': [],
\ 'capabilities_queue': [],
\ 'init_request_id': 3,
\ },
\ },
\ 'completion_trigger_characters': [],
\ 'completion': 0,
\ 'references': 0,
\ 'hover': 0,
\ 'definition': 0,
\ },
\ b:conn
\ b:conn.capabilities
Execute(Results that are not dictionaries should be handled correctly):
call ale#lsp#HandleOtherInitializeResponses(b:conn, {
call ale#lsp#HandleInitResponse(b:conn, {
\ 'jsonrpc': '2.0',
\ 'id': 1,
\ 'result': v:null,

View File

@@ -9,6 +9,7 @@ Before:
let g:preview_called = 0
let g:item_list = []
let g:capability_checked = ''
let g:conn_id = v:null
let g:WaitCallback = v:null
runtime autoload/ale/linter.vim
@@ -17,19 +18,18 @@ Before:
runtime autoload/ale/preview.vim
function! ale#lsp_linter#StartLSP(buffer, linter) abort
let l:conn = ale#lsp#NewConnection({})
let l:conn.id = 347
let l:conn.open_documents = {a:buffer : -1}
let g:conn_id = ale#lsp#Register('executable', '/foo/bar', {})
call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
return {
\ 'buffer': a:buffer,
\ 'connection_id': 347,
\ 'connection_id': g:conn_id,
\ 'project_root': '/foo/bar',
\ 'language_id': 'python',
\}
endfunction
function! ale#lsp#WaitForCapability(conn_id, project_root, capability, callback) abort
function! ale#lsp#WaitForCapability(conn_id, capability, callback) abort
let g:capability_checked = a:capability
let g:WaitCallback = a:callback
endfunction
@@ -38,7 +38,7 @@ Before:
let g:Callback = a:callback
endfunction
function! ale#lsp#Send(conn_id, message, root) abort
function! ale#lsp#Send(conn_id, message) abort
call add(g:message_list, a:message)
return 42
@@ -54,7 +54,10 @@ Before:
endfunction
After:
call ale#lsp#RemoveConnectionWithID(347)
if g:conn_id isnot v:null
call ale#lsp#RemoveConnectionWithID(g:conn_id)
endif
call ale#references#SetMap({})
call ale#test#RestoreDirectory()
call ale#linter#Reset()
@@ -62,6 +65,7 @@ After:
unlet! g:capability_checked
unlet! g:WaitCallback
unlet! g:old_filename
unlet! g:conn_id
unlet! g:Callback
unlet! g:message_list
unlet! g:expr_list
@@ -168,7 +172,7 @@ Execute(tsserver reference requests should be sent):
AssertEqual type(function('type')), type(g:WaitCallback)
AssertEqual 'references', g:capability_checked
call call(g:WaitCallback, [347, '/foo/bar'])
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual
\ 'function(''ale#references#HandleTSServerResponse'')',
@@ -249,7 +253,7 @@ Execute(LSP reference requests should be sent):
AssertEqual type(function('type')), type(g:WaitCallback)
AssertEqual 'references', g:capability_checked
call call(g:WaitCallback, [347, '/foo/bar'])
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual
\ 'function(''ale#references#HandleLSPResponse'')',

View File

@@ -7,6 +7,7 @@ Before:
let g:message_list = []
let g:expr_list = []
let g:capability_checked = ''
let g:conn_id = v:null
let g:WaitCallback = v:null
runtime autoload/ale/linter.vim
@@ -14,19 +15,18 @@ Before:
runtime autoload/ale/util.vim
function! ale#lsp_linter#StartLSP(buffer, linter) abort
let l:conn = ale#lsp#NewConnection({})
let l:conn.id = 347
let l:conn.open_documents = {a:buffer : -1}
let g:conn_id = ale#lsp#Register('executable', '/foo/bar', {})
call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
return {
\ 'buffer': a:buffer,
\ 'connection_id': 347,
\ 'connection_id': g:conn_id,
\ 'project_root': '/foo/bar',
\ 'language_id': 'python',
\}
endfunction
function! ale#lsp#WaitForCapability(conn_id, project_root, capability, callback) abort
function! ale#lsp#WaitForCapability(conn_id, capability, callback) abort
let g:capability_checked = a:capability
let g:WaitCallback = a:callback
endfunction
@@ -35,7 +35,7 @@ Before:
let g:Callback = a:callback
endfunction
function! ale#lsp#Send(conn_id, message, root) abort
function! ale#lsp#Send(conn_id, message) abort
call add(g:message_list, a:message)
return 42
@@ -46,7 +46,10 @@ Before:
endfunction
After:
call ale#lsp#RemoveConnectionWithID(347)
if g:conn_id isnot v:null
call ale#lsp#RemoveConnectionWithID(g:conn_id)
endif
call ale#definition#SetMap({})
call ale#test#RestoreDirectory()
call ale#linter#Reset()
@@ -54,6 +57,7 @@ After:
unlet! g:capability_checked
unlet! g:WaitCallback
unlet! g:old_filename
unlet! g:conn_id
unlet! g:Callback
unlet! g:message_list
unlet! g:expr_list
@@ -153,7 +157,7 @@ Execute(tsserver completion requests should be sent):
AssertEqual type(function('type')), type(g:WaitCallback)
AssertEqual 'definition', g:capability_checked
call call(g:WaitCallback, [347, '/foo/bar'])
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual
\ 'function(''ale#definition#HandleTSServerResponse'')',
@@ -174,7 +178,7 @@ Execute(tsserver tab completion requests should be sent):
AssertEqual type(function('type')), type(g:WaitCallback)
AssertEqual 'definition', g:capability_checked
call call(g:WaitCallback, [347, '/foo/bar'])
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual
\ 'function(''ale#definition#HandleTSServerResponse'')',
@@ -306,7 +310,7 @@ Execute(LSP completion requests should be sent):
AssertEqual type(function('type')), type(g:WaitCallback)
AssertEqual 'definition', g:capability_checked
call call(g:WaitCallback, [347, '/foo/bar'])
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual
\ 'function(''ale#definition#HandleLSPResponse'')',
@@ -342,7 +346,7 @@ Execute(LSP tab completion requests should be sent):
AssertEqual type(function('type')), type(g:WaitCallback)
AssertEqual 'definition', g:capability_checked
call call(g:WaitCallback, [347, '/foo/bar'])
call call(g:WaitCallback, [g:conn_id, '/foo/bar'])
AssertEqual
\ 'function(''ale#definition#HandleLSPResponse'')',