From 49bab8c2a5647af7b7d517c1e0b99ebdd608dfed Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Tue, 22 Apr 2025 22:03:17 +0000 Subject: [PATCH 1/8] Fix Double quotes does not trigger the autocomplete Fixes N1ebieski/vs-code-php-parser-cli#10 --- app/Parser/Walker.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index d2c2f5f..b3edbd4 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -26,7 +26,9 @@ class Walker public function __construct(protected string $document, $debug = false) { $this->debug = $debug; - $this->sourceFile = (new Parser)->parseSourceFile(trim($this->document)); + $this->sourceFile = (new Parser)->parseSourceFile( + $this->replaceLastDoubleQuoteToSingleQuote(trim($this->document)) + ); $this->context = new Context; } @@ -49,6 +51,25 @@ protected function documentSkipsClosingQuote() return false; } + /** + * If a last character is a double quote, for example: + * + * {{ config(" + * + * then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 + * instead 0. Probably the parser turns the string into something like this: + * + * "{{ config(";" + * + * and returns ";" as an argument. + * + * This function replaces the last double quote with a single quote. + */ + private function replaceLastDoubleQuoteToSingleQuote(string $text) + { + return substr($text, -1) === '"' ? substr($text, 0, -1) . "'" : $text; + } + public function walk() { if (!$this->documentSkipsClosingQuote()) { From e997070c8e785462e0d1def1d52e702362f16d2c Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Wed, 23 Apr 2025 08:34:23 +0000 Subject: [PATCH 2/8] Fix Double quotes does not trigger the autocomplete Fixes N1ebieski/vs-code-php-parser-cli#10 --- app/Parser/Walker.php | 23 +-------------------- app/Parsers/InlineHtmlParser.php | 34 ++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index b3edbd4..d2c2f5f 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -26,9 +26,7 @@ class Walker public function __construct(protected string $document, $debug = false) { $this->debug = $debug; - $this->sourceFile = (new Parser)->parseSourceFile( - $this->replaceLastDoubleQuoteToSingleQuote(trim($this->document)) - ); + $this->sourceFile = (new Parser)->parseSourceFile(trim($this->document)); $this->context = new Context; } @@ -51,25 +49,6 @@ protected function documentSkipsClosingQuote() return false; } - /** - * If a last character is a double quote, for example: - * - * {{ config(" - * - * then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 - * instead 0. Probably the parser turns the string into something like this: - * - * "{{ config(";" - * - * and returns ";" as an argument. - * - * This function replaces the last double quote with a single quote. - */ - private function replaceLastDoubleQuoteToSingleQuote(string $text) - { - return substr($text, -1) === '"' ? substr($text, 0, -1) . "'" : $text; - } - public function walk() { if (!$this->documentSkipsClosingQuote()) { diff --git a/app/Parsers/InlineHtmlParser.php b/app/Parsers/InlineHtmlParser.php index 8ce758d..87bb6a2 100644 --- a/app/Parsers/InlineHtmlParser.php +++ b/app/Parsers/InlineHtmlParser.php @@ -46,7 +46,9 @@ public function parse(InlineHtml $node) $this->startLine = $range->start->line; } - $this->parseBladeContent(Document::fromText($node->getText())); + $this->parseBladeContent(Document::fromText( + $this->replaceLastDoubleQuoteToSingleQuote($node->getText()), + )); if (count($this->items)) { $blade = new Blade; @@ -60,6 +62,34 @@ public function parse(InlineHtml $node) return $this->context; } + /** + * If a last character is a double quote, for example: + * + * {{ config(" + * + * then Stillat\BladeParser\Document\Document::fromText returns autocompletingIndex: 1 + * instead 0. Probably the parser turns the string into something like this: + * + * "{{ config(";" + * + * and returns ";" as an argument. + * + * This function replaces the last double quote with a single quote. + */ + private function replaceLastDoubleQuoteToSingleQuote(string $text): string + { + if (substr($text, -1) === '"') { + $countDoubleQuotes = substr_count($text, '"'); + + // We have to exclude cases with an even number of double quotes + if ($countDoubleQuotes % 2 !== 0) { + return substr($text, 0, -1) . "'"; + } + } + + return $text; + } + protected function parseBladeContent($node) { foreach ($node->getNodes() as $child) { @@ -95,7 +125,7 @@ protected function doEchoParse(BaseNode $node, $prefix, $content) } $range->start->line += $this->startLine + $node->position->startLine - 2; - $range->end->line += $this->startLine + $node->position->startLine - 2; + $range->end->line += $this->startLine + $node->position->startLine - 2; return $range; }; From 1b4c1c97d5642d73f8ec67d38b4f820c4c28057e Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Wed, 23 Apr 2025 09:07:56 +0000 Subject: [PATCH 3/8] Fix Double quotes does not trigger the autocomplete Fixes N1ebieski/vs-code-php-parser-cli#10 --- app/Parser/Walker.php | 32 +++++++++++++++++++++++++++++++- app/Parsers/InlineHtmlParser.php | 32 +------------------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index d2c2f5f..537c3a2 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -26,10 +26,40 @@ class Walker public function __construct(protected string $document, $debug = false) { $this->debug = $debug; - $this->sourceFile = (new Parser)->parseSourceFile(trim($this->document)); + $this->sourceFile = (new Parser)->parseSourceFile( + $this->replaceLastDoubleQuoteToSingleQuote(trim($this->document)), + ); $this->context = new Context; } + /** + * If a last character is a double quote, for example: + * + * {{ config(" + * + * then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 + * instead 0. Probably the parser turns the string into something like this: + * + * "{{ config(";" + * + * and returns ";" as an argument. + * + * This function replaces the last double quote with a single quote. + */ + private function replaceLastDoubleQuoteToSingleQuote(string $text): string + { + if (substr($text, -1) === '"') { + $countDoubleQuotes = substr_count($text, '"'); + + // We have to exclude cases with an even number of double quotes + if ($countDoubleQuotes % 2 !== 0) { + return substr($text, 0, -1) . "'"; + } + } + + return $text; + } + protected function documentSkipsClosingQuote() { if (count($this->sourceFile->statementList) === 1 && $this->sourceFile->statementList[0] instanceof InlineHtml) { diff --git a/app/Parsers/InlineHtmlParser.php b/app/Parsers/InlineHtmlParser.php index 87bb6a2..86d424c 100644 --- a/app/Parsers/InlineHtmlParser.php +++ b/app/Parsers/InlineHtmlParser.php @@ -46,9 +46,7 @@ public function parse(InlineHtml $node) $this->startLine = $range->start->line; } - $this->parseBladeContent(Document::fromText( - $this->replaceLastDoubleQuoteToSingleQuote($node->getText()), - )); + $this->parseBladeContent(Document::fromText($node->getText())); if (count($this->items)) { $blade = new Blade; @@ -62,34 +60,6 @@ public function parse(InlineHtml $node) return $this->context; } - /** - * If a last character is a double quote, for example: - * - * {{ config(" - * - * then Stillat\BladeParser\Document\Document::fromText returns autocompletingIndex: 1 - * instead 0. Probably the parser turns the string into something like this: - * - * "{{ config(";" - * - * and returns ";" as an argument. - * - * This function replaces the last double quote with a single quote. - */ - private function replaceLastDoubleQuoteToSingleQuote(string $text): string - { - if (substr($text, -1) === '"') { - $countDoubleQuotes = substr_count($text, '"'); - - // We have to exclude cases with an even number of double quotes - if ($countDoubleQuotes % 2 !== 0) { - return substr($text, 0, -1) . "'"; - } - } - - return $text; - } - protected function parseBladeContent($node) { foreach ($node->getNodes() as $child) { From 2262aa1f82699f0b73a1fb2b2e8c5766978a7036 Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Thu, 24 Apr 2025 08:26:12 +0000 Subject: [PATCH 4/8] fix for component attributes --- app/Parser/Walker.php | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index 537c3a2..1a2f3e7 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -26,9 +26,8 @@ class Walker public function __construct(protected string $document, $debug = false) { $this->debug = $debug; - $this->sourceFile = (new Parser)->parseSourceFile( - $this->replaceLastDoubleQuoteToSingleQuote(trim($this->document)), - ); + $this->document = $document; + $this->sourceFile = (new Parser)->parseSourceFile(trim($this->document)); $this->context = new Context; } @@ -44,20 +43,13 @@ public function __construct(protected string $document, $debug = false) * * and returns ";" as an argument. * - * This function replaces the last double quote with a single quote. + * This function parse source file again if last character is a double quote. */ - private function replaceLastDoubleQuoteToSingleQuote(string $text): string + private function setSourceFileAgainIfLastCharacterIsDoubleQuote(string $text): void { if (substr($text, -1) === '"') { - $countDoubleQuotes = substr_count($text, '"'); - - // We have to exclude cases with an even number of double quotes - if ($countDoubleQuotes % 2 !== 0) { - return substr($text, 0, -1) . "'"; - } + $this->sourceFile = (new Parser)->parseSourceFile(trim(substr($text, 0, -1) . "'")); } - - return $text; } protected function documentSkipsClosingQuote() @@ -85,6 +77,8 @@ public function walk() return new Base; } + $this->setSourceFileAgainIfLastCharacterIsDoubleQuote($this->document); + Parse::$debug = $this->debug; $parsed = Parse::parse($this->sourceFile); From 20fb76877d8ed579ac4ff51e374e6d18c5c478f7 Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Thu, 24 Apr 2025 08:28:48 +0000 Subject: [PATCH 5/8] refactoring --- app/Parser/Walker.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index 1a2f3e7..27edaa4 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -26,8 +26,8 @@ class Walker public function __construct(protected string $document, $debug = false) { $this->debug = $debug; - $this->document = $document; - $this->sourceFile = (new Parser)->parseSourceFile(trim($this->document)); + $this->document = trim($document); + $this->sourceFile = (new Parser)->parseSourceFile($this->document); $this->context = new Context; } @@ -45,10 +45,10 @@ public function __construct(protected string $document, $debug = false) * * This function parse source file again if last character is a double quote. */ - private function setSourceFileAgainIfLastCharacterIsDoubleQuote(string $text): void + private function parseSourceFileAgainIfLastCharacterIsDoubleQuote(): void { - if (substr($text, -1) === '"') { - $this->sourceFile = (new Parser)->parseSourceFile(trim(substr($text, 0, -1) . "'")); + if (substr($this->document, -1) === '"') { + $this->sourceFile = (new Parser)->parseSourceFile(substr($this->document, 0, -1) . "'"); } } @@ -77,7 +77,7 @@ public function walk() return new Base; } - $this->setSourceFileAgainIfLastCharacterIsDoubleQuote($this->document); + $this->parseSourceFileAgainIfLastCharacterIsDoubleQuote(); Parse::$debug = $this->debug; From 0a2fed365cb1c2fd95289c3921992759a58d9333 Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Thu, 24 Apr 2025 20:58:58 +0000 Subject: [PATCH 6/8] refactoring --- app/Parser/Walker.php | 49 ++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index 27edaa4..6bacb55 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -31,27 +31,6 @@ public function __construct(protected string $document, $debug = false) $this->context = new Context; } - /** - * If a last character is a double quote, for example: - * - * {{ config(" - * - * then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 - * instead 0. Probably the parser turns the string into something like this: - * - * "{{ config(";" - * - * and returns ";" as an argument. - * - * This function parse source file again if last character is a double quote. - */ - private function parseSourceFileAgainIfLastCharacterIsDoubleQuote(): void - { - if (substr($this->document, -1) === '"') { - $this->sourceFile = (new Parser)->parseSourceFile(substr($this->document, 0, -1) . "'"); - } - } - protected function documentSkipsClosingQuote() { if (count($this->sourceFile->statementList) === 1 && $this->sourceFile->statementList[0] instanceof InlineHtml) { @@ -71,13 +50,39 @@ protected function documentSkipsClosingQuote() return false; } + private function documentHasDoubleQuoteAsLastCharacter(): bool + { + return substr($this->document, -1) === '"'; + } + + /** + * If a last character is a double quote, for example: + * + * {{ config(" + * + * then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 + * instead 0. Probably the parser turns the string into something like this: + * + * "{{ config(";" + * + * and returns ";" as an argument. + * + * This function replaces the last double quote with a single quote. + */ + private function replaceLastDoubleQuoteWithSingleQuote(): string + { + return substr($this->document, 0, -1) . "'"; + } + public function walk() { if (!$this->documentSkipsClosingQuote()) { return new Base; } - $this->parseSourceFileAgainIfLastCharacterIsDoubleQuote(); + if ($this->documentHasDoubleQuoteAsLastCharacter()) { + return (new self($this->replaceLastDoubleQuoteWithSingleQuote(), $this->debug))->walk(); + } Parse::$debug = $this->debug; From a8df2964eeb4f67bbafc8b77d0c1b90b11878f95 Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Thu, 24 Apr 2025 21:02:22 +0000 Subject: [PATCH 7/8] refactoring --- app/Parser/Walker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index 6bacb55..03e6b99 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -67,7 +67,7 @@ private function documentHasDoubleQuoteAsLastCharacter(): bool * * and returns ";" as an argument. * - * This function replaces the last double quote with a single quote. + * This function replaces the last double quote with a single quote in document. */ private function replaceLastDoubleQuoteWithSingleQuote(): string { From 1f54ba4ed3f50148a8ce91aab9de47853c3f2f46 Mon Sep 17 00:00:00 2001 From: N1ebieski Date: Thu, 24 Apr 2025 21:05:20 +0000 Subject: [PATCH 8/8] refactoring --- app/Parser/Walker.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/Parser/Walker.php b/app/Parser/Walker.php index 03e6b99..4c1ab9c 100644 --- a/app/Parser/Walker.php +++ b/app/Parser/Walker.php @@ -55,20 +55,6 @@ private function documentHasDoubleQuoteAsLastCharacter(): bool return substr($this->document, -1) === '"'; } - /** - * If a last character is a double quote, for example: - * - * {{ config(" - * - * then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 - * instead 0. Probably the parser turns the string into something like this: - * - * "{{ config(";" - * - * and returns ";" as an argument. - * - * This function replaces the last double quote with a single quote in document. - */ private function replaceLastDoubleQuoteWithSingleQuote(): string { return substr($this->document, 0, -1) . "'"; @@ -80,6 +66,20 @@ public function walk() return new Base; } + /** + * If a last character is a double quote, for example: + * + * {{ config(" + * + * then Microsoft\PhpParser\Parser::parseSourceFile returns autocompletingIndex: 1 + * instead 0. Probably the parser turns the string into something like this: + * + * "{{ config(";" + * + * and returns ";" as an argument. + * + * This line of code checks if the last character is a double quote and fixes it. + */ if ($this->documentHasDoubleQuoteAsLastCharacter()) { return (new self($this->replaceLastDoubleQuoteWithSingleQuote(), $this->debug))->walk(); }