Repository navigation
Import missing classes #843
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1c87f27
Import Unresolvable Classes
dantleech d9a16ff
Updates composer lock
dantleech adb5e8d
Adds doc and rename to => missing classes
dantleech 46297d9
Add closing underscore
dantleech ff72b1e
Updates lock
dantleech a3c3913
Revert erronous changes to configs
dantleech 63f41b5
Always use inputlist strategy for collections
dantleech 6dba766
Moves decision to use inputlist strategy to dedicated function
dantleech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ | |
|
|
||
| class ClassNew1 | ||
| { | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
lib/Extension/CodeTransformExtra/Rpc/ImportMissingClassesHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| namespace Phpactor\Extension\CodeTransformExtra\Rpc; | ||
|
|
||
| use Phpactor\CodeTransform\Domain\Helper\UnresolvableClassNameFinder; | ||
| use Phpactor\CodeTransform\Domain\NameWithByteOffset; | ||
| use Phpactor\Extension\Rpc\Handler; | ||
| use Phpactor\Extension\Rpc\Request; | ||
| use Phpactor\Extension\Rpc\RequestHandler; | ||
| use Phpactor\Extension\Rpc\Response\CollectionResponse; | ||
| use Phpactor\Extension\Rpc\Response\EchoResponse; | ||
| use Phpactor\MapResolver\Resolver; | ||
| use Phpactor\Name\Name; | ||
| use Phpactor\TextDocument\TextDocumentBuilder; | ||
|
|
||
| class ImportMissingClassesHandler implements Handler | ||
| { | ||
| public const NAME = 'import_missing_classes'; | ||
| public const PARAM_SOURCE = 'source'; | ||
| public const PARAM_PATH = 'path'; | ||
|
|
||
| /** | ||
| * @var UnresolvableClassNameFinder | ||
| */ | ||
| private $unresolvableClassNameFinder; | ||
|
|
||
| /** | ||
| * @var RequestHandler | ||
| */ | ||
| private $handler; | ||
|
|
||
| public function __construct( | ||
| RequestHandler $handler, | ||
| UnresolvableClassNameFinder $unresolvableClassNameFinder | ||
| ) { | ||
| $this->unresolvableClassNameFinder = $unresolvableClassNameFinder; | ||
| $this->handler = $handler; | ||
| } | ||
|
|
||
| public function configure(Resolver $resolver) | ||
| { | ||
| $resolver->setRequired([ | ||
| self::PARAM_PATH, | ||
| self::PARAM_SOURCE, | ||
| ]); | ||
| } | ||
|
|
||
| public function handle(array $arguments) | ||
| { | ||
| $document = TextDocumentBuilder::create( | ||
| $arguments[self::PARAM_SOURCE] | ||
| )->language('php')->uri($arguments[self::PARAM_PATH])->build(); | ||
|
|
||
| $unresolvedClasses = $this->unresolvableClassNameFinder->find($document); | ||
|
|
||
| $responses = []; | ||
| foreach ($unresolvedClasses as $unresolvedClass) { | ||
| assert($unresolvedClass instanceof NameWithByteOffset); | ||
| $responses[] = $this->handler->handle(Request::fromNameAndParameters(ImportClassHandler::NAME, [ | ||
| ImportClassHandler::PARAM_PATH => $arguments[self::PARAM_PATH], | ||
| ImportClassHandler::PARAM_SOURCE => $arguments[self::PARAM_SOURCE], | ||
| ImportClassHandler::PARAM_OFFSET => $unresolvedClass->byteOffset()->toInt() + 1 | ||
| ])); | ||
| } | ||
|
|
||
| if (empty($responses)) { | ||
| return EchoResponse::fromMessage('No unresolved classes found'); | ||
| } | ||
|
|
||
| return CollectionResponse::fromActions($responses); | ||
| } | ||
|
|
||
| public function name(): string | ||
| { | ||
| return self::NAME; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tests/Unit/Extension/CodeTransformExtra/Rpc/ImportMissingClassesHandlerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
|
|
||
| namespace Phpactor\Tests\Unit\Extension\CodeTransformExtra\Rpc; | ||
|
|
||
| use Phpactor\CodeTransform\Domain\Helper\UnresolvableClassNameFinder; | ||
| use Phpactor\CodeTransform\Domain\NameWithByteOffset; | ||
| use Phpactor\Extension\CodeTransformExtra\Rpc\ImportMissingClassesHandler; | ||
| use Phpactor\Extension\Rpc\Response\CollectionResponse; | ||
| use Phpactor\Extension\Rpc\Response\EchoResponse; | ||
| use Phpactor\Extension\Rpc\RpcExtension; | ||
| use Phpactor\Extension\Rpc\Test\HandlerTester; | ||
| use Phpactor\Name\QualifiedName; | ||
| use Phpactor\Tests\IntegrationTestCase; | ||
| use Phpactor\TextDocument\ByteOffset; | ||
| use Phpactor\TextDocument\TextDocument; | ||
| use Prophecy\Argument; | ||
|
|
||
| class ImportMissingClassesHandlerTest extends IntegrationTestCase | ||
| { | ||
| const EXAMPLE_PATH = '/example/path'; | ||
| const EXAMPLE_SOURCE = 'example-source'; | ||
|
|
||
|
|
||
| private $requestHandler; | ||
| /** | ||
| * @var ObjectProphecy | ||
| */ | ||
| private $finder; | ||
|
|
||
| /** | ||
| * @var HandlerTester | ||
| */ | ||
| private $tester; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->requestHandler = $this->container()->get(RpcExtension::SERVICE_REQUEST_HANDLER); | ||
| $this->finder = $this->prophesize(UnresolvableClassNameFinder::class); | ||
|
|
||
| $this->tester = new HandlerTester(new ImportMissingClassesHandler( | ||
| $this->requestHandler, | ||
| $this->finder->reveal() | ||
| )); | ||
| } | ||
|
|
||
| public function testZeroUnresolvedClasses() | ||
| { | ||
| $this->finder->find(Argument::type(TextDocument::class))->willReturn([]); | ||
|
|
||
| $response = $this->tester->handle(ImportMissingClassesHandler::NAME, [ | ||
| ImportMissingClassesHandler::PARAM_PATH => self::EXAMPLE_PATH, | ||
| ImportMissingClassesHandler::PARAM_SOURCE => self::EXAMPLE_SOURCE, | ||
| ]); | ||
|
|
||
| $this->assertInstanceOf(EchoResponse::class, $response); | ||
| } | ||
|
|
||
| public function testImportsUnresolvedClasses() | ||
| { | ||
| $this->finder->find(Argument::type(TextDocument::class))->willReturn([ | ||
| new NameWithByteOffset( | ||
| QualifiedName::fromString('Foobar'), | ||
| ByteOffset::fromInt(12) | ||
| ) | ||
| ]); | ||
|
|
||
| $response = $this->tester->handle(ImportMissingClassesHandler::NAME, [ | ||
| ImportMissingClassesHandler::PARAM_PATH => self::EXAMPLE_PATH, | ||
| ImportMissingClassesHandler::PARAM_SOURCE => self::EXAMPLE_SOURCE, | ||
| ]); | ||
|
|
||
| $this->assertInstanceOf(CollectionResponse::class, $response); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
So you call
phpactor#_rpc_dispatchfor each action received, before the user have filter them ?Ok I finally understand, you want the classic fzf window to open ant let the user choose which FQCN to import, like if the user did it manually.
I don't see a way to make that work, since fzf is aynchronous.
Maybe with a chained list of action, so that:
sinkis calledDoes it make sense ?
Uh oh!
There was an error while loading. Please reload this page.
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.
yes -- but this is where I'm confused - it think it does that already (it calls the callback handler when the items are selected?). But I confess to not fully understanding everything that's going on 👀
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.
For me what appen is:
So we must not use fzf in a loop, we need to treat it like the chain I mentioned.
So that the user choose the class for the first one, and only once he made a choice we call the next action
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.
I got a question, let's say that we are in a file with 3 classes not yet imported: A, B and C.
The classes A and C have multiple possible FQCN and need the user to choose, B on the other hand as only one possibility.
In the collection that the new handler returns, does all three actions are the same ?
Or do we have two actions to choose the right FQCN and one to import the only possibility for B ?