将内联注释与VSCode中的特定列对齐 [英] Align inline comments to a certain column in VSCode

查看:1073
本文介绍了将内联注释与VSCode中的特定列对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在VSCode中组织内联注释,以便它们都位于同一列中. 从这个:

I'd like to organize the inline comments in VSCode so that they are all positioned in the same column. From this:

int a = 0; //comment 1
int b = 0;       //comment 2
int c = a*b;                    //comment 3

对此:

int a = 0;                      //comment 1
int b = 0;                      //comment 2
int c = a*b;                    //comment 3

尝试使用更好的对齐扩展",但实际上并没有用,因为它只能正确格式化具有等号的行,例如something = something.还有其他方法吗?预先感谢.

Tried using Better Align Extension but that didn't really work, as it only formats correctly lines that have an equal sign, like something = something. Is there any other way to do this? Thanks in advance.

推荐答案

您可以使用

You can do it with a macro extension like multi-command. You do have to hard-code a rough guess as to how far to the right you want to align the comments. You can't simply measure the furthest and use that - you would need a more involved extension to do that. However, you will have multi-cursors at the end and it is easy to adjust all the comments at once if you don't like where they ended up as in the demo where backspacing and tabbing moves all the comments in alignment.

演示:

使用一些键绑定来触发宏:

Use some keybinding to trigger the macro:

{
  "key": "alt+w",                // whatever keybinding you choose
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.alignComments" },
  "when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\\.js/"
},

我为.js文件创建了该文件,但您可以为其他扩展名(例如

I made that for .js files but you can modify that for other extensions, like

resourceExtname =~ /\\.(js$|php)/用于.js和.php文件.

resourceExtname =~ /\\.(js$|php)/ for .js and .php files.

在您的settings.json中为实际宏:

And in your settings.json the actual macro:

"multiCommand.commands": [

  {
      "command": "multiCommand.alignComments",
      // "interval": 3000,
      "sequence": [
        "editor.action.insertCursorAtEndOfEachLineSelected",
         "cursorHomeSelect",
        {
          "command": "editor.action.insertSnippet",  // pad with lots of spaces's'
          "args": {
            // so capture group 1 is before the comment, add spaces after it
            "snippet": "${TM_SELECTED_TEXT/^([^;]+;)(\\s*)(?=\\/\\/.*)/$1                      /g}",
          }
        },

        "cursorHomeSelect",
        {
          "command": "editor.action.insertSnippet",
          "args": {
                   // keep first 25 characters or wherever you want to align
                   //   and then add the comments
            "snippet": "${TM_SELECTED_TEXT/(.{25})(\\s*)(.*)/$1$3/g}",  
          }
        },
        // "removeSecondaryCursors"  // to exit multi-cursor mode
      ]
    }
]

只需按 Escape 即可退出多光标模式(或将该命令添加到宏的末尾).

Just hit Escape to exit multi-cursor mode (or add that command to the end of the macro).

这篇关于将内联注释与VSCode中的特定列对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆