TextMate style indentation for (neo)vim. Also used by atom and vscode to implement auto indent.
I developed this as a supplement to nvim-yati for saner fallback indent computation, and it could also be used as a standalone indentexpr.
See available rules for supported languages.
let g:tmindent = {
\ 'enabled': { -> index(["lua", "yaml"], &filetype) >= 0 },
\ 'default_rule': {},
\ 'rules': {
\ 'json': #{ comment: ['//'], inherit: ['&{}', '&[]'] }
\ }
\ }
require('tmindent').setup({
enabled = function() return vim.tbl_contains({ "lua" }, vim.bo.filetype) end,
use_treesitter = function() return true end, -- used to detect different langauge region and comments
default_rule = {},
rules = {
lua = {
comment = {'--'},
-- inherit pair rules
inherit = {'&{}', '&()'},
-- these patterns are the same as TextMate's
increase = {'\v<%(else|function|then|do|repeat)>((<%(end|until)>)@!.)*$'},
decrease = {'^\v<%(elseif|else|end|until)>'},
unindented = {},
indentnext = {},
}
}
})
call tmindent#get_indent(lnum, bufnr) " lnum is 1-indexed
require('tmindent').get_indent(lnum, bufnr) -- NOTE: lnum is 0-indexed
local tm_fts = { "lua", "javascript", "python" } -- or any other langs
require("nvim-treesitter.configs").setup {
yati = {
default_fallback = function(lnum, computed, bufnr)
if vim.tbl_contains(tm_fts, vim.bo[bufnr].filetype) then
return require('tmindent').get_indent(lnum, bufnr) + computed
end
-- or any other fallback methods
return require('nvim-yati.fallback').vim_auto(lnum, computed, bufnr)
end,
}
}
inherit
: list of other rules to extendcomment
: pattern to match comment, which will be trimmed before matchingstring
: pattern to match string content, which will be replaced by whitespace before matchingincrease
:increaseIndentPattern
in TextMatedecrease
:decreaseIndentPattern
in TextMateunindented
:unindentedLinePattern
in TextMateindentnext
:indentNextLinePattern
in TextMate
Basic rules include "&{}", "&[]", "&()", "&<>", "&tag".
- vscode
- vim-gindent