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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Features:
- [completion] Use declared classes as completion source
- [import-class] Import declared classes (as long as they can be statically
resolved).
- [rpc] Class import uses offset to determine type to import

Bug fixes:

Expand All @@ -25,6 +26,8 @@ Bug fixes:

BC Break:

- [rpc] Import class no longer requires name parameter. RPC version changed
to version 2.
- [code-transform] Generate accessors is now a class action and allows
generation of multiple accessors.

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"symfony/filesystem": "^3.3",
"symfony/yaml": "^3.3",
"webmozart/glob": "^4.1",
"phpactor/text-document": "~1.1",
"phpactor/config-loader": "~0.1",
"phpactor/code-transform-extension": "^0.1",
"phpactor/worse-reference-finder-extension": "~0.1",
Expand Down
19 changes: 10 additions & 9 deletions composer.lock

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

12 changes: 7 additions & 5 deletions lib/Extension/CodeTransformExtra/Rpc/ImportClassHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Phpactor\Extension\Rpc\Response\Input\TextInput;
use Phpactor\CodeTransform\Domain\Refactor\ImportClass\ClassAlreadyImportedException;
use Phpactor\CodeTransform\Domain\Exception\TransformException;
use Phpactor\TextDocument\Util\WordAtOffset;

class ImportClassHandler extends AbstractHandler
{
Expand Down Expand Up @@ -64,7 +65,6 @@ public function configure(Resolver $resolver)
self::PARAM_ALIAS => null,
]);
$resolver->setRequired([
self::PARAM_NAME,
self::PARAM_OFFSET,
self::PARAM_SOURCE,
self::PARAM_PATH,
Expand All @@ -74,12 +74,13 @@ public function configure(Resolver $resolver)
public function handle(array $arguments)
{
if (null === $arguments[self::PARAM_QUALIFIED_NAME]) {
$suggestions = $this->suggestions($arguments[self::PARAM_NAME]);
$name = (new WordAtOffset(WordAtOffset::SPLIT_QUALIFIED_PHP_NAME))($arguments[self::PARAM_SOURCE], $arguments[self::PARAM_OFFSET]);
$suggestions = $this->suggestions($name);

if (count($suggestions) === 0) {
return EchoResponse::fromMessage(sprintf(
'No classes found with short name "%s"',
$arguments[self::PARAM_NAME]
'No classes found with name "%s"',
$name
));
}

Expand Down Expand Up @@ -141,7 +142,7 @@ public function handle(array $arguments)
),
EchoResponse::fromMessage(sprintf(
'Imported class "%s"',
$arguments[self::PARAM_NAME]
$arguments[self::PARAM_QUALIFIED_NAME]
))
]);
}
Expand All @@ -152,6 +153,7 @@ private function suggestions(string $name)
$this->filesystem,
$name
);

return array_map(function (array $suggestion) {
return $suggestion['class'];
}, $suggestions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function __construct(FilesystemRegistry $filesystemRegistry, FileToClass

public function classSearch(string $filesystemName, string $name)
{
$name = $this->convertFqnToRelativePath($name);
$filesystem = $this->filesystemRegistry->get($filesystemName);

/** @var FileList<SplFileInfo> $files */
Expand Down Expand Up @@ -122,4 +123,9 @@ private function resolveShortName($declaredClass): string

return substr($declaredClass, $offset + 1);
}

private function convertFqnToRelativePath(string $name)
{
return str_replace('\\', '/', $name);
}
}
4 changes: 1 addition & 3 deletions plugin/phpactor.vim
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ function! phpactor#_completeImportClass(completedItem)
if !empty(get(suggestion, "class_import", ""))
call phpactor#rpc("import_class", {
\ "qualified_name": suggestion['class_import'],
\ "name": suggestion['name'],
\ "offset": phpactor#_offset(),
\ "source": phpactor#_source(),
\ "path": expand('%:p')})
Expand Down Expand Up @@ -186,8 +185,7 @@ endfunction
" Insert a use statement
""""""""""""""""""""""""
function! phpactor#UseAdd()
let word = expand("<cword>")
call phpactor#rpc("import_class", {"name": word, "offset": phpactor#_offset(), "source": phpactor#_source(), "path": expand('%:p')})
call phpactor#rpc("import_class", {"offset": phpactor#_offset(), "source": phpactor#_source(), "path": expand('%:p')})
endfunction

"""""""""""""""""""""""""""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public function testSearchNameJson()
$this->assertContains('Badger.php"', $process->getOutput());
}

public function testSearchByQualifiedName()
{
$process = $this->phpactor('class:search "Badger\\Carnivorous" --format=json');
$this->assertSuccess($process);
$this->assertContains('Carnivorous.php"', $process->getOutput());
}

/**
* @testdox It should return information baesd on a class "short" name.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
class ImportClassHandlerTest extends HandlerTestCase
{
const TEST_NAME = 'Foo';
const TEST_OFFSET = 1234;
const TEST_OFFSET = 7;
const TEST_PATH = '/path/to';
const TEST_SOURCE = '<?php foo';
const TEST_SOURCE = '<?php Foo';
const TEST_ALIAS = 'Alias';

/**
Expand Down Expand Up @@ -62,7 +62,6 @@ public function testReturnsSuggestionsIfMultipleTargetsFound()

/** @var InputCallbackResponse $response */
$response = $this->handle('import_class', [
ImportClassHandler::PARAM_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_OFFSET => self::TEST_OFFSET,
ImportClassHandler::PARAM_PATH => self::TEST_PATH,
ImportClassHandler::PARAM_SOURCE => self::TEST_SOURCE
Expand All @@ -81,7 +80,6 @@ public function testShowsMessageIfNoClassesFound()

/** @var EchoResponse $response */
$response = $this->handle('import_class', [
ImportClassHandler::PARAM_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_OFFSET => self::TEST_OFFSET,
ImportClassHandler::PARAM_PATH => self::TEST_PATH,
ImportClassHandler::PARAM_SOURCE => self::TEST_SOURCE
Expand All @@ -106,7 +104,6 @@ public function testImportsClassIfOnlyOneSuggestion()

/** @var EchoResponse $response */
$response = $this->handle('import_class', [
ImportClassHandler::PARAM_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_OFFSET => self::TEST_OFFSET,
ImportClassHandler::PARAM_PATH => self::TEST_PATH,
ImportClassHandler::PARAM_SOURCE => self::TEST_SOURCE
Expand All @@ -127,7 +124,6 @@ public function testAsksForAliasIfClassAlreadyUsed()
/** @var EchoResponse $response */
$response = $this->handle('import_class', [
ImportClassHandler::PARAM_QUALIFIED_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_OFFSET => self::TEST_OFFSET,
ImportClassHandler::PARAM_PATH => self::TEST_PATH,
ImportClassHandler::PARAM_SOURCE => self::TEST_SOURCE
Expand Down Expand Up @@ -155,7 +151,6 @@ public function testUsesGivenAlias()
$response = $this->handle('import_class', [
ImportClassHandler::PARAM_ALIAS => self::TEST_ALIAS,
ImportClassHandler::PARAM_QUALIFIED_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_OFFSET => self::TEST_OFFSET,
ImportClassHandler::PARAM_PATH => self::TEST_PATH,
ImportClassHandler::PARAM_SOURCE => self::TEST_SOURCE
Expand All @@ -176,7 +171,6 @@ public function testShowsMessageIfSelectedClassIsAlreadyImported()
/** @var EchoResponse $response */
$response = $this->handle('import_class', [
ImportClassHandler::PARAM_QUALIFIED_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_NAME => self::TEST_NAME,
ImportClassHandler::PARAM_OFFSET => self::TEST_OFFSET,
ImportClassHandler::PARAM_PATH => self::TEST_PATH,
ImportClassHandler::PARAM_SOURCE => self::TEST_SOURCE
Expand Down