Ingo
Ingo

Reputation:

Surround with quotation marks

How is it possible in Eclipse JDT to convert a multiline selection to String. Like the following

From:

xxxx
yyyy
zzz

To:

"xxxx " +
"yyyy " +
"zzz"

I tried the following template

"${line_selection}${cursor}"+

but that way I only get the whole block surrounded not each line separately. How can I achieve a multiline processing like commenting the selected block?

Upvotes: 36

Views: 30535

Answers (4)

Grundlefleck
Grundlefleck

Reputation: 129317

Maybe this is not what you mean but...

If I'm on a line in Eclipse and I enter double quotation marks, then inside that paste a multiline selection (like your xyz example) it will paste out like this:

"xxxx\n" +   
"yyyy\n" +  
"zzz"

Then you could just find/replace in a selection for "\n" to "", if you didn't intend the newlines.

I think the option to enable this is in Window/Preferences, under Java/Editor/Typing/, check the box next to "Escape text when pasting into a string literal". (Eclipse 3.4 Ganymede)

Upvotes: 88

VonC
VonC

Reputation: 1326782

I would go with a Find/Replace eclipse in regexp mode:

  • Find:

    ^((?:\s(?)\S?)((?:\s(?![\r\n])))

  • Replace with

    \1"\2"\3 +

Will preserve exactly whatever space or tabs you have before and after each string, and will surround them with the needed double-quotes. (last '+' needs to be removed)

Upvotes: 3

Rafał Dowgird
Rafał Dowgird

Reputation: 45131

Find/Replace with the regex option turned on. Find:

^(.*)$

Replace with:

"$1" +

Well, the last line will have a surplus +, you have to delete it manually.

Upvotes: 4

Diomidis Spinellis
Diomidis Spinellis

Reputation: 19345

This may not be exactly the answer you're looking for. You can easily achieve what you're asking by using the sed stream editor. This is available on all flavors of Unix, and also on Windows, by downloading a toolkit like cygwin. On the Unix shell command line run the command

sed 's/^/"/;s/$/"+/'

and paste the text you want to convert. On its output you'll obtain the converted text. The argument passed to sed says substitute (s) the beginning of a line (^) with a quote, and substitute (s) the end of each line ($) with a quote and a plus.

If the text you want to convert is large you may want to redirect sed's input and output through files. In such a case run something like

   sed 's/^/"/;s/$/"+/' <inputfile >outputfile

On Windows you can also use the winclip command of the Outwit tool suite to directly change what's in the clipboard. Simply run

winclip -p | sed 's/^/"/;s/$/"+/' | winclip -c

The above command will paste the clipboard's contents into sed and the result back into the clipboard.

Finally, if you're often using this command, it makes sense placing it into a shell script file, so that you can easily run it. You can then even assign an Eclipse keyboard shortcut to it.

Upvotes: 2

Related Questions