dhruvasagar / vim-table-mode

VIM Table Mode for instant table creation.
2.11k stars 96 forks source link

[Bug] Automatic table realignment stopped working #234

Closed pidgeon777 closed 8 months ago

pidgeon777 commented 8 months ago

This is my original table:

| Name | Type | Description |
|------|------|-------------|
|      |      |             |

Then I add some text:

| Name | Type | Description |
|------|------|-------------|
|  AAAAAAAAAAAAA    |      |             |

Then I place cursor on the table and execute TableModeRealign.

I obtain:

| Name          | Type   | Description   |
| ------        | ------ | ------------- |
| AAAAAAAAAAAAA |        |               |

So, it seems that:

I would have expected this output:

| Name          | Type   | Description   |
|---------------|--------|---------------|
| AAAAAAAAAAAAA |        |               |

and this the output I always had. So, it seems that, maybe, a vim-table-mode plugin update broke something? I'm sure it worked some weeks ago.

I did this test on a buffer with filetype set to:

dhruvasagar commented 8 months ago

@pidgeon777 Kindly share your vim-table-mode settings, from the looks of it you're not using the defaults.

pidgeon777 commented 8 months ago

Hello @dhruvasagar, thanks for the prompt reply.

This is my config:

return {
  {
    "dhruvasagar/vim-table-mode",
    cmd = {
      "TableEvalFormulaLine",
      "TableAddFormula",
      "TableSort",
      "TableModeRealign",
      "Tableize",
    },
    keys = {
      {
        "<C-t>e",
        "<CMD>TableEvalFormulaLine<CR>",
        desc = "Eval Formula Line",
      },
      {
        "<C-t>f",
        "<CMD>TableAddFormula<CR>",
        desc = "Add Formula",
      },
      {
        "<C-t>s",
        "<CMD>TableSort<CR>",
        desc = "Sort",
      },
      {
        "<C-t>t",
        "<CMD>TableModeRealign<CR>",
        desc = "Realign",
      },
      {
        "<C-t>z",
        "<CMD>Tableize<CR>",
        desc = "Tableize",
      },
    },
    config = function(_, opts)
      require("lazyvim.util").on_load("which-key.nvim", function()
        require("which-key").register({
          mode = { "n" },
          ["<C-t>"] = {
            name = "Table",
          },
        })
      end)
    end,
    enabled = lvim.builtin.vim_table_mode.active,
  },
}

I'm using vim-table-mode plugin with the LazyVim distribution.

Also, I noticed a strange behaviour. When doing this:

  1. Start Neovim.
  2. Open a markdown document.
  3. Try to realign a table ➡️ Table is not re-aligned correctly.
  4. Reload the markdown document with :e.
  5. Try to realign a table ➡️ Table is correctly realigned.
dhruvasagar commented 8 months ago

@pidgeon777 to me that sounds like something to do with the lazy loading behavior. Are you using the custom keys set for lazy to load the plugin for realigning or are you using something else (like they keybindings shipped with vim-table-mode_ ?

pidgeon777 commented 8 months ago

I used the TableModeRealign directly, placing the cursor on the first element of the table.

Anyway, I tried this new config:

return {
  {
    "dhruvasagar/vim-table-mode",
    event = "VeryLazy",
    keys = {
      {
        "<C-t>e",
        "<CMD>TableEvalFormulaLine<CR>",
        desc = "Eval Formula Line",
      },
      {
        "<C-t>f",
        "<CMD>TableAddFormula<CR>",
        desc = "Add Formula",
      },
      {
        "<C-t>s",
        "<CMD>TableSort<CR>",
        desc = "Sort",
      },
      {
        "<C-t>t",
        "<CMD>TableModeRealign<CR>",
        desc = "Realign",
      },
      {
        "<C-t>z",
        "<CMD>Tableize<CR>",
        desc = "Tableize",
      },
    },
    config = function(_, opts)
      require("lazyvim.util").on_load("which-key.nvim", function()
        require("which-key").register({
          mode = { "n" },
          ["<C-t>"] = {
            name = "Table",
          },
        })
      end)
    end,
    enabled = lvim.builtin.vim_table_mode.active,
  },
}

and it seems to work, without having to reload the markdown first.

Main difference is that I removed the cmd section, and I added the event one set to VeryLazy value.

But by doing so, now the plugin is always loaded at Neovim startup.

I wonder if there is a way to lazy load the plugin only when one of the vim-table-mode commands are executed, so that the realign works without having to reload the markdown file first?

dhruvasagar commented 8 months ago

@pidgeon777 You should know that almost the entire plugin's code is autoloaded, which means, it only gets loaded once you actually use it. The only thing the plugin loads are the keybindings and commands, but they are mapped to autoloaded functions that only load when you use them the first time. Not only that, but different functionality are all autoloaded, for instance if you never use formulas, that code will never get loaded. This is better than the forced lazy loading that lazy.nvim provides and I would think only adds value for badly written plugins.

pidgeon777 commented 8 months ago

After some tests I was able to lazily load the plugin, with no issues (such as the one I initially reported).

This is the working config:

return {
  {
    "dhruvasagar/vim-table-mode",
    cmd = {
      "TableAddFormula",
      "TableEvalFormulaLine",
      "TableModeDisable",
      "TableModeEnable",
      "TableModeRealign",
      "TableModeToggle",
      "TableSort",
      "Tableize",
    },
    keys = {
      {
        "<C-t>f",
        "<CMD>TableAddFormula<CR>",
        desc = "Add Formula",
      },
      {
        "<C-t>e",
        "<CMD>TableEvalFormulaLine<CR>",
        desc = "Eval Formula Line",
      },
      {
        "<C-t>r",
        "<CMD>TableModeRealign<CR>",
        desc = "Realign",
      },
      {
        "<C-t>t",
        "<CMD>TableModeToggle<CR>",
        desc = "Toggle",
      },
      {
        "<C-t>s",
        "<CMD>TableSort<CR>",
        desc = "Sort",
      },
      {
        "<C-t>z",
        "<CMD>Tableize<CR>",
        desc = "Tableize",
      },
    },
    init = function()
      vim.g.table_mode_corner = "|"
      vim.g.table_mode_corner_corner = "|"
      vim.g.table_mode_header_fillchar = "-"
    end,
    config = function(_, opts)
      require("lazyvim.util").on_load("which-key.nvim", function()
        require("which-key").register({
          mode = { "n" },
          ["<C-t>"] = {
            name = "Table",
          },
        })
      end)
    end,
    enabled = lvim.builtin.vim_table_mode.active,
  },
}

The most important section is the init one, which helped solve the problem.