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

README/use case: describe how to make ['_'] = { 'trim_whitespace', 'trim_newlines' }, play nice with editorconfig autocommands #586

Open
1 task done
matu3ba opened this issue Nov 25, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@matu3ba
Copy link

matu3ba commented Nov 25, 2024

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

Wanted: Extend the configuration in the usage section to some more low level usage, but probably it is simpler to mention 1. combined actions, 2. ['_'] problems:

local fmts_by_ft = {
  c = { 'clang-format' }, -- // clang-format off|on
  ['_'] = { 'trim_whitespace', 'trim_newlines' }, -- <- might make problems behavior for external fmting etc
}

local user_fmts_by_ft = {
  zig = function(args)
    vim.lsp.buf.code_action { context = { only = { 'source.fixAll' } }, apply = true }
    vim.loop.sleep(5)
    -- SHENNANIGAN conform.nvim can screw up formatting the first time
    vim.lsp.buf.format()
  end,
}

vim.api.nvim_create_autocmd('BufWritePre', {
  group = aucmds_conform_fmt,
  pattern = '*',
  callback = function(args)
    local ft = vim.bo[args.buf].filetype
    if fmts_by_ft[ft] == nil then
      if user_fmts_by_ft[ft] ~= nil then
        user_fmts_by_ft[ft]()
      else
        conform.format { bufnr = args.buf, formatters = { 'trim_whitespace', 'trim_newlines' } }
      end
    else
      conform.format { bufnr = args.buf, formatters = { fmts_by_ft[vim.bo[args.buf].filetype] } }
    end
  end,
})

Provide background

Justification: Running one or multiple code actions to autofix code and then the formatter (because formatter might break code for the autofix) prevents trim_whitespace and trim_newlines from being usable.

For example, I have

vim.api.nvim_create_autocmd('BufWritePre', {
  group = aucmds_zig_fmt,
  pattern = { '*.zig', '*.zon' },
  callback = function()
    vim.lsp.buf.code_action {
      -- SHENNANIGAN lsp.CodeActionContext: diagnostics field is optional,
      -- but shown as error by lsp from meta information
      context = { only = { 'source.fixAll' } },
      apply = true,
    }
    vim.loop.sleep(5)
    -- SHENNANIGAN conform.nvim can screw up formatting the first time
    vim.lsp.buf.format()
  end,
})

What is the significance of this feature?

nice to have

Additional details

Going more low level makes things less error prone, if the options '*' and _ or doing lsp actions before formatting (or custom checks) are wanted.

@matu3ba matu3ba added the enhancement New feature or request label Nov 25, 2024
@matu3ba matu3ba changed the title README/use case: describe how to prevent ['_'] = { 't', 'trim_newlines' }, from interefering with externally managed formatting and how to do code actions before formatting README/use case: describe how to prevent ['_'] = { 'trim_whitespace', 'trim_newlines' }, from interefering with externally managed formatting and how to do code actions before formatting Nov 25, 2024
@matu3ba
Copy link
Author

matu3ba commented Nov 25, 2024

Feel free to close, if you feel like this is out of scope/user responsibility. I'll leave the config for others inspiration, if they want to do something like this.

@matu3ba
Copy link
Author

matu3ba commented Nov 25, 2024

Currently there is no silent mode for lua autocommand and code_action has neither a silent mode to suppress prompt confirmation.

So one way or the other one has to spam autocommands to workaround that.
Ugh.

See also neovim/neovim#22651.

@matu3ba
Copy link
Author

matu3ba commented Nov 25, 2024

Ok, there is a workaround/hack:

local user_fmts_by_ft = {
  markdown = function() end, -- stub
  supermd = function() end, -- stub
  zig = function(args)
    local original = vim.notify
    ---@diagnostic disable-next-line: duplicate-set-field
    vim.notify = function(msg, level, opts)
      if msg == 'No code actions available' then return end
      original(msg, level, opts)
    end
    vim.lsp.buf.code_action {
      ---@diagnostic disable-next-line: missing-fields
      context = { only = { 'source.fixAll' } },
      apply = true,
    }
    conform.format { bufnr = args.buf, formatters = { fmts_by_ft['zig'] } }
  end,
  --fmts_by_ft[vim.bo[args.buf].filetype]
  zon = function(args) conform.format { bufnr = args.buf, formatters = { fmts_by_ft['zig'] } } end,
}

This is frustrating.

@stevearc
Copy link
Owner

Running one or multiple code actions to autofix code and then the formatter (because formatter might break code for the autofix) prevents trim_whitespace and trim_newlines from being usable

How can formatting the code break it for autofix? And how can performing an autofix code action prevent the trim_whitespace and trim_newlines from working?

@stevearc stevearc added the question Further information is requested label Nov 27, 2024
@matu3ba
Copy link
Author

matu3ba commented Nov 28, 2024

How can formatting the code break it for autofix? And how can performing an autofix code action prevent the trim_whitespace and trim_newlines from working?

Sorry, the cause of the issue was eventually something else:
I eventually figured that the existence of a loaded editorconfig can do leading to line fixup at EOF and truncating empty space. After having consistent insert_final_newline = true the weird behavior was gone.
Not really sure what was the exact reason though.

See :Telescope autocommands -> BufWritePre -> editorconfig

  • function properties.trim_trailing_whitespace(bufnr, val) ~/.local/nvim/share/nvim/runtime/lua/editorconfig.lua
  • function properties.insert_final_newline(bufnr, val) ~/.local/nvim/share/nvim/runtime/lua/editorconfig.lua

Possible fixes:

@github-actions github-actions bot removed the question Further information is requested label Nov 28, 2024
@matu3ba matu3ba changed the title README/use case: describe how to prevent ['_'] = { 'trim_whitespace', 'trim_newlines' }, from interefering with externally managed formatting and how to do code actions before formatting README/use case: describe how to make ['_'] = { 'trim_whitespace', 'trim_newlines' }, play nice with editorconfig autocommands Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants