autos and treesitter text

This commit is contained in:
Dominic DiTaranto 2025-12-04 21:56:45 -05:00
parent f10466e063
commit e30125dfaf
3 changed files with 130 additions and 15 deletions

View file

@ -32,6 +32,7 @@ vim.diagnostic.config({
})
require("config.keymaps")
require("config.autocmds")
require("config.plugins.autosave")
require("config.plugins.barbar")
require("config.plugins.blink")
@ -48,21 +49,6 @@ require("config.plugins.treesitter")
require("config.plugins.venv-selector")
require("config.plugins.markdown-renderer")
-- Highlight On Yank
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Highlight when yanking (copying) text",
group = vim.api.nvim_create_augroup("kickstart-highlight-yank", { clear = true }),
callback = function()
vim.hl.on_yank()
end,
})
-- python specific
vim.api.nvim_create_autocmd("FileType", {
pattern = "python",
command = "setlocal shiftwidth=4 tabstop=4"
})
local enable_providers = {
"python3_provider",
}

82
lua/config/autocmds.lua Normal file
View file

@ -0,0 +1,82 @@
-- Highlight On Yank
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Highlight when yanking (copying) text",
group = vim.api.nvim_create_augroup("kickstart-highlight-yank", { clear = true }),
callback = function()
vim.hl.on_yank()
end,
})
-- python specific
vim.api.nvim_create_autocmd("FileType", {
pattern = "python",
command = "setlocal shiftwidth=4 tabstop=4"
})
-- restore cursor to file position in previous editing session
vim.api.nvim_create_autocmd("BufReadPost", {
callback = function(args)
local mark = vim.api.nvim_buf_get_mark(args.buf, '"')
local line_count = vim.api.nvim_buf_line_count(args.buf)
if mark[1] > 0 and mark[1] <= line_count then
vim.api.nvim_win_set_cursor(0, mark)
-- defer centering slightly so it's applied after render
vim.schedule(function()
vim.cmd("normal! zz")
end)
end
end,
})
-- open help in vertical split
vim.api.nvim_create_autocmd("FileType", {
pattern = "help",
command = "wincmd L",
})
-- auto resize splits when the terminal's window is resized
vim.api.nvim_create_autocmd("VimResized", {
command = "wincmd =",
})
-- no auto continue comments on new line
vim.api.nvim_create_autocmd("FileType", {
group = vim.api.nvim_create_augroup("no_auto_comment", {}),
callback = function()
vim.opt_local.formatoptions:remove({ "c", "r", "o" })
end,
})
-- ide like highlight when stopping cursor
vim.api.nvim_create_autocmd("CursorMoved", {
group = vim.api.nvim_create_augroup("LspReferenceHighlight", { clear = true }),
desc = "Highlight references under cursor",
callback = function()
-- Only run if the cursor is not in insert mode
if vim.fn.mode() ~= "i" then
local clients = vim.lsp.get_clients({ bufnr = 0 })
local supports_highlight = false
for _, client in ipairs(clients) do
if client.server_capabilities.documentHighlightProvider then
supports_highlight = true
break -- Found a supporting client, no need to check others
end
end
-- 3. Proceed only if an LSP is active AND supports the feature
if supports_highlight then
vim.lsp.buf.clear_references()
vim.lsp.buf.document_highlight()
end
end
end,
})
-- ide like highlight when stopping cursor
vim.api.nvim_create_autocmd("CursorMovedI", {
group = "LspReferenceHighlight",
desc = "Clear highlights when entering insert mode",
callback = function()
vim.lsp.buf.clear_references()
end,
})

View file

@ -5,6 +5,9 @@ vim.pack.add({
build = ":TSUpdate",
lazy = false,
},
{
src = "https://github.com/nvim-treesitter/nvim-treesitter-textobjects"
}
})
require("nvim-treesitter.configs").setup({
@ -17,4 +20,48 @@ require("nvim-treesitter.configs").setup({
enable = true,
additional_vim_regex_highlighting = false,
},
textobjects = {
select = {
enable = true,
-- Automatically jump forward to textobj, similar to targets.vim
lookahead = true,
keymaps = {
-- You can use the capture groups defined in textobjects.scm
["af"] = "@function.outer",
["if"] = "@function.inner",
["ac"] = "@class.outer",
-- You can optionally set descriptions to the mappings (used in the desc parameter of
-- nvim_buf_set_keymap) which plugins like which-key display
["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" },
-- You can also use captures from other query groups like `locals.scm`
["as"] = { query = "@local.scope", query_group = "locals", desc = "Select language scope" },
},
-- You can choose the select mode (default is charwise 'v')
--
-- Can also be a function which gets passed a table with the keys
-- * query_string: eg '@function.inner'
-- * method: eg 'v' or 'o'
-- and should return the mode ('v', 'V', or '<c-v>') or a table
-- mapping query_strings to modes.
selection_modes = {
['@parameter.outer'] = 'v', -- charwise
['@function.outer'] = 'V', -- linewise
['@class.outer'] = '<c-v>', -- blockwise
},
-- If you set this to `true` (default is `false`) then any textobject is
-- extended to include preceding or succeeding whitespace. Succeeding
-- whitespace has priority in order to act similarly to eg the built-in
-- `ap`.
--
-- Can also be a function which gets passed a table with the keys
-- * query_string: eg '@function.inner'
-- * selection_mode: eg 'v'
-- and should return true or false
include_surrounding_whitespace = true,
},
},
})