-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Further improve comment extraction #166
Further improve comment extraction #166
Conversation
* Case insenstive comment prefix matching * Find comments on the same line * Find multi-line comments
Hi. I think is better to have a pull request per feature 😃 Not sure whether comment in the same line is valid in xgettext. The gettext docs says "preceding keyword lines". 🤔 And about multiline comments, I think this logic should be placed inside the So, instead: $lineNumber = $value[2] + substr_count($value[1], "\n"); You could do simply: $lineNumber = $comment->getLastLine(); Or even better: if ($comment->isRelatedWith($bufferFunctions[0])) {
$bufferFunctions[0]->addComment($comment);
} This leaves the parser cleaner and move the responsability of how to handle comments to the ParsedComment class. |
Fair enough. An alternative would be to pass the various case variations to the functions scanner, so that's not a big deal. I'll undo it.
It is. Example code: <?php
/* allowed1: boo */ /* allowed2: only this should get extracted. */ /* some other comment */ $bar = strtolower( __( 'Foo' ) ); Result of #. allowed2: only this should get extracted.
#. some other comment
#: example.php:2
msgid "Foo"
msgstr "" Whereas with this PR the library does this: #. allowed2: only this should get extracted.
#: ./example.php:2
msgid "Foo"
msgstr "" So I'd even say this library does it better than
I'll try to come up with something :-) |
Adds a new `ParsedComment::isRelatedWith()` method to compare line numbers.
It would probably be a bit less complex if |
Yes, good idea. And about the last line of the comment, I meant to let the |
After parsing, the comment is on a single line though, so the ending line number can't be calculated from that. |
Ok, so what do you think about moving all this logic to a static factory of protected function parsePhpComment($value)
{
if ($this->extractComments === false) {
return;
}
//this returns a comment or null
$comment = ParsedComment::create($value);
if ($comment && $comment->checkPrefix($this->extractComments)) {
return $comment;
}
} |
Agreed, makes sense. It looks much clearer with that. |
These all pass with php-gettext/Gettext#166 applied.
src/Utils/ParsedComment.php
Outdated
* @param array $prefixes An array of prefixes to check. | ||
* @return bool Whether the comment matches the prefixes or not. | ||
*/ | ||
public function checkPrefixes($prefixes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This argument could be typed (checkPrefixes(array $prefixes)
)
Thanks! |
These all pass with php-gettext/Gettext#166 applied.
Follow-up to #164:
Case insensitive comment prefix matching (previously: Translator comments should be case insensitive #165)