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

Conversation

joaomsa
Copy link
Contributor

@joaomsa joaomsa commented Nov 25, 2023

I decided to make this small refactor mainly for my own benefit. I wanted to utilize the treesitter TsTable helper in order to create a command for transposing tables (similar to org-table-transpose-table-at-point in Emacs). However, it was challenging to implement it because the TsTable object was not accessible from that module.

I imagine this litle helper may not be widely applicable enough to be included in the mainline code. Nevertheless, I'd love if more parts of the API exposed similar "from_current_node" or "at_cursor" functionality to facilitate ad-hoc scripting (even without committing to a stable API).

local function transpose(data)
  local data_t = {}
  for r, row in ipairs(data) do
    for c, cell in ipairs(row) do
      data_t[c] = data_t[c] or {}
      data_t[c][r] = cell
    end
  end
  return data_t
end

local function org_table_transpose()
  local ts_table = require('orgmode.treesitter.table')
  local Table = require('orgmode.parser.table')

  local ts_tbl = ts_table.from_current_node()
  if not ts_tbl then
    return
  end

  local data = vim.tbl_filter(function(row)
    return row ~= 'hr'
  end, ts_tbl.data)

  local data_t = transpose(data)
  local tbl_t = Table.from_list(data_t,
    ts_tbl.tbl.start_row,
    ts_tbl.tbl.start_col)

  local view = vim.fn.winsaveview()
  vim.api.nvim_buf_set_lines(0,
    ts_tbl.tbl.range.start_line - 1,
    ts_tbl.tbl.range.end_line,
    false, tbl_t:draw())
  vim.fn.winrestview(view)
end

vim.api.nvim_create_user_command("OrgTableTranspose", org_table_transpose, {})

Copy link
Member

@kristijanhusak kristijanhusak left a comment

Choose a reason for hiding this comment

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

This is ok, even though I would not suggest using internals.
Let's just do a small change in the mappings and it's good to go.

@@ -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.

@kristijanhusak
Copy link
Member

You can now access the current table like this:

local tbl = require('orgmode.files.elements.table').from_current_node()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants