Skip to content

Instantly share code, notes, and snippets.

@przepompownia
Last active June 23, 2024 20:05
Show Gist options
  • Select an option

  • Save przepompownia/7389cb52de9f5959d97697cd4a99aa4f to your computer and use it in GitHub Desktop.

Select an option

Save przepompownia/7389cb52de9f5959d97697cd4a99aa4f to your computer and use it in GitHub Desktop.
nvim-treesitter on `main` branch: example usage (including `context` and `textobjects`)
local thisInitFile = debug.getinfo(1).source:match('@?(.*)')
local cwd = vim.fs.dirname(thisInitFile)
local appname = vim.env.NVIM_APPNAME or 'nvim'
vim.env.XDG_CONFIG_HOME = cwd
vim.env.XDG_DATA_HOME = vim.fs.joinpath(cwd, '.xdg', 'data')
vim.env.XDG_STATE_HOME = vim.fs.joinpath(cwd, '.xdg', 'state')
vim.env.XDG_CACHE_HOME = vim.fs.joinpath(cwd, '.xdg', 'cache')
vim.fn.mkdir(vim.fs.joinpath(vim.env.XDG_CACHE_HOME, appname), 'p')
local stdPathConfig = vim.fn.stdpath('config')
vim.opt.runtimepath:prepend(stdPathConfig)
vim.opt.packpath:prepend(stdPathConfig)
local function gitClone(url, installPath, branch)
if vim.fn.isdirectory(installPath) ~= 0 then
return
end
local command = {'git', 'clone', '--', url, installPath}
if branch then
table.insert(command, 3, '--branch')
table.insert(command, 4, branch)
end
local sysObj = vim.system(command, {}):wait()
if sysObj.code ~= 0 then
error(sysObj.stderr)
end
vim.notify(sysObj.stdout)
vim.notify(sysObj.stderr, vim.log.levels.WARN)
end
local pluginsPath = 'nvim/pack/plugins/opt'
vim.fn.mkdir(pluginsPath, 'p')
pluginsPath = vim.uv.fs_realpath(pluginsPath)
-- vim.g.pluginDirs = {pluginsPath}
--- @type table<string, {url:string, branch: string?}>
local plugins = {
-- ['arctgx'] = {url = 'https://github.com/przepompownia/nvim-arctgx'},
-- ['git-utils'] = {url = 'przepompownia:przepompownia/git-utils.nvim'},
['nvim-treesitter'] = {url = 'https://github.com/nvim-treesitter/nvim-treesitter', branch = 'main'},
['nvim-treesitter-textobjects'] = {
url = 'https://github.com/nvim-treesitter/nvim-treesitter-textobjects',
branch = 'main',
},
['nvim-treesitter-context'] = {url = 'https://github.com/nvim-treesitter/nvim-treesitter-context'},
}
for name, repo in pairs(plugins) do
local installPath = vim.fs.joinpath(pluginsPath, name)
gitClone(repo.url, installPath, repo.branch)
vim.cmd.packadd({args = {name}, bang = true})
end
vim.wo.foldmethod = 'expr'
vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
vim.wo.foldtext = ''
vim.wo.number = true
vim.wo.numberwidth = 1
vim.opt.signcolumn = 'auto:1-9'
local tsSelectKeymaps = {
['ap'] = '@parameter.outer',
['ip'] = '@parameter.inner',
}
for input, capture in pairs(tsSelectKeymaps) do
vim.keymap.set({'x', 'o'}, input, function ()
require 'nvim-treesitter-textobjects.select'.select_textobject(capture, 'textobjects')
end)
end
local treesitterLib = vim.fs.joinpath(cwd, 'treesitter')
vim.fn.mkdir(treesitterLib, 'p')
vim.opt.runtimepath:append(treesitterLib)
local tsLangMap = {
php = 'php_only',
phpdoc = 'phpdoc',
}
for filetype, lang in pairs(tsLangMap) do
if filetype ~= lang then
vim.treesitter.language.register(lang, filetype)
end
end
require('nvim-treesitter').setup({
install_dir = treesitterLib,
})
require('nvim-treesitter.install').install(vim.tbl_values(tsLangMap), {skip = {installed = true}}, function ()
vim.api.nvim_exec_autocmds('User', {pattern = 'TSInstallFinished'})
end)
vim.api.nvim_create_autocmd('FileType', {
pattern = vim.tbl_keys(tsLangMap),
callback = function (event)
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
local ok, error = pcall(vim.treesitter.start, event.buf, tsLangMap[event.match])
if not ok and error:find('no parser') then
vim.api.nvim_create_autocmd('User', {
buffer = event.buf,
callback = function (tsinstallEvent)
print(vim.inspect(tsinstallEvent))
if not tsinstallEvent.match == 'TSInstallFinished' then
return
end
vim.treesitter.start(event.buf, tsLangMap[event.match])
end
})
end
end
})
require 'treesitter-context'.setup {
enable = true,
on_attach = function (buf)
local excludedFiletypes = {
'noice',
'dapui_stacks',
'dapui_watches',
'dapui_scopes',
'dapui_breakpoints',
}
return not vim.tbl_contains(excludedFiletypes, vim.bo[buf].filetype)
end,
max_lines = 0,
min_window_height = 15,
line_numbers = true,
multiline_threshold = 1,
trim_scope = 'outer',
mode = 'cursor',
separator = '-',
zindex = 20,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment