I use many brackets and braces when I code. Be it casting multiple times, casting multiple times in if blocks, etc. I sometimes get lost in the brackets and also, hate putting a lot of them.
Is there any short-cut key to format this selected part of code?
I have tried Ctrl-Shift-F, but that doesn't give me what I want.
Note: I work on Eclipse Mars.
What you want is Source menu | Cleanup, then customize the profile to add the remove extra parenthesis. From the Code Style tab check the Use parenthesis in expressions Only if necessary. And then complete the wizard:
That changes:
super.start(((BundleContext)(context)));
to:
super.start(context);
As an extra you can set your project to do code cleanup tasks on save automatically if you desire.
did you try ctrl+3 and type formate? for me Ctrl-Shift-F was bound by other app and cause eclipse to miss catching it.
search for formatter Java->code style->formatter
There is a tab Braces.
If you select specific lines and press Ctrl + Shift + F eclipse will only format the lines you selected according to your formatter.
Related
I know how to jump to the next and previous error (F2, see SO Link, jetbrains.com), but is there a way to easily jump between suggestions?
If possible, I would really love to have a keyboard shortcut for this, as I am currently learning Kotlin and IntelliJ often has nice suggestions on how to improve the code.
If there is no keyboard shortcut to jump between suggestions, is there at least any other way?
With suggestion I mean things like the following (Ignore the "nonsense code")):
F2 also works to navigate between warnings. If there are errors in the file, it will jump to the next error. If not, it will jump to the next warning.
Adding to yole's answer: if you want to jump backwards you can use Shift + F2 instead.
Additionally you can alter the severity of each inspection (or inspection group) using the settings menu File | Settings | Editor | Inspections or directly using the intention action context menu (ALT + ENTER or similar) when dealing with a highlighted element (be it warning, error, information, etc.).
Note that some inspections are not enabled by default and many allow customization to your needs (e.g. class naming conventions, etc.).
I did once increase the severity of all the inspections and switch them back one by one as needed or even turned many off (or switched them to "weak warnings"), just to easily jump back and forth between them. I can't recommend that approach anymore ;-) (too many highlightings then... but it may help to find out about inspections you consider more helpful).
Is there a way to tell IntelliJ to stop rewriting my code to match its preferences? I'm especially concerned with this when it comes to code that has already been committed to source control. For example, the following line was mysteriously changed to the second example because IntelliJ preferred the latter spacing.
assertLess((new Date().getTime()/1000)-8,attrs.mtime);
assertLess((new Date().getTime() / 1000) - 8, attrs.mtime);
I don't want this to happen. Any ideas?
You might need to uncheck this:
There is an option in IDE Settings -> Editor -> Smart Keys named Reformat on Paste:
Check it is set to anything other than Reformat Block, for example Indent Each Line.
I look for some trick which gives me possibility to change the syso behaviour in Eclipse (please assume that below there is Eclipse´s editor), now it works in this way:
syso%someVariable
% - means the plase when i typed ctrl + space, and result is:
System.out.println();someVariable
but I want to have of course without copying text..:
System.out.println(someVariable);
Any hints ? :-) Thanks in advance !
Reference
The feature is called "code templates" in Eclipse. You can add templates with Preferences->Java->Editor->Templates. Two good articles:
http://eclipse.dzone.com/news/effective-eclipse-dont-write-c
http://eclipse.dzone.com/news/effective-eclipse-custom-templ
Also, this SO question:
Useful Eclipse Java Code Templates
System.out.println() is already mapped to sysout, so you may save time by learning a few of the existing templates first.
If you wan´t to change current template you can press shift + end (to select 'someVariable') and then press ctrl + space and up, enter to select sysout.
Answering the original question or at least how I understood it:
Modify the existing template syntax for sysout (Preferences->Java->Editor->Templates find sysout and select edit) to read:
System.out.println(${cursor});
Now you can type syso and then ctr+space and instantly get the output:
System.out.println(<cursorHere>);
Or as you put it:
System.out.println(someVariable);
...with the cursor inside the brackets which is indeed more efficient than starting after the semicolon.
Is it normal that the default behavior for code completion when editing Java is the following?
If I import a package, after typing the first ., any key I press results in the first item in the list being selected
Sometimes while typing keywords like public and protected, NetBeans will make some wild leaps and insert private after just a p or pu are typed!
In general, it tends to make some big guesses in terms of autocompletion, guesses that are not supported by my previous input at all.
I feel like this cannot be the desired behavior for every user. Is there a way to make the code completion not autocomplete anything, or at least make it way less eager?
Edit: To be clear, I do not want to have to disable the sometimes-useful "Autopopup Window." I want the window to eagerly appear with tips and suggestions for methods, syntax, whatnot - I just don't want NetBeans to try to guess what I want, because it is wrong 90 - 100% of the time.
Which version of Netbeans are you using because that doesn't seem to be the behaviour I am seeing in 7.1.
AutoCompletion popups don't appear until I hit Ctrl+Space.
If I type "p" and then hit Ctrl + Space, the popup shows "private", "protected" and "public". I can either keep typing (and the list gets smaller as possible matches are removed) or use the cursors to select the word I want and hit Enter.
If there is only one logical option e.g. I start typing "pu" and then hit Ctrl+Space, then Netbeans inserts "public" automatically (though this can be turned off).
Code templates are a different story, I have them set to be activated using Shift+Space. As far as I know, templates don't show any popups. Anyone who likes pain can consider changing the activation key to just Space :P
I definitely share your complaints, especially re: 1. A quick google search reveals ways to turn off autocomplete:
AutoComplete drives me crazy. I’ve always hated it – I feel like it
slows my computer down and sometimes it even seems to derail me in the
middle of typing something because the window comes up and somehow
snags focus. I turn it off under Options, Editor, General (uncheck
Auto Popup Completion Window).
Item 2) sounds more like the "code template" feature rather than the auto-completion.
If that gets in your way, you have two options:
change the key that "triggers" the code-template expansion (e.g. from "Space" to "Tab"
remove all two-stroke templates (or any other abbreviation that you would rather not have)
Both can be done under "Tools -> Options -> Editor -> Code Templates"
I just have two specific questions that over the years I have never been able to figure out in netbeans.
How do I access the Java API documentation without having to use my web browser. It would be nice if I could have a window in netbeans of the Java API. Is this not possible? How do you normally access the API document? Note that I don't mean just access for the documentation for the current code (cntrl-space, I think)... I just mean for browsing.
Code completion will automatically generate right brackets (which I like). When this occurs, the cursor is still within the brackets. How do you jump out of the brackets without mousing? I know a command to move to the end of line, but sometimes the brackets aren't at the end of the line... it seems like there should be a way to just jump to the end of the autocompletion.
For 2) I honestly just use the right arrow.
re 1) I guess you are looking for: Windows -> Others -> Javadoc
Additionally if you use Shift-F1 (instead of Alt-F1) on e.g. a classname it will show the documentation in the internal browser as well.
re 2) I don't think there is a shortcut for this, but you might be able to do this with a (recorded) keyboard macro (by searching for a closing bracket).
You can open the JavaDoc window
with Window > Other > JavaDoc
Typing the closing bracket or
parenthesis just puts you outside of
the generated brackets