Hidden features of Eclipse [closed]

2019-01-01 14:09发布

Alright it can be a lame question, but everybody uses these things differently. What's some of the best time savers out there for this IDE.

Tom

标签: java eclipse ide
30条回答
看淡一切
2楼-- · 2019-01-01 14:22

Type 'syso' then press Ctrl+Space to expand it to System.out.println().

Tres handy.

查看更多
裙下三千臣
3楼-- · 2019-01-01 14:23

Hippie expand/Word Complete, afaik inspired by Emacs: will autocomplete any word in any editor based on other words in that file. Autocomplete inside String literals in Java code, in xml files, everywhere.

Alt + /

查看更多
零度萤火
4楼-- · 2019-01-01 14:24

Alt + Shift + R to refactor and rename.

查看更多
浅入江南
5楼-- · 2019-01-01 14:24

Don't forget Ctrl+Shift+L, which displays a list of all the keyboard shortcut combinations (just in case you forget any of those listed here).

查看更多
有味是清欢
6楼-- · 2019-01-01 14:25

Alt-Up Arrow moves the current selection up a line, Alt-Down Arrow moves it down. I also use Alt-Shift-Up/Down Arrow all the time. Ctrl-K and Ctrl-Shift-K is quite handy, finding next/previous occurrence of the current selection (or the last Find, if nothing is selected).

查看更多
浪荡孟婆
7楼-- · 2019-01-01 14:26

Ctrl-2 something

Seems that nobody mentioned Ctrl-2 L (assign to new local variable) and Ctrl-2 F (assign to a new field), these ones have changed how I write code.

Previously, I was typing, say (| is cursor location):

Display display = new |

and then I pushed Ctrl-Space to complete the constructor call. Now I type:

new Display()|

and press Ctrl-2 L, which results in:

Display display = new Display()|

This really speeds things up. (Ctrl-2 F does the same, but assigns to a new field rather than a new variable.)

Another good shortcut is Ctrl-2 R: rename in file. It is much faster than rename refactoring (Alt-Shift-R) when renaming things like local variables.

Actually I went to Keys customization preference page and assigned all sorts of additional quick fixes to Ctrl-2-something. For example I now press Ctrl-2 J to split/join variable declaration, Ctrl-2 C to extract an inner class into top-level, Ctrl-2 T to add throws declaration to the function, etc. There are tons of assignable quick fixes, go pick your favourite ones and assign them to Ctrl-2 shortcuts.

Templates

Another favourite of mine in my “npe” template, defined as:

if (${arg:localVar} == null)
    throw new ${exception:link(NullPointerException,IllegalArgumentException)}("${arg:localVar} is null");

This allows me to quickly add null argument checks at the start of every function (especially ones that merely save the argument into a field or add it into a collection, especially constructors), which is great for detecting bugs early.

See more useful templates at www.tarantsov.com/eclipse/templates/. I won't list them all here because there are many, and because I often add new ones.

Completion

A few code completion tricks:

  • camel case support mentioned in another answer: type cTM, get currentTimeMillis
  • default constructor: in the class declaration with no default constructor push Ctrl-Space, the first choice will be to create one
  • overloading: in the class declaration start typing name of a method you can overload, Ctrl-Space, pick one
  • getter/setter creation: type “get”, Ctrl-Space, choose a getter to create; same with “is” and “set”

Assign To A New Field

This is how I add fields.

  1. If you have no constructors yet, add one. (Ctrl-Space anywhere in a class declaration, pick the first proposal.)

  2. Add an argument (| is cursor position):

    public class MyClass {
        public MyClass(int something|) {
        }
    }
    
  3. Press Ctrl-1, choose “assign to a new field”. You get:

    public class MyClass {
        private final Object something;
        public MyClass(Object something) {
            this.something = something;
        }
    }
    
  4. Add a null-pointer check if appropriate (see “npe” template above):

    public class MyClass {
        private final Object something;
        public MyClass(Object something) {
            npe|
            this.something = something;
        }
    }
    

    Hit Ctrl-Space, get:

    public class MyClass {
        private final Object something;
        public MyClass(Object something) {
            if (something == null)
                throw new NullPointerException("something is null");
            this.something = something;
        }
    }
    

A great time saver!

查看更多
登录 后发表回答