43

I already have word wrap for Eclipse PHP (How can I get word wrap to work in Eclipse PDT for PHP files?). What about Eclipse Java? Is there a similar tool to wrap Java code lines?

Community
  • 1
  • 1
sandalone
  • 41,141
  • 63
  • 222
  • 338
  • 3
    For those who do not line enabling plug-in each time you open the file, you can always use Ctrl+Shift+F (auto formatting) which will break all long lines. :))))))) – sandalone Dec 31 '10 at 18:15
  • I have installed the plugin in eclipse Juno, but I can't find out any context menu icon or any other control related to it. I don't know how to get it to work. :S – Solace Jun 18 '15 at 02:19

5 Answers5

30

Ctrl+Shift+F will format a file in Eclipse, breaking long lines into multiple lines and nicely word-wrapping comments. You can also highlight just a section of text and format that.

I realize this is not an automatic soft/hard word wrap, but I don't think the question was asking for anything fancy.

jfritz42
  • 5,913
  • 5
  • 50
  • 66
6

This is for anyone who is having the following problem with the "Ahtik" word-wrap plugin in Texlipse: the plugin installs fine, but selecting the "word-wrap" option using the context menu (or pressing ctrl+alt+w) causes the text to wrap momentarily, and then just flick back to being unwrapped (this probably includes anyone using Windows 7).

Here is a fix...straight from the developer himself! I emailed Ahti Kitsik in a last-ditch attempt to find a way round, and was amazed when he actually found a solution, and sent it to me the next day. He suggested that in return I could just make this information widely available.

So, the way to fix it as follows:

Look up the texlipse plugin location. This will probably be in the Eclipse folder, and the address will probably end: ...\eclipse\plugins\net.sourceforge.texlipse_1.5.0.

Backup the plugin.xml file there just in case, and then edit the original by deleting the following chunks of code:

-Firstly, the "action" (starting at line 843):

action

          class="net.sourceforge.texlipse.actions.TexWordWrapAction"
           definitionId="net.sourceforge.texlipse.commands.texWordWrap"
           icon="icons/wrap.gif"
           id="net.sourceforge.texlipse.actions.texWordWrap"
           label="W&rap text"
           menubarPath="net.sourceforge.texlipse.menus.latex/latexGroup"
           style="toggle"
           toolbarPath="latexGroup"
           tooltip="Use word wrap"

-Secondly, the "command" (starting at line 972):

command

        categoryId="net.sourceforge.texlipse.latexEditingCategory"
        name="Wrap text"
        id="net.sourceforge.texlipse.commands.texWordWrap"

(When you have done this, the file should be 1257 lines long, instead of 1271).

Finally, Ahti says: "Start eclipse with -clean command line argument to force reloading of plugin.xml." (Personally I had no idea how to do this, so I just removed and reinstalled his plugin, then exited Eclipse, and edited the code before I restarted it).

The function should work fine now. Hope this helps someone. And thanks again Ahti!

Adam
  • 161
  • 1
  • 1
  • I've tried this but it doesn't work ;/ Using latest eclipse, texeclipse, and ahti. I did `eclipse -clean`. When using "word wrap" from the properties it seems to work but then a second later returns to the way it was. I can use the "correct line wrap" to good effect but it only seems to work per paragraph, and I guess is hard wrapping. – jsmdnq Dec 30 '12 at 18:16
6

In the meantime, there has been an fork to Ahtik's word wrap plugin which, I must say, works very nice and fixes jumbled line numbering bug: Eclipse Word-Wrap Plug-In

UdayKiran Pulipati
  • 6,579
  • 7
  • 67
  • 92
Domchi
  • 10,705
  • 6
  • 54
  • 64
6

Just like to point out that word wrapping is a terrible idea in code. I suppose everyone has their own style, but typically you should refactor or rethink blocks of code that take much more than a single average horizontal screen. (about 1280 pixels)

Of course there are always exceptions... like verbose exceptions for example, but these are not critical for code understanding.

Scanning a line that goes beyond a single horizontal screen, or having to read a thought that stacks on multiple lines will make your code much more difficult to figure out.

Additionally, line counts will be weird, as word wrapping will make a single line seem like it takes up 2, 3, or shudder the thought... 4 lines of code.

As far as comments go, line returns should not be much of a problem for anyone.

Also keep in mind, word wrapping code is not a common practice, and you are essentially saying that everyone reading your code should turn word wrapping on, which is, I'm sorry to say, an unnecessarily painful thing to make others working with your code do....

If nesting is causing you the need for word wrap... well this is also a very bad code smell.

That's my two-cents.

Nicholas
  • 3,286
  • 5
  • 27
  • 35
  • 4
    @Nickolas You are right. Ctrl+Shift+F works better from your point of view because it logically breaks long lines and makes it a beautiful code. Of course, long line is thing to avoid (by definitio), but it is impossible to do so if you call methods where you instantiate some new object with his own parameters, and not to mention if you have to call connecting method via dot (.). Such line is ugly but necessary. – sandalone Dec 31 '10 at 18:26
  • 1
    @Nickolas Forgot to say that line-wrapping add-on is very helpful when writing such long lines because it's tedious to press auto-format shortcut keys each time you need to look at the whole line. I find that auto-formatting should be done once - in the end of coding one logical piece of code. – sandalone Dec 31 '10 at 18:27
  • 3
    @askmo I don't think code should be artificially formatted to simulate word wrapping... this is even a worse idea. My main point is that if you are often creating lines of code that span multiple screens, you are doing something wrong. I admit there are always exceptions, but these exceptions should be very rare. Code should first and foremost be clean and understandable, and I find it very unlikely that code that requires word wrap is a code base that is readable. – Nicholas Dec 31 '10 at 18:36
  • 4
    Nicholas, I completely agree with you, but in my case I'm editing textile documentation, not code, so in this case it'll become quite useful... – opensas Nov 08 '11 at 13:07
  • 12
    For code I agree, however PHP files can contain HTML which can contain paragraphs of text. Personally I'd rather have the option of keeping these on one line and using a soft word-wrap while editing. – Dan Brown Jan 06 '12 at 15:19
  • 2
    Sometimes you need to view a few files side by side. It would be nice if they could temporarily wrap to 20 columns. – nafg Jun 27 '13 at 07:58
  • So - what is the 'best practice' for adding multiple paragraphs (HTML-like) of text in a TextView via a Strings.xml file? They only have newline characters for paragraph marks because I want them to reflow automatically on the device. I usually place this extended text into the named item within the Strings.xml, but it is impossible to view or edit the text because it scrolls off the page. I don't want to install another plugin which might mess with my regular code, I just want a way to select and edit long strings.xml items. Am I doing something wrong to place them in strings.xml? – Dell Anderson Jan 13 '14 at 20:20
  • The plugin posted in the accepted answer is not actively maintained and is no longer working. Some guy made [a successor](http://dev.cdhq.de/eclipse/word-wrap/) which works perfectly with newest Eclipse and automatically wraps all files on start. – xji Dec 01 '14 at 05:03
2

Open Eclipse -> Help -> Install New Software -> Click "Add.." button and copy this line "http://dev.cdhq.de/eclipse/updatesite/luna/" into field Name and Location -> Click OK

Check -> Line Number Ruler and Word Wrap Feature -> Next -> Next -> Finish -> Restard Eclipse!

enter image description here

Somwang Souksavatd
  • 4,947
  • 32
  • 30
  • 1
    Sadly, it seems that it no longer works, or at least it doesn't work with Texlipse and the LateX perspective in Mars. I really like Eclipse's file management and automatic building for my LateX projects and it's a shame the lack of a basic word wrap function is keeping me from definitely switching. – absay Jul 10 '15 at 20:30