fix: Fix table insert

This commit is contained in:
Myzel394 2023-10-08 15:09:39 +02:00
parent 81cb10545b
commit e2ebb2115d
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
2 changed files with 48 additions and 22 deletions

View File

@ -2,6 +2,7 @@ local table = require("easytables.table")
local window = require("easytables.window") local window = require("easytables.window")
local inputHelper = require("easytables.input") local inputHelper = require("easytables.input")
local o = require("easytables.options") local o = require("easytables.options")
local export = require("easytables.export")
local import = require("easytables.import") local import = require("easytables.import")
---Initialize `easytables` with the given options. This function **must** be called. ---Initialize `easytables` with the given options. This function **must** be called.
@ -22,13 +23,33 @@ local function setup(options)
return return
end end
-- tuple do not seem to be working with pcall -- tuples do not seem to be working with pcall
local cols = result[1] local cols = result[1]
local rows = result[2] local rows = result[2]
local markdown_table = table:create(cols, rows) 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:show()
win:register_listeners() win:register_listeners()
@ -62,15 +83,23 @@ local function setup(options)
local markdown_table = table:import(raw_table) 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:show()
win:register_listeners() win:register_listeners()
win:draw_table() win:draw_table()
-- Remove old table
vim.api.nvim_buf_set_lines(buffer, start_row - 1, end_row, false, {})
end, end,
{ {
desc = "Import the current markdown table at the cursor's position into EasyTables" desc = "Import the current markdown table at the cursor's position into EasyTables"

View File

@ -1,5 +1,4 @@
local table_builder = require("easytables.tablebuilder") local table_builder = require("easytables.tablebuilder")
local export = require("easytables.export")
local o = require("easytables.options") local o = require("easytables.options")
local math = require("math") local math = require("math")
@ -30,12 +29,22 @@ local function get_window_size(cols, rows)
return _get_auto_size(cols, rows) return _get_auto_size(cols, rows)
end 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) function M:create(table, options)
options = options or {} options = options or {}
self.table = table self.table = table
self.previous_window = vim.api.nvim_get_current_win() self.previous_window = vim.api.nvim_get_current_win()
self.on_export = options.on_export or DEFAULT_OPTIONS.on_export
return self return self
end end
@ -525,22 +534,10 @@ function M:register_listeners()
self.prompt_buffer, self.prompt_buffer,
"ExportTable", "ExportTable",
function() function()
local markdown_table = export:export_table(self.table)
self:close() self:close()
vim.schedule(function() print("export")
local cursor = vim.api.nvim_win_get_cursor(0) self.on_export()
vim.api.nvim_buf_set_text(
0,
cursor[1] - 1,
cursor[2],
cursor[1] - 1,
cursor[2],
markdown_table
)
end)
end, end,
{} {}
) )