首页 > 解决方案 > 允许自动缩进二维数组(如表格)的编辑器或扩展

问题描述

我正在寻找一个允许我自动格式化这样的数组的编辑器或插件(最好是 VS Code):

const lines = [
    [ "step-1", "step", "step-1", "", `["1"]` ],
    [ "r", "repeater", "r", "", `[]` ],
    [ "mycheckbox__1[]", "input", "mycheckbox[]", "__1", `[]` ],
    [ "myradio__1", "input", "myradio", "__1", `[]` ],
    [ "mytext__1", "input", "mytext", "__1", `[""]` ],
    [ "myurl__1", "input", "myurl", "__1", `[""]` ],
    [ "myemail__1", "input", "myemail", "__1", `[""]` ],
    [ "myselect__1", "input", "myselect", "__1", `[""]` ],
    [ "mymultiselect__1[]", "input", "mymultiselect[]", "__1", `[]` ],
    [ "myfile__1", "input", "myfile", "__1", `[""]` ],
    [ "mymultifile__1[]", "input", "mymultifile[]", "__1", `[""]` ],
    [ "mycheckbox__2[]", "input", "mycheckbox[]", "__2", `[]` ],
    [ "myradio__2", "input", "myradio", "__2", `[]` ],
    [ "mytext__2", "input", "mytext", "__2", `[""]` ],
    [ "myurl__2", "input", "myurl", "__2", `[""]` ],
    [ "myemail__2", "input", "myemail", "__2", `[""]` ],
    [ "myselect__2", "input", "myselect", "__2", `[""]` ],
    [ "mymultiselect__2[]", "input", "mymultiselect[]", "__2", `[]` ],
    [ "myfile__2", "input", "myfile", "__2", `[""]` ],
    [ "mymultifile__2[]", "input", "mymultifile[]", "__2", `[""]` ],
    [ "r_count", "input", "r_count", "", `["1"]` ],
    [ "root-mycheckbox[]", "input", "root-mycheckbox[]", "", `[]` ],
    [ "root-myradio", "input", "root-myradio", "", `[]` ],
    [ "root-mytext", "input", "root-mytext", "", `[""]` ],
    [ "root-myurl", "input", "root-myurl", "", `[""]` ],
    [ "root-myemail", "input", "root-myemail", "", `[""]` ],
    [ "root-myselect", "input", "root-myselect", "", `[""]` ],
    [ "root-mymultiselect[]", "input", "root-mymultiselect[]", "", `[]` ],
    [ "root-myfile", "input", "root-myfile", "", `[""]` ],
    [ "root-mymultifile[]", "input", "root-mymultifile[]", "", `[""]` ],
    [ "step-2", "step", "step-2", "", `["2"]` ],
];

更可读的东西,像这样:

const lines = [
    [ "step-1",               "step",     "step-1",                 "",     `["1"]` ],
    [ "r",                    "repeater", "r",                      "",     `[]`    ],
    [ "mycheckbox__1[]",      "input",    "mycheckbox[]",           "__1",  `[]`    ],
    [ "myradio__1",           "input",    "myradio",                "__1",  `[]`    ],
    [ "mytext__1",            "input",    "mytext",                 "__1",  `[""]`  ],
    [ "myurl__1",             "input",    "myurl",                  "__1",  `[""]`  ],
    [ "myemail__1",           "input",    "myemail",                "__1",  `[""]`  ],
    [ "myselect__1",          "input",    "myselect",               "__1",  `[""]`  ],
    [ "mymultiselect__1[]",   "input",    "mymultiselect[]",        "__1",  `[]`    ],
    [ "myfile__1",            "input",    "myfile",                 "__1",  `[""]`  ],
    [ "mymultifile__1[]",     "input",    "mymultifile[]",          "__1",  `[""]`  ],
    [ "mycheckbox__2[]",      "input",    "mycheckbox[]",           "__2",  `[]`    ],
    [ "myradio__2",           "input",    "myradio",                "__2",  `[]`    ],
    [ "mytext__2",            "input",    "mytext",                 "__2",  `[""]`  ],
    [ "myurl__2",             "input",    "myurl",                  "__2",  `[""]`  ],
    [ "myemail__2",           "input",    "myemail",                "__2",  `[""]`  ],
    [ "myselect__2",          "input",    "myselect",               "__2",  `[""]`  ],
    [ "mymultiselect__2[]",   "input",    "mymultiselect[]",        "__2",  `[]`    ],
    [ "myfile__2",            "input",    "myfile",                 "__2",  `[""]`  ],
    [ "mymultifile__2[]",     "input",    "mymultifile[]",          "__2",  `[""]`  ],
    [ "r_count",              "input",    "r_count",                "",     `["1"]` ],
    [ "root-mycheckbox[]",    "input",    "root-mycheckbox[]",      "",     `[]`    ],
    [ "root-myradio",         "input",    "root-myradio",           "",     `[]`    ],
    [ "root-mytext",          "input",    "root-mytext",            "",     `[""]`  ],
    [ "root-myurl",           "input",    "root-myurl",             "",     `[""]`  ],
    [ "root-myemail",         "input",    "root-myemail",           "",     `[""]`  ],
    [ "root-myselect",        "input",    "root-myselect",          "",     `[""]`  ],
    [ "root-mymultiselect[]", "input",    "root-mymultiselect[]",   "",     `[]`    ],
    [ "root-myfile",          "input",    "root-myfile",            "",     `[""]`  ],
    [ "root-mymultifile[]",   "input",    "root-mymultifile[]",     "",     `[""]`  ],
    [ "step-2",               "step",     "step-2",                 "",     `["2"]` ],
];

我找了一些在线工具,但没有找到。虽然有一些扩展来格式化 CSV 文件,但首先将我的代码转换为有效的 CSV 文件,然后使用查找和替换添加括号有点麻烦。

所以目前,如果它们不是太大,我仍然会手动标记这样的数组。不过,在我的代码编辑器中拥有一个自动格式化工具会很酷。

标签: visual-studio-codeeditortext-editor

解决方案


我在您的代码上尝试了这个扩展,它运行良好: Smart Column Indenter

用代码演示制作列


这个扩展提供了命令,例如extension.smartColumnIndenter.indentN默认绑定到键绑定的命令: ++Ctrl所以你可以使用它或重新绑定它,而不是像我在演示中那样使用上下文菜单。I CtrlN


推荐阅读