tags:

views:

1052

answers:

4

Hi,

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?

Thanks, Ingo

+1  A: 

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.

Diomidis Spinellis
I have a ridiculous addition, which I think you might appreciate as a seder. You can avoid the additional `+` on the last line with `sed 's/^/"/; s/$/"/; $! s/$/+/'` --- `$` means *last line*, and `$!` means *not the last line*.
13ren
A: 

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.

Rafał Dowgird
+4  A: 

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)

Grundlefleck
+1  A: 

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

  • Find:

    ^((?:\s(?)(\S(?:[^\r\n" title="" />)\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)

VonC