Neovim中TypeScript枚举缩进不一致的原因及解决设置问询
Since you rely on the typescript-vim plugin for TypeScript autoindent (and lose that functionality without it), the inconsistent indentation for enum members is almost certainly coming from the plugin's default behavior. Here are targeted solutions to get all enum items aligned with 2 spaces:
Solution 1: Tweak typescript-vim's Built-In Settings
The plugin lets you customize indent levels for specific TypeScript constructs. Add these lines to your config to enforce consistent 2-space indent for enums:
For Vimscript (init.vim):
-- First, set base 2-space indent for all TypeScript files autocmd FileType typescript setlocal shiftwidth=2 tabstop=2 softtabstop=2 expandtab -- Tell the plugin to use 2 spaces for enum bodies and members let g:typescript_indent_enum_body = 2 let g:typescript_indent_enum_members = 2
For Lua (init.lua):
-- Configure base TypeScript indent settings vim.api.nvim_create_autocmd("FileType", { pattern = "typescript", callback = function() vim.opt_local.shiftwidth = 2 vim.opt_local.tabstop = 2 vim.opt_local.softtabstop = 2 vim.opt_local.expandtab = true end, }) -- Override enum indent behavior in typescript-vim vim.g.typescript_indent_enum_body = 2 vim.g.typescript_indent_enum_members = 2
These variables explicitly set the indent for lines inside enum braces and each individual member to 2 spaces, eliminating the gap between the first and subsequent items.
Solution 2: Override the Indent Expression (If Solution 1 Fails)
If the plugin's built-in settings don't resolve the issue, you can manually adjust the indent logic for enum lines. Add this custom indent function to your config:
Vimscript:
autocmd FileType typescript setlocal indentexpr=CustomTypeScriptIndent() function! CustomTypeScriptIndent() let base_indent = typescript#indent#Indent() -- Check if we're inside an enum block if search('^\s*enum', 'b') != 0 -- Adjust indent for enum members to match shiftwidth let current_line = getline(v:lnum) if current_line =~# '^\s*\w\+\s*=' return base_indent - &sw endif endif return base_indent endfunction
Lua:
vim.api.nvim_create_autocmd("FileType", { pattern = "typescript", callback = function() vim.opt_local.indentexpr = "v:lua.CustomTypeScriptIndent()" end, }) function CustomTypeScriptIndent() local base_indent = vim.fn["typescript#indent#Indent"]() -- Check if current line is inside an enum block if vim.fn.search('^%s*enum', 'b') ~= 0 then local current_line = vim.fn.getline(vim.v.lnum) -- If it's an enum member, adjust to use 2 spaces if current_line:match('^%s*%w+%s*=') then return base_indent - vim.opt_local.shiftwidth:get() end end return base_indent end
This function uses the plugin's default indent as a base, then reduces the indent for enum members to ensure they all align with 2 spaces.
Solution 3: Switch to LSP-Based Formatting (Long-Term Fix)
For more consistent formatting across all TypeScript features, consider using an LSP like tsserver with Prettier. This replaces the need for typescript-vim's indent logic entirely.
- Install the required tools:
npm install -g typescript-language-server prettier
- Configure Neovim to use
tsserver(usinglspconfig):
require('lspconfig').tsserver.setup({ on_attach = function(client, bufnr) -- Enable formatting on save vim.api.nvim_create_autocmd("BufWritePre", { buffer = bufnr, callback = function() vim.lsp.buf.format({ async = false }) end, }) end, settings = { typescript = { format = { indentSize = 2, convertTabsToSpaces = true, }, }, }, })
This setup will automatically format your files on save, ensuring uniform 2-space indent for enums (and everything else) without relying on the typescript-vim plugin.
内容的提问来源于stack exchange,提问作者Michael Stack

