How to make Eclipse align the ?: ternary operator? - java

I need the exact same effect as in this question but in Eclipse.
It should only do the alignment if I explcitly insert a new line before the ":" or if the second operand (the "true" expression) is too long.
Example:
a = cond ? "a veeeeeeeeeeeeeeeery loooooooooooooooooooooooooooong string"
: "";
// ^ put the colon here

This original answer was for Galileo which is well over a decade ago, so I'm updating it to be a little more current. Original answer remains below.
For Eclipse 2021-12 (under Window though I hope it would be the same across platforms):
Go into Project, Properties on the menu.
Under Java Code Style, choose Formatter.
For the current project only, enable project-specific settings. For workspace settings, simply click on the Configure workspace settings. Then click the edit button.
In the selection pane, under Line Wrapping, Wrapping settings, Other expressions, there is a Conditionals item that you can edit.
The options you want are:
wrap before operators;
wrap where necessary;
no force split; and
indent on column.
They give this as an example:
This is the dated answer for Galileo:
Go into Project, Properties.
Under Java Code Style, choose Formatter.
For the current project only, enable project-specific settings. For workspace settings, simply click on the Configure workspace settings. Then click the edit button.
On the line-wrapping tab, choose Expressions, Conditionals.
Choose the relevant line wrapping policy and indentation policy.
I have "wrap only when necessary" and "indent on column" which appears to be close to what you want.
If you're not running Galileo, other versions of Eclipse should at least be close to that process. If my settings aren't quite what you want, you may need to fiddle with them a bit, but that Formatter section is pretty well the entire exposed interface so, if it can't be done with that, it can't be done (short of writing your own plug-in or finding a more configurable third-party one).

Related

Eclipse style auto close bracket in IntelliJ Idea (java) [duplicate]

Does IntelliJ have the ability to tab out of auto-completed brackets? As in, when you press "tab" key near a closing bracket, the cursor will jump over it instead of adding a new tab. This is a pretty standard feature in Eclipse. In IntelliJ IDEA you must use arrow keys to do so.
Similar question was asked 4 years ago here, but maybe 4 year isn't enough time to complete this complex feature, so I'm asking again.
UPDATE:
Initial implementation for this feature is available starting from IntelliJ IDEA 2018.2 version. It works more like in MS Visual Studio - without visual indication of tab 'exit' position. For now, it should work in Java, SQL, Python and some other files. It will take some time for other languages to catch up - some language-specific code changes are required. The feature is disabled by default, you can enable it in:
Settings (Preferences on Mac)| Editor | General | Smart Keys | Jump outside closing bracket/quote with Tab.
Original answer:
Nothing new since the original question was submitted, except a third-party plug-in that was started by one of the users.
For some reason the author didn't release a binary version of the plug-in yet.
I went ahead and built the plug-in, you can get the jar file here.
Copy it into the IDE plugins folder, restart IDEA and use Shift+Space.
Keyboard shortcut can be changed in the Keymap settings:
Feature requests and bug reports should go here.
Just type in the closing bracket again, it will get you out of the bracket.
For example if you are typing "Sandeep", as soon as you enter ".. it will be like ""... then you enter rest of the text "sandeep".. and at the end type " again... it will take you out of the double quotes.
I have demonstrated above for double quotes , you can do the same for any brackets.

How can I prioritize variable names over types in Eclipse's content assist?

My Eclipse seems really bad at choosing what I want for my content assist. As you can see in this picture below, I literally have a variable defined on the previous line called "deleteTable", yet when I type "deT" and hit ctrl+space, it gives me suggestions for random classes which start with "Det" which I've never used before in my life.
In my Preferences -> Java -> Editor -> Content Assist -> Advanced options, in the first section, I only have the options "Java Proposals (Code Recommenders)" and "Template Proposals" as the two types of proposals I have enabled. And in the second section, I have cycling turned off (so when I hit ctrl+space multiple times by accident it doesn't change the suggestion drop-down).
What can I do to fix these unintuitive content assist suggestions?
This behavior is due to a bug Code Recommenders’ Subwords completion, which has since been fixed. Update to the newest version of Code Recommenders from the stable update site.

CheckStyle plugin for IDEA - java way of warnings presentation and automatic fixes?

Is it possible to make it present the list of warnings java/spellchecker style as opposed to a old school list of errors with line numbers?
Can it automatically fix things like missed space near '=' etc?
Maybe I need a different plugin, which one then?
Try QAPlug - Checkstyle plugin. Then go Analyze - Analyze Code (not the standard Analyze!) then click "..." near Run with chosen profile select a concrete profile and press the import button.
In the next dialog select the checkstyle rules file and then click the OK button. That's it. Just wait for the results. I got something like this.
That plugin has its limitations however. For example, I didn't manage to use a file with suppressions with it.
You can also use the plugin CheckStyle-IDEA which also has good configurations and allows you to use suppressions, custom checks and displays the inspection warnings in the editor immediately, but to apply that you need to set Project Settings - Inspections - CheckStyle - Real-Time Scan in the IDE settins.
Answering the second question, I didn't find any automatical reformat capabilities in these plugins, just checks.

Can't fold comments in Eclipse 3.5.2

Aren't i supposed to be able to do this? Folding is enabled in the Java preferences, but no +/- button appears next to comments. I can fold imports and classes and methods, but not comments. Also, checking the Comments box in "Initially fold these elements" does nothing.
It could be a side-effect of another option setting the code folding for a plugin you don't have.
See bug 153449 for an example (written at the time when Mylyn was called Mylar):
I can enable and disable folding, and I see my editor margin increase/decrease
in size providing a gutter for the code folding markers to appear in, but they
simply aren't there. Also, all of the menu options to expand all and collapse
all are there and enabled but they do nothing.
Actually, I just saw this in my exported preferences:
/instance/org.eclipse.jdt.ui/editor_folding_provider=org.eclipse.mylar.java.ui.editor.foldingprovider
And I don't have Mylar installed. I'm betting this is the problem. I'll try
to get rid of it and see what happens.

Keyboard shortcuts in Eclipse

Is there a way to change a shortcut in Eclipse that when I press that shortcut Eclipse will write some line of code? Let's say I press Ctrl+Shift+P would it be possible that Eclipse will write <p></p>?
Thanks
Have a look at the templates support.
Window → Preferences → Java → Editor → Templates
Here is an article describing custom eclipse templates.
As far as I know, no, but as aioobe answered, you can use templates for it. Instead of that keyboard combo, you'd instead type something like par Ctrl+Space to get Eclipse's autosuggest for creating an paragraph open / close tag (with the cursor in the middle). This'll either display a dropdown if Eclipse is unsure, or just chuck your template down if it has no doubts.
Note also that, when editing HTML files (or anything XML~like), you can get Eclipse to auto-close tags, so typing <p> will automatically add a closing tag too. This might of course be a bit trickier due to the Shift+< keyboard combo's (depending on your finger's agility and, possibly, keyboard configuration). I have no trouble with it though.

Categories

Resources