简体   繁体   中英

Is there a way to create a shortcut in Eclipse that would jump the cursor to the nth column in the current row?

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?

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>

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).

在此处输入图片说明

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

在此处输入图片说明

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.

Check this out: eclipse indentation

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

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.

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:

在此处输入图片说明

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).

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM