I am using Eclipse Mars to write my Java code. When I use Ctrl-/ to comment some lines and then apply Ctrl-Shift-F to format them and then un-comment the lines, it turns out to two lines of code instead of just the original one:
System.out.printf("%s => Cowz Testing: testing Add a Representative\n",
new Date());
Instead of that, I wish new Date()); to be at the end of first line.
Edit: The right sequence of steps to reproduce my error have been perfectly described in Jonah Graham's answer.
(I can't see exactly the error you have faced, but I have seen something similar.)
Starting with some code like this:
public static void main(String[] args) {
System.out.printf("%s => Cowz Testing: testing Add a Representative\n", new Date());
}
Then, comment out the printf line, so it now looks like this:
public static void main(String[] args) {
// System.out.printf("%s => Cowz Testing: testing Add a Representative\n", new Date());
}
Apply formatting and the result you have is this:
public static void main(String[] args) {
// System.out.printf("%s => Cowz Testing: testing Add a
// Representative\n", new Date());
}
Uncomment the code and you have an error because the string is not terminated:
public static void main(String[] args) {
System.out.printf("%s => Cowz Testing: testing Add a
Representative\n", new Date());
}
If the above describes your issue, then I recommend disabling Enable line comment formatting in your code style.
How to do it
From the Window menu, select Preferences
Select Java / Code Style / Formatter
Press Edit...
In Comments tab, uncheck Enable line comment formatting (see screenshot)
If you are editing a built-in profile, provide a new name
Press OK twice
Now when you comment out code, it will not edit the contents of comments when you format your code.
You can still use /* */ comments to have reflowed comments, i.e. for "real" comments, not temporarily commented out code.
Finally, I would recommend that you apply the code style to your projects in addition to your workspace to ensure your whole team shares a code style.
Try this
Window > Preferences > Java > Code Style > Formatter
Click Edit
Select the Line Wrapping tab
Uncheck Force split, even if line shorter than maximum line width (...)
I've noticed Eclipse Mars had other bugs in formatting, for example when using tabs for indentation. In this bug it's explained a workaround consisting in setting the indentation policy to spaces only, set the same value to tab size and indentation size, and then switch back to tabs only.
Related
I recently upgraded my IDEA and now using IntelliJ IDEA 2022.2 Ultimate Edition.
I found the Complete Current Statement in Scala code behaves differently as in Java code, which is very annoying.
For example in Java code:
public static void main(String[] args) {
String foo = "bar"
}
Press Complete Current Statement shortcut(shift+cmd+enter for me) anywhere in line #2, will add a ; at the end of the line, and an auto-indent will be applied too:
public static void main(String[] args) {
String foo = "bar";
}
Then press Complete Current Statement again will bring you to a new line when there is nothing more to adjust.
public static void main(String[] args) {
String foo = "bar";
}
In previous version of IntelliJ, I roughly remember the behavior is same for Scala code.
But in this version of IntelliJ, when I try to do samething in Scala code, for example:
def foo (): Unit = {
throw new RuntimeException
}
When I press Complete Current Statement in line #2, nothing happens.
Could anyone please help me checkout why or how should I config to align with Java code's behavior? Thank you very much!
You don't need to use that in Scala because semicolons are optional, and almost never used. Actually, your Scala code sample is already what you would call a "complete statement".
For formatting what I do and recommend is having set File -> Settings -> Tools -> Actions on save and check Reformat code and optionally Optimize imports, and it will do both whenever you save your source file using Ctrl + S. I believe it's Cmd + S on your Mac.
This uses the default Intellij Formatter for Scala. Scala also has it's own Formatter called Scalafmt with customizable setups more control of formatting different Scala features based on your preferences. This is located at Settings -> Editor -> Code Style -> Scala.
If for some reason you would still like to use your shortcut key, then the only thing the Complete current statement can do to your Scala code is auto-indenting the current line, which for some reason it doesn't so it seems to be a bug on Intellij's side. But what you can do is replace the Auto-Indent Lines shortcut key to use your Complete current statement shortcut key instead and get the same behavior.
I am typing some class or method, and using line breaks to help me see where everything is, and it keeps getting edited out when I save. I've tried looking for an answer in the extensions and settings, but can't figure out what might be deleting the new lines.
I'll have something like:
public class SomeClass
{
public static void main (String[] args)
{
Some code here;
}
}
and it will delete the first line break after SomeClass and after args)
it will edit it to look like this:
public class SomeClass {
public static void main (String[] args) {
some code here;
}
}
How do I turn off the editing on save that deletes my new lines?
Here is settings.json:
{
"files.autoSave": "afterDelay",
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"files.exclude": {
"**/.classpath": true,
"**/.project": true,
"**/.settings": true,
"**/.factorypath": true
},
"editor.bracketPairColorization.enabled": true,
"editor.padding.bottom": 5,
"editor.padding.top": 5,
"editor.roundedSelection": false,
"javascript.format.placeOpenBraceOnNewLineForControlBlocks": true,
"javascript.format.placeOpenBraceOnNewLineForFunctions": true,
"files.insertFinalNewline": true,
"editor.renderFinalNewline": false,
"editor.trimAutoWhitespace": false,
"launch": {
"configurations": [],
"compounds": []
}
}
Hit Ctrl + Shift + P and type Save Without Formatting. Hit Enter(Return) and it should save without applying any formatting. Doing this every time doesn't make sense. So maybe you would like to change the key binding from File -> Preferences -> Keyborad Shortcuts.
The doc talks more on that here
EDIT 1: START
If you happen to have a liking for a particular style(like eclipse-java-google-style), you can consider setting up the java.format.settings.url option under File->Preferences->Settings -> Java.
You can also create your own formatting profile in eclipse and export the profile as xml file and provide the path to the xml file.
But in order to be able to do that you will first need to install the extension Language Support for Java(TM) by Red Hat.
Creating profile in eclipse:
Generally speaking you create profiles in Eclipse by going to the Windows -> Preferences -> Java -> Code Style -> Formatter -> New -> Brace Positions -> Choose the option you want(perhaps Next line is what you are looking for, click on the checkbox to modify all with the same value) -> In the same window you will see the export button(somewhere top right corner) -> export your new profile to some safe location of your choice. It will be saved as an XML file.
Copy the full path of the file and paste it in the java.format.settings.url section mentioned above.
Having a profile setup will save you lot of time. You will never have to manually format your braces/ indentation or any other piece. Define the formatting once and forget about it.
This profile setup also gives you the privilege of using the default Save of VSCode. It will format on save but as per your defined profile.
Kind of Win-Win situation for both you and VSCode's default settings.
EDIT 1: END
Press Ctrl+Shift+P in VS Code. It will open settings. There disable auto formatting of code, as in below image.
I'm trying to studying how a method in java String class works, so I created some customised code that calls that String class method.
As you can see, I have set a break point in my own code and I have set another break point in the java String class source code.
While I'm in debug mode and is on line 7 of my code, I pressed step into.
However, rather than stepping into the String class method indexOf, eclipse instead moved onto line 8 of my code.
Why is this happening? how can I step into the java string method source code?
public class TestingIndexOfMethod {
public static void main(String[] args) {
final String stringToBeSearchedThrough = "hello world";
final String substringToLookFor = "ll";
int a = stringToBeSearchedThrough.indexOf(substringToLookFor, 0);
System.out.println(a);
}
}
Edit 1:
I have already check to see, if "use step filter" is activated before asking this question on SO, and it is not activated. So I dont think "use step filter" is the problem here.
Edit 2:
step into works fine with methods I defined myself
Most probably there is a step filter which instructs the debugger to skip certain classes.
In the preferences dialog (menu Window -> Preferences) check the step filtering settings.
Either deactivate Use Step Fitlers which deactivates all step filters or deactivate the filter for the classes java.* only.
edit Another reason might be that your project is using a JRE instead of a JDK for the execution. Find below an example using a Java 8 JRE respective a Java 8 JDK.
project build path using a JRE (pay attention to jre1.8.0_112)
project build path using a JDK (pay attention to JavaSE-1.8)
edit 2 To determine the used Java runtime library add following statement in your code and run it in debug mode.
...
public static void main(String[] args) {
Stream.of(System.getProperty("sun.boot.class.path")
.split(File.pathSeparator))
.filter(s -> s.endsWith("rt.jar"))
.forEach(System.out::println);
...
In Eclipse 3.7 is code:
public class AppResources
{
private static Image titleIcon;
static
{
AppResources.titleIcon = getIconImage();
}
public static Image getTitleIcon()
{
return AppResources.titleIcon;
}
}
Where in Formatter is possible to say that before static should be empty line?
There is no separate formatting setting for the static block. For formatting purposes it appears to be treated as any other member variable (as opposed to a function or some other construct).
There is another way to end up with the formatting you want though. If you set the formatting option Blank Lines->Existing Blank Lines->Number of empty lines to preserve to be 1, then you can manually add a blank line before the static block and the formatter will not remove it.
As other answers point out, this is not possible to do for static blocks through the Eclipse formatter. One other way to accomplish this would be through search/replace in File Search in Eclipse:
From:
(^\s*\n)*(^\s*static\s*\{)
To:
\n\2
with Regular Expression enabled
How can I setup Eclipse to stop at the point an exception occurred.
I have an Eclipse breakpoint setup to break on an exception. In the code example below, the problem I'm having is Eclipse tries to open the Integer source code. Is there any way to just have debugger break at the point shown in my code example? If I move down the stack trace, I will get to this line, it'd be nice if there's a way to do this without the "Source not found" window coming up.
This can be done in Visual Studio, so it's driving me crazy not being able to find a way to do this in Eclipse.
package com.test;
public class QuickTest
{
public static void main(String[] args)
{
try
{
test();
}
catch(NumberFormatException e)
{
System.out.println(e.getMessage());
}
}
private static void test()
{
String str = "notAnumber";
Integer.parseInt(str);//<----I want debugger to stop here
}
}
I'm not sure how else to get there, but I just do:
Window -> Show View -> Breakpoints
And in that tab there is a "J!" that lets you set breakpoints on exceptions.
Preferences -> Java -> Debug -> Step Filtering
Choose packages you want to filter out when debugging (java.*, sun.*, etc)
On debug make sure the little step filtering arrow is selected.
This should do the trick.
Can't you just set a breakpoint on the Integer.parseInt(str) line?
If you point your Eclipse project at using a JDK instead of a JRE it should pick up the source code for all of the platform classes (Integer etc).
The Debug view will show the entire stack of the current execution. You need to select the line you want to see. It will be same as you had a breakpoint there, you can see all the variables etc.