10

I have to follow some strict indentation rules of Java code.

In many cases I have to jump to the 26th or 28th column of the current row. The characters between my original location and the 26th/28th column should be space characters (not TABs).

Is there a way to do this in Eclipse with some keyboard shortcuts (or some other way)? I checked all the available code style rules and couldn't find anything to help me.

Example :

    package                    com.companyname.something;

    import                     java.io.*;

    public                     class Something
                               extends SomethingElse
                               implements AnotherThing
    {
      public static final String
        SOME_NAME            = "SomeValue";

      private int              _var;

    }

As you can see, following these standards requires typing a lot of spaces. Is there a way to do it easily in Eclipse?

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 5
    Oh my, what a standard. Do you have some COBOLs trying to write java standards? – Andrew White Feb 27 '13 at 22:29
  • I'm sorry that I gave you such strange Java coding standards. -- Your Boss – rgettman Feb 27 '13 at 22:30
  • I bet your boss would want you to write code using a typewriter. – Luiggi Mendoza Feb 27 '13 at 22:31
  • I can't imagine having to write 1000s of lines of code and have to indent like that. – Jane Doh Feb 27 '13 at 22:43
  • Well, copy and paste can be helpful, but still, it's not an ideal solution. – Eran Feb 27 '13 at 22:45
  • Is this a customer or your actual boss? Do they know anything about java? – Jane Doh Feb 27 '13 at 22:45
  • If you use Vrapper, you could hit 18a after entering public to insert 18 spaces before your next variables. – corvec Feb 27 '13 at 22:47
  • 8
    this is just the most ridiculous thing I've ever read – mre Feb 27 '13 at 23:01
  • @JaneDoh It is my boss, and he knows a lot about Java. I don't think indentation has anything to do with knowing a language. – Eran Feb 27 '13 at 23:05
  • @Eran can u use eclipse plugin ??? – Madhawa Priyashantha Dec 17 '14 at 17:58
  • @FastSnail I'm not familiar with eclipse plugins, so I can't say I know how to use them. Do you have a specific plugin in mind? – Eran Dec 17 '14 at 21:09
  • @Eran actually not a specific plugin but i think you will be able to develop a simple plugin which allows you to insert 26 spaces just in one click http://www.vogella.com/tutorials/EclipsePlugIn/article.html – Madhawa Priyashantha Dec 17 '14 at 21:19
  • 2
    If your boss knows anything about Java, then he knows that what he's asking you to do is patently ridiculous and makes for extremely illegible code. Any moron can write code a computer can read. Good programmers write code PEOPLE can read. – Steve K Dec 18 '14 at 01:16
  • 2
    My recommendation has nothing to do with Eclipse: find a new job. I'm not being trite; Java developers have many employment opportunities, no reason whatsoever to work for someone or an organization that mandates such nonsense. – E-Riz Dec 19 '14 at 17:46
  • @E-Riz That comment makes very little sense. You know nothing about my job, yet you assume it's a bad position just because of some indentation requirements. I happen to have a great job in a great company, and I don't mind the indentation - it took me about 2 minutes to get used to it, and it actually makes the code very legible. I'm not saying I'm going to stick with this indentation on future jobs, I'm just saying it's not a bad indentation. – Eran Dec 20 '14 at 09:44
  • You should create a formatting profile in Preferences / Java and define all rules to be close to what you need. I think you can setup many things to behave just like that, so when you select a bunch of source code and press the rules are applied and your code reformatted. – rupps Dec 22 '14 at 23:08

5 Answers5

10

This is not a good practices but you can use this

Create Templates in eclipse Window -> Preferences -> Java -> Editor -> Templates -> New...

Name : 1
Pattern :${cursor}<give one space>

-

Name :2
Pattern :${cursor}<give two space>

.

.

.

Name : 26
Pattern :${cursor}<give 26 space>
Srinivasu
  • 1,215
  • 14
  • 32
  • This is actually not bad. It still doesn't automatically go to the column I want (i.e. if I'm at the 5th column and want to go to the 28th, I have to check the column I'm on and figure out I need to add 23 spaces), but it can save some work. I'll definitely accept if nothing better comes up soon. – Eran Dec 23 '14 at 06:44
  • Tryed so many ways to get the current column but didn't find any – Srinivasu Dec 23 '14 at 06:53
  • Is there a way to make the cursor jump to the end of the added spaces? – Eran Dec 23 '14 at 06:58
  • No,Tryed with ${cursor} , but all the spaces are removing in code – Srinivasu Dec 23 '14 at 07:05
  • @SrinivasuTalluri does this really work?? when pattern is ${cursor} it worked but doesn't work when replace give 26 with spaces ` – Madhawa Priyashantha Dec 23 '14 at 16:07
2

As ridiculous as this requirement is, one Formatter preference setting can help you adhere to it. Open the Java Code Style > Formatter preferences (either workspace or project-level) and click Edit... to modify the active profile (or create a new one if you don't want this change to affect all code you write).

enter image description here

In the Formatter dialog, select the Indentation tab and then set Tab Policy to Spaces Only and Indentation size to 28.

enter image description here

With these settings, Tab will jump to the 28th column, and you can Backspace from there as needed.

Just be aware that this will pretty much ruin the ability to use the "format in bulk" feature of Eclipse (Right-click and choose Source > Format), because it will treat all indentation places with 28 spaces, which isn't what your mandated style seems to want. This only helps during typing.


Even though this helps with such silly mandates, I repeat my comment above that the best advice here is to consider a new employer; if this is the kind of thing that is mandated, there are probably many other silly issues to deal with.

E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • That's not what I need. – Eran Dec 20 '14 at 09:55
  • Perhaps you could explain where it misses the mark. You asked: "As you can see, following these standards requires typing a lot of spaces. Is there a way to do it easily in Eclipse?" and the above procedure tries to minimize all that space-typing. – E-Riz Dec 22 '14 at 14:07
  • Your suggestion only adds 28 spaces at the start of an line empty each time TAB is pressed. If I start writing `private int` and then hit TAB, it doesn't bring me to the 28th column, nor does it even add 28 spaces. And even if it did add 28 spaces when in the middle of a line, it's still not what I need. If I start the line in the 7th column and need to skip to the 28th column, it doesn't help me to have a key that always inserts 28 spaces. – Eran Dec 22 '14 at 14:23
1

Check this out: eclipse indentation

Don't check that box, but change the tab width to 27 or 28 or whatever you need.

Jane Doh
  • 2,883
  • 3
  • 15
  • 17
  • Thanks for the link, but that's not what I need. It refers to PHP and Python, not Java. And the `Displayed tab width` in the General\Editors preferences is not what I need either, since I'm not using tabs, and I have to actually insert spaces. – Eran Feb 27 '13 at 23:17
  • 2
    You can use tabs, and eclipse can convert tabs into spaces. – Steve K Dec 18 '14 at 01:15
1

Does not entirely hit the mark, but you could use Sublime to edit while Eclipse runs. Please see top answer at https://superuser.com/questions/636699/how-to-go-to-column-by-its-number-in-sublime-text-3

A quick web search shows some Eclipse plugins. Have you tried http://jalopy.sourceforge.net/existing/indentation.html? (there's also a commercial version)

If nothing else works for you, you could probably write a Python script and set a hotkey to run it on the current file. Or you could write an AutoHotkey script to do it with sendkeys I'm pretty sure, not exactly sure how though. The AutoHotkey forum might be a good place to start (http://www.autohotkey.com/board/forum/48-support/).

But before I did anything, I'd ask my boss what s/he uses. If they say the spacebar, I'd give an incredulous look.

Community
  • 1
  • 1
dwn
  • 543
  • 4
  • 15
0

I didn't find exactly what I wanted, but I found something that helps.

Instead of defining a short cut that would jump the cursor to the 28th column, I managed to mark the 28th column with a vertical line, so that I can easily find it, without constantly checking the current column number:

enter image description here

This can be configured in Window -> Preferences -> General -> Editors -> Text Editors.

You have to check "Show print margin" and set the "Print margin column" to 27. You can even configure the color of the vertical line (I chose light gray color, so that it doesn't distract me too much).

enter image description here

Eran
  • 387,369
  • 54
  • 702
  • 768