Skip to content

Comments

Export handle_write_pre function#5

Open
chancez wants to merge 1 commit intofnune:mainfrom
chancez:pr/chancez/export_handle_write_pre
Open

Export handle_write_pre function#5
chancez wants to merge 1 commit intofnune:mainfrom
chancez:pr/chancez/export_handle_write_pre

Conversation

@chancez
Copy link

@chancez chancez commented Jan 8, 2026

Closes #4

Example of my usage:

local function LspOrgImports()
  local cos = require("codeactions-on-save")
  cos.handle_write_pre({ "source.organizeImports" }, 0, 1000)
end

local function LspFixAll()
  local cos = require("codeactions-on-save")
  cos.handle_write_pre({ "source.fixAll" }, 0, 1000)
end


local function lspAttach(bufnr, client)
 ...
   if client.server_capabilities.documentFormattingProvider then
    vim.api.nvim_buf_create_user_command(bufnr, 'LspFormat', function() vim.lsp.buf.format() end, {})
    vim.api.nvim_buf_create_user_command(bufnr, 'LspOrgImports', function() LspOrgImports() end, {})
    vim.api.nvim_buf_create_user_command(bufnr, 'LspToggleAutoFormat', function() LspToggleAutoFormat() end, {})
    vim.api.nvim_buf_create_user_command(bufnr, 'LspToggleAutoFormatBuffer', function() LspToggleAutoFormat() end, {})

    vim.api.nvim_create_augroup('CodeFormat', { clear = false })
    vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
      group = 'CodeFormat',
      buffer = bufnr,
      desc = 'Format code on save',
      callback = function()
        -- Check if autoformatting is enabled
        if not IsLspAutoFormatEnabled() then
          return
        end
        LspOrgImports()
        vim.lsp.buf.format()
      end,
    })
  end


end

Signed-off-by: Chance Zibolski <chance.zibolski@gmail.com>
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.

Expose the handle_write_pre function

1 participant