Skip to content

feat: Add textDocument/formatting support via phpcbf#54

Merged
mikebronner merged 1 commit intoGeneaLabs:mainfrom
s3rgiosan:feature/code-formatting
Mar 12, 2026
Merged

feat: Add textDocument/formatting support via phpcbf#54
mikebronner merged 1 commit intoGeneaLabs:mainfrom
s3rgiosan:feature/code-formatting

Conversation

@s3rgiosan
Copy link
Contributor

@s3rgiosan s3rgiosan commented Mar 10, 2026

Fixes #55
Fixes #46

Summary

Adds textDocument/formatting support so users can format PHP files using PHPCBF directly from Zed via Cmd+Shift+I or format-on-save.

  • Formatting handler — Implements textDocument/formatting in the Tower-LSP server, reusing the existing run_phpcbf infrastructure. Returns a full-document TextEdit when phpcbf makes changes.
  • Capability registration — Announces documentFormattingProvider: true in the server's initialize response.
  • Improved binary discovery — Adds PHPCS_PATH / PHPCBF_PATH environment variable support and unifies the detection priority for both tools:
    1. Project vendor/bin (Composer)
    2. User-configured path from LSP settings
    3. Environment variable
    4. System $PATH
    5. Bundled PHAR
  • Removed hardcoded PSR12 default — The extension.toml no longer sets standard = "PSR12", allowing PHPCS/PHPCBF to respect the user's global config, project config files, or Zed settings without interference.

Configuration

{
  "languages": {
    "PHP": {
      "language_servers": ["intelephense", "phpcs", "!phpactor"],
      "formatter": {
        "language_server": {
          "name": "phpcs"
        }
      },
      "format_on_save": "on"
    }
  }
}

Test plan

  • Open a PHP file with style violations and run Cmd+Shift+I — phpcbf fixes are applied
  • Enable format_on_save — formatting triggers on save
  • Verify linting diagnostics still work correctly after formatting
  • Test with project phpcs.xml — both linting and formatting use the project standard
  • Test without any standard configured — falls back to PHPCS native defaults
  • Test with PHPCBF_PATH env var — binary is discovered correctly

@s3rgiosan
Copy link
Contributor Author

@mikebronner for your review.

@mikebronner
Copy link
Member

@s3rgiosan Thanks for submitting this PR! I wanted to check though: did you know that this plugin already supported who-document fixes via Zed's quick action menu and shortcuts? Could you explain what your PR does differently, just to help me understand. Thanks!

@s3rgiosan
Copy link
Contributor Author

@mikebronner Thank you for the question and for your time reviewing my PR! The existing code actions (via textDocument/codeAction) are great for on-demand fixes through the quick action menu.

What this PR adds is textDocument/formatting support — a different LSP capability. The key benefit is that it lets users enable format-on-save (by setting "format_on_save": "on" in their Zed settings) so phpcbf runs automatically every time they save. It also hooks into Zed's standard "Format Document" command (Cmd+Shift+I), making it feel like a native formatter rather than a manual code action.

In short: code actions = manual trigger from quick action menu; formatting = integrates with Zed's formatting pipeline (format-on-save, format keybinding, formatter selection).

@mikebronner
Copy link
Member

@s3rgiosan thank you for clarifying the difference. That makes a lot of sense! Thank you for working on this feature :) I will review and integrate your PR this week.

@mikebronner mikebronner merged commit bf03e8e into GeneaLabs:main Mar 12, 2026
2 checks passed
@mikebronner
Copy link
Member

@s3rgiosan I made some changes to how the fix on save was implemented, as this is not really a formatter, but a linter fixer. please see the readme on how to update the settings for this. This way it will work collaboratively with other linter fixers as well as document formatters. Please let me know if you run into any issues because of this! Thanks again for your work on this PR. :)

The extension update is currently awaiting approval: zed-industries/extensions#5221

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.

Add textDocument/formatting support via PHPCBF Implement "fix on save" option

2 participants