Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions autoload/phpactor/input.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function! phpactor#input#text(label, default, completionType, ResultHandler)
if v:null != a:completionType
let text = input(a:label, a:default, a:completionType)
else
let text = input(a:label, a:default)
endif

call a:ResultHandler(text)
endfunction

function! phpactor#input#confirm(label, ResultHandler)
let choice = confirm(a:label, "&Yes\n&No\n")

if choice == 1
let response = v:true
else
let response = v:false
endif

call a:ResultHandler(response)
endfunction

function! phpactor#input#choice(label, choices, ResultHandler)
let list = []
let choices = []
let usedShortcuts = []

if empty(a:choices)
call confirm("No choices available")
throw "cancelled"
endif

for choiceLabel in keys(a:choices)
let buffer = []
let foundShortcut = v:false

for char in split(choiceLabel, '\zs')
if v:false == foundShortcut && -1 == index(usedShortcuts, tolower(char))
call add(buffer, '&')
let foundShortcut = v:true
call add(usedShortcuts, tolower(char))
endif

call add(buffer, char)
endfor

let confirmLabel = join(buffer, "")

call add(list, confirmLabel)
call add(choices, choiceLabel)
endfor

let choice = confirm(a:label, join(list, "\n"))

if (choice == 0)
" this is an exception, not a message!
throw "cancelled"
endif

call a:ResultHandler(choices[choice - 1])
endfunction

function! phpactor#input#list(label, choices, ResultHandler)
let choices = sort(keys(a:choices))

try
let strategy = phpactor#input#list#strategy()
call call(strategy, [a:label, choices, a:ResultHandler])
catch /E117/
redraw!
echo 'The strategy "'. strategy .'" is unknown, check the value of "g:phpactorInputListStrategy".'
endtry
endfunction
48 changes: 48 additions & 0 deletions autoload/phpactor/input/list.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function! phpactor#input#list#strategy()
if has_key(g:, 'phpactorInputListStrategy')
return g:phpactorInputListStrategy
endif

if has_key(g:, 'phpactorCustomInputListStrategy')
let g:phpactorInputListStrategy = g:phpactorCustomInputListStrategy
else
let g:phpactorInputListStrategy = s:auto_detect_strategy()
endif

return g:phpactorInputListStrategy
endfunction

function! phpactor#input#list#inputlist(label, choices, ResultHandler)
echo a:label
let choice = inputlist(s:add_number_to_choices(a:choices))

if (choice == 0)
throw "cancelled"
endif

call a:ResultHandler(a:choices[choice - 1])
endfunction

function! phpactor#input#list#fzf(label, choices, ResultHandler)
" sink works because "key" is converted to integer, so only the number is kept
call fzf#run({
\ 'source': s:add_number_to_choices(a:choices),
\ 'sink': {key -> a:ResultHandler(a:choices[key - 1])},
\ 'down': '30%',
\ 'options': ['--tiebreak=index', '--layout=reverse-list']
\ })
endfunction

function! s:auto_detect_strategy()
let strategy = 'inputlist'

if get(g:, 'loaded_fzf', 0)
let strategy = 'fzf'
endif

return 'phpactor#input#list#'. strategy
endfunction

function! s:add_number_to_choices(choices)
return map(copy(a:choices), {key, value -> key + 1 .') '. value})
endfunction
41 changes: 39 additions & 2 deletions doc/vim-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,35 @@ Configuration

The plugin has some configuration options:

```
```vim
let g:phpactorPhpBin = 'php'
let g:phpactorBranch = 'master'
let g:phpactorOmniAutoClassImport = v:true
let g:phpactorInputListStrategy = 'inputlist|fzf'

" Example of implementation with vim's inputlist() function
function! InputListCustomStrategy(label, choices, ResultHandler)
echo a:label
let choice = inputlist(s:add_number_to_choices(a:choices))

if (choice == 0)
throw "cancelled"
endif

call a:ResultHandler(a:choices[choice - 1])
endfunction

let g:phpactorCustomInputListStrategy = 'InputListCustomStrategy'
```

- `g:phpactorPhpBin`: PHP executable to use.
- `g:phpactorBranch`: Phpactor branch (default is `master`, use `develop` for
bleeding edge).
- `g:phpactorOmniAutoClassImport`: Automatically import classes when
completing class names with OmniComplete.

- `g:phpactorInputListStrategy`: Select a strategy for the [Input
List](#input-list).
- `g:phpactorCustomInputListStrategy`: Specify your own strategy.

Extensions
----------
Expand Down Expand Up @@ -272,3 +289,23 @@ should see something like the following:
Method "execute":
[r]eplace_references, (f)ind_references, (g)enerate_method, g(o)to_definition:
```

Input List
----------

The input list is the window shown to let you choose an item among a list.

### Strategies

This plugin provides two strategy to handle input lists:
- `inputlist`: Vim's internal `inputlist()` function.
- `fzf`: Fuzzy finder using [Fzf](https://github.com/junegunn/fzf) plugin.

You can choose between those strategies by specifying the option
[g:phpactorInputListStrategy](#configuration).

### Input list strategies auto-detection

When no strategy is defined the plugin will default to the `fzf` strategy if
the [Fzf](https://github.com/junegunn/fzf) plugin is loaded or to `inputlist`
if it's not.
151 changes: 49 additions & 102 deletions plugin/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -441,69 +441,6 @@ function! phpactor#rpc(action, arguments)
endif
endfunction

function! phpactor#_input_choice(label, choices)
let list = []
let choices = []
let usedShortcuts = []

if empty(a:choices)
call confirm("No choices available")
throw "cancelled"
endif

for choiceLabel in keys(a:choices)
let buffer = []
let foundShortcut = v:false

for char in split(choiceLabel, '\zs')
if v:false == foundShortcut && -1 == index(usedShortcuts, tolower(char))
call add(buffer, '&')
let foundShortcut = v:true
call add(usedShortcuts, tolower(char))
endif

call add(buffer, char)
endfor

let confirmLabel = join(buffer, "")

call add(list, confirmLabel)
call add(choices, choiceLabel)
endfor

let choice = confirm(a:label, join(list, "\n"))

if (choice == 0)
" this is an exception, not a message!
throw "cancelled"
endif

let choice = choice - 1
return a:choices[get(choices, choice)]
endfunction

function! phpactor#_input_list(label, choices)
let list = []
let choices = []

let c = 1
for choiceLabel in keys(a:choices)
call add(list, c . ") " . choiceLabel)
call add(choices, choiceLabel)
let c = c + 1
endfor

echo a:label
let choice = inputlist(list)

if (choice == 0)
throw "cancelled"
endif

let choice = choice - 1
return a:choices[choices[choice]]
endfunction

function! phpactor#_rpc_dispatch(actionName, parameters)

" >> return_choice
Expand Down Expand Up @@ -633,21 +570,17 @@ function! phpactor#_rpc_dispatch(actionName, parameters)

" >> input_callback
if a:actionName == "input_callback"
let inputs = a:parameters['inputs']
let action = a:parameters['callback']['action']
let parameters = a:parameters['callback']['parameters']
for input in a:parameters['inputs']

try
let value = phpactor#_rpc_dispatch_input(input['type'], input['parameters'])
catch /cancelled/
execute ':redraw'
echo "Cancelled"
return
endtry

let parameters[input['name']] = value
endfor
call phpactor#rpc(a:parameters['callback']['action'], parameters)
return
try
return phpactor#_rpc_dispatch_input(inputs, action, parameters)
catch /cancelled/
redraw
echo 'Cancelled'
return
endtry
endif

" >> information
Expand Down Expand Up @@ -708,38 +641,52 @@ function! phpactor#_rpc_dispatch(actionName, parameters)
throw "Do not know how to handle action '" . a:actionName . "'"
endfunction

function! phpactor#_rpc_dispatch_input(type, parameters)
" Remove any existing output in the message window
execute ':redraw'
function! phpactor#_rpc_dispatch_input_handler(Next, parameters, parameterName, result)

@dantleech dantleech May 30, 2019 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should Next be lower case? Or is this a convention for anonymous functions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly yes.
A public function should start with a letter in upper case.
Exception for autoloaded function where everything is lower case (most of the time).
But I'm not 100% sure.

I will look into the error you mentioned.

let a:parameters[a:parameterName] = a:result

" >> text
if a:type == 'text'
if v:null != a:parameters['type']
return input(a:parameters['label'], a:parameters['default'], a:parameters['type'])
endif
return input(a:parameters['label'], a:parameters['default'])
endif
call a:Next(a:parameters)
endfunction

" >> choice
if a:type == 'choice'
return phpactor#_input_choice(a:parameters['label'], a:parameters['choices'])
endif
function! phpactor#_rpc_dispatch_input(inputs, action, parameters)
let input = remove(a:inputs, 0)
let inputParameters = input['parameters']

if a:type == 'list'
return phpactor#_input_list(a:parameters['label'], a:parameters['choices'])
endif
let Next = empty(a:inputs)
\ ? function('phpactor#rpc', [a:action])
\ : function('phpactor#_rpc_dispatch_input', [a:inputs, a:action])

" >> confirm
if a:type == 'confirm'
let choice = confirm(a:parameters["label"], "&Yes\n&No\n")
let ResultHandler = function('phpactor#_rpc_dispatch_input_handler', [
\ Next,
\ a:parameters,
\ input['name'],
\ ])

if choice == 1
return v:true
endif
" Remove any existing output in the message window
execute ':redraw'

return v:false
if 'text' == input['type']
let TypeHandler = function('phpactor#input#text', [
\ inputParameters['label'],
\ inputParameters['default'],
\ inputParameters['type']
\ ])
elseif 'choice' == input['type']
let TypeHandler = function('phpactor#input#choice', [
\ inputParameters['label'],
\ inputParameters['choices']
\ ])
elseif 'list' == input['type']
let TypeHandler = function('phpactor#input#list', [
\ inputParameters['label'],
\ inputParameters['choices']
\ ])
elseif 'confirm' == input['type']
let TypeHandler = function('phpactor#input#confirm', [
\ inputParameters['label']
\ ])
else
throw "Do not know how to handle input '" . input['type'] . "'"
endif


throw "Do not know how to handle input '" . a:type . "'"
call TypeHandler(ResultHandler)
endfunction