Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

Commit

Permalink
Extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
BladeMF committed May 8, 2021
1 parent 1ca1b27 commit b8f81ed
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Phpactor\Extension\LanguageServerCodeTransform\CodeAction;

use Amp\Promise;
use Phpactor\CodeTransform\Domain\Refactor\ExtractMethod;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\LanguageServerBridge\Converter\PositionConverter;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\ExtractMethodCommand;
use Phpactor\LanguageServerProtocol\CodeAction;
use Phpactor\LanguageServerProtocol\Command;
use Phpactor\LanguageServerProtocol\Range;
use Phpactor\LanguageServerProtocol\TextDocumentItem;
use Phpactor\LanguageServer\Core\CodeAction\CodeActionProvider;
use function Amp\call;

class ExtractMethodProvider implements CodeActionProvider
{
public const KIND = 'quickfix.extract_method';

/**
* @var ExtractMethod
*/
private $extractMethod;

public function __construct(ExtractMethod $extractMethod)
{
$this->extractMethod = $extractMethod;
}
/**
* {@inheritDoc}
*/
public function kinds(): array
{
return [
self::KIND
];
}
public function provideActionsFor(TextDocumentItem $textDocument, Range $range): Promise
{
return call(function () use ($textDocument, $range) {
if (!$this->extractMethod->canExtractMethod(
SourceCode::fromStringAndPath($textDocument->text, $textDocument->uri),
PositionConverter::positionToByteOffset($range->start, $textDocument->text)->toInt(),
PositionConverter::positionToByteOffset($range->end, $textDocument->text)->toInt()
)) {
return [];
}

return [
CodeAction::fromArray([
'title' => 'Extract method',
'kind' => self::KIND,
'diagnostics' => [],
'command' => new Command(
'Extract method',
ExtractMethodCommand::NAME,
[
$textDocument->uri,
PositionConverter::positionToByteOffset($range->start, $textDocument->text)->toInt(),
PositionConverter::positionToByteOffset($range->end, $textDocument->text)->toInt()
]
)
])
];
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Phpactor\Extension\LanguageServerCodeTransform;

use Phpactor\CodeTransform\Domain\Helper\UnresolvableClassNameFinder;
use Phpactor\CodeTransform\Domain\Refactor\ExtractMethod;
use Phpactor\CodeTransform\Domain\Refactor\GenerateMethod;
use Phpactor\CodeTransform\Domain\Refactor\ImportName;
use Phpactor\Container\Container;
Expand All @@ -12,10 +13,12 @@
use Phpactor\Extension\CodeTransform\CodeTransformExtension;
use Phpactor\Extension\LanguageServerBridge\Converter\TextEditConverter;
use Phpactor\Extension\LanguageServerCodeTransform\CodeAction\CreateClassProvider;
use Phpactor\Extension\LanguageServerCodeTransform\CodeAction\ExtractMethodProvider;
use Phpactor\Extension\LanguageServerCodeTransform\CodeAction\GenerateMethodProvider;
use Phpactor\Extension\LanguageServerCodeTransform\CodeAction\ImportNameProvider;
use Phpactor\Extension\LanguageServerCodeTransform\CodeAction\TransformerCodeActionPovider;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\CreateClassCommand;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\ExtractMethodCommand;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\GenerateMethodCommand;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\ImportNameCommand;
use Phpactor\Extension\LanguageServerCodeTransform\LspCommand\TransformCommand;
Expand Down Expand Up @@ -102,6 +105,18 @@ private function registerCommands(ContainerBuilder $container): void
'name' => GenerateMethodCommand::NAME
],
]);

$container->register(ExtractMethodCommand::class, function (Container $container) {
return new ExtractMethodCommand(
$container->get(ClientApi::class),
$container->get(LanguageServerExtension::SERVICE_SESSION_WORKSPACE),
$container->get(ExtractMethod::class)
);
}, [
LanguageServerExtension::TAG_COMMAND => [
'name' => ExtractMethodCommand::NAME
],
]);
}

private function registerCodeActions(ContainerBuilder $container): void
Expand Down Expand Up @@ -184,5 +199,13 @@ private function registerCodeActions(ContainerBuilder $container): void
// LanguageServerExtension::TAG_DIAGNOSTICS_PROVIDER => [],
LanguageServerExtension::TAG_CODE_ACTION_PROVIDER => []
]);

$container->register(GenerateMethodProvider::class, function (Container $container) {
return new ExtractMethodProvider(
$container->get(ExtractMethod::class)
);
}, [
LanguageServerExtension::TAG_CODE_ACTION_PROVIDER => []
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Phpactor\Extension\LanguageServerCodeTransform\LspCommand;

use Amp\Promise;
use Amp\Success;
use Phpactor\CodeTransform\Domain\Exception\TransformException;
use Phpactor\CodeTransform\Domain\Refactor\ExtractMethod;
use Phpactor\CodeTransform\Domain\SourceCode;
use Phpactor\Extension\LanguageServerBridge\Converter\TextEditConverter;
use Phpactor\LanguageServerProtocol\ApplyWorkspaceEditResponse;
use Phpactor\LanguageServer\Core\Command\Command;
use Phpactor\LanguageServerProtocol\WorkspaceEdit;
use Phpactor\LanguageServer\Core\Server\ClientApi;
use Phpactor\LanguageServer\Core\Workspace\Workspace;

class ExtractMethodCommand implements Command
{
public const NAME = 'extract_method';

/**
* @var ExtractMethod
*/
private $extractMethod;
/**
* @var ClientApi
*/
private $clientApi;
/**
* @var Workspace
*/
private $workspace;

public function __construct(
ClientApi $clientApi,
Workspace $workspace,
ExtractMethod $extractMethod
) {
$this->extractMethod = $extractMethod;
$this->clientApi = $clientApi;
$this->workspace = $workspace;
}
/**
* @return Promise<ApplyWorkspaceEditResponse>
*/
public function __invoke(string $uri, int $startOffset, int $endOffset): Promise
{
$textDocument = $this->workspace->get($uri);

try {
$textEdits = $this->extractMethod->extractMethod(
SourceCode::fromStringAndPath($textDocument->text, $textDocument->uri),
$startOffset,
$endOffset,
'newMethod'
);
} catch (TransformException $error) {
$this->clientApi->window()->showMessage()->warning($error->getMessage());
return new Success(null);
}

return $this->clientApi->workspace()->applyEdit(new WorkspaceEdit([
$uri => TextEditConverter::toLspTextEdits($textEdits->textEdits(), $textDocument->text)
]), 'Extract method');
}
}

0 comments on commit b8f81ed

Please sign in to comment.