From 18d618610a863487aca0b847d9a91058745693fb Mon Sep 17 00:00:00 2001 From: brendt Date: Thu, 28 Mar 2024 06:14:23 +0100 Subject: [PATCH] Add JSDoc --- .../DocComment/DocCommentLanguage.php | 2 -- .../JavaScript/Injections/JsDocInjection.php | 25 ++++++++++++++++ .../JavaScript/JavaScriptLanguage.php | 2 ++ src/Languages/JavaScript/JsDocLanguage.php | 21 ++++++++++++++ .../Patterns/JsDocParamNamePattern.php | 29 +++++++++++++++++++ .../JavaScript/Patterns/JsDocTypePattern.php | 27 +++++++++++++++++ src/Languages/Php/PhpDocCommentLanguage.php | 9 ++++++ .../JavaScript/JavaScriptLanguageTest.php | 6 ++-- tests/targets/test.md | 14 ++++----- 9 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 src/Languages/JavaScript/Injections/JsDocInjection.php create mode 100644 src/Languages/JavaScript/JsDocLanguage.php create mode 100644 src/Languages/JavaScript/Patterns/JsDocParamNamePattern.php create mode 100644 src/Languages/JavaScript/Patterns/JsDocTypePattern.php diff --git a/src/Languages/DocComment/DocCommentLanguage.php b/src/Languages/DocComment/DocCommentLanguage.php index 40994f3..bc2649f 100644 --- a/src/Languages/DocComment/DocCommentLanguage.php +++ b/src/Languages/DocComment/DocCommentLanguage.php @@ -6,7 +6,6 @@ use Tempest\Highlight\Languages\Base\BaseLanguage; use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentTagPattern; -use Tempest\Highlight\Languages\Php\Injections\PhpGenericTypeInjection; class DocCommentLanguage extends BaseLanguage { @@ -14,7 +13,6 @@ public function getInjections(): array { return [ ...parent::getInjections(), - new PhpGenericTypeInjection(), ]; } diff --git a/src/Languages/JavaScript/Injections/JsDocInjection.php b/src/Languages/JavaScript/Injections/JsDocInjection.php new file mode 100644 index 0000000..717fe07 --- /dev/null +++ b/src/Languages/JavaScript/Injections/JsDocInjection.php @@ -0,0 +1,25 @@ +\/\*\*(.|\n)*?\*\/)/m'; + } + + public function parseContent(string $content, Highlighter $highlighter): string + { + return $highlighter->parse($content, new JsDocLanguage()); + } +} diff --git a/src/Languages/JavaScript/JavaScriptLanguage.php b/src/Languages/JavaScript/JavaScriptLanguage.php index 1efe1d8..37562db 100644 --- a/src/Languages/JavaScript/JavaScriptLanguage.php +++ b/src/Languages/JavaScript/JavaScriptLanguage.php @@ -5,6 +5,7 @@ namespace Tempest\Highlight\Languages\JavaScript; use Tempest\Highlight\Languages\Base\BaseLanguage; +use Tempest\Highlight\Languages\JavaScript\Injections\JsDocInjection; use Tempest\Highlight\Languages\JavaScript\Patterns\JsClassNamePattern; use Tempest\Highlight\Languages\JavaScript\Patterns\JsDoubleQuoteValuePattern; use Tempest\Highlight\Languages\JavaScript\Patterns\JsKeywordPattern; @@ -24,6 +25,7 @@ public function getInjections(): array { return [ ...parent::getInjections(), + new JsDocInjection(), ]; } diff --git a/src/Languages/JavaScript/JsDocLanguage.php b/src/Languages/JavaScript/JsDocLanguage.php new file mode 100644 index 0000000..f0cc7eb --- /dev/null +++ b/src/Languages/JavaScript/JsDocLanguage.php @@ -0,0 +1,21 @@ +(.*?))(\s|$)'; + } + + public function getTokenType(): TokenTypeEnum + { + return TokenTypeEnum::VALUE; + } +} diff --git a/src/Languages/JavaScript/Patterns/JsDocTypePattern.php b/src/Languages/JavaScript/Patterns/JsDocTypePattern.php new file mode 100644 index 0000000..3b10c89 --- /dev/null +++ b/src/Languages/JavaScript/Patterns/JsDocTypePattern.php @@ -0,0 +1,27 @@ +\{(.*?)\})'; + } + + public function getTokenType(): TokenTypeEnum + { + return TokenTypeEnum::TYPE; + } +} diff --git a/src/Languages/Php/PhpDocCommentLanguage.php b/src/Languages/Php/PhpDocCommentLanguage.php index 5bad572..13e235c 100644 --- a/src/Languages/Php/PhpDocCommentLanguage.php +++ b/src/Languages/Php/PhpDocCommentLanguage.php @@ -5,6 +5,7 @@ namespace Tempest\Highlight\Languages\Php; use Tempest\Highlight\Languages\DocComment\DocCommentLanguage; +use Tempest\Highlight\Languages\Php\Injections\PhpGenericTypeInjection; use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentGenericTypePattern; use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentParamTypePattern; use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentReturnTypePattern; @@ -14,6 +15,14 @@ class PhpDocCommentLanguage extends DocCommentLanguage { + public function getInjections(): array + { + return [ + ...parent::getInjections(), + new PhpGenericTypeInjection(), + ]; + } + public function getPatterns(): array { return [ diff --git a/tests/Languages/JavaScript/JavaScriptLanguageTest.php b/tests/Languages/JavaScript/JavaScriptLanguageTest.php index 6108051..a5ae815 100644 --- a/tests/Languages/JavaScript/JavaScriptLanguageTest.php +++ b/tests/Languages/JavaScript/JavaScriptLanguageTest.php @@ -108,9 +108,9 @@ function getMandatedParams() { <<<'TXT' /** * Class making something fun and easy. - * @param {string} arg1 An argument that makes this more interesting. - * @param {Array.<number>} arg2 List of numbers to be processed. - * @constructor + * @param {string} arg1 An argument that makes this more interesting. + * @param {Array.<number>} arg2 List of numbers to be processed. + * @constructor */ function someMethod(obj) { // ... diff --git a/tests/targets/test.md b/tests/targets/test.md index 5ca455e..cac72fc 100644 --- a/tests/targets/test.md +++ b/tests/targets/test.md @@ -1,12 +1,8 @@ -```php +```js /** - * This function will do some things - * - * @template T - * @param class-string $className the class' name - * @param int $id - * @return T|null - * - * That's about all + * Class making something fun and easy. + * @param {string} arg1 An argument that makes this more interesting. + * @param {Array.} arg2 List of numbers to be processed. + * @constructor */ ``` \ No newline at end of file