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

Use with autoformatting #12

Closed
piersolenski opened this issue Sep 19, 2021 · 5 comments
Closed

Use with autoformatting #12

piersolenski opened this issue Sep 19, 2021 · 5 comments

Comments

@piersolenski
Copy link

I tend to use Prettier to format my code via something like Neoformat. It works almost perfectly, however, some plugins such as nvim-ts-autotag trigger the InsertLeave and TextChanged events. In the case of autotag, the autosaving and formatting means the tag is closed before I get a chance to populate it for example.

Another issue is that often the saving and formatting can run before linting errors have been properly addressed, which can crash whatever code you're working on.

The only solution I've found so far is to turn off AutoSaving each time I want to change a tag, or make some changes that might result in some temporary linting errors, but that's a bit of a frustrating workflow and somewhat defeats the point of the plugin.

I was wondering if, in the first instance, it might be possible to temporarily disable the plugin whilst another plugin runs. In the case of the linting problem, perhaps something like Neoformat should run the linter before attempting to format the code, but this might end up being quite language and formatter specific.

I realise this is probably an issue that's out of the scope of what this library tries to solve using a more complex stack, but I was interested to know if you had any thoughts in regards to a workaround?

Thanks for your hard work!

@pocco81
Copy link
Owner

pocco81 commented Sep 20, 2021

The only general solution I see doable: What about adding a g:autosave_abort that you can use to forcefully disable the save momentarily?

I tend to use Prettier to format my code via something like Neoformat. It works almost perfectly, however, some plugins such as nvim-ts-autotag trigger the InsertLeave and TextChanged events. In the case of autotag, the autosaving and formatting means the tag is closed before I get a chance to populate it for example.

It works almost perfectly, however, some plugins such as nvim-ts-autotag trigger the InsertLeave and TextChanged events.

Increase the debounce_delay in the plugin's settings. Just because it's an auto-saving plugin you don't have to save instantly after every change. For reference, I have mine set up at 2.5 seconds. Increasing the time it takes before saving will [hopefully] decrease the chances of it borking other plugins.

Another issue is that often the saving and formatting can run before linting errors have been properly addressed, which can crash whatever code you're working on.

Perhaps you could use this plugin's hooks to auto-save after auto-formatting?

I was wondering if, in the first instance, it might be possible to temporarily disable the plugin whilst another plugin runs. In the case of the linting problem, perhaps something like Neoformat should run the linter before attempting to format the code, but this might end up being quite language and formatter specific.

Correct me If I'm wrong but in order of execution, what you want is:

Linter -> Formatting -> Auto Saving

Indeed this is out of the scope of either of the three plugins. I'd recommend you write a simple function that runs the three of them, one by one, evaluating the exit status of the previous one to see if the next one can run, and bind that to a command.

@piersolenski
Copy link
Author

The only problem with increasing the debounce_delay is that in terms of front end development, it means waiting longer for the browser to refresh - which means hot reloading becomes very slow.

I ​think adding g:autosave_abort would still mean manually triggering that every time, unless the plugin in question has a hook that it can call before it runs?

The order of execution is correct, however, it's very specific to the language's ecosystem, so definitely out of the scope of AutoSave.

All good suggestions and food for thought though!

@maxmx03
Copy link

maxmx03 commented Jul 9, 2022

This is my best attempt.
My formatter: prettier_d_slim

lsp.lua

local lsp = require 'lspconfig'

local on_attach = function(client, bufnr)
  vim.api.nvim_create_autocmd({ 'User' }, {
    pattern = 'format',
    callback = function()
      vim.cmd 'lua vim.lsp.buf.format()'
    end,
  })
end

autocmd.lua

vim.api.nvim_create_autocmd({ 'InsertLeave' }, {
  pattern = '*.*',
  callback = function()
    vim.cmd 'doautocmd User format'
    vim.cmd 'update'
  end,
})

vim.api.nvim_create_autocmd({ 'TextChanged' }, {
  pattern = '*.*',
  callback = function()
    vim.cmd 'update'
  end,
})
``

@pocco81
Copy link
Owner

pocco81 commented Jul 31, 2022

Heya! please check #33 🎉

@pocco81 pocco81 closed this as completed Jul 31, 2022
@kohane27
Copy link

Thanks to the effort of @pocco81 , with the rewrite (#33), we can set up callbacks in config:

callbacks = { -- functions to be executed at different intervals
    enabling = nil, -- ran when enabling auto-save
    disabling = nil, -- ran when disabling auto-save
    before_asserting_save = nil, -- ran before checking `condition`
    before_saving = nil, -- ran before doing the actual save
    after_saving = nil -- ran after doing the actual save
}

If you want to format the code before auto-save saves it for you:

callbacks = {
    before_saving = function()
      return vim.lsp.buf.format()
    end
}

However, currently, there is a bug (please see #56). My current workaround is to use the fork https://github.com/AnonymusRaccoon/auto-save.nvim until it's merged. Thanks guys!

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

No branches or pull requests

4 participants