Sometimes I want annotations on fields be in a single line and sometimes in a line each. Is there any way to make Eclipse formatter just ignore these annotations and leave the lines breaks just as I did?
Not quite sure what you mean, but you break up lines for field this way:
String text =
"cake" +
"more cake" +
"alot more cake";
This is also an option:
You can go to Properties -> Java code style -> Formatter -> Edit: Then there should be some tags to on/off.
Also include this line in your code:
/* #formatter:on */
I think so. In Preferences go to Java - Code Style - Wrapper. Then edit your active profile, click "Line Wrapping" and then "Annotations - Element-value pairs". Then set the Line wrapping policy to "Do not wrap".
Related
How to automatically format comments with one space between // and words? For example, selecting format in my editors should convert
//login to // login
public interface LoginService {
//login // -> // login
SysAdminDO login(LoginForm form);
}
You are probably looking for the setting
Editor ~> Code Style ~> Java(language) ~> Code Generation
Or else you can alternatively use Comment with line comment shortcut
Just type a line and on that line use the shortcut. e.g.
String var;
I would make this a comment [Press the shortcut]
// I would make this a comment
I am using a MessageConsole in Eclipse to display output information. The output is formatted into Error 1 - (MyClass.java:10), which is expected to generate a clickable link to code (MyClass.java line 10, in this case), since the console should be able to parse the pattern (FileName.java:LineNumber) automatically as suggested in this post.
However, it failed to work this way. But when I use System.out.println() to output this pattern directly in the plugin Eclipse, the link can be generated.
I also considered the possibility of multiple consoles in the plugin, but streaming the patterned text to other consoles did not work either. Any insights?
My code is like below:
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
MessageConsole myConsole = new MessageConsole( name, null );
conMan.addConsoles( new IConsole[]{myConsole} );
MessageConsoleStream out = myConsole.newMessageStream();
out.println("Error 1 - (MyClass.java:10)");
Matching for Java code links is only done for consoles which have the javaStackTraceConsole console type.
So you can use the org.eclipse.ui.console.consolePatternMatchListeners extension point to define your own pattern matcher to do the same thing for your console.
Or you can use the:
public MessageConsole(String name, String consoleType, ImageDescriptor imageDescriptor, boolean autoLifecycle)
constructor to specify the console type for your console to match the existing matchers.
I am working on a project with legacy code that had used Log4J in the past, and now uses SLF4J. Most of the old style Log4J logging statements remain, and I convert the log statements slowly to the new SLF4J style as I come across them.
I've just realised that pressing Alt+Enter when in the old style log statements gives me the following options (unfortunately I cannot upload a screenshot):
Copy String concatenation text to the clipboard
Inject language or reference
Replace '+' with 'String.format()'
Replace '+' with 'StringBuilder.append()'
Replace '+' with 'java.test.MessageFormat.format()'
The option Replace '+' with 'String.format()' is very close to what I need, although I don't need the String.format( bit.
Is there something that would give me an Intention Action of: Replace Log4J style log statement with SLF4J style log statement?
As an example of old logging style (e.g. Log4J), I mean:
LOGGER.debug("User " + user.getUserId() + " has performed a search with the criteria " + dto);
And by new style I mean:
LOGGER.debug("User {} has performed a search with the criteria {}", user.getUserId(), dto);
Have you tried the Java | Logging issues | Non-constant string concatenation as argument to logging call inspection? It has a quickfix to automatically convert a string concatenation to a parameterized log message.
Thanks to Wim Deblauwe's comment about SSR I have discovered Edit | Find | Structural Replace and using the following to fix simple cases where two arguments are used in a logging statement:
Search template:
LOGGER.debug("$str1$" + $arg1$ + "$str2$" + $arg2$)
Replacement template:
LOGGER.debug("$str1${}$str2${}", $arg1$, $arg2$)
I doubt I am using the Structural Search and Replace feature to its maximum capability, and will have to do a few sweeps to get all the logging statements, but this is great progress. Thank you Wim.
I am not that much aware of Eclipse Shortcuts.
I copied code from some link and I pasted in Eclipse Indigo but it is coming like
"public String doLogin() throws ApplicationException{ long executionStartTime = System.cu... }"
I want to format it in java style like
public String doLogin() throws ApplicationException{
long executionStartTime = System.cu...
}
I google it and found few shortcuts like,
"Shift + Tab" , "Ctrl + I", "Ctrl + Shift + F". but is not giving me the behavior I want.
is there I need to add custom formatter or I am expecting more.
You can format text using the Ctrl+Shift+F shortcuts.
You can use Ctrl+A to select all text or you can format only several lines (which you have selected).
There is a caveat however: If your code does not compile the formatting does not work. I don't know whether this is intentional or a bug.
You may want to enable auto-formatting in Eclipse it is a handy feature I usually use:
You can find the formatter settings here:
I also recommend the Eclipse Color Theme plugin!
With default key mapping Ctrl+Shift+F should format your code (current class or selection if any). Of course syntax has to be valid.
You should be able to access that feature using the menu Source > Format where shortcut is displayed if existing.
Type Ctrl+Shift+L to get list of shortcuts in Eclipse.... and to format Ctrl+Shift+F
You can create your own formatter:
Window -> Preferences -> Java -> Code Style -> Formatter.
"Ctrl + Shift + F" should work but make sure you remove " from start and end of the code you copied.
select all text using ctrl + a
and press
Ctrl + Shift + F
to format text
I just created a generator for some fluent interfaces. Now I have lots of code looking like this:
new MyFluentInterface()
.setFirst( "first" )
.setSecond( "second" )
.setThird( "third" )
.invoke( obj );
I like the indentation shown above, but I can't find a way to configure eclipse to indent this correctly.
eclipse always indents like this:
new MyFluentInterface()
.setFirst( "first" )
.setSecond( "second" )
.setThird( "third" )
.invoke( obj );
How can I configure eclipse so that it indents this fluent interface pattern as shown in my first code example?
With Eclipse 3.6, this seems doable by configuring your custom Java > Code Style > Formatter profile. Edit it and go to the Line Wrapping tab and select Function Call > Qualified invocations. Then, in the Settings for qualified invocations, configure things like this:
This will (should) produce the expected result:
SomeEntity e1 = new SomeEntity.Builder()
.age(10)
.amount(10.0d)
.firstname("foo")
.lastname("bar")
.build();
But this will obviously affect all the code, which I personally don't like. So I'm using the new Off/On Tags from Eclipse 3.6 (last tab when editing a profile):
And enclose the parts that don't get formatted as I want and do it myself:
// #formatter:off
SomeEntity e2 = new SomeEntity.Builder()
.age(10)
.amount(10.0d)
.firstname("foo")
.lastname("bar")
.build();
// #formatter:on
Pick your poison :)