Home > Net >  how to make a custom comment style in vscode
how to make a custom comment style in vscode

Time:01-28

I got a source code commented like below.

// //------------------------------------------------------------------------------
// //======================= Variable Declaration =================================

Is there any way to make a comment like the above in vs code.

I only got single and multiline comments.

But I want the above custom style.

CodePudding user response:

You can make a snippet to do that. In one of your snippets files:

"Variable Declaration Comment": {
    "prefix": "_gc",          // whatever trigger you want
    "body": [
      "$LINE_COMMENT $LINE_COMMENT------------------------------------------------------------------------------",
      "$LINE_COMMENT $LINE_COMMENT======================= Variable Declaration =================================",
    ],
    "description": "Insert a Variable Declaration comment header"
}

That will use whatever the line comment style is for the type of file you are in: $LINE_COMMENT.

You could set that to a keybinding like this (in your keybindings.json file):

{
  "key": "alt q",    // whatever keybinding you want 
  "command": "editor.action.insertSnippet",
  "args": {
    "name": "Variable Declaration Comment"
  },
  "when": "editorTextFocus"
}

CodePudding user response:

You can use the extension HyperSnips

Define the following snippet in the all.hsnips file

snippet comment "Comment Section"
// //------------------------------------------------------------------------------
// //``s = '='.repeat((78-(t[0].length   2))/2); rv = s;`` $1 ``rv = '='.repeat(78-(t[0].length   2   s.length));``
endsnippet

If you type comment and select the snippet with Tab you can type the text and the === strings adjust to keep the text centered.

You can also cut some text - start the snippet - and paste the text.

  •  Tags:  
  • Related