Reggie
Reggie

Reputation: 523

Eclipse (ctrl+shift+f) does nothing

I made a new Java file in Gedit.

When I open it in Eclipse (CTRL + SHIFT + F), it does not format the code.

(CTRL + SHIFT + F) still formats other Java files (which I also did not create in Eclipse), and it even formats individual lines of code in this particular file when I press (enter) at the beginning of a line.

It just will not format the entire file with (CTRL + SHIFT + F). I restored defaults in:

Window > Preferences > General > Keys

But still nothing. I have installed no plugins.

Any idea why Eclipse isn't recognizing the file as something that should be formatted when I use CTRL + SHIFT + F?

Upvotes: 5

Views: 17947

Answers (10)

ryvantage
ryvantage

Reputation: 13496

My solution:

I had to right-click the project -> Properties -> Java Code Style -> Formatter. The Active profile said "Unmanaged project from ..." (I can't recall the exact wording because it's gone now ha). I changed that to the formatter I wanted and now it works.

Upvotes: 0

K.Andy Wang
K.Andy Wang

Reputation: 421

Lee Duhem's answer let me realize where the issue is.The key CTRL + SHIFT + F may be conflicted by other apps,in my case,it's language input method.

System: Windows 10

  1. Go to settings Language options enter image description here

  2. Open Microsoft Pinyin Options enter image description here

  3. Swift off or change another hot key enter image description here

Upvotes: 0

Daniel
Daniel

Reputation: 1

This is an issue caused by the Razer Cortex application. Its keybind for enabling/disabling its in-game FPS monitor is the same as Eclipse's keybind for formatting a highlighted block of code (Ctrl + Shift + F). This program takes precedence over Eclipse in that regard.

Circumventing this issue is extremely simple:

  1. Open the Razer Cortex application
  2. Change or disable the keybind for showing/hiding the FPS Display

Your formatting keybind will work in Eclipse now :)

Upvotes: 0

henriquefalc
henriquefalc

Reputation: 361

The following is the solution that worked for me:

In Window -> Preferences -> General -> Keys, look for all commands using the Ctrl+Shift+F shortcut. To do so, click on the Binding column, so the commands will be sorted by shortcut, then scroll down the grid.

In my case, there was another command using this shortcut, that probably had higher priority than the format source command. So I just cleared the Binding field for it, and leave only the "Format" commands using Ctrl+Shift+F.

Upvotes: 0

codewiz
codewiz

Reputation: 196

I don't understand why it wouldn't format the code with CTRL + SHIFT + F by try selecting the whole document with CTRL + A) and then using CTRL + I to format the document. You may have the same problem though.

Upvotes: 4

sffc
sffc

Reputation: 6424

Check for weird syntax in your file. In my case, I had a method with an underscore parameter name:

@Deprecated
public synchronized void setParseMaxDigits(int _) {}

I had to delete sections of my file until I pinpointed that this was the function causing SHIFT+CTRL+F to not format the file. As soon as I changed the above method to,

@Deprecated
public synchronized void setParseMaxDigits(int unused) {}

then SHIFT+CTRL+F started working again.

Upvotes: 1

Arash moradabadi
Arash moradabadi

Reputation: 365

Check the followings:

  1. The chosen active formatter profile is exactly what you want(You can find it in Preferences->Java->Code Style->Formatter)
  2. The code lines which needs to format is selected correctly
  3. And the most important one is not to have any syntax error in the file which is formatting

Upvotes: 1

Lee Duhem
Lee Duhem

Reputation: 15121

I also encountered this problem. And I figured out the reason by luck.

I am using IBus input method, and when IBus in Chinese mode, Ctrl+Shift+F does not format Java source code in Eclipse Java project. When I changed IBus to English mode, that shortcut works just fine.

Upvotes: 2

Kacy
Kacy

Reputation: 3430

I had this exact same problem just now. The auto formatter seemed to not work for only a single file. I even deleted and recreated the file but no luck. I eventually decided to keep working and all of a sudden it worked again.

The problem was I had a syntax error (a random comma laying around) that the auto formatter did not know how to format. Fixing the syntax error allowed the auto formatter to do its thing again.

Upvotes: 0

nitind
nitind

Reputation: 20013

Eclipse can only format a Java file when it is in a Java Source Folder of a Java Project. It won't do random ones you just Open from the File menu.

Upvotes: 0

Related Questions