diff --git a/lua/easytables/init.lua b/lua/easytables/init.lua index 08d5b05..ddb24fe 100644 --- a/lua/easytables/init.lua +++ b/lua/easytables/init.lua @@ -2,6 +2,7 @@ local table = require("easytables.table") local window = require("easytables.window") local inputHelper = require("easytables.input") local o = require("easytables.options") +local export = require("easytables.export") local import = require("easytables.import") ---Initialize `easytables` with the given options. This function **must** be called. @@ -22,13 +23,33 @@ local function setup(options) return end - -- tuple do not seem to be working with pcall + -- tuples do not seem to be working with pcall local cols = result[1] local rows = result[2] local markdown_table = table:create(cols, rows) - local win = window:create(markdown_table) + local win = window:create( + markdown_table, + { + on_export = function() + local new_table = export:export_table(markdown_table) + + vim.schedule(function() + local cursor = vim.api.nvim_win_get_cursor(0) + + vim.api.nvim_buf_set_text( + 0, + cursor[1] - 1, + cursor[2], + cursor[1] - 1, + cursor[2], + new_table + ) + end) + end + } + ) win:show() win:register_listeners() @@ -62,15 +83,23 @@ local function setup(options) local markdown_table = table:import(raw_table) - local win = window:create(markdown_table) + local win = window:create( + markdown_table, + { + on_export = function() + local new_table = export:export_table(markdown_table) + + vim.schedule(function() + -- Remove old table + vim.api.nvim_buf_set_lines(buffer, start_row, end_row, false, new_table) + end) + end + } + ) win:show() win:register_listeners() win:draw_table() - - - -- Remove old table - vim.api.nvim_buf_set_lines(buffer, start_row - 1, end_row, false, {}) end, { desc = "Import the current markdown table at the cursor's position into EasyTables" diff --git a/lua/easytables/window.lua b/lua/easytables/window.lua index 6545ba0..970b8ea 100644 --- a/lua/easytables/window.lua +++ b/lua/easytables/window.lua @@ -1,5 +1,4 @@ local table_builder = require("easytables.tablebuilder") -local export = require("easytables.export") local o = require("easytables.options") local math = require("math") @@ -30,12 +29,22 @@ local function get_window_size(cols, rows) return _get_auto_size(cols, rows) end +-- Just used for autocompletion +local DEFAULT_OPTIONS = { + on_export = function() end, +} + +---Create a new window for the given table +---@param table table +---@param options table function M:create(table, options) options = options or {} self.table = table self.previous_window = vim.api.nvim_get_current_win() + self.on_export = options.on_export or DEFAULT_OPTIONS.on_export + return self end @@ -525,22 +534,10 @@ function M:register_listeners() self.prompt_buffer, "ExportTable", function() - local markdown_table = export:export_table(self.table) - self:close() - vim.schedule(function() - local cursor = vim.api.nvim_win_get_cursor(0) - - vim.api.nvim_buf_set_text( - 0, - cursor[1] - 1, - cursor[2], - cursor[1] - 1, - cursor[2], - markdown_table - ) - end) + print("export") + self.on_export() end, {} )