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.
Related
When i type sout in IntelliJ, it doesn't show the System.out.println() shortcut, and instead shows WSDLOutput, WSDLOutputImpl, JSWBlend_SRC_Outpeer, and LSOutput.
This live template works in Java files inside a method (not in the root of the class where there can be no executable code except the static initializer blocks).
So, make sure you are inside a method and also check Java class in inside the sources root.
Sample class to try:
public class Main {
public static void main(String[] args) {
sout <- press Tab here
}
}
You can expand it using the Tab key.
Go to Preferences -> Live Templates -> Java. Check that you have the sout live template. If you don't, add a new live template under the user section with the abbreviation sout and the text System.out.println().
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);
...
I am working on a Java project that has some Clojure involved. I know how to run compile and run clojure code:
public static void main(String[] args) throws Exception {
RT.init();
runCode();
}
public static Object runCode() {
String str = "(ns my-ns)" +
"(defn add [a b] (+ a b))" +
"(println (add 1 2))";
Compiler.load(new StringReader(str));
/* I know how to invoke it: */
Var foo = RT.var("my-ns", "add");
return foo.invoke(1,2);
}
What would be very useful at the point is to have a way to iterate over forms in Java, and in some sense "analyze" the compiler output. Basic things I want to know is:
What is the text source of a form?
What function is being called in a form.
What arguments are being passed to the function (forms are ok)
Be able to do this on top level forms, or drill in as needed.
Is there a way to do this using the clojure compiler, or runtime (or other Java classes in Clojure?) I see such compiler methods as analyze, for example:
Expr target = analyze(C.EXPRESSION, RT.second(form));
Though its not clear to me yet how form was constructed, and there are no Javadoc :-). Do I need to go The Compiler Source and figure out how it works?
I'm using intellij communicated version and also add checkstyle plugin
how ever, i made simple java file just say hello
public class hello {
public static void main(string[] final args)
{
system.out.println("hello, world");
}
}
it's simply run. however checkstyle tell me there is problem at the last line.
he told me
Got an exception-expecting EOF, Found '}' error
I don't know what is the problem. block is correctly close.
is there something i need to add or fix that ?
This problem became a real issue in the checkstyle project. Basically it was related with the use of lambdas, yet the problem exposed herebefore is clarely not related to Java 8 lambdas. You can check the issue here. You can solve it by specifying a valid version. In gradle it would be:
apply plugin: 'checkstyle'
checkstyle {
toolVersion = "6.1.1"
}
Try formatting it like
public class Hello
{
public static void main(String[] args)
{
System.out.println("hello, world");
}
}
and the "s" in system.out.println and string[], should be capital
Checkstyle is a tool which is used to find flaws in formatting and coding conventions followed if any exists in the code. The rules are set using by configuring the checkstyle. And if any part of your code does not abide by them, it will throw an exception. In most of the cases exceptions will be self explanatory. You can use google depending on the exception you get.
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.