phpactor / completion
Completion library for Worse Reflection
Requires
- php: ^7.3 || ^8.0
- phpactor/class-to-file: ~0.4.2
- phpactor/container: ^2.0.0
- phpactor/reference-finder: ^0.1.5
- phpactor/source-code-filesystem: ~0.1.6
- phpactor/text-document: ~1.2.3
- phpactor/worse-reflection: ^0.4.8
Requires (Dev)
- dms/phpunit-arraysubset-asserts: dev-master
- ergebnis/composer-normalize: ^2.0
- friendsofphp/php-cs-fixer: ^2.17
- phpactor/test-utils: ~1.1.3
- phpbench/phpbench: ^1.0.0@alpha
- phpspec/prophecy-phpunit: dev-master
- phpstan/phpstan: ~0.12.0
- phpunit/phpunit: ~9.0
- symfony/var-dumper: ^5.2
- dev-master / 0.4.x-dev
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.1
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.0
- dev-enum
- dev-tolerant-parser-0.1
- dev-phpbench-alpha
- dev-update
- dev-merge-extension
- dev-signature_support
- dev-declared_class
- dev-byte_offset_not_char_offset
- dev-keyword_completion
- dev-parameter_completion
This package is auto-updated.
Last update: 2022-03-19 11:50:39 UTC
README
PHP Code Completion library for Phpactor.
This package includes:
- Completion APIs and implementations.
- Phpactor RPC extension and handlers.
- Language Server extension and handlers.
Usage
Each completor implements the Completor
interface which accepts the source
code as a string and a byte offset from which to complete from. The completor
must yield
instances of the Suggestion
class:
$completor = new MyCompletor(); $suggestions = $completor->complete($sourceCode, $byteOffset); /** @var Suggestion $suggestion */ foreach ($suggestions as $suggestion) { echo $suggestion->name(); echo $suggestion->shortDescription(); }
Multiple completors can be chained together with the ChainCompletor:
$completor = new ChainCompletor([ new MyCompletor1(), new MyCompletor2(), ]); $suggestions = $completor->complete($sourceCode, $byteOffset);
Tolerant Completors
The library currently includes a suite of completors using the Tolerant PHP
Parser and WorseReflection.
All of the tolerant completors are instances of TolerantCompletorInterface
and accept a parser node rather than a byte offset. They can be collected in a
TolerantChainCompletor
class which in turn implements the primary
Completor
interface:
use Phpactor\WorseReflection\ReflectorBuilder; use Phpactor\Completion\Bridge\TolerantParser\ChainTolerantCompletor; use Phpactor\Completion\Bridge\TolerantParser\WorseReflection\WorseLocalVariableCompletor; use Phpactor\Completion\Bridge\TolerantParser\WorseReflection\WorseClassMemberCompletor; $reflector = ReflectorBuilder::create()->addSource($sourceCode)->build(); $formatter = new ObjectFormatter([ // ... instances of ObjectFormatter ]); $completor = new ChainTolerantCompletor([ new WorseLocalVariableCompletor($reflector, $formatter), new WorseClassMemberCompletor($reflector, $formatter), ]); $completor->complete($sourceCode, $byteOffset);
Formatters
This library can format arbitrary objects as strings, for example to go from a
ReflectionMethod
to the synopsis: pub function($foobar)
.
$formatter = new ObjectFormatter([ new TypeFormatter(), new TypesFormatter(), new FunctionFormatter(), new MethodFormatter(), new ParameterFormatter(), new ParametersFormatter(), new PropertyFormatter(), new VariableFormatter(), ]); $reflectionMethod = $reflector->reflectClass(Foobar::class)->methods()->get('barfoo'); $formatter->format($reflectionMethod);
Each formatter is able to indicate if it can format a given object, and if so it is chosen.
Contributing
This package is open source and welcomes contributions! Feel free to open a pull request on this repository.
Support
- Create an issue on the main Phpactor repository.
- Join the
#phpactor
channel on the Slack Symfony Devs channel.