nvim-treesitter / nvim-treesitter-context

Show code context
MIT License
2.46k stars 196 forks source link

feat: Lua API to get the status of treesitter context #408

Open UtkarshVerma opened 7 months ago

UtkarshVerma commented 7 months ago

Currently, there's no Lua API for knowing whether the plugin is enabled. I have to get the upvalue for tsc.toggle to query this.

-- Get up value for {func}'s {name} variable.
---@generic T
---@param func fun(...):T
---@param name string
---@return unknown?
---@nodiscard
function M.get_upvalue(func, name)
  local i = 1

  while true do
    local n, v = debug.getupvalue(func, i)
    if n == nil then
      return nil
    end

    if n == name then
      return v
    end

    i = i + 1
  end
end

local function toggle_context()
          local util = require("util")
          local tsc = require("treesitter-context")
          tsc.toggle()

          if util.get_upvalue(tsc.toggle, "enabled") then
            util.log.info("Enabled treesitter context", "Option")
          else
            util.log.warn("Disabled treesitter context", "Option")
          end
end

Having a function like tsc.status() would be really helpful.

lewis6991 commented 3 months ago

Why do you need to know if the plugin is enabled?

UtkarshVerma commented 6 days ago

I wanted to show a UI message based on whether the context was enabled or not.