mirror of
https://github.com/Myzel394/easytables.nvim.git
synced 2025-06-18 23:05:27 +02:00
fix: Fix table insert
This commit is contained in:
parent
81cb10545b
commit
e2ebb2115d
@ -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"
|
||||||
|
@ -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,
|
||||||
{}
|
{}
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user