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
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AstroCore provides the core Lua API that powers [AstroNvim](https://github.com/A

## ⚡️ Requirements

- Neovim >= 0.10
- Neovim >= 0.11
- [lazy.nvim](https://github.com/folke/lazy.nvim)
- [resession.nvim][resession] (_optional_)

Expand Down Expand Up @@ -197,6 +197,54 @@ local opts = {
buftypes = {}, -- buffer types to ignore sessions
},
},
-- Configuration of treesitter features in Neovim
treesitter = {
-- Globally enable or disable treesitter features
-- can be a boolean or a function (`fun(lang: string, bufnr: integer): boolean`)
enabled = true,
-- Enable or disable treesitter based highlighting
-- can be a boolean, list of parsers, or a function (`fun(lang: string, bufnr: integer): boolean`)
highlight = true,
-- Enable or disable treesitter based indenting
-- can be a boolean, list of parsers, or a function (`fun(lang: string, bufnr: integer): boolean`)
indent = true,
-- List of treesitter parsers that should be installed automatically
-- ("all" can be used to install all available parsers)
ensure_installed = { "lua", "vim", "vimdoc" },
-- Automatically detect missing treesitter parser and install when editing file
auto_install = false,
-- Configure treesitter based text objects
textobjects = {
select = {
select_textobject = {
["af"] = { query = "@function.outer", desc = "around function" },
["if"] = { query = "@function.inner", desc = "around function" },
},
},
move = {
goto_next_start = {
["]f"] = { query = "@function.outer", desc = "Next function start" },
},
goto_next_end = {
["]F"] = { query = "@function.outer", desc = "Next function end" },
},
goto_previous_start = {
["[f"] = { query = "@function.outer", desc = "Previous function start" },
},
goto_previous_end = {
["[F"] = { query = "@function.outer", desc = "Previous function end" },
},
},
swap = {
swap_next = {
[">F"] = { query = "@function.outer", desc = "Swap next function" },
},
swap_previous = {
["<F"] = { query = "@function.outer", desc = "Swap previous function" },
},
},
},
},
}
```

Expand Down
127 changes: 124 additions & 3 deletions lua/astrocore/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@
---@field filetypes string[]? filetypes to ignore
---@field buftypes string[]? buffer types to ignore

-- TODO: remove note about version after dropping support for Neovim v0.10

---@class AstroCoreDiagnosticsFeature
---@field virtual_text boolean? show virtual text on startup
---@field virtual_lines boolean? show virtual lines on startup (Neovim v0.11+ only)
---@field virtual_lines boolean? show virtual lines on startup
---
---@class AstroCoreSessionOpts
---Session autosaving options
Expand All @@ -83,6 +81,111 @@
---```
---@field ignore AstroCoreSessionIgnore?

---@alias AstroCoreTreesitterEnable boolean|(fun(lang: string, bufnr: integer): (boolean|nil))

---@alias AstroCoreTreesitterFeature string[]|AstroCoreTreesitterEnable

---@class AstroCoreTreesitterTextObjectsKey
---@field query string The textobject query capture group to perform against
---@field group string? The textobject query group to capture from (default: "textobjects")
---@field desc string The description of the keymap

---@alias AstroCoreTreesitterTextObjectsKeys table<string, AstroCoreTreesitterTextObjectsKey>

---@class AstroCoreTreesitterTextObjectsSelectOpts
---@field select_textobject AstroCoreTreesitterTextObjectsKeys? Keymaps for selecting a given treesitter capture group

---@class AstroCoreTreesitterTextObjectsMoveOpts
---@field goto_next_start AstroCoreTreesitterTextObjectsKeys? Keymaps for going to the start of the next treesitter capture group
---@field goto_next_end AstroCoreTreesitterTextObjectsKeys? Keymaps for going to the end of the next treesitter capture group
---@field goto_previous_start AstroCoreTreesitterTextObjectsKeys? Keymaps for going to the start of the previous treesitter capture group
---@field goto_previous_end AstroCoreTreesitterTextObjectsKeys? Keymaps for going to the end of the previous treesitter capture group

---@class AstroCoreTreesitterTextObjectsSwapOpts
---@field swap_next AstroCoreTreesitterTextObjectsKeys? Keymaps for swapping with the next treesitter capture group
---@field swap_previous AstroCoreTreesitterTextObjectsKeys? Keymaps for swapping with the previous treesitter capture group

---@class AstroCoreTreesitterTextObjects
---@field select AstroCoreTreesitterTextObjectsSelectOpts? Keymaps for selection of treesitter capture groups
---@field move AstroCoreTreesitterTextObjectsMoveOpts? Keymaps for moving treesitter capture groups
---@field swap AstroCoreTreesitterTextObjectsSwapOpts? Keymaps for swapping treesitter capture groups

---@class AstroCoreTreesitterOpts
---@field enabled AstroCoreTreesitterEnable? Control over the global enabling of treesitter features
---Whether or not to enable treesitter based highlighting. Can be one of the following:
---
--- - A boolean to apply to all languages
--- - A list of languages to enable
--- - A function that takes a language and a buffer number and returns a boolean
---Examples:
---
---```lua
---highlight = true -- enables for all languages
---highlight = { "c", "rust" } -- only enables for some languages
---highlight = function(lang, bufnr) -- use a function to decide, for example setting a max filesize
--- local max_filesize = 100 * 1024 -- 100KB
--- local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(bufnr))
--- if ok and stats and stats.size > max_filesize then return true
---end
---```
---@field highlight AstroCoreTreesitterFeature?
---Whether or not to enable treesitter based indentation. Can be one of the following:
---
--- - A boolean to apply to all languages
--- - A list of languages to enable
--- - A function that takes a language and a buffer number and returns a boolean
---Examples:
---
---```lua
---indent = true -- enables for all languages
---indent = { "c", "rust" } -- only enables for some languages
---indent = function(lang, bufnr) -- use a function to decide, for example setting a max filesize
--- local max_filesize = 100 * 1024 -- 100KB
--- local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(bufnr))
--- if ok and stats and stats.size > max_filesize then return true
---end
---```
---@field indent AstroCoreTreesitterFeature?
---@field auto_install boolean? whether or not to automatically detect and install missing treesitter parsers
---@field ensure_installed string[]|"all"? a list of treesitter parsers to ensure are installed, "all" will install all parsers, "auto" will install when opening a filetype with an available parser
---Configuration of textobject mappings to create using `nvim-treesitter-textobjects`
---
---Examples:
---
---```lua
---textobjects = {
--- select = {
--- select_textobject = {
--- ["af"] = { query = "@function.outer", desc = "around function" },
--- ["if"] = { query = "@function.inner", desc = "around function" },
--- },
--- },
--- move = {
--- goto_next_start = {
--- ["]f"] = { query = "@function.outer", desc = "Next function start" },
--- },
--- goto_next_end = {
--- ["]F"] = { query = "@function.outer", desc = "Next function end" },
--- },
--- goto_previous_start = {
--- ["[f"] = { query = "@function.outer", desc = "Previous function start" },
--- },
--- goto_previous_end = {
--- ["[F"] = { query = "@function.outer", desc = "Previous function end" },
--- },
--- },
--- swap = {
--- swap_next = {
--- [">F"] = { query = "@function.outer", desc = "Swap next function" },
--- },
--- swap_previous = {
--- ["<F"] = { query = "@function.outer", desc = "Swap previous function" },
--- },
--- },
---}
---```
---@field textobjects AstroCoreTreesitterTextObjects?

---@class AstroCoreFeatureOpts
---@field autopairs boolean? enable or disable autopairs on start (boolean; default = true)
---@field cmp boolean? enable or disable cmp on start (boolean; default = true)
Expand Down Expand Up @@ -301,6 +404,17 @@
---}
---```
---@field sessions AstroCoreSessionOpts?
---Configuration table of treesitter options for AstroNvim
---Example:
--
---```lua
---treesitter = {
--- highlight = true,
--- indent = true,
--- ensure_installed = { "lua", "vim", "vimdoc" }
---}
---```
---@field treesitter AstroCoreTreesitterOpts?

---@type AstroCoreOpts
local M = {
Expand Down Expand Up @@ -330,6 +444,13 @@ local M = {
buftypes = {},
},
},
treesitter = {
enabled = true,
highlight = true,
indent = true,
ensure_installed = {},
textobjects = nil,
},
}

return M
2 changes: 2 additions & 0 deletions lua/astrocore/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ function M.setup(opts)
end
end

if vim.tbl_get(M.config, "treesitter") then require("astrocore.treesitter").setup(M.config.treesitter) end

local astroui_avail, astroui = pcall(require, "astroui")
if astroui_avail then astroui.set_colorscheme() end
end
Expand Down
8 changes: 3 additions & 5 deletions lua/astrocore/toggles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ end
function M.buffer_syntax(bufnr, silent)
-- HACK: this should just be `bufnr = bufnr or 0` but it looks like `vim.treesitter.stop` has a bug with `0` being current
bufnr = (bufnr and bufnr ~= 0) and bufnr or vim.api.nvim_win_get_buf(0)
local ts_avail, parsers = pcall(require, "nvim-treesitter.parsers")
local treesitter = require "astrocore.treesitter"
local astrolsp_avail, lsp_toggle = pcall(require, "astrolsp.toggles")
if vim.bo[bufnr].syntax == "off" then
if ts_avail and parsers.has_parser() then vim.treesitter.start(bufnr) end
if treesitter.has_parser() then vim.treesitter.start(bufnr) end
vim.bo[bufnr].syntax = "on"
if astrolsp_avail and not vim.b[bufnr].semantic_tokens then lsp_toggle.buffer_semantic_tokens(bufnr, true) end
else
if ts_avail and parsers.has_parser() then vim.treesitter.stop(bufnr) end
if treesitter.has_parser() then vim.treesitter.stop(bufnr) end
vim.bo[bufnr].syntax = "off"
if astrolsp_avail and vim.b[bufnr].semantic_tokens then lsp_toggle.buffer_semantic_tokens(bufnr, true) end
end
Expand Down Expand Up @@ -245,8 +245,6 @@ local previous_virtual_lines
---@param silent? boolean if true then don't sent a notification
function M.virtual_lines(silent)
local virtual_lines = vim.diagnostic.config().virtual_lines
-- TODO: remove check when dropping support for Neovim v0.10
if virtual_lines == nil then ui_notify(silent, "Virtual lines not available") end
local new_virtual_lines = false
if virtual_lines then
previous_virtual_lines = virtual_lines
Expand Down
Loading