Skip to content
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

[enhancement] Support codeActionsOnSave source.fixAll #135

Closed
kitschpatrol opened this issue Dec 13, 2023 · 4 comments
Closed

[enhancement] Support codeActionsOnSave source.fixAll #135

kitschpatrol opened this issue Dec 13, 2023 · 4 comments

Comments

@kitschpatrol
Copy link

kitschpatrol commented Dec 13, 2023

Hi, thanks for all the effort that has gone into this extension.

I'm trying to configure things in such a way that xo handles lint fixes, and prettier handles formatting. (I don't want to run prettier through xo / ESLint, both to avoid red-squiggly noise and to separate the concern / consolidate the process of formatting files that don't fall under xo's purview.)

My VSCode settings.json looks roughly like:

{
	"xo.enable": true,
	"xo.format.enable": false,
	"editor.codeActionsOnSave": {
		"xo.fix": "explicit"
	},
	"editor.defaultFormatter": "esbenp.prettier-vscode",
	"editor.formatOnSave": true,
}

This approach works great with the ESLint plugin (in that case passing "source.fixAll": "explicit" instead of "xo.fix": "explicit" to editor.codeActionsOnSave), but it never seems to trigger xo.fix on save.

I can invoke the action manually through the VSCode command palette > xo.fix, and it works great.

Tried a bunch of permutations in settings.json, with no luck. I added some tracing to the extension and it looks like the fixAllProblems function isn't failing, it's simply never invoked.

Also tried explicitly specifying xo.runtime, but that wasn't it.

When using xo as the formatter, fixes are correctly applied via editor.formatOnSave.

Not sure where to start with this one. Maybe something about hooking into the code that runs on source.fixAll (Providing an action for CodeActionKind.SourceFixAll), so xo fixes are aggregated with changes provided by other extensions and are therefore invoked via source.fixAll?

Thanks for any insights.

@spence-s
Copy link
Collaborator

Hello! Thanks for feature request, I currently haven't implemented this code action kind so I don't think it will work without doing so. I don't think it's too hard as I have already implemented the quick fix code actions for when you hover over an error. So I think basically when source.fixAll requests come through we should just send back the entire code edits cache as an array of edits.

I can definitely implement this but I am not exactly sure when I will get around to it and I just haven't had a personal need for it.

If you are interested in taking a stab, look here for the code actions handlers:
https://github.com/xojs/vscode-linter-xo/blob/master/server/server.ts#L318
https://github.com/xojs/vscode-linter-xo/blob/master/server/code-actions-builder.ts#L18

You might also want to look at an extension like this https://marketplace.visualstudio.com/items?itemName=Jota0222.multi-formatter that will allow you to use both xo and prettier as a formatter at the same time, which should work just fine as long as you extend the eslint-prettier-config package to make sure formatting rules get turned off in xo.

I'll leave this open as a feature request until I can get around to it.

@kitschpatrol
Copy link
Author

Cool thanks for the quick reply and the leads on an implementation approach!

I might dig into it, but will give the multi-formatter extension a try first. (I had been trying out vscode-run-on-save, but it seemed a bit shaky sometimes.)

@spence-s spence-s changed the title Trouble with xo.fix in codeActionsOnSave [enhancement] Support codeActionsOnSave source.fixAll Dec 14, 2023
@spence-s
Copy link
Collaborator

source.fixAll code actions are now supported as of v3.18

{
    "editor.codeActionsOnSave": {
        "source.fixAll": "explicit"
    }
}

@lachieh
Copy link

lachieh commented Mar 21, 2024

Wasn't sure if I should open a new issue, but it'd be good to support the explicit "source.fixAll.xo" option as well. This is the same way that the eslint and tslint extensions work. example.

Thank you for your work, @spence-s!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants