Skip to content

Add format on save option (issue #3944) #4753

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

Merged

Conversation

Ndot
Copy link
Contributor

@Ndot Ndot commented Apr 3, 2025

Add format on save (issue #3944) with two option:

  • lsp-format-buffer-on-save to turn on / off
  • lsp-format-buffer-on-save-list to specify what major modes to save (nil saves all buffer)

lsp-mode.el Outdated
(defcustom lsp-format-buffer-on-save-list '()
"If the list is empty format all buffer on save. Else only format buffers
if their major-mode is in the list."
:type '(repeat string)
Copy link
Member

Choose a reason for hiding this comment

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

I think we should store a list of symbols instead of string. Comparing symbol is always faster than compare string and cost less to store it. Plus, it's more natural to set a list of symbols than a list of string.

Copy link
Contributor Author

@Ndot Ndot Apr 3, 2025

Choose a reason for hiding this comment

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

I actually used symbols at first and then changed it to strings cause for some reason though that would be more accessible, but I agree with you using symbols is better.

Updated the branch to use symbols.

@Ndot Ndot force-pushed the fix-3944-add-format-on-save-option branch from 9048e33 to 6b79dd2 Compare April 3, 2025 18:01
@jcs090218 jcs090218 merged commit 1fb656e into emacs-lsp:master Apr 6, 2025
18 checks passed
@jcs090218
Copy link
Member

Thanks for adding this!

@Ndot
Copy link
Contributor Author

Ndot commented Apr 13, 2025

@jcs090218 Thanks. Maybe issue #3944 can be closed?

ncaq added a commit to ncaq/lsp-mode that referenced this pull request Apr 13, 2025
Add `:safe t` and `:local t` properties to `lsp-format-buffer-on-save` to enable
project-specific configuration through `.dir-locals.el` without security prompts.

Thanks to PR emacs-lsp#4753 for implementing the `format-on-save` functionality.
This enhancement allows keeping automatic formatting disabled by default
(avoiding unwanted diffs in third-party projects)
while enabling it for personal or team projects with established formatting standards.

The security implications are minimal
since LSP formatting capabilities are limited to code reformatting,
and any project where you're running an LSP server
already has significant execution privileges.
jcs090218 pushed a commit that referenced this pull request Apr 13, 2025
…4760)

Add `:safe t` and `:local t` properties to `lsp-format-buffer-on-save` to enable
project-specific configuration through `.dir-locals.el` without security prompts.

Thanks to PR #4753 for implementing the `format-on-save` functionality.
This enhancement allows keeping automatic formatting disabled by default
(avoiding unwanted diffs in third-party projects)
while enabling it for personal or team projects with established formatting standards.

The security implications are minimal
since LSP formatting capabilities are limited to code reformatting,
and any project where you're running an LSP server
already has significant execution privileges.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants