Skip to content
Draft
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AstroUI provides a simple API for configuring and setting up the user interface

## ⚡️ Requirements

- Neovim >= 0.10
- Neovim >= 0.11
- [astrocore][astrocore]

## 📦 Installation
Expand Down
2 changes: 2 additions & 0 deletions lua/astroui/ffi.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- AstroUI C Extensions

-- TODO: Delete when dropping support for Neovim v0.11

local ffi = require "ffi"

-- Custom C extension to get direct fold information from Neovim
Expand Down
68 changes: 24 additions & 44 deletions lua/astroui/folding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ local config = require("astroui").config.folding

local is_setup = false
local lsp_bufs = {}
local ts_bufs = {}

local fold_methods = {
lsp = function(lnum, bufnr)
if lsp_bufs[bufnr or vim.api.nvim_get_current_buf()] then return vim.lsp.foldexpr(lnum) end
end,
treesitter = function(lnum, bufnr)
if ts_bufs[bufnr] == nil then
if not require("astrocore").is_available "nvim-treesitter" or package.loaded["nvim-treesitter"] then
ts_bufs[bufnr] = vim.bo.filetype and pcall(vim.treesitter.get_parser, bufnr)
end
local treesitter = require "astrocore.treesitter"
if treesitter.has_parser(bufnr, "folds") and treesitter.is_enabled(bufnr) then
return vim.treesitter.foldexpr(lnum)
end
if ts_bufs[bufnr] then return vim.treesitter.foldexpr(lnum) end
end,
indent = function(lnum, bufnr)
if not lnum then lnum = vim.v.lnum end
Expand Down Expand Up @@ -91,47 +88,30 @@ function M.info(bufnr)
require("astrocore").notify(table.concat(lines, "\n"), vim.log.levels.INFO, { title = "AstroNvim Folding" })
end

-- TODO: remove helper function when dropping support for Neovim v0.10

---@param client vim.lsp.Client
---@param method string
---@param bufnr? integer
local function supports_method(client, method, bufnr)
if vim.fn.has "nvim-0.11" == 1 then
return client:supports_method(method, bufnr)
else
---@diagnostic disable-next-line: param-type-mismatch
return client.supports_method(method, { bufnr = bufnr })
end
end

function M.setup()
vim.api.nvim_create_user_command("AstroFoldInfo", function() M.info() end, { desc = "Display folding information" })
-- TODO: remove check when dropping support for Neovim v0.10
if vim.lsp.foldexpr then
local augroup = vim.api.nvim_create_augroup("astroui_foldexpr", { clear = true })
vim.api.nvim_create_autocmd("LspAttach", {
desc = "Monitor attached LSP clients with fold providers",
group = augroup,
callback = function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
if supports_method(client, "textDocument/foldingRange", args.buf) then lsp_bufs[args.buf] = true end
end,
})
vim.api.nvim_create_autocmd("LspDetach", {
group = augroup,
desc = "Safely remove LSP folding providers when language servers detach",
callback = function(args)
if not vim.api.nvim_buf_is_valid(args.buf) then return end
for _, client in pairs(vim.lsp.get_clients { bufnr = args.buf }) do
if client.id ~= args.data.client_id and supports_method(client, "textDocument/foldingRange", args.buf) then
return
end
local augroup = vim.api.nvim_create_augroup("astroui_foldexpr", { clear = true })
vim.api.nvim_create_autocmd("LspAttach", {
desc = "Monitor attached LSP clients with fold providers",
group = augroup,
callback = function(args)
local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
if client:supports_method("textDocument/foldingRange", args.buf) then lsp_bufs[args.buf] = true end
end,
})
vim.api.nvim_create_autocmd("LspDetach", {
group = augroup,
desc = "Safely remove LSP folding providers when language servers detach",
callback = function(args)
if not vim.api.nvim_buf_is_valid(args.buf) then return end
for _, client in pairs(vim.lsp.get_clients { bufnr = args.buf }) do
if client.id ~= args.data.client_id and client:supports_method("textDocument/foldingRange", args.buf) then
return
end
lsp_bufs[args.buf] = nil
end,
})
end
end
lsp_bufs[args.buf] = nil
end,
})
is_setup = true
end

Expand Down
2 changes: 1 addition & 1 deletion lua/astroui/status/condition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ end
function M.treesitter_available(bufnr)
if type(bufnr) == "table" then bufnr = bufnr.bufnr end
if not bufnr then bufnr = vim.api.nvim_get_current_buf() end
return vim.treesitter.highlighter.active[bufnr] ~= nil
return require("astrocore.treesitter").is_enabled(bufnr)
end

--- A condition function if the foldcolumn is enabled
Expand Down
4 changes: 3 additions & 1 deletion lua/astroui/status/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,9 @@ return {
},
on_click = {
name = "heirline_lsp",
callback = function() vim.schedule(vim.cmd.LspInfo) end,
callback = function()
vim.schedule(function() vim.cmd.checkhealth "vim.lsp" end)
end,
},
},
virtual_env = {
Expand Down
4 changes: 3 additions & 1 deletion lua/astroui/status/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ end

--- A provider function for building a foldcolumn
---@param opts? AstroUIProviderFoldcolumnOpts provider options
---@return function # a custom foldcolumn function for the statuscolumn that doesn't show the nest levels
---@return function|string # a custom foldcolumn function for the statuscolumn that doesn't show the nest levels
-- @usage local heirline_component = { provider = require("astroui.status").provider.foldcolumn }
-- @see astroui.status.utils.stylize
function M.foldcolumn(opts)
opts = extend_tbl(vim.tbl_get(config, "providers", "foldcolumn"), opts)
if vim.fn.has "nvim-0.12" == 1 then return status_utils.stylize("%C", opts) end
-- TODO: Remove code when deprecating Neovim 0.11 support
local ffi = require "astroui.ffi" -- get AstroUI C extensions
local fillchars = vim.opt.fillchars:get()
local foldopen = fillchars.foldopen or get_icon "FoldOpened"
Expand Down