Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Surround multiple lines with quotes

I'm trying to build a live template that will work like the 'wrap in comment' live template (that puts a // before each selected line). For example, select multiple lines of text, click Code > Surround With > Single quotes (custom live template) and quotes will appear around each line.

E.g. from:

text1
text2
text3

to:

'text1'
'text2'
'text3'

Unfortunately the template I define:

'$SELECTION$'

produces:

'text1
text2
text3'  

which makes sense.

Is there any way to define a Live Template that will work on each line of my selection?

like image 437
Rob Campion Avatar asked Sep 12 '25 00:09

Rob Campion


2 Answers

"Wrap in comment" is not a live template, but an action that is implemented in Java. In the same way, you can't accomplish what you need using a live template, but you can write a small plugin in Java to implement that feature. Please refer to the Editor Basics tutorial to get started with writing the plugin.

like image 172
yole Avatar answered Sep 15 '25 22:09

yole


You can use find and replace feature in IntelliJ :

  • Find by regex ^(.+)$ (make sure to enable regex search with the * next to the find field).
  • Replace all with '$1' and if you need to put a , after '$1', instead.

you can follow the same as in this screenshot

like image 28
Juba Fourali Avatar answered Sep 15 '25 23:09

Juba Fourali