skip to Main Content

Using VSCode snippets, I would like to have the following behaviour. Say I select the word "foobarbaz", then (using a snippet) I would like it to turn into

/******************************************************************************/
/*                                  foobarbaz                                 */
/******************************************************************************/

where the length of the comment block is fixed. This means that the number of spaces before and after "foobarbaz" depends on the length of "foobarbaz" itself. The idea is that the number of spaces on the left and on the right of the selected word is the same. In the cases where the length of the word is an odd number, I would have the left number of spaces to be one less than the right number of spaces.
Would it be possible to have this behviour implemented with VSCode snippets, or is it too complex?

2

Answers


  1. there is an extension in vscode called Comment Bars that does the trick. with your arbitrary filler character.
    enter image description here

    Login or Signup to reply.
  2. Using an extension I wrote, Comment Blocks, this is quite easy.

    Either this setting (in your settings.json):

    "commentBlocks.defaults": {  
      "subjects": ["", "${selectedText}", ""],    
      "padLines": ["*", " ", "*"]
    }
    

    and a keybinding to trigger it (in your keybindings.json):

    {
      "key": "alt+b",
      "command": "comment-blocks.createBlock"
    }
    

    Or just a keybinding:

    {
      "key": "alt+b",
      "command": "comment-blocks.createBlock",
      "args": {
        "subjects": ["", "${selectedText}", ""],    // three lines to the block
        "padLines": ["*", " ", "*"]    // pad each line separately, middle line with spaces
      }
    }
    

    will do this:

    create a comment block with centered selected on middle line


    The extension can do a lot more customization, it just happens that your example is pretty simple and uses some of the defaults set by the extension, like "justify": "center".

    Options are:

      selectCurrentLine?: boolean,         // default is true
      lineLength: number | Array<number>,  // default is 80
      startText: string | Array<string>,   // default to block comment start
      endText: string | Array<string>,     // default to block comment end
      justify: string | Array<string>,     // default is center (left/right)
      gapLeft: number | Array<number>,     // spaces around the text
      gapRight: number | Array<number>,    
      padLines: string | Array<string>,    // what character to pad out each line
      subjects: Array<string>              // like ${selectedText}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search