Skip to content

Conversation

@N1ebieski
Copy link
Contributor

@N1ebieski N1ebieski commented Apr 22, 2025

Fixes laravel/vs-code-extension#339

If a last character is a double quote, for example:

{{ config("

then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 instead 0. Example:

php-parser autocomplete "{{ config(\"" --debug

returns:

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

Microsoft\PhpParser\Node\SourceFileNode {{ config("
+ Context: App\Contexts\Base
* Parsing: App\Parsers\SourceFileNodeParser

 Microsoft\PhpParser\Node\Statement\InlineHtml {{ config("
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\InlineHtmlParser


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

Microsoft\PhpParser\Node\SourceFileNode <?php config(";
+ 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\ExpressionStatement config(";
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\ExpressionStatementParser

  Microsoft\PhpParser\Node\Expression\CallExpression config(";
  + Context: App\Contexts\MethodCall
  * Parsing: App\Parsers\CallExpressionParser

   Microsoft\PhpParser\Node\QualifiedName config
   Microsoft\PhpParser\Node\DelimitedList\ArgumentExpressionList ";
   + Context: App\Contexts\MethodCall
   * Parsing: App\Parsers\ArgumentExpressionListParser

    Microsoft\PhpParser\Node\Expression\ArgumentExpression ";
    + Context: App\Contexts\Argument
    * Parsing: App\Parsers\ArgumentExpressionParser

     Microsoft\PhpParser\Node\StringLiteral ";
     + Context: App\Contexts\StringValue
     * Parsing: App\Parsers\StringLiteralParser

{
    "type": "methodCall",
    "autocompleting": true,
    "methodName": "config",
    "className": null,
    "arguments": {
        "type": "arguments",
        "autocompletingIndex": 1,
        "children": [
            {
                "type": "argument",
                "name": null,
                "children": [
                    {
                        "type": "string",
                        "value": ";"
                    }
                ]
            }
        ]
    },
    "parent": {
        "type": "base",
        "parent": null
    }
}

Comparison with a "single quote" example:

php-parser autocomplete "{{ config('" --debug

returns:

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

Microsoft\PhpParser\Node\SourceFileNode {{ config('
+ Context: App\Contexts\Base
* Parsing: App\Parsers\SourceFileNodeParser

 Microsoft\PhpParser\Node\Statement\InlineHtml {{ config('
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\InlineHtmlParser


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

Microsoft\PhpParser\Node\SourceFileNode <?php config(';
+ 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\ExpressionStatement config(
 + Context: App\Contexts\Base
 * Parsing: App\Parsers\ExpressionStatementParser

  Microsoft\PhpParser\Node\Expression\CallExpression config(
  + Context: App\Contexts\MethodCall
  * Parsing: App\Parsers\CallExpressionParser

   Microsoft\PhpParser\Node\QualifiedName config
{
    "type": "methodCall",
    "autocompleting": true,
    "methodName": "config",
    "className": null,
    "arguments": {
        "type": "arguments",
        "autocompletingIndex": 0,
        "children": []
    },
    "parent": {
        "type": "base",
        "parent": null
    }
}

Probably the parser turns the string into something like this:

"{{ config(";"

and returns ";" as an argument.

This PR adds an extra check that the last character is a double quote. If so, replace it with a single quote.

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.

[Bug]: Double quotes does not trigger the autocomplete

1 participant