So I am new to the Intellij Idea IDE and so far I like it a lot. Just having some trouble getting the code style settings exactly how I like them for Java. I am a fan of not using braces for one line if statements but I like for them to be on two lines:
if(true)
dosomething();
Right now when I run code formatting in Idea, it moves up into one line:
if(true) dosomething();
and I find that very hard to read, but I cannot seem to get the settings just right. Can anyone help me out and tell me what I need to set to keep it on two lines but still no braces? Feel pretty dumb not being able to find it. Thanks!
Deselect Simple blocks in one line in settings -> CodeStyle -> WrappingandBraces, then it should work. It worked for me.
Have you tried Control Statements on one line? Does it for me.
Well this might sound strange, but even I had the option simple blocks in one line unchecked. And I was facing same issue. But then if I left one empty line after the opening brace of method, IntelliJ didn't move entire code to one line.
For better understanding,
Problematic behavior
public void methodA(){
System.out.println("No blank line left");
}
Now when you close and open the file again, IntelliJ does this
public void methodA(){ System.out.println("No blank line left"); }
Correct Behavior
public void methodA(){
System.out.println("left a blank line above");
}
Now when you close and open the file again, it still stays like that
public void methodA(){
System.out.println("left a blank line above");
}
Just to make a note, IntelliJ version is Ultimate 14.1.4
For Webstorm - IntelliJ for Javascript, this option is in Preferences->Editor->Code Style -> Javascript-> Wrapping and Braces.
Related
I have a habit when writing code:
If I want to write a code block inside a {}, I will type {} first, then move the cursor into between { and }, press Enter and write my code.
In IntelliJ IDE, after I press Enter, IDE auto break and format for {} for me to write code (see this image with green arrow).
With Netbeans, it doesn't work like that (see the image with red screen).
My question is how to setting Netbeans IDE to work like IntelliJ to convenient for me to write code.
I have never written in NetBeans before. But I can understand you - it is so inconvenient and painful.
I have been going through all the combinations and haven't found the one needed.
The options that might repeat this behaviour somehow include:
Typing a { and pressing the enter. Code completion will automatically add the second brace and put you in the right place.
Using the combination cmd+enter and enter.
Then I figured out that we can write a macro:
split-line insert-break
The result:
Unfortunately, the macro is contextless. We can't write the condition "if the caret is between braces do our action otherwise, do the standard one". So, it can be assigned to another hotkey (not enter) to make enter work correctly in its cases.
When I write my code in NetBeans, there are times when I want to add/remove if statements and loops. However, every time I want to add/remove a statement but keep the content, I have to fix all the indenting for the contained code. Likewise, every time I try to paste code into a statement, the indenting is all messed up and I have to fix it.
Is there any way to highlight the code and fix indentation?
I'm aware that Eclipse has an option to fix all formatting upon saving. I'm not looking for something that changes formatting upon saving, rather only something I can click on the moment I want the formatting fixed. I put an example of what I'm talking about below.
if (condition == true){
//pasted code
//pasted code
//pasted code
}
or
//other code
//removed if statement
//original code that is spaced too far right
//original code that is spaced too far right
//removed }
//other code
Try to paste using Ctrl + Shift + V.
If it worked, look at the netbeans options to configure the shortcut you want.
To change shortcuts go to : Tools > Options > Keymap
Here is a table of defaults shortcuts in netbeans : https://netbeans.org/project_downloads/usersguide/shortcuts-80.pdf
It might be useful, you can notice the "Ctrl-Shift-V Paste formatted" shortcut.
I recently found the AIDE IDE for Android, and love the fact that the editor puts it's braces on the same line when declaring and new code block, like so:
public void foo(){}
Instead of
public void foo(){
}
I think it looks a lot cleaner when you don't need to put much in the method. Is there anyway in Eclipse to do this? I've already looked at the braces settings, but it doesn't seem there's anything in here about this.
You can uncheck the "insert new line in empty method/class/block body" under "New Lines" which is three tabs to the right of "Braces."
I am using Eclipse Juno and have never had any problems with it, until its content assist stopped working only in some parts of my code. The code below shows what I am talking about:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
mWTBatch.setText(mRTBatch.getText());
mWTExp.setText(mRTExp.getText());
mWTName.setText(mRTName.getText());
mWTQuantity.setText(mRTQuantity.getText());
/* Here content assist is not working */
}
});
mWTBatch.setText("Here it is working again");
That piece of code is just inside one method of my class. The problem is that it works fine inside the methods of the class, but inside objects (new ActionListener(){}) it returns "No default proposals". It has this behavior when I either use "Ctrl+space" or type in "." after the object I want to get suggestions of. I've already searched here for the solution and googled it, but couldn't find a solution. What I have already tried:
Window->Preferences->Editor->Content Assist->Advanced and checked Java Proposals. It didn't work. I've even tried checking it myself, without just using Restore Defaults. I also tried the Java Proposals from the other table. Nothing. At last I tried checking all the fields with Java on them. Nothing worked.
Deleted my workspace folder completely, created a new one and imported my project. As my project is from an SVN repository, I used the project folder inside "trunk" folder, but I didn't copy the content of the folder to the workspace when importing (I need it to be in the repository). Still nothing, it continues with the error.
I reproduced the same situation of the code above into another class of another different project, which was also imported to the workspace, but having its content copied to it. The content assist worked for that one.
I then re-imported the project with the original code from above, but this time copying its content to workspace (which means it had nothing to do anymore with the SVN repository). It didn't work either.
I also checked if the ctrl+space is bind to the content assist on Eclipse settings and it is ok. I also checked if it had nothing to do with advanced key settings of Windows 7 language bar. I had seen on the internet that could be problem, but it was not my case.
I even created a new Java project on the workspace and copied the .java files one by one from the old project, set all the build path manually and in the end it still doesn't work.
For all the first four cases above I also cleaned the projects and closed and reopened them, not forgetting to refresh it after each action. Could anyone tell me how to fix this? I would like to find a solution to this problem, so that others may not be like me on it for 2 days. I think my question is important because I am gathering here many probable solutions found by googling the problem and from StackOverflow, and nothing solved it.
I stumbled into this issue the other day, and after seeing this post thought that I'd have to just live with it.
However, I did find a "hack" around it:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener( //<---------------------- Problem arises because we're inside a function declaration ...
new ActionListener()
{ // <--------------------------------------------- ... yet we're trying to write a function
public void actionPerformed(ActionEvent e)
{
/* Here content assist is not working */
}
}
);
mWTBatch.setText("Here it is working again");
From the content assistants point of view, this is just plain wrong, so we need to give it a little help:
mWTBatch.setText("Here content assist works");
medCopyBtn.addActionListener(
new ActionListener()
// <--------------------------------------------- CURLY BRACKET MISSING
public void actionPerformed(ActionEvent e)
{
/* Here content assist IS WORKING */
}
}
);
mWTBatch.setText("Here it is still working");
This will obviously give you an error upon compilation time, but it gives you full access to the content assist for the rest of the function declaration.
Also, it doesn't matter which curly bracket you remove from inside the function declaration, so long as it's an opening curly bracket.
One other point, if you only remove the opening curly bracket like I did above, then eclipse will NOT add in another closing curly bracket automatically for most cases (because by the time you enter the new opening curly bracket, you've equalised the opening vr closing of curly brackets). You can get around this by deleting the closing curly bracket as well, but then you have to remember to put two curly brackets back.
Hope that helps the 1165 views this question has garnered over the past year =)
Do a search for "Change display language" on the start menu. 'Change keyboards' > Look to see if you have any non English keyboards under 'General' > 'Installed services'. If you have more than just English then pressing Ctrl+Spacebar is changing your focus from Eclipse to the language selector on the taskbar. Remove any other keyboard languages from the list if that is your problem.
Working with eclipse in a java collapsed file if i try to write a method between two collapsed methods when I write public and press space automatically the ide collapses the word public inside the method below.
State initial
- public methodA()
- public methodC()
Step 1:
- public methodA()
public
- public methodC()
Eclipse bug
- public methodA()
- public methodC() //Error because public is declared twice!
If I write the method faster the problem doesn't occur. Any form to avoid this weird behaviour without disable code folding?
Thanks in advance
What I do is to unfold the method before where I want to insert the code. Then it doesn't actually fold the new text into the previous (folded) method. But the simplest way is probably to just not fold the code and use the outline view to navigate around the file.
You really have only a few things you can do besides disabling folding. Even though the "public" you just typed gets folded into the following method it is still present in the text and the cursor is still just after the last letter. You can:
Continue typing and the public will become visible, as will the following method that was previously folded. This will through off the formatting, though.
Press Enter. This will move you to the next line and unfold the following method, but it will disentangle the code you're typing from that next method. You're on the next line after the "public" but the formatting stays nice. Just typing a backspace will get the cursor back into the right place and you can continue typing from there.