Search code examples
visual-studio-code

How to change the comment style for a particular language


I am new to VScode. I was wondering if there is any way to change the behavior of the line comment shortcut key for a particular file extension. In particular, I have a file whose name is file.frm. Now a line comment for this file type should be * i.e.

 cat file.frm
 * This is a comment
 // This is not a comment
 // However ctrl+k ctrl+c puts line comment as // like in C

The default ctrl+k ctrl+c puts line comment as // similar to C. I want to change this to *. But of course, the behavior of this shortcut will be unchanged for other file types as usual. How do I do it?

There is official extension support for syntax highlighting for this file-type but apparently no support for line comment shortcut:

Name: FORM Syntax Highlighting
Id: benruijl0774.vscode-form
Description: Syntax highlighting for the symbolic manipulation toolkit FORM.
Version: 0.0.1
Publisher: Ben Ruijl
VS Marketplace Link: https://marketplace.visualstudio.com/items? 
itemName=benruijl0774.vscode-form

Solution

  • I made an extension that makes this process easier: Custom Language Properties.

    1. Have a file of the language open that you want to change the comment or bracket style.

    2. Run the following command from the Command Palette:

      "Show the language configuration file for the current editor"
      "command": "custom-language-syntax.showConfigFile",
      
    3. That will open an editor with the language-configuration.json file that pertains to the language ID of the active text editor. It will look similar to this:

      {
        "comments": {
          "lineComment": "//",  // this is wrong
          "blockComment": [
            "/*",
            "*/"
        ]
      }, 
      // <etc.>
      
    4. Now you have two options:

      • Option A: Change the lineComment value to:

        "lineComment": "*",
        

        That changes the actual file within the language extension you installed. Reload vscode to see it take effect.

        The downside to this option is that if the language-configuration.json file is ever updated by the extension author in the future you will lose your changes and have to re-edit the file. [In the case of the form language, it doesn't seem likely that that will happen very often.] In any case, the Custom Language Properties command makes it easy to view and change that file.

      • Option B: Run the following command from the Command Palette with the language configuration file as the active editor:

        "Transform the current language configuration file for intellisense"
        

        This will make the following steps easier as you should get intellisense in your settings.

    5. In your settings.json:

      "custom-language-properties": {
         "form.comments.lineComment": "*"  // you should get intellisense for most of this
      }
      

      This will not change the underlying language-configuration.json file, just override its lineComment entry. You do not need to reload vscode to see this setting take effect.


    Changing a language comment style demo

    In your case, if you run step 4(b) this extension will not be able to set the language ID itself to the unusual extensionID of some languages like form. In that case an input box will appear for you to input the language identifier (as it appears in the lower right corner of the vscode window for those file types). In your case, enter 'form', Enter to confirm and you should be all set.