diff options
author | sridhars <sridhars@users.noreply.github.com> | 2018-07-24 16:42:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-24 16:42:26 -0500 |
commit | 4446cf15bea403204b5097f1bfc74267af32553c (patch) | |
tree | 325b0370a3b511efa2a498ee6959fe16c56c1b20 /autoload | |
parent | 0da4c6e4ad009a37b482d301a19b6d024aa0ae0a (diff) | |
parent | 341857477011f703664b31d8d76f6872f8399c66 (diff) | |
download | ale-4446cf15bea403204b5097f1bfc74267af32553c.zip |
Merge pull request #1 from w0rp/master
update
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale.vim | 7 | ||||
-rw-r--r-- | autoload/ale/balloon.vim | 17 | ||||
-rw-r--r-- | autoload/ale/c.vim | 5 | ||||
-rw-r--r-- | autoload/ale/completion.vim | 89 | ||||
-rw-r--r-- | autoload/ale/definition.vim | 61 | ||||
-rw-r--r-- | autoload/ale/fix.vim | 2 | ||||
-rw-r--r-- | autoload/ale/handlers/writegood.vim | 11 | ||||
-rw-r--r-- | autoload/ale/hover.vim | 58 | ||||
-rw-r--r-- | autoload/ale/lsp.vim | 202 | ||||
-rw-r--r-- | autoload/ale/lsp_linter.vim | 56 | ||||
-rw-r--r-- | autoload/ale/references.vim | 47 | ||||
-rw-r--r-- | autoload/ale/test.vim | 23 | ||||
-rw-r--r-- | autoload/ale/toggle.vim | 2 |
13 files changed, 361 insertions, 219 deletions
diff --git a/autoload/ale.vim b/autoload/ale.vim index 26c73547..6d1e8521 100644 --- a/autoload/ale.vim +++ b/autoload/ale.vim @@ -192,12 +192,7 @@ endfunction " Every variable name will be prefixed with 'ale_'. function! ale#Var(buffer, variable_name) abort let l:full_name = 'ale_' . a:variable_name - let l:vars = getbufvar(str2nr(a:buffer), '', 0) - - if l:vars is 0 - " Look for variables from deleted buffers, saved from :ALEFix - let l:vars = get(get(g:ale_fix_buffer_data, a:buffer, {}), 'vars', {}) - endif + let l:vars = getbufvar(str2nr(a:buffer), '', {}) return get(l:vars, l:full_name, g:[l:full_name]) endfunction diff --git a/autoload/ale/balloon.vim b/autoload/ale/balloon.vim index 7a380da2..72f6b91c 100644 --- a/autoload/ale/balloon.vim +++ b/autoload/ale/balloon.vim @@ -34,26 +34,25 @@ function! ale#balloon#Expr() abort endfunction function! ale#balloon#Disable() abort + if has('balloon_eval') + set noballooneval + set balloonexpr= + endif + if has('balloon_eval_term') set noballoonevalterm + set balloonexpr= endif - - set noballooneval - set balloonexpr= endfunction function! ale#balloon#Enable() abort - if !has('balloon_eval') && !has('balloon_eval_term') - return - endif - if has('balloon_eval') set ballooneval + set balloonexpr=ale#balloon#Expr() endif if has('balloon_eval_term') set balloonevalterm + set balloonexpr=ale#balloon#Expr() endif - - set balloonexpr=ale#balloon#Expr() endfunction diff --git a/autoload/ale/c.vim b/autoload/ale/c.vim index 5ab10f00..fbfe9509 100644 --- a/autoload/ale/c.vim +++ b/autoload/ale/c.vim @@ -4,8 +4,11 @@ call ale#Set('c_parse_makefile', 0) let s:sep = has('win32') ? '\' : '/' +" Set just so tests can override it. +let g:__ale_c_project_filenames = ['.git/HEAD', 'configure', 'Makefile', 'CMakeLists.txt'] + function! ale#c#FindProjectRoot(buffer) abort - for l:project_filename in ['.git/HEAD', 'configure', 'Makefile', 'CMakeLists.txt'] + for l:project_filename in g:__ale_c_project_filenames let l:full_path = ale#path#FindNearestFile(a:buffer, l:project_filename) if !empty(l:full_path) diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim index e7da4028..7440f8cd 100644 --- a/autoload/ale/completion.vim +++ b/autoload/ale/completion.vim @@ -336,7 +336,9 @@ function! ale#completion#ParseLSPCompletions(response) abort endif " See :help complete-items for Vim completion kinds - if l:item.kind is s:LSP_COMPLETION_METHOD_KIND + if !has_key(l:item, 'kind') + let l:kind = 'v' + elseif l:item.kind is s:LSP_COMPLETION_METHOD_KIND let l:kind = 'm' elseif l:item.kind is s:LSP_COMPLETION_CONSTRUCTOR_KIND let l:kind = 'm' @@ -422,54 +424,65 @@ endfunction function! s:GetLSPCompletions(linter) abort let l:buffer = bufnr('') - let l:Callback = a:linter.lsp is# 'tsserver' - \ ? function('ale#completion#HandleTSServerResponse') - \ : function('ale#completion#HandleLSPResponse') - - let l:lsp_details = ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback) + let l:lsp_details = ale#lsp_linter#StartLSP(l:buffer, a:linter) if empty(l:lsp_details) return 0 endif let l:id = l:lsp_details.connection_id + let l:root = l:lsp_details.project_root - if a:linter.lsp is# 'tsserver' - let l:message = ale#lsp#tsserver_message#Completions( - \ l:buffer, - \ b:ale_completion_info.line, - \ b:ale_completion_info.column, - \ b:ale_completion_info.prefix, - \) - else - " Send a message saying the buffer has changed first, otherwise - " completions won't know what text is nearby. - call ale#lsp#NotifyForChanges(l:lsp_details) - - " For LSP completions, we need to clamp the column to the length of - " the line. python-language-server and perhaps others do not implement - " this correctly. - let l:message = ale#lsp#message#Completion( - \ l:buffer, - \ b:ale_completion_info.line, - \ min([ - \ b:ale_completion_info.line_length, - \ b:ale_completion_info.column, - \ ]), - \ ale#completion#GetTriggerCharacter(&filetype, b:ale_completion_info.prefix), - \) - endif + function! OnReady(...) abort closure + " If we have sent a completion request already, don't send another. + if b:ale_completion_info.request_id + return + endif - let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) + let l:Callback = a:linter.lsp is# 'tsserver' + \ ? function('ale#completion#HandleTSServerResponse') + \ : function('ale#completion#HandleLSPResponse') + call ale#lsp#RegisterCallback(l:id, l:Callback) + + if a:linter.lsp is# 'tsserver' + let l:message = ale#lsp#tsserver_message#Completions( + \ l:buffer, + \ b:ale_completion_info.line, + \ b:ale_completion_info.column, + \ b:ale_completion_info.prefix, + \) + else + " Send a message saying the buffer has changed first, otherwise + " completions won't know what text is nearby. + call ale#lsp#NotifyForChanges(l:id, l:root, l:buffer) + + " For LSP completions, we need to clamp the column to the length of + " the line. python-language-server and perhaps others do not implement + " this correctly. + let l:message = ale#lsp#message#Completion( + \ l:buffer, + \ b:ale_completion_info.line, + \ min([ + \ b:ale_completion_info.line_length, + \ b:ale_completion_info.column, + \ ]), + \ ale#completion#GetTriggerCharacter(&filetype, b:ale_completion_info.prefix), + \) + endif - if l:request_id - let b:ale_completion_info.conn_id = l:id - let b:ale_completion_info.request_id = l:request_id + let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) - if has_key(a:linter, 'completion_filter') - let b:ale_completion_info.completion_filter = a:linter.completion_filter + if l:request_id + let b:ale_completion_info.conn_id = l:id + let b:ale_completion_info.request_id = l:request_id + + if has_key(a:linter, 'completion_filter') + let b:ale_completion_info.completion_filter = a:linter.completion_filter + endif endif - endif + endfunction + + call ale#lsp#WaitForCapability(l:id, l:root, 'completion', function('OnReady')) endfunction function! ale#completion#GetCompletions() abort diff --git a/autoload/ale/definition.vim b/autoload/ale/definition.vim index 6c70b64c..6c7d7d32 100644 --- a/autoload/ale/definition.vim +++ b/autoload/ale/definition.vim @@ -60,43 +60,50 @@ endfunction function! s:GoToLSPDefinition(linter, options) abort let l:buffer = bufnr('') let [l:line, l:column] = getcurpos()[1:2] + let l:lsp_details = ale#lsp_linter#StartLSP(l:buffer, a:linter) - let l:Callback = a:linter.lsp is# 'tsserver' - \ ? function('ale#definition#HandleTSServerResponse') - \ : function('ale#definition#HandleLSPResponse') - - let l:lsp_details = ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback) + if a:linter.lsp isnot# 'tsserver' + let l:column = min([l:column, len(getline(l:line))]) + endif if empty(l:lsp_details) return 0 endif let l:id = l:lsp_details.connection_id + let l:root = l:lsp_details.project_root + + function! OnReady(...) abort closure + let l:Callback = a:linter.lsp is# 'tsserver' + \ ? function('ale#definition#HandleTSServerResponse') + \ : function('ale#definition#HandleLSPResponse') + call ale#lsp#RegisterCallback(l:id, l:Callback) + + if a:linter.lsp is# 'tsserver' + let l:message = ale#lsp#tsserver_message#Definition( + \ l:buffer, + \ l:line, + \ l:column + \) + else + " Send a message saying the buffer has changed first, or the + " definition position probably won't make sense. + call ale#lsp#NotifyForChanges(l:id, l:root, l:buffer) + + " For LSP completions, we need to clamp the column to the length of + " the line. python-language-server and perhaps others do not implement + " this correctly. + let l:message = ale#lsp#message#Definition(l:buffer, l:line, l:column) + endif - if a:linter.lsp is# 'tsserver' - let l:message = ale#lsp#tsserver_message#Definition( - \ l:buffer, - \ l:line, - \ l:column - \) - else - " Send a message saying the buffer has changed first, or the - " definition position probably won't make sense. - call ale#lsp#NotifyForChanges(l:lsp_details) - - let l:column = min([l:column, len(getline(l:line))]) - - " For LSP completions, we need to clamp the column to the length of - " the line. python-language-server and perhaps others do not implement - " this correctly. - let l:message = ale#lsp#message#Definition(l:buffer, l:line, l:column) - endif + let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) - let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) + let s:go_to_definition_map[l:request_id] = { + \ 'open_in_tab': get(a:options, 'open_in_tab', 0), + \} + endfunction - let s:go_to_definition_map[l:request_id] = { - \ 'open_in_tab': get(a:options, 'open_in_tab', 0), - \} + call ale#lsp#WaitForCapability(l:id, l:root, 'definition', function('OnReady')) endfunction function! ale#definition#GoTo(options) abort diff --git a/autoload/ale/fix.vim b/autoload/ale/fix.vim index 62674b87..8dfdeca8 100644 --- a/autoload/ale/fix.vim +++ b/autoload/ale/fix.vim @@ -420,9 +420,7 @@ function! ale#fix#InitBufferData(buffer, fixing_flag) abort " The 'done' flag tells the function for applying changes when fixing " is complete. let g:ale_fix_buffer_data[a:buffer] = { - \ 'vars': getbufvar(a:buffer, ''), \ 'lines_before': getbufline(a:buffer, 1, '$'), - \ 'filename': expand('#' . a:buffer . ':p'), \ 'done': 0, \ 'should_save': a:fixing_flag is# 'save_file', \ 'temporary_directory_list': [], diff --git a/autoload/ale/handlers/writegood.vim b/autoload/ale/handlers/writegood.vim index aee7c4de..aff66d5f 100644 --- a/autoload/ale/handlers/writegood.vim +++ b/autoload/ale/handlers/writegood.vim @@ -59,3 +59,14 @@ function! ale#handlers#writegood#Handle(buffer, lines) abort return l:output endfunction + +" Define the writegood linter for a given filetype. +function! ale#handlers#writegood#DefineLinter(filetype) abort + call ale#linter#Define(a:filetype, { + \ 'name': 'writegood', + \ 'aliases': ['write-good'], + \ 'executable_callback': 'ale#handlers#writegood#GetExecutable', + \ 'command_callback': 'ale#handlers#writegood#GetCommand', + \ 'callback': 'ale#handlers#writegood#Handle', + \}) +endfunction diff --git a/autoload/ale/hover.vim b/autoload/ale/hover.vim index 6d131adc..5e97e16e 100644 --- a/autoload/ale/hover.vim +++ b/autoload/ale/hover.vim @@ -93,45 +93,51 @@ function! ale#hover#HandleLSPResponse(conn_id, response) abort endfunction function! s:ShowDetails(linter, buffer, line, column, opt) abort - let l:Callback = a:linter.lsp is# 'tsserver' - \ ? function('ale#hover#HandleTSServerResponse') - \ : function('ale#hover#HandleLSPResponse') - - let l:lsp_details = ale#lsp_linter#StartLSP(a:buffer, a:linter, l:Callback) + let l:lsp_details = ale#lsp_linter#StartLSP(a:buffer, a:linter) if empty(l:lsp_details) return 0 endif let l:id = l:lsp_details.connection_id + let l:root = l:lsp_details.project_root let l:language_id = l:lsp_details.language_id - if a:linter.lsp is# 'tsserver' - let l:column = a:column + function! OnReady(...) abort closure + let l:Callback = a:linter.lsp is# 'tsserver' + \ ? function('ale#hover#HandleTSServerResponse') + \ : function('ale#hover#HandleLSPResponse') + call ale#lsp#RegisterCallback(l:id, l:Callback) - let l:message = ale#lsp#tsserver_message#Quickinfo( - \ a:buffer, - \ a:line, - \ l:column - \) - else - " Send a message saying the buffer has changed first, or the - " hover position probably won't make sense. - call ale#lsp#NotifyForChanges(l:lsp_details) + if a:linter.lsp is# 'tsserver' + let l:column = a:column - let l:column = min([a:column, len(getbufline(a:buffer, a:line)[0])]) + let l:message = ale#lsp#tsserver_message#Quickinfo( + \ a:buffer, + \ a:line, + \ l:column + \) + else + " Send a message saying the buffer has changed first, or the + " hover position probably won't make sense. + call ale#lsp#NotifyForChanges(l:id, l:root, a:buffer) - let l:message = ale#lsp#message#Hover(a:buffer, a:line, l:column) - endif + let l:column = min([a:column, len(getbufline(a:buffer, a:line)[0])]) + + let l:message = ale#lsp#message#Hover(a:buffer, a:line, l:column) + endif + + let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) - let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) + let s:hover_map[l:request_id] = { + \ 'buffer': a:buffer, + \ 'line': a:line, + \ 'column': l:column, + \ 'hover_from_balloonexpr': get(a:opt, 'called_from_balloonexpr', 0), + \} + endfunction - let s:hover_map[l:request_id] = { - \ 'buffer': a:buffer, - \ 'line': a:line, - \ 'column': l:column, - \ 'hover_from_balloonexpr': get(a:opt, 'called_from_balloonexpr', 0), - \} + call ale#lsp#WaitForCapability(l:id, l:root, 'hover', function('OnReady')) endfunction " Obtain Hover information for the specified position diff --git a/autoload/ale/lsp.vim b/autoload/ale/lsp.vim index e44b5bc3..312319ab 100644 --- a/autoload/ale/lsp.vim +++ b/autoload/ale/lsp.vim @@ -15,13 +15,23 @@ function! ale#lsp#NewConnection(initialization_options) abort " open_documents: A Dictionary mapping buffers to b:changedtick, keeping " track of when documents were opened, and when we last changed them. " callback_list: A list of callbacks for handling LSP responses. + " initialization_options: Options to send to the server. + " capabilities: Features the server supports. let l:conn = { + \ 'is_tsserver': 0, \ 'id': '', \ 'data': '', \ 'projects': {}, \ 'open_documents': {}, \ 'callback_list': [], \ 'initialization_options': a:initialization_options, + \ 'capabilities': { + \ 'hover': 0, + \ 'references': 0, + \ 'completion': 0, + \ 'completion_trigger_characters': [], + \ 'definition': 0, + \ }, \} call add(s:connections, l:conn) @@ -44,6 +54,11 @@ function! s:FindConnection(key, value) abort return {} endfunction +" Get the capabilities for a connection, or an empty Dictionary. +function! ale#lsp#GetConnectionCapabilities(id) abort + return get(s:FindConnection('id', a:id), 'capabilities', {}) +endfunction + function! ale#lsp#GetNextMessageID() abort " Use the current ID let l:id = g:ale_lsp_next_message_id @@ -174,6 +189,16 @@ function! s:MarkProjectAsInitialized(conn, project) abort " Remove the messages now. let a:conn.message_queue = [] + + " Call capabilities callbacks queued for the project. + for [l:capability, l:Callback] in a:project.capabilities_queue + if a:conn.is_tsserver || a:conn.capabilities[l:capability] + call call(l:Callback, [a:conn.id, a:project.root]) + endif + endfor + + " Clear the queued callbacks now. + let a:project.capabilities_queue = [] endfunction function! s:HandleInitializeResponse(conn, response) abort @@ -185,6 +210,38 @@ function! s:HandleInitializeResponse(conn, response) abort endif endfunction +" Update capabilities from the server, so we know which features the server +" supports. +function! s:UpdateCapabilities(conn, capabilities) abort + if type(a:capabilities) != type({}) + return + endif + + if get(a:capabilities, 'hoverProvider') is v:true + let a:conn.capabilities.hover = 1 + endif + + if get(a:capabilities, 'referencesProvider') is v:true + let a:conn.capabilities.references = 1 + endif + + if !empty(get(a:capabilities, 'completionProvider')) + let a:conn.capabilities.completion = 1 + endif + + if type(get(a:capabilities, 'completionProvider')) is type({}) + let l:chars = get(a:capabilities.completionProvider, 'triggerCharacters') + + if type(l:chars) is type([]) + let a:conn.capabilities.completion_trigger_characters = l:chars + endif + endif + + if get(a:capabilities, 'definitionProvider') is v:true + let a:conn.capabilities.definition = 1 + endif +endfunction + function! ale#lsp#HandleOtherInitializeResponses(conn, response) abort let l:uninitialized_projects = [] @@ -200,6 +257,8 @@ function! ale#lsp#HandleOtherInitializeResponses(conn, response) abort if get(a:response, 'method', '') is# '' if has_key(get(a:response, 'result', {}), 'capabilities') + call s:UpdateCapabilities(a:conn, a:response.result.capabilities) + for [l:dir, l:project] in l:uninitialized_projects call s:MarkProjectAsInitialized(a:conn, l:project) endfor @@ -254,22 +313,43 @@ function! s:HandleCommandMessage(job_id, message) abort call ale#lsp#HandleMessage(l:conn, a:message) endfunction -function! ale#lsp#RegisterProject(conn, project_root) abort +" Given a connection ID, mark it as a tsserver connection, so it will be +" handled that way. +function! ale#lsp#MarkConnectionAsTsserver(conn_id) abort + let l:conn = s:FindConnection('id', a:conn_id) + + if !empty(l:conn) + let l:conn.is_tsserver = 1 + endif +endfunction + +" Register a project for an LSP connection. +" +" This function will throw if the connection doesn't exist. +function! ale#lsp#RegisterProject(conn_id, project_root) abort + let l:conn = s:FindConnection('id', a:conn_id) + " Empty strings can't be used for Dictionary keys in NeoVim, due to E713. " This appears to be a nonsensical bug in NeoVim. let l:key = empty(a:project_root) ? '<<EMPTY>>' : a:project_root - if !has_key(a:conn.projects, l:key) + if !has_key(l:conn.projects, l:key) " Tools without project roots are ready right away, like tsserver. - let a:conn.projects[l:key] = { + let l:conn.projects[l:key] = { + \ 'root': a:project_root, \ 'initialized': empty(a:project_root), \ 'init_request_id': 0, \ 'message_queue': [], + \ 'capabilities_queue': [], \} endif endfunction function! ale#lsp#GetProject(conn, project_root) abort + if empty(a:conn) + return {} + endif + let l:key = empty(a:project_root) ? '<<EMPTY>>' : a:project_root return get(a:conn.projects, l:key, {}) @@ -279,7 +359,7 @@ endfunction " " The job ID will be returned for for the program if it ran, otherwise " 0 will be returned. -function! ale#lsp#StartProgram(executable, command, project_root, callback, initialization_options) abort +function! ale#lsp#StartProgram(executable, command, init_options) abort if !executable(a:executable) return 0 endif @@ -287,7 +367,7 @@ function! ale#lsp#StartProgram(executable, command, project_root, callback, init let l:conn = s:FindConnection('executable', a:executable) " Get the current connection or a new one. - let l:conn = !empty(l:conn) ? l:conn : ale#lsp#NewConnection(a:initialization_options) + let l:conn = !empty(l:conn) ? l:conn : ale#lsp#NewConnection(a:init_options) let l:conn.executable = a:executable if !has_key(l:conn, 'id') || !ale#job#IsRunning(l:conn.id) @@ -305,18 +385,15 @@ function! ale#lsp#StartProgram(executable, command, project_root, callback, init endif let l:conn.id = l:job_id - " Add the callback to the List if it's not there already. - call uniq(sort(add(l:conn.callback_list, a:callback))) - call ale#lsp#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, project_root, callback, initialization_options) abort +function! ale#lsp#ConnectToAddress(address, init_options) 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 : ale#lsp#NewConnection(a:initialization_options) + let l:conn = !empty(l:conn) ? l:conn : ale#lsp#NewConnection(a:init_options) if !has_key(l:conn, 'channel_id') || !ale#socket#IsOpen(l:conn.channel_id) let l:conn.channel_id = ale#socket#Open(a:address, { @@ -329,13 +406,21 @@ function! ale#lsp#ConnectToAddress(address, project_root, callback, initializati endif let l:conn.id = a:address - " Add the callback to the List if it's not there already. - call uniq(sort(add(l:conn.callback_list, a:callback))) - call ale#lsp#RegisterProject(l:conn, a:project_root) return a:address endfunction +" Given a connection ID and a callback, register that callback for handling +" messages if the connection exists. +function! ale#lsp#RegisterCallback(conn_id, callback) abort + let l:conn = s:FindConnection('id', a:conn_id) + + if !empty(l:conn) + " Add the callback to the List if it's not there already. + call uniq(sort(add(l:conn.callback_list, a:callback))) + endif +endfunction + " Stop all LSP connections, closing all jobs and channels, and removing any " queued messages. function! ale#lsp#StopAll() abort @@ -373,11 +458,6 @@ 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 = ale#lsp#GetProject(l:conn, l:project_root) if empty(l:project) @@ -411,45 +491,22 @@ function! ale#lsp#Send(conn_id, message, ...) abort return l:id == 0 ? -1 : l:id endfunction -" The Document details Dictionary should contain the following keys. -" -" buffer - The buffer number for the document. -" connection_id - The connection ID for the LSP server. -" command - The command to run to start the LSP connection. -" project_root - The project root for the LSP project. -" language_id - The language ID for the project, like 'python', 'rust', etc. - -" Create a new Dictionary containing more connection details, with the -" following information added: -" -" conn - An existing LSP connection for the document. -" document_open - 1 if the document is currently open, 0 otherwise. -function! s:ExtendDocumentDetails(details) abort - let l:extended = copy(a:details) - let l:conn = s:FindConnection('id', a:details.connection_id) - - let l:extended.conn = l:conn - let l:extended.document_open = !empty(l:conn) - \ && has_key(l:conn.open_documents, a:details.buffer) - - return l:extended -endfunction - " Notify LSP servers or tsserver if a document is opened, if needed. " If a document is opened, 1 will be returned, otherwise 0 will be returned. -function! ale#lsp#OpenDocument(basic_details) abort - let l:d = s:ExtendDocumentDetails(a:basic_details) +function! ale#lsp#OpenDocument(conn_id, project_root, buffer, language_id) abort + let l:conn = s:FindConnection('id', a:conn_id) let l:opened = 0 - if !empty(l:d.conn) && !l:d.document_open - if empty(l:d.language_id) - let l:message = ale#lsp#tsserver_message#Open(l:d.buffer) + " FIXME: Return 1 if the document is already open? + if !empty(l:conn) && !has_key(l:conn.open_documents, a:buffer) + if l:conn.is_tsserver + let l:message = ale#lsp#tsserver_message#Open(a:buffer) else - let l:message = ale#lsp#message#DidOpen(l:d.buffer, l:d.language_id) + let l:message = ale#lsp#message#DidOpen(a:buffer, a:language_id) endif - call ale#lsp#Send(l:d.connection_id, l:message, l:d.project_root) - let l:d.conn.open_documents[l:d.buffer] = getbufvar(l:d.buffer, 'changedtick') + call ale#lsp#Send(a:conn_id, l:message, a:project_root) + let l:conn.open_documents[a:buffer] = getbufvar(a:buffer, 'changedtick') let l:opened = 1 endif @@ -458,25 +515,50 @@ endfunction " Notify LSP servers or tsserver that a document has changed, if needed. " If a notification is sent, 1 will be returned, otherwise 0 will be returned. -function! ale#lsp#NotifyForChanges(basic_details) abort - let l:d = s:ExtendDocumentDetails(a:basic_details) +function! ale#lsp#NotifyForChanges(conn_id, project_root, buffer) abort + let l:conn = s:FindConnection('id', a:conn_id) let l:notified = 0 - if l:d.document_open - let l:new_tick = getbufvar(l:d.buffer, 'changedtick') + if !empty(l:conn) && has_key(l:conn.open_documents, a:buffer) + let l:new_tick = getbufvar(a:buffer, 'changedtick') - if l:d.conn.open_documents[l:d.buffer] < l:new_tick - if empty(l:d.language_id) - let l:message = ale#lsp#tsserver_message#Change(l:d.buffer) + if l:conn.open_documents[a:buffer] < l:new_tick + if l:conn.is_tsserver + let l:message = ale#lsp#tsserver_message#Change(a:buffer) else - let l:message = ale#lsp#message#DidChange(l:d.buffer) + let l:message = ale#lsp#message#DidChange(a:buffer) endif - call ale#lsp#Send(l:d.connection_id, l:message, l:d.project_root) - let l:d.conn.open_documents[l:d.buffer] = l:new_tick + call ale#lsp#Send(a:conn_id, l:message, a:project_root) + let l:conn.open_documents[a:buffer] = l:new_tick let l:notified = 1 endif endif return l:notified endfunction + +" Given some LSP details that must contain at least `connection_id` and +" `project_root` keys, +function! ale#lsp#WaitForCapability(conn_id, project_root, capability, callback) abort + let l:conn = s:FindConnection('id', a:conn_id) + let l:project = ale#lsp#GetProject(l:conn, a:project_root) + + if empty(l:project) + return 0 + endif + + if type(get(l:conn.capabilities, a:capability, v:null)) isnot type(0) + throw 'Invalid capability ' . a:capability + endif + + if l:project.initialized + if l:conn.is_tsserver || l:conn.capabilities[a:capability] + " The project has been initialized, so call the callback now. + call call(a:callback, [a:conn_id, a:project_root]) + endif + else + " Call the callback later, once we have the information we need. + call add(l:project.capabilities_queue, [a:capability, a:callback]) + endif +endfunction diff --git a/autoload/ale/lsp_linter.vim b/autoload/ale/lsp_linter.vim index 6dc78e4c..87aee759 100644 --- a/autoload/ale/lsp_linter.vim +++ b/autoload/ale/lsp_linter.vim @@ -126,10 +126,9 @@ function! ale#lsp_linter#GetOptions(buffer, linter) abort return l:initialization_options endfunction -" Given a buffer, an LSP linter, and a callback to register for handling -" messages, start up an LSP linter and get ready to receive errors or -" completions. -function! ale#lsp_linter#StartLSP(buffer, linter, callback) abort +" Given a buffer, an LSP linter, start up an LSP linter and get ready to +" receive messages for the document. +function! ale#lsp_linter#StartLSP(buffer, linter) abort let l:command = '' let l:address = '' let l:root = ale#util#GetFunction(a:linter.project_root_callback)(a:buffer) @@ -140,16 +139,11 @@ function! ale#lsp_linter#StartLSP(buffer, linter, callback) abort return {} endif - let l:initialization_options = ale#lsp_linter#GetOptions(a:buffer, a:linter) + let l:init_options = ale#lsp_linter#GetOptions(a:buffer, a:linter) if a:linter.lsp is# 'socket' let l:address = ale#linter#GetAddress(a:buffer, a:linter) - let l:conn_id = ale#lsp#ConnectToAddress( - \ l:address, - \ l:root, - \ a:callback, - \ l:initialization_options, - \) + let l:conn_id = ale#lsp#ConnectToAddress(l:address, l:init_options) else let l:executable = ale#linter#GetExecutable(a:buffer, a:linter) @@ -164,14 +158,10 @@ function! ale#lsp_linter#StartLSP(buffer, linter, callback) abort let l:conn_id = ale#lsp#StartProgram( \ l:executable, \ l:command, - \ l:root, - \ a:callback, - \ l:initialization_options, + \ l:init_options, \) endif - let l:language_id = ale#util#GetFunction(a:linter.language_callback)(a:buffer) - if empty(l:conn_id) if g:ale_history_enabled && !empty(l:command) call ale#history#Add(a:buffer, 'failed', l:conn_id, l:command) @@ -180,6 +170,16 @@ function! ale#lsp_linter#StartLSP(buffer, linter, callback) abort return {} endif + " tsserver behaves differently, so tell the LSP API that it is tsserver. + if a:linter.lsp is# 'tsserver' + call ale#lsp#MarkConnectionAsTsserver(l:conn_id) + endif + + " Register the project now the connection is ready. + call ale#lsp#RegisterProject(l:conn_id, l:root) + + let l:language_id = ale#util#GetFunction(a:linter.language_callback)(a:buffer) + let l:details = { \ 'buffer': a:buffer, \ 'connection_id': l:conn_id, @@ -188,7 +188,7 @@ function! ale#lsp_linter#StartLSP(buffer, linter, callback) abort \ 'language_id': l:language_id, \} - if ale#lsp#OpenDocument(l:details) + if ale#lsp#OpenDocument(l:conn_id, l:root, a:buffer, l:language_id) if g:ale_history_enabled && !empty(l:command) call ale#history#Add(a:buffer, 'started', l:conn_id, l:command) endif @@ -196,7 +196,7 @@ function! ale#lsp_linter#StartLSP(buffer, linter, callback) abort " The change message needs to be sent for tsserver before doing anything. if a:linter.lsp is# 'tsserver' - call ale#lsp#NotifyForChanges(l:details) + call ale#lsp#NotifyForChanges(l:conn_id, l:root, a:buffer) endif return l:details @@ -204,11 +204,7 @@ endfunction function! ale#lsp_linter#CheckWithLSP(buffer, linter) abort let l:info = g:ale_buffer_info[a:buffer] - let l:lsp_details = ale#lsp_linter#StartLSP( - \ a:buffer, - \ a:linter, - \ function('ale#lsp_linter#HandleLSPResponse'), - \) + let l:lsp_details = ale#lsp_linter#StartLSP(a:buffer, a:linter) if empty(l:lsp_details) return 0 @@ -217,25 +213,25 @@ function! ale#lsp_linter#CheckWithLSP(buffer, linter) abort let l:id = l:lsp_details.connection_id let l:root = l:lsp_details.project_root + " Register a callback now for handling errors now. + let l:Callback = function('ale#lsp_linter#HandleLSPResponse') + call ale#lsp#RegisterCallback(l:id, l:Callback) + " Remember the linter this connection is for. let s:lsp_linter_map[l:id] = a:linter.name if a:linter.lsp is# 'tsserver' let l:message = ale#lsp#tsserver_message#Geterr(a:buffer) - let l:request_id = ale#lsp#Send(l:id, l:message, l:root) - - let l:notified = l:request_id != 0 + let l:notified = ale#lsp#Send(l:id, l:message, l:root) != 0 else - let l:notified = ale#lsp#NotifyForChanges(l:lsp_details) + let l:notified = ale#lsp#NotifyForChanges(l:id, l:root, a:buffer) endif " If this was a file save event, also notify the server of that. if a:linter.lsp isnot# 'tsserver' \&& getbufvar(a:buffer, 'ale_save_event_fired', 0) let l:save_message = ale#lsp#message#DidSave(a:buffer) - let l:request_id = ale#lsp#Send(l:id, l:save_message, l:root) - - let l:notified = l:request_id != 0 + let l:notified = ale#lsp#Send(l:id, l:save_message, l:root) != 0 endif if l:notified diff --git a/autoload/ale/references.vim b/autoload/ale/references.vim index 89df69eb..3a710b7b 100644 --- a/autoload/ale/references.vim +++ b/autoload/ale/references.vim @@ -68,37 +68,46 @@ function! s:FindReferences(linter) abort let l:buffer = bufnr('') let [l:line, l:column] = getcurpos()[1:2] - let l:Callback = a:linter.lsp is# 'tsserver' - \ ? function('ale#references#HandleTSServerResponse') - \ : function('ale#references#HandleLSPResponse') + if a:linter.lsp isnot# 'tsserver' + let l:column = min([l:column, len(getline(l:line))]) + endif - let l:lsp_details = ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback) + let l:lsp_details = ale#lsp_linter#StartLSP(l:buffer, a:linter) if empty(l:lsp_details) return 0 endif let l:id = l:lsp_details.connection_id + let l:root = l:lsp_details.project_root - if a:linter.lsp is# 'tsserver' - let l:message = ale#lsp#tsserver_message#References( - \ l:buffer, - \ l:line, - \ l:column - \) - else - " Send a message saying the buffer has changed first, or the - " references position probably won't make sense. - call ale#lsp#NotifyForChanges(l:lsp_details) + function! OnReady(...) abort closure + let l:Callback = a:linter.lsp is# 'tsserver' + \ ? function('ale#references#HandleTSServerResponse') + \ : function('ale#references#HandleLSPResponse') - let l:column = min([l:column, len(getline(l:line))]) + call ale#lsp#RegisterCallback(l:id, l:Callback) - let l:message = ale#lsp#message#References(l:buffer, l:line, l:column) - endif + if a:linter.lsp is# 'tsserver' + let l:message = ale#lsp#tsserver_message#References( + \ l:buffer, + \ l:line, + \ l:column + \) + else + " Send a message saying the buffer has changed first, or the + " references position probably won't make sense. + call ale#lsp#NotifyForChanges(l:id, l:root, l:buffer) + + let l:message = ale#lsp#message#References(l:buffer, l:line, l:column) + endif + + let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) - let l:request_id = ale#lsp#Send(l:id, l:message, l:lsp_details.project_root) + let s:references_map[l:request_id] = {} + endfunction - let s:references_map[l:request_id] = {} + call ale#lsp#WaitForCapability(l:id, l:root, 'references', function('OnReady')) endfunction function! ale#references#Find() abort diff --git a/autoload/ale/test.vim b/autoload/ale/test.vim index bea10c53..083b546f 100644 --- a/autoload/ale/test.vim +++ b/autoload/ale/test.vim @@ -52,3 +52,26 @@ function! ale#test#SetFilename(path) abort silent! noautocmd execute 'file ' . fnameescape(ale#path#Simplify(l:full_path)) endfunction + +function! s:RemoveModule(results) abort + for l:item in a:results + if has_key(l:item, 'module') + call remove(l:item, 'module') + endif + endfor +endfunction + +" Return loclist data without the module string, only in newer Vim versions. +function! ale#test#GetLoclistWithoutModule() abort + let l:results = getloclist(0) + call s:RemoveModule(l:results) + + return l:results +endfunction + +function! ale#test#GetQflistWithoutModule() abort + let l:results = getqflist() + call s:RemoveModule(l:results) + + return l:results +endfunction diff --git a/autoload/ale/toggle.vim b/autoload/ale/toggle.vim index 6b1affc4..da108782 100644 --- a/autoload/ale/toggle.vim +++ b/autoload/ale/toggle.vim @@ -43,7 +43,7 @@ function! ale#toggle#Toggle() abort call s:CleanupEveryBuffer() call s:DisablePostamble() - if has('balloon_eval') + if exists('*ale#balloon#Disable') call ale#balloon#Disable() endif endif |