Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(table): expose TsTable and reorganize table utility functions #632

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lua/orgmode/org/format.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand Down
20 changes: 19 additions & 1 deletion lua/orgmode/org/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this under OrgMappings as a private method (prefixed with _) and call it accordingly.

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('<Down>'), 'n', true)
vim.schedule(function()
vim.cmd([[norm! F|]])
vim.api.nvim_feedkeys(utils.esc('<Right><Right>'), '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
Expand Down
34 changes: 1 addition & 33 deletions lua/orgmode/treesitter/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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('<Down>'), 'n', true)
vim.schedule(function()
vim.cmd([[norm! F|]])
vim.api.nvim_feedkeys(utils.esc('<Right><Right>'), 'n', true)
end)
return true
end

return {
format = format,
handle_cr = handle_cr,
}
return TsTable