Skip to content
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

Add ArrayValue to search for autocompleting #16

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

N1ebieski
Copy link

@N1ebieski N1ebieski commented Mar 17, 2025

This PR adds ArrayValue context to AbstractContext::searchForAutocompleting method.

With this feature, the command:

php-parser autocomplete "<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ExampleRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => [
                '" --debug

returns:

================================================================================
================================================================================
                              STARTING TO PARSE
================================================================================
================================================================================

Microsoft\PhpParser\Node\SourceFileNode <?php namespace App\Http\Requests; use Illuminate\
+ Context: App\Contexts\Base
* Parsing: App\Parsers\SourceFileNodeParser

 Microsoft\PhpParser\Node\Statement\InlineHtml <?php 
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\InlineHtmlParser

 Microsoft\PhpParser\Node\Statement\NamespaceDefinition namespace App\Http\Requests;
  Microsoft\PhpParser\Node\QualifiedName App\Http\Requests
 Microsoft\PhpParser\Node\Statement\NamespaceUseDeclaration use Illuminate\Foundation\Http\FormRequest;
  Microsoft\PhpParser\Node\DelimitedList\NamespaceUseClauseList Illuminate\Foundation\Http\FormRequest
   Microsoft\PhpParser\Node\NamespaceUseClause Illuminate\Foundation\Http\FormRequest
    Microsoft\PhpParser\Node\QualifiedName Illuminate\Foundation\Http\FormRequest
 Microsoft\PhpParser\Node\Statement\ClassDeclaration class ExampleRequest extends FormRequest { public 
 + Context: App\Contexts\ClassDefinition
 * Parsing: App\Parsers\ClassDeclarationParser

  Microsoft\PhpParser\Node\ClassBaseClause extends FormRequest
  + Context: App\Contexts\ClassDefinition
  * Parsing: App\Parsers\ClassBaseClauseParser

   Microsoft\PhpParser\Node\QualifiedName FormRequest
  Microsoft\PhpParser\Node\ClassMembersNode { public function rules(): array { return [ 'name'
   Microsoft\PhpParser\Node\MethodDeclaration public function rules(): array { return [ 'name' =
   + Context: App\Contexts\MethodDefinition
   * Parsing: App\Parsers\MethodDeclarationParser

    Microsoft\PhpParser\Node\DelimitedList\QualifiedNameList array
    Microsoft\PhpParser\Node\Statement\CompoundStatementNode { return [ 'name' => [ '
    + Context: App\Contexts\MethodDefinition
    * Parsing: App\Parsers\CompoundStatementNodeParser

     Microsoft\PhpParser\Node\Statement\ReturnStatement return [ 'name' => [
     + Context: App\Contexts\MethodCall
     * Parsing: App\Parsers\ReturnStatementParser

      Microsoft\PhpParser\Node\Expression\ArrayCreationExpression [ 'name' => [
      + Context: App\Contexts\ArrayValue
      * Parsing: App\Parsers\ArrayCreationExpressionParser

       Microsoft\PhpParser\Node\DelimitedList\ArrayElementList 'name' => [
        Microsoft\PhpParser\Node\ArrayElement 'name' => [
        + Context: App\Contexts\ArrayItem
        * Parsing: App\Parsers\ArrayElementParser

         Microsoft\PhpParser\Node\StringLiteral 'name'
         + Context: App\Contexts\StringValue
         * Parsing: App\Parsers\StringLiteralParser

         Microsoft\PhpParser\Node\Expression\ArrayCreationExpression [
         + Context: App\Contexts\ArrayValue
         * Parsing: App\Parsers\ArrayCreationExpressionParser
{
    "type": "array",
    "autocompleting": true,
    "autocompletingKey": true,
    "autocompletingValue": true,
    "parent": {
        "type": "array_item",
        "key": {
            "type": "string",
            "value": "name"
        },
        "value": {
            "type": "array",
            "autocompleting": true,
            "children": [],
            "autocompletingKey": true,
            "autocompletingValue": true
        },
        "autocompletingValue": true,
        "parent": {
            "type": "array",
            "autocompleting": true,
            "autocompletingKey": false,
            "autocompletingValue": true,
            "parent": {
                "type": "methodCall",
                "methodName": null,
                "className": null,
                "arguments": {
                    "type": "arguments",
                    "autocompletingIndex": 0,
                    "children": []
                },
                "parent": {
                    "type": "methodDefinition",
                    "methodName": "rules",
                    "parameters": [],
                    "parent": {
                        "type": "classDefinition",
                        "className": "App\\Http\\Requests\\ExampleRequest",
                        "extends": "Illuminate\\Foundation\\Http\\FormRequest",
                        "implements": [],
                        "properties": [],
                        "parent": {
                            "type": "base",
                            "parent": null
                        }
                    }
                }
            }
        }
    }
}

instead:

================================================================================
================================================================================
                              STARTING TO PARSE
================================================================================
================================================================================

Microsoft\PhpParser\Node\SourceFileNode <?php namespace App\Http\Requests; use Illuminate\
+ Context: App\Contexts\Base
* Parsing: App\Parsers\SourceFileNodeParser

 Microsoft\PhpParser\Node\Statement\InlineHtml <?php 
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\InlineHtmlParser

 Microsoft\PhpParser\Node\Statement\NamespaceDefinition namespace App\Http\Requests;
  Microsoft\PhpParser\Node\QualifiedName App\Http\Requests
 Microsoft\PhpParser\Node\Statement\NamespaceUseDeclaration use Illuminate\Foundation\Http\FormRequest;
  Microsoft\PhpParser\Node\DelimitedList\NamespaceUseClauseList Illuminate\Foundation\Http\FormRequest
   Microsoft\PhpParser\Node\NamespaceUseClause Illuminate\Foundation\Http\FormRequest
    Microsoft\PhpParser\Node\QualifiedName Illuminate\Foundation\Http\FormRequest
 Microsoft\PhpParser\Node\Statement\ClassDeclaration class ExampleRequest extends FormRequest { public 
 + Context: App\Contexts\ClassDefinition
 * Parsing: App\Parsers\ClassDeclarationParser

  Microsoft\PhpParser\Node\ClassBaseClause extends FormRequest
  + Context: App\Contexts\ClassDefinition
  * Parsing: App\Parsers\ClassBaseClauseParser

   Microsoft\PhpParser\Node\QualifiedName FormRequest
  Microsoft\PhpParser\Node\ClassMembersNode { public function rules(): array { return [ 'name'
   Microsoft\PhpParser\Node\MethodDeclaration public function rules(): array { return [ 'name' =
   + Context: App\Contexts\MethodDefinition
   * Parsing: App\Parsers\MethodDeclarationParser

    Microsoft\PhpParser\Node\DelimitedList\QualifiedNameList array
    Microsoft\PhpParser\Node\Statement\CompoundStatementNode { return [ 'name' => [ '
    + Context: App\Contexts\MethodDefinition
    * Parsing: App\Parsers\CompoundStatementNodeParser

     Microsoft\PhpParser\Node\Statement\ReturnStatement return [ 'name' => [
     + Context: App\Contexts\MethodCall
     * Parsing: App\Parsers\ReturnStatementParser

      Microsoft\PhpParser\Node\Expression\ArrayCreationExpression [ 'name' => [
      + Context: App\Contexts\ArrayValue
      * Parsing: App\Parsers\ArrayCreationExpressionParser

       Microsoft\PhpParser\Node\DelimitedList\ArrayElementList 'name' => [
        Microsoft\PhpParser\Node\ArrayElement 'name' => [
        + Context: App\Contexts\ArrayItem
        * Parsing: App\Parsers\ArrayElementParser

         Microsoft\PhpParser\Node\StringLiteral 'name'
         + Context: App\Contexts\StringValue
         * Parsing: App\Parsers\StringLiteralParser

         Microsoft\PhpParser\Node\Expression\ArrayCreationExpression [
         + Context: App\Contexts\ArrayValue
         * Parsing: App\Parsers\ArrayCreationExpressionParser

[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant