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

[Question] How to prevent keeping history of changes (undo list) made by TSTools* commands? #309

Open
Al3bad opened this issue Nov 16, 2024 · 0 comments

Comments

@Al3bad
Copy link

Al3bad commented Nov 16, 2024

Currently I have a setup to format and run TSToolsSortImports command when saving the buffer but the changes made by TSToolsSortImports are added in the undo list. Is it possible to prevent that?

The command gets executed with vim.cmd api:

vim.cmd 'TSToolsSortImports'

I've tried with the sync argument and using undojoin but nothing worked.

this is the current setup I have:

return {
  'stevearc/conform.nvim',
  opts = {
    notify_on_error = true,
    format_on_save = function(bufnr)
      if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
        return
      end
      local lsp_format_opt
      local disable_filetypes = { c = true, cpp = true }
      if disable_filetypes[vim.bo[bufnr].filetype] then
        lsp_format_opt = 'never'
      else
        lsp_format_opt = 'fallback'
      end

      -- Javascript/Typescript: Sort imports
      for _, value in ipairs { 'javascript', 'typescript', 'javascripteract', 'typescriptreact' } do
        if vim.bo[bufnr].filetype == value then
          vim.cmd 'TSToolsSortImports'

          -- attempt 1
          -- vim.cmd.undojoin()
          -- vim.cmd 'TSToolsSortImports'

          -- attempt 2
          -- vim.cmd.undojoin()
          -- vim.cmd 'TSToolsSortImports sync'
        end
      end

      return { timeout_ms = 500, lsp_format = lsp_format_opt }
    end,
    formatters_by_ft = {
      lua = { 'stylua' },
      yaml = { 'prettierd' },
      html = { 'prettierd' },
      css = { 'prettierd' },
      javascript = { 'prettierd' },
      typescript = { 'prettierd' },
      javascriptreact = { 'prettierd' },
      typescriptreact = { 'prettierd' },
    },
  },
}
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

1 participant