This should be easy to answer:
I have Eclipse set to wrap Java code over 80 chars to respect my margin.
This code never wraps:
if (expressionItem.type.isTypeCompatibleWith(containingNameLink.type) == false) {
reportParsingError("expression type incompatible with containing "
+ "context.");
}
Question: how do I set my code formatter preferences so that the margin is respected?
Did you press CTRL + SHIFT + F (or right click > source > format) ? Here it works fine. The code is not automatically formatted as you type, you need to tell eclipse to format it.
Click Windows > Preferences. Go to Java > Editor > Save Action Enable Perform the selected action on save then enable Formatt source code option.
Refer below pic:
Click on Formatter link to edit settings for Formatter.
Related
I'm on Ubuntu. Pushing Ctrl + Shift + i in VS Code auto formats the file.
I was editing a solidity contract and pushed Ctrl + Shift + i, VS Code didn't have a formatter configured so it asked me to pick one - I accidentally chose my JS prettifier extension instead of the solidity one. This badgered up my code by trying to use single quotes instead of double quotes (not allowed in solidity) and some other non-solidity friendly changes.
Pushing Ctrl + Shift + i now just auto formats with the wrong formatter with no option to pick a different one.
How can I change which linter/formatter is associated to which file types in VS Code?
Solution A:
Press Ctrl+Shift+P
Then type Format Document With...
At the end of the list click on Configure Default Formatter...
Now you can choose your favorite beautifier from the list.
Solution B:
go to file -> preferences -> settings search for format, on the left side, click on Text Editor, the very first item on the right side is Editor: Default Formatter from the dropdown you can pick any document formatter which you installed before.
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.
How do you auto-format code in Eclipse?
On Windows and Linux : Ctrl + Shift + F
On Mac : ⌘ + ⇧ + F
(Alternatively you can press Format in Main Menu > Source)
Another option is to go to Window->Preferences->Java->Editor->SaveActions and check the Format source code option. Then your source code will be formatted truly automatically each time you save it.
CTRL + SHIFT + F will auto format your code (whether it is highlighted or non highlighted).
This can also be done at the Project Level:
In the Package Explorer, right-click on the project > Properties > Java Editor > Save Actions
This might be preferable when working as a team so that everyone's code is saved with the same format settings.
Notice: It did not format the document unless I corrected all mistakes. Check your file before pressing CTRL + SHIFT + F.
Press: Ctrl + A or highlight the part of the code you wish to indent
and then press Ctrl + I.
Windows -> Preferences -> Java -> Editor -> save actions -> Format source code -> Format Edited lines (or) format all lines.
Some time when you work as a team, lead don't want you to format all lines of the code in a source file (Huge track changes will be there on commit). So, select 'Format Edited lines'. This will edit and format only the lines you modified.
Gubs
You can do with the steps below
press Ctr + A (windows) or cmd + A (Mac os)
Ctr + I in windows or cmd + I in Mac os
It will auto format your code
The secret is simple: Ctrl+Shift+F
Update your IDE with the latest PDT version for better code formatting.
On the main menu click Help -> Install New Software and then add the following URL in the Work with field:
http://download.eclipse.org/tools/pdt/updates/4.0.1
When asked for the name, give it PDT4.0.1 and then move along with the update or install.
It will see if the appropriate PDT is already installed or if it is a lower version, which then would be updated.
After restarting or applying the changes go to Windows -> Preferences
on the side bar and expand PHP -> Code Style. Here you will see an item named Formatter. Select it and choose the active profile for the code formating. Thats it.
The next time you format it, it will choose a format according to the chosen active profile.
Hope it helps.
Also note that you can also "protect" a block from being formatted with #formatter:off and #formatter:on, avoiding a reformat on a comment for example, like in:
// Master dataframe
Dataset<Row> countyStateDf = df
.withColumn(
"countyState",
split(df.col("label"), ", "));
// I could split the column in one operation if I wanted:
// #formatter:off
// Dataset<Row> countyState0Df = df
// .withColumn(
// "state",
// split(df.col("label"), ", ").getItem(1))
// .withColumn(
// "county",
// split(df.col("label"), ", ").getItem(0));
// #formatter:on
countyStateDf.sample(.01).show(5, false);
You can do this with
Ctrl + I
or
Ctrl + Shift + F
right-click on the project > Properties > Java Editor > Save Actions
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
When I format in eclipse long lines become from this :
String str = instance.someMethod("jhdajhajsha").someOtherMethod("sakjsaksja");
to this :
String str = instance.someMethod("jhdajhajsha")
.someOtherMethod("sakjsaksja");
How do I prevent this?
Good practice is to place line-break like that.
Any way you can edit you format setting
Window > Preferences
It will show the Preferences Window
In that Expand the Java then Code Style and select Formatter
Here you can edit Active profile
In the edit, Line Wrapping tab, The General Settings there is a option to set the
Maximum line width give the maximum limit here, When you format, it will break the line more than this limit
windows > preferences > java > code style > formatter
Create a new profile. in the profile setting, select Line Wrapping [tab]
disable line wrapping for all . (class declaration, constructor declaration etc etc etc.. )