1

I am using VS Code Version: 1.40.0.

for quicken up my development I would need to set my own keybinding for block comment when I am in .phtml file.

I managed to get into keybindings.json, put this inside:

{
    "key": "shift+alt+q",
    "command": "editor.action.blockComment",
    "blockComment": [ "{*<!--", "-->*}" ],
    "when": "editorTextFocus && !editorReadonly && resourceExtname == .phtml"
}

I got the part

"blockComment": [ "{*<!--", "-->*}" ],

from here How to customize comment block characters in visual studio code?.

It might be a complete trash. I just tried. It doesn't work, of course.

Optimal solution: Even better would be, if the default key parameter would stay the same (shift+alt+a) for toggle block comment and in .phtml files i would get my desired result ("{*<!-- -->*}"). If I think about it, there is default block comments for .css, .html etc, so there must be a way to put my condition somewhere, rigth?

I would be very glad for any help. Thanks in advance

  • Find my solution here : https://stackoverflow.com/questions/38483225/how-to-comment-jsx-code-out-in-js-files-in-vscode/63777644#63777644 – CyberChris Sep 07 '20 at 14:42
  • Upvoted. I have the same problem. Did you manage to solve it? – szegheo Oct 15 '20 at 12:03
  • @ARS81 Unofrtunately not. I started to use Sublime text ... and it was much more simple there ... but I am going to go back to VS Code probably ... and I would be glad if someone get to the solution ... I need to check CyberChris link yet. – Marťas David Oct 16 '20 at 10:58

1 Answers1

0

As far as I know the best would be to write your own Language Extension plugin for .phtml files and set the desired comment pairs in its configuration.

Not a real solution but until then, here's my supper ugly workaround (use at your own risk):

Put this in your keybindings.json

  {
    "key": "ctrl+numpad_divide",
    "command": "editor.action.insertSnippet",
    "args": {
      "snippet": "${TM_SELECTED_TEXT/^(\\s*)({\\*<!-- (.*) -->\\*})?(.*)/$1${3:-{*<!-- }$4${4:+ -->*\\}}/s}"
    },
    "when": "editorTextFocus && editorHasSelection && !editorReadonly && resourceExtname =~ /phtml?$/"
  }

This way ctrl + / can be used for comment/uncomment (toggle) the selected code as you described. Of course you can set the key binding to something else like alt + / to keep the default block comment behaviour.

enter image description here

Tested with Visual Studio Code v1.50.1

szegheo
  • 4,175
  • 4
  • 31
  • 35