-
Notifications
You must be signed in to change notification settings - Fork 859
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
This adds an AOSP formatter to the Eclipse plugin. #251
Open
bryanschofield
wants to merge
2
commits into
google:master
Choose a base branch
from
bryanschofield:feature/eclipse-aosp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,27 +16,13 @@ | |
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Iterators; | ||
import com.google.common.collect.Range; | ||
import com.google.common.collect.RangeSet; | ||
import com.google.common.collect.TreeRangeSet; | ||
import com.google.common.io.CharSink; | ||
import com.google.common.io.CharSource; | ||
import com.google.errorprone.annotations.Immutable; | ||
import com.google.googlejavaformat.Doc; | ||
import com.google.googlejavaformat.DocBuilder; | ||
import com.google.googlejavaformat.FormattingError; | ||
import com.google.googlejavaformat.Newlines; | ||
import com.google.googlejavaformat.Op; | ||
import com.google.googlejavaformat.OpsBuilder; | ||
import java.io.IOError; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.openjdk.javax.tools.Diagnostic; | ||
import org.openjdk.javax.tools.DiagnosticCollector; | ||
import org.openjdk.javax.tools.DiagnosticListener; | ||
|
@@ -52,6 +38,22 @@ | |
import org.openjdk.tools.javac.util.Log; | ||
import org.openjdk.tools.javac.util.Options; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Iterators; | ||
import com.google.common.collect.Range; | ||
import com.google.common.collect.RangeSet; | ||
import com.google.common.collect.TreeRangeSet; | ||
import com.google.common.io.CharSink; | ||
import com.google.common.io.CharSource; | ||
import com.google.errorprone.annotations.Immutable; | ||
import com.google.googlejavaformat.Doc; | ||
import com.google.googlejavaformat.DocBuilder; | ||
import com.google.googlejavaformat.FormattingError; | ||
import com.google.googlejavaformat.Newlines; | ||
import com.google.googlejavaformat.Op; | ||
import com.google.googlejavaformat.OpsBuilder; | ||
|
||
/** | ||
* This is google-java-format, a new Java formatter that follows the Google Java Style Guide quite | ||
* precisely---to the letter and to the spirit. | ||
|
@@ -65,8 +67,8 @@ | |
* is a final EOF token to hold final comments. | ||
* | ||
* <p>The formatter walks the AST to generate a Greg Nelson/Derek Oppen-style list of formatting | ||
* {@link Op}s [1--2] that then generates a structured {@link Doc}. Each AST node type has a visitor | ||
* to emit a sequence of {@link Op}s for the node. | ||
* {@link Op}s [1--2] that then generates a structured {@link Doc} . Each AST node type has a | ||
* visitor to emit a sequence of {@link Op}s for the node. | ||
* | ||
* <p>Some data-structure operations are easier in the list of {@link Op}s, while others become | ||
* easier in the {@link Doc}. The {@link Op}s are walked to attach the comments. As the {@link Op}s | ||
|
@@ -100,9 +102,14 @@ public Formatter(JavaFormatterOptions options) { | |
this.options = options; | ||
} | ||
|
||
/** @return The options used for this formatter. */ | ||
public JavaFormatterOptions options() { | ||
return options; | ||
} | ||
|
||
/** | ||
* Construct a {@code Formatter} given a Java compilation unit. Parses the code; builds a {@link | ||
* JavaInput} and the corresponding {@link JavaOutput}. | ||
* JavaInput} and the corresponding {@link JavaOutput} . | ||
* | ||
* @param javaInput the input, a Java compilation unit | ||
* @param javaOutput the {@link JavaOutput} | ||
|
@@ -114,7 +121,8 @@ static void format(final JavaInput javaInput, JavaOutput javaOutput, JavaFormatt | |
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); | ||
context.put(DiagnosticListener.class, diagnostics); | ||
Options.instance(context).put("allowStringFolding", "false"); | ||
// TODO(cushon): this should default to the latest supported source level, remove this after | ||
// TODO(cushon): this should default to the latest supported source | ||
// level, remove this after | ||
// backing out | ||
// https://github.com/google/error-prone-javac/commit/c97f34ddd2308302587ce2de6d0c984836ea5b9f | ||
Options.instance(context).put(Option.SOURCE, "9"); | ||
|
@@ -138,9 +146,9 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept | |
JavacParser parser = | ||
parserFactory.newParser( | ||
javaInput.getText(), | ||
/*keepDocComments=*/ true, | ||
/*keepEndPos=*/ true, | ||
/*keepLineMap=*/ true); | ||
/* keepDocComments= */ true, | ||
/* keepEndPos= */ true, | ||
/* keepLineMap= */ true); | ||
unit = parser.parseCompilationUnit(); | ||
unit.sourcefile = source; | ||
|
||
|
@@ -168,7 +176,8 @@ static boolean errorDiagnostic(Diagnostic<?> input) { | |
} | ||
switch (input.getCode()) { | ||
case "compiler.err.invalid.meth.decl.ret.type.req": | ||
// accept constructor-like method declarations that don't match the name of their | ||
// accept constructor-like method declarations that don't match the | ||
// name of their | ||
// enclosing class | ||
return false; | ||
default: | ||
|
@@ -210,8 +219,8 @@ public String formatSource(String input) throws FormatterException { | |
* @param input the input string | ||
* @return the output string | ||
* @throws FormatterException if the input string cannot be parsed | ||
* @see <a | ||
* href="https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing"> | ||
* @see <a href= | ||
* "https://google.github.io/styleguide/javaguide.html#s3.3.3-import-ordering-and-spacing"> | ||
* Google Java Style Guide - 3.3.3 Import ordering and spacing</a> | ||
*/ | ||
public String formatSourceAndFixImports(String input) throws FormatterException { | ||
|
@@ -246,8 +255,10 @@ public ImmutableList<Replacement> getFormatReplacements( | |
String input, Collection<Range<Integer>> characterRanges) throws FormatterException { | ||
JavaInput javaInput = new JavaInput(input); | ||
|
||
// TODO(cushon): this is only safe because the modifier ordering doesn't affect whitespace, | ||
// and doesn't change the replacements that are output. This is not true in general for | ||
// TODO(cushon): this is only safe because the modifier ordering doesn't | ||
// affect whitespace, | ||
// and doesn't change the replacements that are output. This is not true | ||
// in general for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you reformatted the code, maybe manually fix this section? |
||
// 'de-linting' changes (e.g. import ordering). | ||
javaInput = ModifierOrderer.reorderModifiers(javaInput, characterRanges); | ||
|
||
|
@@ -276,7 +287,8 @@ public static RangeSet<Integer> lineRangesToCharRanges( | |
for (Range<Integer> lineRange : | ||
lineRanges.subRangeSet(Range.closedOpen(0, lines.size() - 1)).asRanges()) { | ||
int lineStart = lines.get(lineRange.lowerEndpoint()); | ||
// Exclude the trailing newline. This isn't strictly necessary, but handling blank lines | ||
// Exclude the trailing newline. This isn't strictly necessary, but | ||
// handling blank lines | ||
// as empty ranges is convenient. | ||
int lineEnd = lines.get(lineRange.upperEndpoint()) - 1; | ||
Range<Integer> range = Range.closedOpen(lineStart, lineEnd); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems the imports were reordered according to Eclipse's default ordering, instead of retaining the order specified by the style guide.