Skip to content

Add support for lsp_code_actions_on_format#2747

Open
mj026 wants to merge 8 commits intosublimelsp:mainfrom
mj026:feature/lsp-actions-on-format
Open

Add support for lsp_code_actions_on_format#2747
mj026 wants to merge 8 commits intosublimelsp:mainfrom
mj026:feature/lsp-actions-on-format

Conversation

@mj026
Copy link

@mj026 mj026 commented Feb 3, 2026

This PR allows the execution of (user defined) code actions before formatting when the LSP: Format File command is triggered by using a new setting lsp_code_actions_on_format.

Implemented as described in issue #2701.

mj026 added 5 commits February 3, 2026 17:39
Add a reusable CodeActionTaskBase class which can be used for both CodeActionOnSaveTask and CodeActionsOnFormatTask
So this can be used for the LspFormatDocumentCommand
Copy link
Member

@rchl rchl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LSP.sublime-settings and sublime-package.json should also be updated.

The amount of time the task is allowed to run is defined by user-controlled setting. If the task
runs longer, the native save will be triggered before waiting for results.
"""
CODE_ACTIONS = "lsp_code_actions_on_save"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe CODE_ACTIONS_SETTING_NAME?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably name it just SETTING_NAME, but I guess it isn't too important.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be fine with that too.

It has to be a specifically structured setting so I was thinking of more specific name but it's not easy.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll change it into SETTING_NAME, feels like the most obvious name for it.

@mj026
Copy link
Author

mj026 commented Feb 3, 2026

Thank you for reviewing so fast! Will work on your suggestions the following day(s) and update the PR accordingly.

mj026 added 3 commits February 4, 2026 15:51
- Make "on_tasks_completed" a "public" hook
- Add a overridable "on_before_tasks" hook
- Make SaveTasksRunner a hidden implementation detail
- Share the "run" method for both LspSaveCommand and LspFormatDocumentCommand
- Pass **kwargs instead of kwargs to the "on_tasks_completed" hook so we can unpack specific keyword arguments easily (for example, the "select" parameter)
@mj026
Copy link
Author

mj026 commented Feb 4, 2026

Hi @rchl, I implemented all your suggestions: I like it now how LspTextCommandWithTasks became self containing regarding managing tasks while LspSaveCommand and LspFormatDocumentCommand just contain specific implementation details for saving and formatting. Nice!

Let me know if you need anything else for this PR to go through.

},
"markdownDescription": "A dictionary of code action identifiers that should be triggered on save.\n\nCode action identifiers are not officially standardized so refer to specific server's documentation on what is supported but `source.fixAll` is commonly used to apply fix-on-save code actions.\n\nThis option is also supported in syntax-specific settings and/or in the `\"settings\"` section of project files. Settings from all those places will be merged and more specific (syntax and project) settings will override less specific (from LSP or Sublime settings).\n\nOnly \"source.*\" actions are supported."
},
"lsp_code_actions_on_format": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be referenced here also:

LSP/sublime-package.json

Lines 474 to 478 in 57388ca

"lsp_code_actions_on_save": {
"$ref": "sublime://settings/LSP#/definitions/lsp_code_actions_on_save",
},
"show_diagnostics_in_view_status": {
"type": "boolean",

Comment on lines +114 to +119
def on_before_tasks(self) -> None:
if self._save_tasks_runner:
self._save_tasks_runner.cancel()
sublime.set_timeout_async(self._trigger_on_pre_save_async)
self._save_tasks_runner = SaveTasksRunner(self, self._tasks, partial(self._on_tasks_completed, kwargs))

def on_tasks_completed(self, **kwargs: dict[str, Any]) -> None:
self._save_tasks_runner = None
Copy link
Member

@rchl rchl Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that the implementation contract could be made even more obvious by having abstract methods in the base that would have to be implemented (and wouldn't have to call super() which can be error-prone).

I can push the changes later to avoid back and forth.

Copy link
Author

@mj026 mj026 Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point. I’ll see what I can think of to make this a bit more robust.

r("log_debug", False)
r("log_max_size", 8 * 1024)
r("lsp_code_actions_on_save", {})
r("lsp_code_actions_on_format", {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem to be ordered alphabetically (also for the variable name above).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will fix this.

@rchl rchl force-pushed the main branch 2 times, most recently from f806ca0 to 90081a4 Compare February 4, 2026 22:22
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.

3 participants