Vim 自动缩进 (gg=G) 在 JS 缩进中严重损坏

Vim autoindent (gg=G) is terribly broken for JS indentation

我的最终目标是使用 gg=G 自动缩进符合 eslintrc.js 文件的所有 JS 代码。

所以,目前我 syntastic and vim-javascript 在我的 .vimrc

中查看我的 JS 代码
let g:syntastic_javascript_checkers=["eslint"]

假设我有一些不错的 JS,如下所示

const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');

const PATHS = {
  app : path.join(__dirname, 'app'),
  build : path.join(__dirname, 'build'),
};

const commonConfig = {
  entry : {
    app : PATHS.app,
  },
  output : {
    path : PATHS.build,
    filename : '[name].js',
  },
  plugins : [
    new HtmlWebpackPlugin({
      title : 'Webpack Demo',
    }),
  ],
};

gg=G(正常模式)命令将上面的内容分解为下面的内容。

const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');

const PATHS = {
app : path.join(__dirname, 'app'),
      build : path.join(__dirname, 'build'),
};

const commonConfig = {
entry : {
app : PATHS.app,
        },
output : {
path : PATHS.build,
       filename : '[name].js',
         },
plugins : [
              new HtmlWebpackPlugin({
title : 'Webpack Demo',
}),
],
    };

这不酷。

顺便说一句,vim-js-indent and vim-jsx-improve 也没有做任何事情。

非常欢迎任何帮助,非常感谢。

您的 "not cool" 示例是 "generic" 缩进的结果,当 Vim 无法识别您的缓冲区时 JavaScript and/or 没有不应用 JavaScript 特定的缩进规则。

该代码使用这个最小设置正确缩进:

$ vim -Nu NONE --cmd 'filetype indent on' filename.js

其中:

  • 检测到您的缓冲区包含 JavaScript、
  • 应用 JavaScript 特定的缩进规则。

为确保正确缩进,您必须将此行添加到 vimrc:

filetype indent on