如何使用正则表达式替换和片段将行填充到一定长度 [英] How to pad out line to certain length using regex replace and snippets

查看:57
本文介绍了如何使用正则表达式替换和片段将行填充到一定长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//我的部分-----------------------------------------------------------------

以上就是想要的结果.

<小时>

想象一下这个场景

//----------------------------------------------------------------------------//火力基地//-----------------------------------------------------------------------------//实用程序

可以使用例如这个正则表达式 /(^.{20}).*$/$1/ 将线条修剪到一定长度,这将得到

//-----------------//火力基地//-----------------//实用程序

<块引用>

但是如果我想填充其他行而不是设置一口气长成这样?这可能吗?

  • 一个仅用于正确填充的正则表达式,而不用于修剪

//-----------------//火力基地 --------//-----------------//实用程序 -----------

很久以前,我正在做一些正则表达式忍者挑战,我们应该做数学,所以......正则表达式很神奇.


我最终想要实现的是一个 VSCode 片段,它允许我编写:My Section --
然后触发一个片段,将插入的文本转换为 80 个字符宽的注释,其中包含

//我的部分-----------------------------------------------------------------

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

The above is the desired result.


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

解决方案

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.commentSection",
   // "interval": 750,  // you don't need this, just for illustration

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

     "editor.action.addCommentLine",

     // 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": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.commentSection" }
},

The basic idea is to add too many hyphens - say 75 - 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.

It works on blank lines too as the end of the demo demonstrates.

这篇关于如何使用正则表达式替换和片段将行填充到一定长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆