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
6 changes: 4 additions & 2 deletions autoload/phpactor/input.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ function! phpactor#input#choice(label, choices, ResultHandler)
call a:ResultHandler(choices[choice - 1])
endfunction

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

try
let strategy = phpactor#input#list#strategy()
call call(strategy, [a:label, choices, a:ResultHandler])
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".'
endtry
endfunction

" vim: et ts=4 sw=4 fdm=marker
35 changes: 29 additions & 6 deletions autoload/phpactor/input/list.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function! phpactor#input#list#strategy()
return g:phpactorInputListStrategy
endfunction

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

Expand All @@ -23,14 +23,35 @@ function! phpactor#input#list#inputlist(label, choices, ResultHandler)
call a:ResultHandler(a:choices[choice - 1])
endfunction

function! phpactor#input#list#fzf(label, choices, ResultHandler)
function! phpactor#input#list#fzf(label, choices, multi, ResultHandler)
let options = [
\ '--tiebreak=index',
\ '--layout=reverse-list',
\ ]
let sink = {
\ 'sink': {key -> a:ResultHandler(a:choices[key - 1])},
\ }

if a:multi
call extend(options, [
\ '--multi',
\ '--bind=ctrl-a:select-all,ctrl-d:deselect-all',
\ ])

let sink = {
\ 'sink*': {results -> a:ResultHandler(map(
\ results,
\ {key, value -> a:choices[value - 1]}
\ ))}
\ }
endif

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

function! s:auto_detect_strategy()
Expand All @@ -46,3 +67,5 @@ endfunction
function! s:add_number_to_choices(choices)
return map(copy(a:choices), {key, value -> key + 1 .') '. value})
endfunction

" vim: et ts=4 sw=4 fdm=marker
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions lib/Extension/CodeTransformExtra/Rpc/OverrideMethodHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,25 @@ public function handle(array $arguments)
self::PARAM_METHOD_NAME,
sprintf('Methods from "%s"', $parentClass->name()),
$this->methodChoices($parentClass)
));
)->withMultiple(true));

if ($this->hasMissingArguments($arguments)) {
return $this->createInputCallback($arguments);
}

$transformedCode = $this->overrideMethod->overrideMethod(
SourceCode::fromString($arguments[self::PARAM_SOURCE]),
(string) $class->name(),
$arguments[self::PARAM_METHOD_NAME]
);
$newCode = $arguments[self::PARAM_SOURCE];
foreach ((array) $arguments[self::PARAM_METHOD_NAME] as $methodName) {
$newCode = $this->overrideMethod->overrideMethod(
SourceCode::fromString((string) $newCode),
(string) $class->name(),
$methodName
);
}

return UpdateFileSourceResponse::fromPathOldAndNewSource(
$arguments[self::PARAM_PATH],
$arguments[self::PARAM_SOURCE],
(string) $transformedCode
(string) $newCode
);
}

Expand Down Expand Up @@ -121,6 +124,7 @@ private function parentClass(ReflectionClass $class)

private function methodChoices(ReflectionClass $parentClass)
{
// TODO filter methods already implemented in the current class
$methodNames = array_map(function (ReflectionMethod $method) {
return $method->name();
}, iterator_to_array(
Expand Down
8 changes: 4 additions & 4 deletions plugin/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,6 @@ function! phpactor#_rpc_dispatch(actionName, parameters)

" >> open_file
if a:actionName == "open_file"

let cmd = 'edit'

if a:parameters['target'] == 'focused_window'
call phpactor#_switchToBufferOrEdit(a:parameters['path'])

Expand Down Expand Up @@ -678,7 +675,8 @@ function! phpactor#_rpc_dispatch_input(inputs, action, parameters)
elseif 'list' == input['type']
let TypeHandler = function('phpactor#input#list', [
\ inputParameters['label'],
\ inputParameters['choices']
\ inputParameters['choices'],
\ inputParameters['multi']
\ ])
elseif 'confirm' == input['type']
let TypeHandler = function('phpactor#input#confirm', [
Expand All @@ -690,3 +688,5 @@ function! phpactor#_rpc_dispatch_input(inputs, action, parameters)

call TypeHandler(ResultHandler)
endfunction

" vim: et ts=4 sw=4 fdm=marker
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class OverrideMethodHandlerTest extends HandlerTestCase

public function setUp()
{
$this->reflector = ReflectorBuilder::create()->addSource('<?php class ParentClass { public function foobar() {} }')->build();
$this->reflector = ReflectorBuilder::create()->addSource('<?php class ParentClass { public function foobar() {} public function barfoor() {} }')->build();
$this->overrideMethod = $this->prophesize(OverrideMethod::class);
}

Expand All @@ -43,7 +43,7 @@ public function testSuggestsPossibleMethods()
'class_name' => 'ChildClass',
'path' => __FILE__,
'source' => <<<'EOT'
<?php
<?php

class ChildClass extends ParentClass
{
Expand All @@ -55,13 +55,13 @@ class ChildClass extends ParentClass
$input = reset($input);
$this->assertInstanceOf(ListInput::class, $input);
$choices = $input->choices();
$this->assertCount(1, $choices);
$this->assertCount(2, $choices);
}

public function testOverrideMethod()
public function testOverrideAMethodGivenAsAString()
{
$source = <<<'EOT'
<?php
<?php

class ChildClass extends ParentClass
{
Expand All @@ -81,4 +81,37 @@ class ChildClass extends ParentClass
$this->assertInstanceOf(UpdateFileSourceResponse::class, $action);
$this->assertEquals('hello', $action->newSource());
}

public function testOverrideMethodsGivenAsArray()
{
$source = <<<'EOT'
<?php

class ChildClass extends ParentClass
{
}
EOT
;

$foobarTransformedCode = TransformSourceCode::fromString('foobar was added');
$barfooTransformedCode = TransformSourceCode::fromString('barfoo was also added');

$this->overrideMethod->overrideMethod($source, 'ChildClass', 'foobar')
->willReturn($foobarTransformedCode)
->shouldBeCalledTimes(1);
$this->overrideMethod->overrideMethod($foobarTransformedCode, 'ChildClass', 'barfoo')
->willReturn($barfooTransformedCode)
->shouldBeCalledTimes(1);

$action = $this->handle('override_method', [
'class_name' => 'ChildClass',
'method_name' => ['foobar', 'barfoo'],
'path' => __FILE__,
'source' => $source
]);

/** @var UpdateFileSourceResponse $action */
$this->assertInstanceOf(UpdateFileSourceResponse::class, $action);
$this->assertEquals((string) $barfooTransformedCode, $action->newSource());
}
}