Surround with quotation marks
Asked Answered
A

4

36

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?

Anglofrench answered 23/9, 2008 at 14:6 Comment(0)
G
88

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)

Gobang answered 23/9, 2008 at 14:50 Comment(1)
Thank you! FYI - I also had to restart Eclipse after changing the Preferences.Damaraland
M
4

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.

Marola answered 23/9, 2008 at 14:38 Comment(0)
E
3

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)

Exodontics answered 23/9, 2008 at 15:7 Comment(0)
P
2

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.

Petiolule answered 23/9, 2008 at 14:37 Comment(1)
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.Evangelical

© 2022 - 2024 — McMap. All rights reserved.