Skip to content

Commit

Permalink
Switch to lambda and closure syntax
Browse files Browse the repository at this point in the history
Lambdas are easier to read than the `'v:val'` syntax, and closures are
less clunky that dict functions.

Delete the `lsc#util#compose` helper and inline the the call to the
composed function. Also inline the SearchCompletions function since it's
separation was a leftover from an attempt to support multiple completion
sources.

Make `lsc#diagnostics#convert` function script-private since it was
unused outside the file.
  • Loading branch information
natebosch committed Aug 6, 2017
1 parent 7454a19 commit 7c7c708
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 56 deletions.
39 changes: 17 additions & 22 deletions autoload/lsc/complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,28 @@ function! s:startCompletion() abort
let s:completion_id += 1
let s:completion_canceled = v:false
call s:MarkCompleting(&filetype)
let data = {'old_pos': getcurpos(),
\ 'completion_id': s:completion_id,
\ 'filetype': &filetype}
function data.trigger(completion)
call s:MarkNotCompleting(self.filetype)
if s:isCompletionValid(self.old_pos, self.completion_id)
let old_pos = getcurpos()
let completion_id = s:completion_id
let filetype = &filetype
function! OnComplete(completion) closure abort
let completions = s:CompletionItems(a:completion)
call s:MarkNotCompleting(filetype)
if s:isCompletionValid(old_pos, completion_id)
if (g:lsc_enable_autocomplete)
call s:SuggestCompletions(a:completion)
call s:SuggestCompletions(completions)
else
let b:lsc_completion = a:completion
let b:lsc_completion = completions
endif
else
let b:lsc_is_completing = v:false
endif
endfunction
call s:SearchCompletions(data.trigger)
call lsc#file#flushChanges()
let params = { 'textDocument': {'uri': lsc#util#documentUri()},
\ 'position': {'line': line('.') - 1, 'character': col('.') - 1}
\ }
call lsc#server#call(&filetype, 'textDocument/completion', params,
\ funcref('OnComplete'))
endfunction

function! s:SuggestCompletions(completion) abort
Expand Down Expand Up @@ -170,7 +176,7 @@ endfunction
function! s:FindSuggestions(base, completion) abort
let items = copy(a:completion.items)
if len(a:base) == 0 | return items | endif
return filter(items, '<SID>MatchSuggestion(a:base, v:val)')
return filter(items, {_, item -> s:MatchSuggestion(a:base, item)})
endfunction

function! s:MatchSuggestion(base, suggestion) abort
Expand All @@ -179,17 +185,6 @@ function! s:MatchSuggestion(base, suggestion) abort
return word =~? a:base
endfunction

" Flush file contents and call the server to request completions for the current
" cursor position.
function! s:SearchCompletions(onFound) abort
call lsc#file#flushChanges()
let params = { 'textDocument': {'uri': lsc#util#documentUri()},
\ 'position': {'line': line('.') - 1, 'character': col('.') - 1}
\ }
call lsc#server#call(&filetype, 'textDocument/completion', params,
\ lsc#util#compose(a:onFound, function('<SID>CompletionItems')))
endfunction

" Normalize LSP completion suggestions to the format used by vim.
"
" Returns a dict with:
Expand All @@ -205,7 +200,7 @@ function! s:CompletionItems(completion_result) abort
else
let completion_items = a:completion_result.items
endif
call map(completion_items, 's:CompletionItem(v:val)')
call map(completion_items, {_, item -> s:CompletionItem(item)})
let completion = {'items' : completion_items}
for item in completion_items
if has_key(item, 'start_col')
Expand Down
4 changes: 2 additions & 2 deletions autoload/lsc/diagnostics.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ endif

" Converts between an LSP diagnostic and the internal representation used for
" highlighting.
function! lsc#diagnostics#convert(diagnostic) abort
function! s:Convert(diagnostic) abort
let line = a:diagnostic.range.start.line + 1
let character = a:diagnostic.range.start.character + 1
" TODO won't work for multiline error
Expand Down Expand Up @@ -81,7 +81,7 @@ function! s:DiagnosticsVersion(file_path) abort
endfunction

function! lsc#diagnostics#setForFile(file_path, diagnostics) abort
call map(a:diagnostics, 'lsc#diagnostics#convert(v:val)')
call map(a:diagnostics, {_, diagnostic -> s:Convert(diagnostic)})
let diagnostics_by_line = {}
for diagnostic in a:diagnostics
if !has_key(diagnostics_by_line, diagnostic.range[0])
Expand Down
12 changes: 6 additions & 6 deletions autoload/lsc/reference.vim
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function! lsc#reference#goToDefinition() abort
call lsc#file#flushChanges()
let s:goto_definition_id += 1
let data = {'old_pos': getcurpos(),
\ 'goto_definition_id': s:goto_definition_id}
function data.trigger(result) abort
if !s:isGoToValid(self.old_pos, self.goto_definition_id)
let old_pos = getcurpos()
let goto_definition_id = s:goto_definition_id
function! Jump(result) closure abort
if !s:isGoToValid(old_pos, goto_definition_id)
echom 'GoToDefinition skipped'
return
endif
Expand All @@ -23,7 +23,7 @@ function! lsc#reference#goToDefinition() abort
call s:goTo(file, line, character)
endfunction
call lsc#server#call(&filetype, 'textDocument/definition',
\ s:TextDocumentPositionParams(), data.trigger)
\ s:TextDocumentPositionParams(), function('Jump'))
endfunction

function! s:TextDocumentPositionParams() abort
Expand All @@ -41,7 +41,7 @@ function! lsc#reference#findReferences() abort
endfunction

function! s:setQuickFixReferences(results) abort
call map(a:results, 's:QuickFixItem(v:val)')
call map(a:results, {_, ref -> s:QuickFixItem(ref)})
call sort(a:results, 'lsc#util#compareQuickFixItems')
call setqflist(a:results)
copen
Expand Down
17 changes: 8 additions & 9 deletions autoload/lsc/server.vim
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ function! s:RunCommand(command) abort
let channel = job_getchannel(job)
let ch_id = ch_info(channel)['id']
let s:channel_buffers[ch_id] = ''
let data = {'command': a:command, 'channel': channel}
function data.onInitialize(params) abort
function! OnInitialize(params) closure abort
" TODO: Check capabilities?
if has_key(a:params, 'capabilities')
let capabilities = a:params['capabilities']
Expand All @@ -98,18 +97,18 @@ function! s:RunCommand(command) abort
if has_key(completion_provider, 'triggerCharacters')
let trigger_characters = completion_provider['triggerCharacters']
for filetype in keys(g:lsc_server_commands)
if g:lsc_server_commands[filetype] != self.command | continue | endif
if g:lsc_server_commands[filetype] != a:command | continue | endif
call lsc#complete#setTriggers(filetype, trigger_characters)
endfor
endif
endif
endif
call add(s:initialized_servers, self.command)
if has_key(s:buffered_calls, self.command)
for buffered_call in s:buffered_calls[self.command]
call ch_sendraw(self.channel, buffered_call)
call add(s:initialized_servers, a:command)
if has_key(s:buffered_calls, a:command)
for buffered_call in s:buffered_calls[a:command]
call ch_sendraw(channel, buffered_call)
endfor
unlet s:buffered_calls[self.command]
unlet s:buffered_calls[a:command]
endif
endfunction
if exists('g:lsc_trace_level') &&
Expand All @@ -124,7 +123,7 @@ function! s:RunCommand(command) abort
\ 'trace': trace_level
\}
call lsc#server#call(&filetype, 'initialize',
\ params, data.onInitialize, v:true)
\ params, function('OnInitialize'), v:true)
return v:true
endfunction

Expand Down
18 changes: 1 addition & 17 deletions autoload/lsc/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@ function! lsc#util#documentPath(uri) abort
return substitute(a:uri, '^file://', '', 'v')
endfunction

" Returns a funcref which is the result of first calling `inner` and then using
" the result as the argument to `outer`. `inner` may take any number of
" arguments, but `outer` must take a single argument.
"
" For examples lsc#util#compose(g, f) returns a function (args) => g(f(args)).
function! lsc#util#compose(outer, inner) abort
let data = {'funcs': [a:outer, a:inner]}
function data.composed(...) abort
let Outer = self['funcs'][0]
let Inner = self['funcs'][1]
let result = call(Inner, a:000)
return call(Outer, [result])
endfunction
return data.composed
endfunction

function! lsc#util#error(message) abort
echohl Error
echom '[lsc] '.a:message
Expand Down Expand Up @@ -111,7 +95,7 @@ endfunction
function! s:createOrJumpToPreview(line_count) abort
let want_height = min([a:line_count, &previewheight])
let windows = range(1, winnr('$'))
call filter(windows, 'getwinvar(v:val, "&previewwindow") == 1')
call filter(windows, {_, win -> getwinvar(win, "&previewwindow") == 1})
if len(windows) > 0
execute string(windows[0]).' wincmd W'
edit __lsc_preview__
Expand Down

0 comments on commit 7c7c708

Please sign in to comment.