-
-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stable Context Menu Shortcuts #897
Changes from all commits
99a6741
7705716
e03e87a
3865c56
eb5d721
7f9266c
014af41
a58176f
c459451
ac120cc
bd6b8f4
8ac1871
ae575b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,10 +20,10 @@ function! phpactor#input#confirm(label, ResultHandler) | |
call a:ResultHandler(response) | ||
endfunction | ||
|
||
function! phpactor#input#choice(label, choices, ResultHandler) | ||
let s:usedShortcuts = [] | ||
function! phpactor#input#choice(label, choices, keyMap, ResultHandler) | ||
let s:usedShortcuts = [] | ||
let list = [] | ||
let choices = [] | ||
let usedShortcuts = [] | ||
|
||
if empty(a:choices) | ||
call confirm("No choices available") | ||
|
@@ -32,22 +32,16 @@ function! phpactor#input#choice(label, choices, ResultHandler) | |
|
||
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, "") | ||
" note that a:keyMap can be an empty list because PHP's json_decode | ||
" can't tell the difference between an empty list and an empty dict | ||
if !empty(a:keyMap) && has_key(a:keyMap, choiceLabel) && !empty(a:keyMap[choiceLabel]) | ||
let confirmLabel = s:determineConfirmLabelFromPreference(choiceLabel, a:keyMap[choiceLabel]) | ||
else | ||
let confirmLabel = s:determineConfirmLabel(choiceLabel) | ||
endif | ||
|
||
call add(list, confirmLabel) | ||
call add(choices, choiceLabel) | ||
endfor | ||
|
||
let choice = confirm(a:label, join(list, "\n")) | ||
|
@@ -57,18 +51,59 @@ function! phpactor#input#choice(label, choices, ResultHandler) | |
throw "cancelled" | ||
endif | ||
|
||
call a:ResultHandler(choices[choice - 1]) | ||
call a:ResultHandler(keys(a:choices)[choice - 1]) | ||
endfunction | ||
|
||
function! s:determineConfirmLabelFromPreference(choiceLabel, preference) | ||
let buffer = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure about variable scoping here -- should we use a scope prefix for local vars? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they are local by default apparently 😌 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, I didn't know there was a linter for VimL :) |
||
let foundShortcut = v:false | ||
|
||
for char in split(a:choiceLabel, '\zs') | ||
if foundShortcut == v:false && tolower(char) == tolower(a:preference) | ||
let foundShortcut = v:true | ||
|
||
call add(buffer, '&' . a:preference) | ||
call add(s:usedShortcuts, a:preference) | ||
continue | ||
endif | ||
|
||
call add(buffer, char) | ||
endfor | ||
|
||
if foundShortcut == v:false | ||
" Could not find char in the label - add the shortcut at the end | ||
call add(buffer, '&' . a:preference) | ||
endif | ||
|
||
return join(buffer, "") | ||
endfunction | ||
|
||
function! s:determineConfirmLabel(choiceLabel) | ||
let foundShortcut = v:false | ||
let buffer = [] | ||
for char in split(a:choiceLabel, '\zs') | ||
if v:false == foundShortcut && -1 == index(s:usedShortcuts, tolower(char)) | ||
let foundShortcut = v:true | ||
|
||
call add(buffer, '&') | ||
call add(s:usedShortcuts, tolower(char)) | ||
endif | ||
|
||
call add(buffer, char) | ||
endfor | ||
|
||
return join(buffer, "") | ||
endfunction | ||
|
||
function! phpactor#input#list(label, choices, multi, ResultHandler) | ||
let choices = sort(keys(a:choices)) | ||
|
||
try | ||
let strategy = g:phpactorInputListStrategy | ||
call call(strategy, [a:label, choices, a:multi, a:ResultHandler]) | ||
let strategy = g:phpactorInputListStrategy | ||
call call(strategy, [a:label, choices, a:multi, a:ResultHandler]) | ||
catch /E117/ | ||
redraw! | ||
echo 'The strategy "'. strategy .'" is unknown, check the value of "g:phpactorInputListStrategy".' | ||
redraw! | ||
echo 'The strategy "'. strategy .'" is unknown, check the value of "g:phpactorInputListStrategy".' | ||
endtry | ||
endfunction | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Phpactor\Extension\ContextMenu\Model; | ||
|
||
class Action | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $action; | ||
/** | ||
* @var string|null | ||
*/ | ||
private $key; | ||
/** | ||
* @var array | ||
*/ | ||
private $parameters; | ||
|
||
public function __construct(string $action, ?string $key = null, array $parameters = []) | ||
{ | ||
$this->action = $action; | ||
$this->key = $key; | ||
$this->parameters = $parameters; | ||
} | ||
|
||
public function action(): string | ||
{ | ||
return $this->action; | ||
} | ||
|
||
public function parameters(): array | ||
{ | ||
return $this->parameters; | ||
} | ||
|
||
public function key(): ?string | ||
{ | ||
return $this->key; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v:false == has_key({}, 'x')
is true. No need to check if a:keyMap is empty.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keyMap
can be alist
when empty due to using anarray
as the JSON source in PHP 🤔 in anycase - you can't usehas_key
on a list.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn'tI have to review the way of conversion keymap from JSON string to a vim variable.JSON_FORCE_OBJECT
helpful in such case (assuming thatjson_encode
is then used)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, we just switch to another problem then. There is probably a way to fix it on the PHP side, but I'm not that concerned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing too relevant to be concerned ;)