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

feat: add "on_save" option #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adoyle-h
Copy link

@adoyle-h adoyle-h commented Dec 6, 2022

User can disable formatting on save.

Copy link
Owner

@lukas-reineke lukas-reineke left a comment

Choose a reason for hiding this comment

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

In theory, I like this 👍

But there is a problem with this implementation. If there are more than 1 LSP attached to the buffer, they will overwrite each other.
If there is one LSP with on_save = false, and one with on_save = true, the autocommand will still be created. And on save, even the first LSP with on_save = false will format the buffer.
We need to check if on_save is true for each client that is attached to the buffer.

README.md Outdated Show resolved Hide resolved
lua/lsp-format/init.lua Outdated Show resolved Hide resolved
@adoyle-h
Copy link
Author

adoyle-h commented Dec 8, 2022

If there are more than 1 LSP attached to the buffer, they will overwrite each other. If there is one LSP with on_save = false, and one with on_save = true, the autocommand will still be created. And on save, even the first LSP with on_save = false will format the buffer. We need to check if on_save is true for each client that is attached to the buffer.

This won't happen. Because format_options is relative to buffer filetype, not LSP client. The format_options are same for same filetype, even on_attach invoked with different client.

local format_options = vim.tbl_deep_extend('keep', M.format_options[vim.bo.filetype] or {}, { on_save = true })

@adoyle-h adoyle-h force-pushed the feat/option-auto-save branch from 67e2dd5 to 1375073 Compare December 8, 2022 05:43
@lukas-reineke
Copy link
Owner

This won't happen. Because format_options is relative to buffer filetype, not LSP client

Right, sorry. It's been a while since I worked on this. Ignore what I said

Comment on lines 147 to 150
vim.api.nvim_clear_autocmds {
buffer = bufnr,
group = group,
}
Copy link
Owner

Choose a reason for hiding this comment

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

Overwriting from a different LSP is not possible 👍 , but this will still not properly work if the filetype changes.
You need to run the clear autocommands even if on_save is false

Copy link
Author

Choose a reason for hiding this comment

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

If filetype changes, the on_attach function will not be triggered. And the previous LSP clients are still attaching on the buffer. You can try it by :set ft=<other filetype> after opened a file. I think this issue won't happen too.

Copy link
Owner

Choose a reason for hiding this comment

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

It will if you use nvim-lspconfig, which most people do. I'd like to support this.

@adoyle-h adoyle-h force-pushed the feat/option-auto-save branch from 1375073 to 91db598 Compare December 9, 2022 12:54
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.

2 participants