How to fill line to certain length using regex rep

2019-09-22 17:15发布

问题:

Imagine this scenario

// ----------------------------------------------------------------------------
// firebase

// ----------------------------------------------------------------------------
// utils

It is possible to trim lines to a certain length using for example this regex /(^.{20}).*$/$1/, which will give

// -----------------
// firebase

// -----------------
// utils

But what if I want to fill the other lines instead up to the set length like this in one run? Is that possible?

  • one regex for only the right fill, not the trim
// -----------------
// firebase --------

// -----------------
// utils -----------

Ages ago, I was doing some regex ninja challenges and we were supposed to do math, so.... regex is magical.


What I am ultimately trying to achieve is a VSCode snippet that allows me to write: My Section --
then trigger a snippet that transforms the inserted text into an 80 character wide comment containing

// My Section -----------------------------------------------------------------

https://code.visualstudio.com/docs/editor/userdefinedsnippets

回答1:

I doubt you can do what you want in one step, without running some code. But you can do it with a macro so you can have multiple steps fired at once. In this example I am using the macro extension multi-command, but there are other macro extensions out there.

In your settings.json:

"multiCommand.commands": [

 {
   "command": "multiCommand.pad--'s",
   "interval": 750,  // you don't need this, just for illustration

   "sequence": [              
     {
       "command": "type",  // add 80 -'s'
       "args": {
         "text": "--------------------------------------------------------------------------------"
       }
     },

     //select this wrapped line so the next snippet can use TM_SELECTED_TEXT
     "cursorHomeSelect",
     "cursorHomeSelect",

     {
       "command": "editor.action.insertSnippet",  // trim to first 80 characters
       "args": {
         "snippet": "${TM_SELECTED_TEXT/(.{80}).*/$1/g}",
       }
     }
   ]
 }
],

And then whatever keybinding you choose in keybindings.json

{
  "key": "ctrl+alt+-",
  "command": "multiCommand.pad--'s",
},

In the demo below, I have left the interval on just so you can see the steps but you can comment that out. Also I have my vscode set to wrap at 80 characters.

The basic idea is to add too many hyphens - 80 - and then select the entire wrapped line and keep only the first 80 characters, thus trimming the trailing hyphens to fill out to 80 total characters on the line.



回答2:

You can use callback function of replace and based on length you can replace values

let str = `// ----------------------------------------------------------------------------
// firebase

// ----------------------------------------------------------------------------
// utils`

let final = str.replace(/^.*$/gm, (match)=>{
  return match.length === 0 ? match : match.length > 20 ? match.substr(0,20) : match + `-`.repeat(20-match.length)
})

console.log(final)