Skip to content
Closed
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
37 changes: 19 additions & 18 deletions composer.lock

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

2 changes: 2 additions & 0 deletions lib/Extension/ContextMenu/ContextMenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phpactor\Extension\ContextMenu;

use Phpactor\Extension\CodeTransform\CodeTransformExtension;
use Phpactor\Extension\ContextMenu\Handler\ContextMenuHandler;
use Phpactor\Container\Extension;
use Phpactor\Container\ContainerBuilder;
Expand All @@ -22,6 +23,7 @@ public function load(ContainerBuilder $container)
$container->register('rpc.handler.context_menu', function (Container $container) {
return new ContextMenuHandler(
$container->get(WorseReflectionExtension::SERVICE_REFLECTOR),
$container->get(CodeTransformExtension::SERVICE_CLASS_INTERESTING_OFFSET_FINDER),
$container->get('application.helper.class_file_normalizer'),
json_decode(file_get_contents(__DIR__ . '/menu.json'), true),
$container
Expand Down
29 changes: 24 additions & 5 deletions lib/Extension/ContextMenu/Handler/ContextMenuHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Phpactor\Extension\ContextMenu\Handler;

use Phpactor\CodeTransform\Domain\Helper\InterestingOffsetFinder;
use Phpactor\MapResolver\Resolver;
use Phpactor\Extension\Rpc\Handler;
use Phpactor\TextDocument\ByteOffset;
use Phpactor\WorseReflection\Reflector;
use Phpactor\WorseReflection\Core\SourceCode;
use Phpactor\Extension\Rpc\Response\EchoResponse;
use Phpactor\Extension\Rpc\Request;
use Phpactor\Extension\Rpc\Response\InputCallbackResponse;
use Phpactor\Extension\Rpc\Response\Input\ChoiceInput;
use Phpactor\Extension\ContextMenu\ContextMenuExtension;
use Phpactor\WorseReflection\Core\Offset;
use Phpactor\WorseReflection\Core\Reflection\ReflectionOffset;
use Phpactor\Extension\Core\Application\Helper\ClassFileNormalizer;
use Phpactor\WorseReflection\Core\Inference\Symbol;
Expand All @@ -31,6 +32,11 @@ class ContextMenuHandler implements Handler
*/
private $reflector;

/**
* @var InterestingOffsetFinder
*/
private $offsetFinder;

/**
* @var array
*/
Expand All @@ -48,11 +54,13 @@ class ContextMenuHandler implements Handler

public function __construct(
Reflector $reflector,
InterestingOffsetFinder $offsetFinder,
ClassFileNormalizer $classFileNormalizer,
array $menu,
Container $container
) {
$this->reflector = $reflector;
$this->offsetFinder = $offsetFinder;
$this->menu = $menu;
$this->container = $container;
$this->classFileNormalizer = $classFileNormalizer;
Expand All @@ -78,7 +86,11 @@ public function configure(Resolver $resolver)

public function handle(array $arguments)
{
$offset = $this->offsetFromSourceAndOffset($arguments[self::PARAMETER_SOURCE], $arguments[self::PARAMETER_OFFSET], $arguments[self::PARAMETER_CURRENT_PATH]);
$offset = $this->offsetFromSourceAndOffset(
$arguments[self::PARAMETER_SOURCE],
$arguments[self::PARAMETER_OFFSET],
$arguments[self::PARAMETER_CURRENT_PATH]
);
$symbol = $offset->symbolContext()->symbol();

return $this->resolveAction($offset, $symbol, $arguments);
Expand Down Expand Up @@ -122,7 +134,7 @@ private function actionSelectionAction(Symbol $symbol, $symbolMenu, array $argum
self::NAME,
[
self::PARAMETER_SOURCE => $arguments[self::PARAMETER_SOURCE],
self::PARAMETER_OFFSET => (int) $arguments[self::PARAMETER_OFFSET],
self::PARAMETER_OFFSET => $symbol->position()->start(),
self::PARAMETER_CURRENT_PATH => $arguments[self::PARAMETER_CURRENT_PATH],
]
),
Expand All @@ -138,9 +150,16 @@ private function actionSelectionAction(Symbol $symbol, $symbolMenu, array $argum

private function offsetFromSourceAndOffset(string $source, int $offset, string $currentPath)
{
$sourceCode = SourceCode::fromPathAndString($currentPath, $source);

$interestingOffset = $this->offsetFinder->find(
$sourceCode,
ByteOffset::fromInt($offset)
);

return $this->reflector->reflectOffset(
SourceCode::fromPathAndString($currentPath, $source),
Offset::fromInt($offset)
$sourceCode,
$interestingOffset->toInt()
);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Extension/WorseReflectionExtra/Rpc/ContextMenuHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function actionSelectionAction(Symbol $symbol, $symbolMenu, array $argum
self::NAME,
[
self::PARAMETER_SOURCE => $arguments[self::PARAMETER_SOURCE],
self::PARAMETER_OFFSET => (int) $arguments[self::PARAMETER_OFFSET],
self::PARAMETER_OFFSET => $symbol->position()->start(),
self::PARAMETER_CURRENT_PATH => $arguments[self::PARAMETER_CURRENT_PATH],
]
),
Expand All @@ -140,7 +140,8 @@ private function offsetFromSourceAndOffset(string $source, int $offset, string $
{
return $this->reflector->reflectOffset(
SourceCode::fromPathAndString($currentPath, $source),
Offset::fromInt($offset)
Offset::fromInt($offset),
true
);
}

Expand Down
56 changes: 45 additions & 11 deletions tests/Unit/Extension/ContextMenu/Handler/ContextMenuHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Phpactor\Tests\Unit\Extension\ContextMenu\Handler;

use Phpactor\CodeTransform\Domain\Helper\InterestingOffsetFinder;
use Phpactor\Extension\Rpc\Handler;
use Phpactor\Extension\ContextMenu\Handler\ContextMenuHandler;
use Phpactor\TextDocument\ByteOffset;
use Phpactor\WorseReflection\Reflector;
use Phpactor\WorseReflection\Core\SourceCode;
use Phpactor\Extension\Rpc\Response\EchoResponse;
Expand Down Expand Up @@ -47,9 +49,15 @@ class ContextMenuHandlerTest extends HandlerTestCase
*/
private $classFileNormalizer;

/**
* @var InterestingOffsetFinder
*/
private $offsetFinder;

public function setUp()
{
$this->reflector = ReflectorBuilder::create()->addSource(SourceCode::fromPath(__FILE__))->build();
$this->offsetFinder = $this->prophesize(InterestingOffsetFinder::class);
$this->classFileNormalizer = $this->prophesize(ClassFileNormalizer::class);
$this->container = $this->prophesize(Container::class);
$this->requestHandler = $this->prophesize(RequestHandler::class);
Expand All @@ -59,6 +67,7 @@ public function createHandler(): Handler
{
return new ContextMenuHandler(
$this->reflector,
$this->offsetFinder->reveal(),
$this->classFileNormalizer->reveal(),
$this->menu,
$this->container->reveal()
Expand All @@ -67,10 +76,19 @@ public function createHandler(): Handler

public function testNoActionsAvailable()
{
$source = SourceCode::fromPathAndString(
'/hello.php',
'<?php $hello = "world"; echo $hello;'
);
$offset = ByteOffset::fromInt(4);

$this->offsetFinder->find($source, $offset)
->willReturn($offset);

$action = $this->handle(ContextMenuHandler::NAME, [
'source' => '<?php $hello = "world"; echo $hello;',
'offset' => 4,
'current_path' => '/hello.php',
'source' => (string) $source,
'offset' => $offset->toInt(),
'current_path' => $source->path(),
]);

$this->assertInstanceOf(EchoResponse::class, $action);
Expand All @@ -87,10 +105,20 @@ public function testReturnMenu()
],
]
];

$source = SourceCode::fromPathAndString(
'/hello.php',
'<?php $hello = "world"; echo $hello;'
);
$offset = ByteOffset::fromInt(8);

$this->offsetFinder->find($source, $offset)
->willReturn($offset);

$action = $this->handle(ContextMenuHandler::NAME, [
'source' => '<?php $hello = "world"; echo $hello;',
'offset' => 8,
'current_path' => '/hello.php',
'source' => (string) $source,
'offset' => $offset->toInt(),
'current_path' => $source->path(),
]);

$this->assertInstanceOf(InputCallbackResponse::class, $action);
Expand All @@ -106,12 +134,18 @@ public function testReplaceTokens()

$this->classFileNormalizer->classToFile('string')->willReturn(__FILE__);

$source = SourceCode::fromPathAndString('/hello.php', self::SOURCE);
$offset = ByteOffset::fromInt(8);

$this->offsetFinder->find($source, $offset)
->willReturn($offset);

$this->requestHandler->handle(
Request::fromNameAndParameters(
self::VARIABLE_ACTION,
[
'some_source' => self::SOURCE,
'some_offset' => 8,
'some_source' => (string) $source,
'some_offset' => $offset->toInt(),
'some_path' => __FILE__
]
)
Expand All @@ -134,9 +168,9 @@ public function testReplaceTokens()

$action = $this->handle(ContextMenuHandler::NAME, [
'action' => self::VARIABLE_ACTION,
'source' => self::SOURCE,
'offset' => 8,
'current_path' => '/hello.php',
'source' => (string) $source,
'offset' => $offset->toInt(),
'current_path' => $source->path(),
]);

$parameters = $action->parameters();
Expand Down