使 VSCode 的注释从列位置 0 开始 [英] Make comments of VSCode start at column position 0

查看:31
本文介绍了使 VSCode 的注释从列位置 0 开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VSCode 中,当我按下组合键 ctrl+/ 时,VSCode 将对所选行进行注释,确保缩进完好无损.因此,如果一行代码从位置 16 开始,那么注释的双斜线(即 //)将位于位置 16,将代码向右移动一点.

我想设置它,这样当我按下 ctrl+/ 时,注释双斜线 // 总是从列位置 0.这可能吗?

谢谢.

解决方案

这有点棘手,但请测试一下.您需要一个宏扩展,例如


这种方法的一个缺点是,如果您选择多行并对其进行注释,您将丢失多行选择(如演示中所示).

In VSCode, when I press the key combination ctrl+/, VSCode will comment the selected lines, ensuring indentation is intact. So if a line of code starts at position 16, then the double slashes of comment (i.e., //) will be at position 16, shifting the code to the right a little.

I would like to set it, so that when I press ctrl+/, the comment double slashes // will always start at column position 0. Is this possible?

Thanks.

解决方案

It is a little tricky, but test this out. You need a macro extension like multi-command.

In your keybindings.json:

 {                   // disable ctrl+/ for js/php files only
   "key": "ctrl+/",
   "command": "-editor.action.commentLine",
   "when": "editorTextFocus && !editorReadonly && resourceExtname =~ /\.(js$|php)/"
 },

{                   // call the macro multiCommand.insertCommentColumn0 when
                     // commenting a single line
  "key": "ctrl+/",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.insertCommentColumn0" },
  "when": "!editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\.(js$|php)/" 
},      

{                    // call the macro multiCommand.AddCommentColumn0MultipleLines when
                     // commenting more than one line
  "key": "ctrl+/",
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.AddCommentColumn0MultipleLines" },
  "when": "editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\.(js$|php)/" 
},

 {                   // call the command editor.action.removeCommentLine when
                     // commenting a single or multiple line(s)
   "key": "ctrl+shift+/",
   "command": "editor.action.removeCommentLine",
   "when": "!editorHasSelection && editorTextFocus && !editorReadonly && resourceExtname =~ /\.(js$|php)/"
 },


In your settings.json, the macros:

"multiCommand.commands": [

  {
    "command": "multiCommand.insertCommentColumn0",
    "sequence": [
      "cursorLineStart",
      {
        "command": "type",
        "args": {
          "text": "// "
        }
      },
    ]
  },
  {
    "command": "multiCommand.AddCommentColumn0MultipleLines",
    "sequence": [
      "editor.action.insertCursorAtEndOfEachLineSelected",        
      "cursorLineStart",
      {
        "command": "type",
        "args": {
          "text": "// "
        }
      },
      "removeSecondaryCursors"
    ]
  },


This resourceExtname =~ /\.(js$|php)/ restricts the keybindings to .js and .php files (and not .json files). You can change that if you want the keybindings to apply to more file types.

Ctrl+/ to apply the comment characters at column position 0 and Ctrl+Shift+Ctrl to remove the comment characters.

You can change those keys to whatever you want. Note it isn't (and currently cannot be) a simple toggle using Ctrl+/ - with a keybinding there is no way to detect whether a comment already exists. You would need an extension to get that kind of functionality.


One downside of this method is that if you select multiple lines and comment them, you will lose that mult-line selection (as can be seen in the demo).

这篇关于使 VSCode 的注释从列位置 0 开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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