对于Javascript嵌套对象,Vim缩进不正确

时间:2014-06-02 02:06:22

标签: javascript vim

我正在使用auto-indentvim-javascript来在Vim中编写javascript。但是,我无法正确缩进javascript对象。例如:

这是我手动缩进的:

var NetworkSchema = new Schema({
    'timeStamp' : { type : Date, index: true },
    "avaiable" : Boolean,
    "status" : String,
    "metrics" : [ { "txDropped" : { "data" : Number,
                           "type" : String,
                           "unit" : String
                          }
                   },
                   { "txErrors": { "data" : Number,
                           "type" : String,
                           "unit" : String
                          }
                   },
                   { "txOverruns": { "data" : Number,
                           "type" : String,
                           "unit" : String
                          }
                   }
                  ]
});
当使用gg=G自动缩进时,

看起来像这样:

var NetworkSchema = new Schema({
    'timeStamp' : { type : Date, index: true },
    "avaiable" : Boolean,
    "status" : String,
    "metrics" : [ { "txDropped" : { "data" : Number,
        "type" : String,   
        "unit" : String    
    }                     
    },             
    { "txErrors": { "data" : Number,
        "type" : String,   
        "unit" : String    
    }                     
    },             
    { "txOverruns": { "data" : Number,
        "type" : String,   
        "unit" : String    
    }                     
    }              
    ]             
});

如何自动为嵌套对象缩进缩进?

1 个答案:

答案 0 :(得分:0)

这是vim-javascrit's issue tracker的问题。

无论如何,我使用js-beautify格式化我的JavaScript:

command! -buffer -range=% Format let b:winview = winsaveview() |
  \ execute <line1> . "," . <line2> . "!js-beautify -f - -j -B -s " . &shiftwidth |
  \ call winrestview(b:winview)

使用以下命令格式化整个缓冲区:

:Format

使用以下命令格式化当前视觉选择所涵盖的线条(自动为您插入'<,'>范围):

:'<,'>Format

使用以下命令格式化任意范围所涵盖的行:

:23,89Format