Skip to content

[mini.trailspace] How to setup automatic trim on write IF there are any whitespaces? #309

Answered by echasnovski
IceS2 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi!

There is no way to check any of that with 'mini.trailspace'. You can write custom functions for that:

local has_trailspace = function(buf_id)
  buf_id = buf_id or vim.api.nvim_get_current_buf()

  local lines = vim.api.nvim_buf_get_lines(buf_id, 0, -1, true)
  for _, l in ipairs(lines) do
    if l:find('%s$') then return true end
  end
  return false
end

local has_trail_lines = function(buf_id)
  buf_id = buf_id or vim.api.nvim_get_current_buf()

  local n_lines = vim.api.nvim_buf_line_count(buf_id)
  local last_nonblank = vim.api.nvim_buf_call(buf_id, function() return vim.fn.prevnonblank(n_lines) end)
  return last_nonblank < n_lines
end

Now you can add something like if not (has_t…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@IceS2
Comment options

Answer selected by IceS2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants