如何在不同长度的文本周围创建//的注释块

问题描述 投票:0回答:2

我很好奇,是否有办法围绕文本自动创建这些块?当前在VS / VSC项目中工作。

/////////////////////////////////////////////////////////
///////////////////// Demo Text /////////////////////////
/////////////////////////////////////////////////////////

由于文本长度不同,这可能真的很烦人。

谢谢!

javascript regex visual-studio-code vscode-snippets
2个回答
1
投票

这里是vscode解决方案。它看起来很复杂,但只是一堆链接在一起的简单命令。您将需要一些宏扩展才能连续运行多个命令。在这里我以多命令为例。参见https://marketplace.visualstudio.com/items?itemName=ryuta46.multi-command

settings.json:

{
  "command": "multiCommand.createTextBlock",
  // "interval": 350,
  "sequence": [
    "editor.action.insertLineBefore",
    {
      "command": "type",   // add 57 //' to line above  STEP 1
      "args": {
        "text": "/////////////////////////////////////////////////////////"
      }
    },
    "cursorDown",

    "cursorHomeSelect",                     //  STEP 2
    "editor.action.clipboardCutAction",
    {
      "command": "editor.action.insertSnippet",  // pad start and end with lots of //'s'
      "args": {
        "snippet": "////////////////////////////// $CLIPBOARD //////////////////////////////",
      }
    },

    "cursorHomeSelect",                   // STEP 3
    {
      "command": "editor.action.insertSnippet",  // keep the middle 57 characters
      "args": {
        "snippet": "${TM_SELECTED_TEXT/(.*)(.{57})(\\1).*/$2/g}",
      }
    },

    "editor.action.insertLineAfter",         // STEP 4    
    {
      "command": "type",   // add 57 //'s to the line after
      "args": {
        "text": "/////////////////////////////////////////////////////////"
      }
    },

    "cursorDown"
  ]
},

和用于触发该宏的键绑定(keybindings.json)。

{ 
  "key": "alt+b",                             // whatever binding you wish
  "command": "multiCommand.createTextBlock",
  "when": "editorTextFocus && !editorReadonly"
},

解释宏:

步骤1:您有57 /,所以我用了-选择一些金额。在文本上方插入一行57 /。

[第2步:剪切文本,并在文本的开头和结尾加上/。文本前后必须等于/的数字-我用了大约30个左右。

步骤3:有趣的步骤。用正则表达式(.*)(.{57})(\\1).*保留包含文本的行的57个中间字符\\1是在第一个捕获组中捕获的任何内容的backreference-因此捕获组1和3的长度相同。捕获组2将是我们的文本,并由等号/包围(如果我们在进行正则表达式转换之前添加了等号/)。正则表达式末尾的.*用于处理文本中的奇数/偶数字符。

步骤4:在下一行插入57 /的行。

以较慢的速度演示(启用间隔选项后才能正常工作:

demo at slower speed

没有间隔选项的演示:

demo of different length texts


0
投票

这必须通过插件/扩展来完成,这本来不可能。

© www.soinside.com 2019 - 2024. All rights reserved.