Skip to content

Commit b69c81b

Browse files
committed
feat: ✨ Added support for source.fixAll.phpcs code action (for auto-fix on save).
1 parent bf03e8e commit b69c81b

File tree

4 files changed

+163
-190
lines changed

4 files changed

+163
-190
lines changed

README.md

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
[![Zed](https://img.shields.io/badge/Zed-Editor-blue?logo=zed&logoColor=white)](https://zed.dev)
88
[![PHPCS](https://img.shields.io/badge/PHPCS-3.13.2%2B-green)](https://github.com/PHPCSStandards/PHP_CodeSniffer)
99

10-
This extension integrates PHP_CodeSniffer with Zed Editor to provide real-time code style checking. It highlights violations as you code and supports various PHP coding standards including PSR-12, custom rulesets, and project-specific configurations.
10+
This extension integrates PHP_CodeSniffer with Zed Editor to provide real-time code style checking and automatic formatting. It highlights violations as you code, auto-fixes them on save via PHPCBF, and supports various PHP coding standards including PSR-12, custom rulesets, and project-specific configurations.
1111

1212
## Features
1313

1414
- **Real-time diagnostics** - See code style violations as you type
15-
- **Code formatting** - Format files with phpcbf via `Cmd+Shift+I` or format-on-save
15+
- **Auto-fix on save** - Automatically fix all PHPCS issues on save via `source.fixAll.phpcs`
1616
- **Zero configuration** - Works out of the box using PHPCS native defaults
1717
- **Live configuration** - Settings changes apply immediately without restart
1818
- **Auto-recovery** - Automatically handles deleted or invalid config files
@@ -69,6 +69,24 @@ if ($x === 1) {
6969
}
7070
```
7171

72+
4. **Enable auto-fix on save** (optional) to fix all PHPCS issues when you save:
73+
74+
```json
75+
{
76+
"languages": {
77+
"PHP": {
78+
"code_actions_on_format": {
79+
"source.fixAll.phpcs": true
80+
},
81+
"formatter": [],
82+
"format_on_save": "on"
83+
}
84+
}
85+
}
86+
```
87+
88+
> **Note:** This runs PHPCBF on every save, automatically fixing all fixable code style issues. The `"formatter": []` is required to prevent Zed's default formatter from interfering. See the [Auto-Fix on Save](#auto-fix-on-save) section for more configuration options.
89+
7290
## Configuration
7391

7492
> **Note:** The extension works without any configuration, using PHPCS's natural defaults and bundled binaries.
@@ -170,11 +188,13 @@ export PHPCBF_PATH="/custom/path/to/phpcbf"
170188
<details>
171189
<summary><strong>Automatic Discovery</strong> (recommended)</summary>
172190

173-
The extension finds PHPCS in this priority order:
191+
The extension finds PHPCS and PHPCBF in this priority order:
174192

175193
1. **Project composer** - `vendor/bin/phpcs` (includes project dependencies like Slevomat)
176-
2. **System PATH** - Global phpcs installation (respects your `phpcs --config-set` settings)
177-
3. **Bundled PHAR** - Modern PHPCS v3.13.2+ (fallback, included with extension)
194+
2. **User-configured path** - Custom path from Zed LSP settings
195+
3. **Environment variable** - `PHPCS_PATH` / `PHPCBF_PATH`
196+
4. **System PATH** - Global phpcs installation (respects your `phpcs --config-set` settings)
197+
5. **Bundled PHAR** - Modern PHPCS v3.13.2+ (fallback, included with extension)
178198

179199
> **💡 Global Config Support:** The extension now respects your system PHPCS configuration. Set global defaults with `phpcs --config-set default_standard PSR12` or `phpcs --config-set installed_paths /path/to/sniffs` and they'll work automatically without any Zed configuration.
180200
@@ -264,22 +284,59 @@ If a config file becomes corrupted or references missing standards:
264284
- **Workspace changes** - Config re-discovered when switching projects
265285
- **File system changes** - Config errors trigger automatic re-discovery
266286

267-
## Formatting
287+
## Auto-Fix on Save
288+
289+
The extension supports automatic fixing of code style issues via PHPCBF (PHP Code Beautifier and Fixer), using the `source.fixAll.phpcs` code action. This follows the same convention used by ESLint, Biome, and Ruff.
290+
291+
### PHPCS Fixes Only
292+
293+
```json
294+
{
295+
"languages": {
296+
"PHP": {
297+
"code_actions_on_format": {
298+
"source.fixAll.phpcs": true
299+
},
300+
"formatter": [],
301+
"format_on_save": "on"
302+
}
303+
}
304+
}
305+
```
306+
307+
### All Linter Fixes
308+
309+
Fix issues from PHPCS and any other linters that support `source.fixAll`:
268310

269-
The extension supports code formatting via PHPCBF (PHP Code Beautifier and Fixer). When triggered, it runs `phpcbf` on the current file content and applies all fixable changes.
311+
```json
312+
{
313+
"languages": {
314+
"PHP": {
315+
"code_actions_on_format": {
316+
"source.fixAll": true
317+
},
318+
"formatter": [],
319+
"format_on_save": "on"
320+
}
321+
}
322+
}
323+
```
270324

271-
### Enabling Format-on-Save
325+
### Combine with a Separate Formatter
272326

273-
Add the following to your Zed `settings.json` (`Cmd+,` or `Ctrl+,`):
327+
Use PHPCS fixing alongside a separate formatter (e.g., Prettier for embedded HTML/JS):
274328

275329
```json
276330
{
277331
"languages": {
278332
"PHP": {
279-
"language_servers": ["intelephense", "phpcs", "!phpactor"],
333+
"code_actions_on_format": {
334+
"source.fixAll.phpcs": true
335+
},
280336
"formatter": {
281-
"language_server": {
282-
"name": "phpcs"
337+
"external": {
338+
"command": "prettier",
339+
"arguments": ["--stdin-filepath", "{buffer_path}"]
283340
}
284341
},
285342
"format_on_save": "on"
@@ -288,15 +345,14 @@ Add the following to your Zed `settings.json` (`Cmd+,` or `Ctrl+,`):
288345
}
289346
```
290347

291-
> **Note:** Using `"name": "phpcs"` ensures Zed routes formatting to this extension rather than another language server like intelephense.
292-
293-
You can also format manually with `Cmd+Shift+I` (macOS) or `Ctrl+Shift+I` (Linux/Windows).
348+
> **Important:** When using `code_actions_on_format` without a separate formatter, you must set `"formatter": []` to prevent Zed's default `"auto"` formatter from interfering. See [zed-industries/zed#51490](https://github.com/zed-industries/zed/issues/51490) for details.
294349
295350
### How It Works
296351

297-
- Formatting uses the same coding standard as linting (phpcs.xml discovery, Zed settings, etc.)
352+
- Auto-fixing uses the same coding standard as linting (phpcs.xml discovery, Zed settings, etc.)
298353
- PHPCBF is discovered automatically using the same priority as PHPCS: project `vendor/bin` → user-configured path → `PHPCBF_PATH` env var → system PATH → bundled PHAR
299-
- Formatting and linting run from the same LSP process — no extra configuration needed
354+
- Auto-fixing and linting run from the same LSP process — no extra configuration needed
355+
- The `source.fixAll.phpcs` code action is also available in the lightbulb menu for manual use
300356

301357
## Troubleshooting
302358

extension.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "PHPCS"
33
version = "0.3.0"
44
schema_version = 1
55
authors = ["Mike Bronner <mike@genealabs.com>"]
6-
description = "PHP CodeSniffer Language Server for linting PHP code. We are looking for feedback, especially for Windows and Linux platforms."
6+
description = "PHP CodeSniffer Language Server for linting, formatting, and auto-fixing PHP code via PHPCS and PHPCBF."
77
repository = "https://github.com/GeneaLabs/zed-phpcs-lsp"
88

99
[lib]

extension.wasm

10.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)