Skip to content

refactor: add generic wrap Twig filter to dedupe comment-wrapping logic#1586

Open
KevalPrajapati wants to merge 3 commits into
appwrite:mainfrom
KevalPrajapati:feat/wrap-comment-filter
Open

refactor: add generic wrap Twig filter to dedupe comment-wrapping logic#1586
KevalPrajapati wants to merge 3 commits into
appwrite:mainfrom
KevalPrajapati:feat/wrap-comment-filter

Conversation

@KevalPrajapati

Copy link
Copy Markdown

Closes #1479 (partially — see scope below).

The same wordwrap() + prefix logic was copy-pasted across ~8 comment filters. This adds one wrap(width, prefix) filter in SDK.php and points the existing call sites at it, then deletes the duplicates.

Removed: comment1, comment2, dartComment, dotnetComment, swiftComment, rubyComment. Also dropped comment3, which wasn't referenced by any template.

Templates now call e.g. {{ method.description | wrap(75, ' /// ') }}, with the prefix at the call site instead of buried in PHP.

No output change

This is a straight refactor — I kept the width at 75 and the exact prefixes, so the generated SDKs come out identical. I verified by generating every SDK before and after the change and diffing the output directories (all 24 match). git diff alone isn't enough here since examples/ is gitignored.

Added a few unit tests for the new filter — there weren't any covering this before.

Left for follow-ups

  • Go and Rust also have their own versions, but they trim() and take an indent argument, so wrap would need a couple of extra params. Didn't want to widen the API in the same PR.
  • The inline replace({"\n": ...}) templates (Kotlin, Android, Python, etc.) mentioned in the issue — those don't word-wrap today, so switching them to wrap would change their output. That's a behavior change, not a no-op, so it makes more sense as its own PR.

Happy to fold those in here instead if you'd prefer one bigger PR.

…ogic

Introduce a single `wrap(width, prefix)` Twig filter in SDK.php and route the
duplicated comment-wrapping logic through it, removing the near-identical
per-language copies.

Removed filters (all reproduced exactly by `wrap`):
- comment1, comment2 (SDK.php / Web.php)
- comment3 (Web.php) — was dead code, never referenced by any template
- dartComment, dotnetComment, swiftComment, rubyComment

Call sites migrated to `| wrap(75, '<prefix>')` across dart, flutter, dotnet,
unity, swift, apple, ruby, php, deno and react-native templates.

This is a pure no-op cleanup: generated output is byte-identical for all 24
SDKs (verified by diffing before/after generation, not git, since examples/
is gitignored). Adds 5 unit tests for the new filter (none existed before).

Out of scope (deliberate follow-ups):
- Go/Rust filters, which add trim()/indent and would need extra `wrap` options.
- The ~12 templates using inline replace({"\n": ...}); folding them in changes
  output (they currently don't word-wrap), so they belong in a separate PR.
@greptile-apps

greptile-apps Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR consolidates ~8 duplicated comment-wrapping Twig filters into a single generic wrap(width, prefix) filter registered in SDK.php, then updates every call site to use it. The removed filters (comment1, comment2, comment3, dartComment, dotnetComment, swiftComment, rubyComment) all had identical structure — split on newlines, prefix . wordwrap(line, 75, "\n" . prefix), rejoin — so the new filter is a faithful drop-in replacement.

  • New filter (SDK.php): wrap(int $width = 75, string $prefix = '') is registered as a global Twig filter with is_safe => html, matching the safety annotation of every filter it replaces.
  • Template updates (12 files): All 12 changed templates now pass the prefix at the call site (e.g. | wrap(75, ' /// ')); no template still references any of the removed filter names.
  • Removed language filters: Dart.php, DotNet.php, Ruby.php, Swift.php, and Web.php each lose one or two filter registrations with no replacement needed.

Confidence Score: 5/5

This is a pure deduplication refactor — the generated SDK output is unchanged and all call sites have been updated correctly.

Every removed filter has been replaced by wrap with the identical prefix string and width of 75. A grep across all templates confirms no stale references to the old filter names remain. The new filter adds a defensive (string) cast over the originals and carries the same is_safe => html annotation. There are no behavioral differences on any code path exercised by the templates.

No files require special attention.

Important Files Changed

Filename Overview
src/SDK/SDK.php Adds the new generic wrap filter and removes comment1; new implementation is a faithful drop-in with a defensive (string) cast and identical is_safe => html annotation.
src/SDK/Language/Dart.php Removes dartComment filter; prefix ' /// ' and width 75 preserved in call sites of dart and flutter templates.
src/SDK/Language/DotNet.php Removes dotnetComment filter; prefix ' /// ' preserved in dotnet and unity template call sites.
src/SDK/Language/Ruby.php Removes rubyComment filter; prefix ' # ' preserved at ruby template call site.
src/SDK/Language/Swift.php Removes swiftComment filter; prefix ' /// ' preserved at swift and apple template call sites.
src/SDK/Language/Web.php Removes comment2 and unreferenced comment3; comment2 prefix ' * ' preserved in react-native template call sites.
templates/dart/lib/services/service.dart.twig Replaces dartComment with wrap(75, ' /// ') at both the service description and method description sites; the split/join pipeline that trims extra leading spaces is preserved.
templates/flutter/lib/services/service.dart.twig Same dartCommentwrap(75, ' /// ') swap as the dart template; both call sites updated consistently.
templates/deno/src/services/service.ts.twig Replaces comment1 with wrap(75, ' * '); prefix and width match the original exactly.
templates/php/src/Services/Service.php.twig Replaces comment1 with wrap(75, ' * '); functionally identical to the removed global filter.
templates/dotnet/Package/Services/ServiceTemplate.cs.twig Replaces dotnetComment with wrap(75, ' /// '); prefix matches the removed filter verbatim.
templates/unity/Assets/Runtime/Core/Services/ServiceTemplate.cs.twig Replaces dotnetComment with wrap(75, ' /// '); identical prefix to the dotnet template change.
templates/ruby/lib/container/services/service.rb.twig Replaces rubyComment with wrap(75, ' # '); prefix matches original 8-space Ruby comment prefix.
templates/apple/Sources/Services/Service.swift.twig Replaces swiftComment with wrap(75, ' /// ') at both method description sites; prefix matches original 4-space Swift doc-comment prefix.
templates/swift/Sources/Services/Service.swift.twig Replaces swiftComment with wrap(75, ' /// ') at both method description sites; identical to the apple template change.
templates/react-native/src/services/template.ts.twig Replaces comment2 with wrap(75, ' * '); prefix matches the original comment2 exactly.
templates/react-native/src/client.ts.twig Replaces comment2 with wrap(75, ' * ') for header descriptions; functionally identical.

Reviews (3): Last reviewed commit: "test: drop dedicated wrap filter test" | Re-trigger Greptile

Comment thread tests/unit/WrapFilterTest.php Outdated
The helper only emitted filter arguments when a width was passed, so a
prefix given without a width was silently dropped. Fall back to the
filter's default width (75) whenever a prefix is present.
The filter is a thin wrapper around wordwrap(); a dedicated test isn't
warranted. Behaviour is already covered by the unchanged generated output
across all SDKs.
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.

🚀 Feature: Add generic \wrap\ Twig filter to replace duplicated comment-wrapping logic

1 participant