Table Picker

Table Picker

Ctrl-Shift-t here we go

Realization

one has to first def a global func getTables()

function getTables()
  local rows = query[[
    from index.tag "table"
    select {
      ref      = _.ref,
      tableref = _.tableref,
      page     = _.page,
      pos      = _.pos,
    }
    order by _.page, _.pos
  ]]

  local out, seen = {}, {}
  for _, r in ipairs(rows) do
    local key = r.tableref
    if not seen[key] then
      seen[key] = true
      table.insert(out, r)
    end
  end
  return out
end

then it’s time to create the Table Picker CMD

local function navigateToTable(ref, pos)
  if ref then
    editor.navigate(ref)
    if pos then
      editor.moveCursor(tonumber(pos), true)
    end
    return true
  end
  return false
end

command.define {
  name = "Navigate: Table Picker",
  key = "Ctrl-Shift-t",
  priority = 1,
  run = function()
    local tables = getTables()
    if not tables or #tables == 0 then
      editor.flashNotification("No tables found.")
      return
    end

    local items = {}
    for _, r in ipairs(tables) do
      table.insert(items, {
        name = string.format("%s @ %d", r.page, r.pos),
        -- description = string.format("%s @ %d", r.page, r.pos),
        ref = r.ref,
        page = r.page,
        pos = r.pos
      })
    end

    local sel = editor.filterBox("Jump to", items, "Select a table", "Page @ Pos where the Table locates")
    if not sel then return end

    if not navigateToTable(sel.ref, sel.pos) then
      editor.flashNotification("Failed to navigate to selected table.")
    end
  end
}
3 Likes

i like this :+1:
It’s a nice way to jump to tables inside different pages.
I dunno yet if I have a utility for it, but it works as advertised.
nice idea!

1 Like

cleaned up

command.define {
  name = "Navigate: Table Picker",
  key = "Ctrl-Shift-t",
  priority = 1,
  run = function()
    local tables = getTables()
    if not tables or #tables == 0 then
      editor.flashNotification("No tables found.")
      return
    end

    local sel = editor.filterBox("Jump to", tables, "Select a Table...", "Page @ Pos where the Table locates")
    if not sel then return end
    editor.navigate(sel.ref)
    editor.invokeCommand("Navigate: Center Cursor")
  end
}

function getTables()
  local rows = query[[
    from index.tag "table"
    select {
      name = string.format("%s @ %d", _.page, _.pos),
      ref      = _.ref,
      tableref = _.tableref,
    }
    order by _.page, _.pos
  ]]

  local out, seen = {}, {}
  for _, r in ipairs(rows) do
    local key = r.tableref
    if not seen[key] then
      seen[key] = true
      table.insert(out, r)
    end
  end
  return out
end