【问题标题】:Can VSCode do block-comments line-by-line?VSCode 可以逐行进行块注释吗?
【发布时间】:2020-09-22 09:59:41
【问题描述】:

这是我在 VS Code 中使用ctrl + / 注释 HTML 代码的示例:


    <!-- </label>
    <label>Confirm Your Email:
        <!-- <input type="email" name="emailConfirm" required>
    <!-- </label>
    <label> -->
        <input type="checkbox" name="termsAgree" required/>
            I agree to the <a href="/legal/terms/">Terms & Conditions</a>. -->
    </label> -->

  1. 选中整个代码块和ctrl+/ 来评论整个事情
  2. 选择了一段代码和ctrl+/取消注释一行,但它只是添加了一个新注释,从而破坏了较大的注释
  3. 再次执行 #2,现在这是一场噩梦

当我ctrl + / CSS 代码块时,我遇到了同样的问题。 PHP代码中没有问题,因为它使用//逐行注释

如何改变这种行为?

我在想:

  • 一个“聪明”的评论者,可以使用块 cmets 并“知道”它当前是否在评论中
  • 一个愚蠢的评论者,只是像这样单独对每一行进行 cmets/unmets:
<!-- </label> -->
<!-- <label>Confirm Your Email: -->
    <!-- <input type="email" name="emailConfirm" required> -->
</label>
<label>
    <!-- <input type="checkbox" name="termsAgree" required/> -->
        <!-- I agree to the <a href="/legal/terms/">Terms & Conditions</a>. -->
<!-- </label> -->

请忽略 html 无效的事实。这只是我在这个例子中使用的一个随机块。

【问题讨论】:

    标签: visual-studio-code comments keyboard-shortcuts


    【解决方案1】:

    我刚刚做了一个扩展,Toggle Line Comments,它使用你的“笨方法”来单独切换每一行。

    这是一个运行良好的代码演示:



    原答案:

    是的,vscode 可以逐行阻止 cmets,但该功能不是内置的。您将不得不使用宏命令,这里我使用了multi-command,将行分开并为每个行应用块注释。

    把这个放到你的settings.json:

    "multiCommand.commands": [
    
      {
        "command": "multiCommand.blockHTMLCommentByLine",
        "sequence": [
          "editor.action.insertCursorAtEndOfEachLineSelected",
          "cursorHomeSelect",
          "editor.action.blockComment",
          "cancelSelection",
        ]
      }
    ]
    

    它将您的选择分成单独的行,然后在每行上切换一个块注释。

    添加键绑定以触发该宏 - 重载 ctrl+/ 键绑定(将其放入您的 keybindings.json):

    {
      "key": "ctrl+/",
      "command": "extension.multiCommand.execute",
      "args": { "command": "multiCommand.blockHTMLCommentByLine" },
      // "when": "editorTextFocus && editorHasSelection && resourceExtname =~ /\\.(html|css|scss)/"
      "when": "editorTextFocus && editorHasSelection && resourceExtname =~ /\\.html/"
    },
    

    我将其限制为 .html 文件,但您可以查看如何在另一个 when 子句中包含其他扩展名。

    演示:


    如果您想对整行进行注释,则必须选择整行。

    【讨论】:

    • 这太棒了!我为我的做了一些小的修改。对于文件分机,resourceExtname =~ /\\.(html|css|scss|php)/。对于命令"sequence",我使用:"sequence": [ "editor.action.insertCursorAtEndOfEachLineSelected", "editor.action.addCommentLine", ]。您共享的版本在我的行首插入了 cmets... 在 html 开始之前。
    • 我在code.visualstudio.com/api/extension-guides/command 上找到了addCommentLine 命令。哦,另外......它正在为我评论整行,无论选择整行。
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 2021-01-26
    • 2020-06-25
    • 2016-11-04
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多