How to add tab space infront of mutiple lines of code - eclipse

I am using eclipse IDE for development I have written some code in if-else block and now I want to tab indent every block, Is there any way or command or key sequence so that I can select the lines of code and simply add tab space in front of every line selected.

Use the formatter for this. Right click your file and select Source -> Format. You can also execute this for a single code block. Just select the block and right click -> Source -> Format. You can also format Javadoc this way.
If you don't like the way your code gets formatted you can change the formatter template in Windows -> Preferences -> Java -> Code -> Formatter.

Select the block(or set of statements) and just press TAB to indent. The whole selected block(or set of statements) will be moved.

Related

How can I get Eclipse to auto-indent code blocks within if and for statements?

I tried to get Eclipse to convert all of the tabs in my project to spaces like this:
Java Editor:
Click Window » Preferences
Expand Java » Code Style
Click Formatter
Click the Edit button
Click the Indentation tab
Under General Settings, set Tab policy to: Spaces only
Click OK ad nauseum to apply the changes.
And now my code is formatted without any indentations within if and for blocks, like this:
private void addAppointment(Resource resource) {
if (resource != null) {
Appt appt1 = new Appt();
appt1.setTime(new Date());
resource.setAppointment(appt1);
}
}
I really don't want to have to manually fix this in the hundreds of files in the project, how can I format to indent within if and for blocks in the whole project?
I should also say that the "Statements within blocks" checkbox in the active Formatter profile is checked. The preview it shows has a for block with an indented body, so I have no idea why that isn't being applied to my project.
#gnac provides some good options, in addition to:
Similarly you can use ctrl+shift+f (Source->Format) on each class to format it on the fly
You can select the project(s) and do Source menu -> Format to format everything in that project in one go. (No keyboard shortcut for it AFAIK.)
So once you set your formatting options you have a couple of options. You can set the preferences to format your files when saving.
Preferences->Java->Editor->Save Actions
However, if you have a lot of files this will be a pain as well. Similarly you can use ctrl+shift+f (Source->Format) on each class to format it on the fly, again having to do it on each file individually.
Inside Eclipse you can use Search->Find, enter "\t" in the text box and select the "Regular Expression" check box and then click the "Replace..." button. When the search is done, it will ask you what to replace it with. Enter 4 spaces into the "With" text field. Click Preview to see what it will do, or OK to make the changes.
I would use a find and sed to find all of the java files in a directory and replace the tabs, although this is outside of eclipse
find -iname ".java" -exec sed -i.orig 's/\t/ /g' {} +
If you're not on Linux you could use cygwin to do the same on Windows.

How to find extra newlines with RubyMine?

I am trying to detect extra newlines between two methods in my code.
How can I detect all of them with RubyMine? For example:
def some_method
end
# two new lines
def other_method
end
Edit -> Find -> Find in Path
Set "Text to find" to
end\n\n\n
Check "Regular expression"
click Find
There might be spaces before any of those newlines, so you might find more matches with
end *\n *\n *\n
You could also consider removing extra newlines automatically by reformatting your code:
make sure that Preferences -> Project settings -> Code Style -> Ruby -> Other -> "Blank lines around methods" is set the way you want (and all the other Ruby code style settings are the way you want)
in the project page, select the parent directory of all of the files you want to reformat
Code -> Reformat Code...
hit Run
Of course you would only do this if you were prepared to reformat everything.

Eclipse - How to format selected code from multiple lines to a single line?

In Eclipse, if I select some code which is split on more than one line and I want to format it on one line only, how can I do that?
I want to know if it's possible to configure the js formatter inside eclipse so to be able to use just SHIFT + CMD + F.
Code split on several lines:
$('a#pop1').fancybox({
'padding': 0,
'margin-left': 30,
'width': 728,
'height': 400,
'maxWidth': 728,
'maxHeight': 400
});
and how I would like to achieve, split on a single line.
$('a#pop1').fancybox({'padding': 0,'margin-left': 30,'width': 728,'height': 400,'maxWidth': 728,'maxHeight': 400});
You need to do couple of thing to prevent code formatting.
Follow below step :
Right click on project and open properties.
Go to Preferences > JavaScript > Code Style > Formatter
Check Enable Project Specific settings.
Click on Edit button. Change Profile Name on top of dialog.
Go to New Line tab. Un-Checked option under Object initializers section.
Now go to Line Wrapping tab. increase Maximum line width parameter value.
Click on Ok than Applly than Ok.
This will now prevent your JavaScript code to format like above.
After that select your code and press Ctrl + Shift + f key combination to format your code.
Maybe this link can be helpful for you: Using the code formatter - Eclipse
Or this one, maybe, that has got some instructions: Formatting Your Code Using the Eclipse Code Formatter
try this -
Preferences > JavaScript > Code Style > Formatter
These are the steps:
Create a new profile (since you cannot edit the builted-in one), if you haven't already, and click Edit....
Open the Line Wrapping tab.
in it edit settings for your function
Click Apply, and Ok.

Eclipse code formatter: do not wrap lines on method call

I'm trying the Eclipse Auto Formatter, but I cannot find a proper options to format according to my liking.
When I have a line like this:
MyLongNameClass myLongNameObject = otherObject.callAVeryLongMethodWithParameters(param1, param2, param3)
Eclipse auto formatter wraps it this way:
MyLongNameClass myLongNameObject = otherObject
.callAVeryLongMethodWithParameters(param1, param2, param3)
I'd rather like to have this result:
MyLongNameClass myLongNameObject =
otherObject.callAVeryLongMethodWithParameters(
param1, param2, param3)
Keeping the object and the method accessor on the same line, and avoiding starting a new line with '.'. Is this possible? Is there an option for this?
I'm not sure if you can really do what you wanted, but this might get you close:
In eclipse Mars:
Go to Window -> Preferences -> Java -> Code Style -> Formatter
Create your own profile based on one of the built-ins using the New... button. By default this will take you into the editing window. If you already have a formatter set up that you want to change, select it and use the Edit... button.
In the profile editing window do the following:
Select the Line Wrapping tab
In the left scroll-box, go to Function Calls -> Qualified invocations.
In the Settings for qualified invocations area select Do not wrap.
In the left scroll-box, go to Function Calls -> Arguments
Select Wrap first element, others where necessary in the argument settings then select Indent by one.
You can test these settings by adjusting the line width in the preview window.
I know this is a late submission, but I hope it helps someone else who searches for this same feature.
Go to Windows -> Preferences -> Java -> Code style -> Formatter
You can create your own profile with customised formatting, you can also modify existing formatter.
Edit -> Line Wrapping
Explore line wrapping and change it accordingly.

How to disable auto formatting upon save in Eclipse?

Eclipse keeps auto formatting upon save, for example: it is insisting the following code should be in a single line once I save, despite breaking it up the way I want to.
#Transactional(rollbackFor = DataAccessException.class, readOnly = false, timeout = 30, propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT)
Window > Preferences > Java > Editor > Save Actions.
Can also be configured at the project level. Project > Properties > Java Editor > Save Actions.
Absolutely maddening improvement. For if statements do the following:
Window | Preferences | Java | Code Style | Formatter | Control statements tab
Select Keep simple if on one line.
You might also try the Line wrapping tab
This is a Late answer, But just for novice to understand it clearly I have a snapshot of the setting.
Please follow the path on left.
And change highlighted setting.
If you wish to keep the auto-formatter, but don't want this behavior, you can also configure it in the fomatter :
right click on the project -> properties -> java code style -> formatter
(If you don't have a project specific formatter the global configuration can be accessed this way : Window -> Preference -> Java -> Code Style -> Formatter).
Edit... the active profile, open the Line Wrapping tab, and there check the Never join already wrapped lines option.
This way you can wrap lines wherever you want and eclipse won't remove your line breaks (which is a must if you wish to have readable lambda expressions), but keep the formatter active.
This option is there at least since eclipse Luna
Here is a screenshot where this option is :
Just wanted to add a tip. If you want to avoid the line wrapping you can simply increase the "Maximum line width" value to something enormous. As long as that is sufficiently high eclipse won't do any wrapping.
Preferences > Java > Code Style > Formatter > Edit > Line Wrapping > Maximum line width