Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for external Eclipse formatter settings #623

Closed
fbricon opened this issue Apr 12, 2018 · 12 comments
Closed

Add support for external Eclipse formatter settings #623

fbricon opened this issue Apr 12, 2018 · 12 comments

Comments

@fbricon
Copy link
Contributor

fbricon commented Apr 12, 2018

We should expose (wrapped) jdt formatter options

@gorkem
Copy link
Contributor

gorkem commented Apr 12, 2018

related to redhat-developer/vscode-java#2

@fbricon
Copy link
Contributor Author

fbricon commented Apr 20, 2018

So the idea would be to

  • map java.formatting.* options to org.eclipse.jdt.core.formatter.*
  • same default values as corresponding jdt options
  • formatter configuration would be received during startup or workspace/didChangeConfiguration requests, as part of the java settings
  • Formatting options should be held in a dedicated class.

We need a way to gather all properties keys, default values and descriptions, by scripting something that'd extract all the info from JDT's DefaultCodeFormatterOptions , FormatterMessages and FormatterMessages.properties

That'll be a LOT of keys to expose. So we'll start small with a small subset of 10 sensible options

@snjeza
Copy link
Contributor

snjeza commented Apr 21, 2018

@fbricon We could enable importing an Eclipse formatter xml file and define two following properties:

  • java.formatter.fileName - a URL to the formatter xml file; if it isn't set, we would set JDT default formatting options
  • java.formatter.profileName - a profile name; if it isn't set we would choose the first profile from the formatter xml file

I have attached the jdtls formatting options as an example.

jdtls.xml.zip

@fbricon
Copy link
Contributor Author

fbricon commented Apr 27, 2018

I can't get line wrapping to work for some reason (<setting id="org.eclipse.jdt.core.formatter.lineSplit" value="100"/> and <setting id="org.eclipse.jdt.core.formatter.comment.line_length" value="100"/> are ignored).

Seems referencing "java.formatter.url": "eclipse-java-google-style.xml" with eclipse-java-google-style.xml present at the root of my project is ignored

@snjeza
Copy link
Contributor

snjeza commented Apr 27, 2018

Seems referencing "java.formatter.url": "eclipse-java-google-style.xml" with eclipse-java-google-style.xml present at the root of my project is ignored
"org.eclipse.jdt.core.formatter.lineSplit"

I can't reproduce the issue.

"org.eclipse.jdt.core.formatter.comment.line_length"

Java LS doesn't format comments.
See https://github.com/eclipse/eclipse.jdt.ls/blob/master/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/FormatterHandler.java#L88
To format comments, we would need to use:

TextEdit format = formatter.format(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, sourceToFormat, region.getOffset(), region.getLength(), 0, lineDelimiter);

We could also add the "java.format.comments" property, false by default.

@fbricon
Copy link
Contributor Author

fbricon commented Apr 27, 2018

Changing eclipse-java-google-style.xml definitely does not trigger a workspace/didChangeWatchedFiles event

@fbricon
Copy link
Contributor Author

fbricon commented Apr 27, 2018

To format comments, we would need to use:
TextEdit format = formatter.format(CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, sourceToFormat, region.getOffset(), region.getLength(), 0, lineDelimiter);
We could also add the "java.format.comments" property, false by default.

ok let's do that, but with java.format.comments:true by default, same as eclipse

@fbricon
Copy link
Contributor Author

fbricon commented Apr 27, 2018

Any idea why <setting id="org.eclipse.jdt.core.formatter.blank_lines_after_package" value="1"/> is not applied if current blank lines after package is bigger than org.eclipse.jdt.core.formatter.blank_lines_after_package's value?

@snjeza
Copy link
Contributor

snjeza commented Apr 27, 2018

Any idea why is not applied if current blank lines after packages is bigger than org.eclipse.jdt.core.formatter.blank_lines_after_package's value?

Does it work in Eclipse?

@fbricon
Copy link
Contributor Author

fbricon commented Apr 27, 2018

yes, it works fine in Eclipse. Say if you have

package foo.bar;










public class Foo

with <setting id="org.eclipse.jdt.core.formatter.blank_lines_after_package" value="1"/>, this is actually formatted into

package foo.bar;



public class Foo

instead of

package foo.bar;

public class Foo

@snjeza
Copy link
Contributor

snjeza commented Apr 27, 2018

@fbricon I think you have used the standard Eclipse formatter in Eclipse and the Google formatter in VS Code.

I have updated the PR. Could you, please, test again.

@fbricon fbricon changed the title Expose formatter options Add support for external Eclipse formatter settings Apr 30, 2018
@snjeza
Copy link
Contributor

snjeza commented Apr 30, 2018

In order to use the formatter, you need an Eclipse formatter file like https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml

Set the following property:

"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",

The property can point to an URL or a local file path.
If the formatter xml file contains more profiles you will be able set a profile name as:

"java.format.settings.profile": "GoogleStyle",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants