From 0ee9c7737e6a238920aed17bdde3d8b3f983332c Mon Sep 17 00:00:00 2001 From: Joao Sa Date: Sat, 25 Nov 2023 01:41:57 +0100 Subject: [PATCH] refactor(table): expose ts_table and reorganize table utility functions --- lua/orgmode/org/format.lua | 6 ++++-- lua/orgmode/org/mappings.lua | 20 ++++++++++++++++++- lua/orgmode/treesitter/table.lua | 34 +------------------------------- 3 files changed, 24 insertions(+), 36 deletions(-) diff --git a/lua/orgmode/org/format.lua b/lua/orgmode/org/format.lua index d12e08ed8..d03aacb9f 100644 --- a/lua/orgmode/org/format.lua +++ b/lua/orgmode/org/format.lua @@ -1,5 +1,5 @@ local Files = require('orgmode.parser.files') -local Table = require('orgmode.treesitter.table') +local ts_table = require('orgmode.treesitter.table') local ts_org = require('orgmode.treesitter') local function format_line(linenr) @@ -13,7 +13,9 @@ local function format_line(linenr) end end - if Table.format(linenr) then + local tbl = ts_table.from_current_node(linenr) + if tbl then + tbl:reformat() return true end diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index 9402b7811..3ac8b141a 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -502,9 +502,27 @@ function OrgMappings:do_demote(whole_subtree) EventManager.dispatch(events.HeadlineDemoted:new(Files.get_closest_headline(), ts_org.closest_headline(), old_level)) end +local function table_handle_cr() + if vim.fn.col('.') == vim.fn.col('$') then + return false + end + local tbl = ts_table.from_current_node() + if not tbl then + return false + end + + tbl:add_row() + vim.api.nvim_feedkeys(utils.esc(''), 'n', true) + vim.schedule(function() + vim.cmd([[norm! F|]]) + vim.api.nvim_feedkeys(utils.esc(''), 'n', true) + end) + return true +end + function OrgMappings:org_return() local actions = { - ts_table.handle_cr, + table_handle_cr, } for _, action in ipairs(actions) do diff --git a/lua/orgmode/treesitter/table.lua b/lua/orgmode/treesitter/table.lua index f81060289..0011937ae 100644 --- a/lua/orgmode/treesitter/table.lua +++ b/lua/orgmode/treesitter/table.lua @@ -96,36 +96,4 @@ function TsTable:add_row() return TsTable.from_current_node():reformat() end -local function format(linenr) - local tbl = TsTable.from_current_node(linenr) - - if not tbl then - return false - end - - tbl:reformat() - return true -end - -local function handle_cr() - if vim.fn.col('.') == vim.fn.col('$') then - return false - end - local tbl = TsTable.from_current_node() - if not tbl then - return false - end - - tbl:add_row() - vim.api.nvim_feedkeys(utils.esc(''), 'n', true) - vim.schedule(function() - vim.cmd([[norm! F|]]) - vim.api.nvim_feedkeys(utils.esc(''), 'n', true) - end) - return true -end - -return { - format = format, - handle_cr = handle_cr, -} +return TsTable