Giter Club home page Giter Club logo

phpactor-completion's Introduction

Worse Completion

Build Status

PHP Code Completion library which uses Phpactor's Worse Reflection.

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.

phpactor-completion's People

Contributors

dantleech avatar kermorgant avatar weirdan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.