-
Notifications
You must be signed in to change notification settings - Fork 861
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 all commits
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
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
162 changes: 162 additions & 0 deletions
162
eclipse_plugin/src/com/google/googlejavaformat/java/AbstractJavaFormatter.java
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 |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright 2017 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.googlejavaformat.java; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.core.dom.ASTParser; | ||
import org.eclipse.jdt.core.formatter.CodeFormatter; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.jface.text.Region; | ||
import org.eclipse.text.edits.MultiTextEdit; | ||
import org.eclipse.text.edits.ReplaceEdit; | ||
import org.eclipse.text.edits.TextEdit; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.Range; | ||
import com.google.googlejavaformat.java.SnippetFormatter.SnippetKind; | ||
|
||
/** | ||
* This class provides the bulk of the logic to run java formatter within Eclipse. It provides the | ||
* ability to formatting options to be specified. | ||
*/ | ||
public abstract class AbstractJavaFormatter extends CodeFormatter { | ||
|
||
@Override | ||
public TextEdit format( | ||
int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { | ||
IRegion[] regions = new IRegion[] {new Region(offset, length)}; | ||
return formatInternal(kind, source, regions, indentationLevel); | ||
} | ||
|
||
@Override | ||
public TextEdit format( | ||
int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { | ||
return formatInternal(kind, source, regions, indentationLevel); | ||
} | ||
|
||
@Override | ||
public String createIndentationString(int indentationLevel) { | ||
Preconditions.checkArgument( | ||
indentationLevel >= 0, | ||
"Indentation level cannot be less than zero. Given: %s", | ||
indentationLevel); | ||
int spaces = indentationLevel * indentationSize(); | ||
StringBuilder buf = new StringBuilder(spaces); | ||
for (int i = 0; i < spaces; i++) { | ||
buf.append(' '); | ||
} | ||
return buf.toString(); | ||
} | ||
|
||
/** Runs the Google Java formatter on the given source, with only the given ranges specified. */ | ||
private TextEdit formatInternal(int kind, String source, IRegion[] regions, int initialIndent) { | ||
try { | ||
boolean includeComments = | ||
(kind & CodeFormatter.F_INCLUDE_COMMENTS) == CodeFormatter.F_INCLUDE_COMMENTS; | ||
kind &= ~CodeFormatter.F_INCLUDE_COMMENTS; | ||
SnippetKind snippetKind; | ||
switch (kind) { | ||
case ASTParser.K_EXPRESSION: | ||
snippetKind = SnippetKind.EXPRESSION; | ||
break; | ||
case ASTParser.K_STATEMENTS: | ||
snippetKind = SnippetKind.STATEMENTS; | ||
break; | ||
case ASTParser.K_CLASS_BODY_DECLARATIONS: | ||
snippetKind = SnippetKind.CLASS_BODY_DECLARATIONS; | ||
break; | ||
case ASTParser.K_COMPILATION_UNIT: | ||
snippetKind = SnippetKind.COMPILATION_UNIT; | ||
break; | ||
default: | ||
throw new IllegalArgumentException(String.format("Unknown snippet kind: %d", kind)); | ||
} | ||
List<Replacement> replacements = | ||
createSnippetFormatter() | ||
.format( | ||
snippetKind, source, rangesFromRegions(regions), initialIndent, includeComments); | ||
if (idempotent(source, regions, replacements)) { | ||
// Do not create edits if there's no diff. | ||
return null; | ||
} | ||
// Convert replacements to text edits. | ||
return editFromReplacements(replacements); | ||
} catch (IllegalArgumentException | FormatterException exception) { | ||
// Do not format on errors. | ||
return null; | ||
} | ||
} | ||
|
||
private List<Range<Integer>> rangesFromRegions(IRegion[] regions) { | ||
List<Range<Integer>> ranges = new ArrayList<>(); | ||
for (IRegion region : regions) { | ||
ranges.add(Range.closedOpen(region.getOffset(), region.getOffset() + region.getLength())); | ||
} | ||
return ranges; | ||
} | ||
|
||
/** @return {@code true} if input and output texts are equal, else {@code false}. */ | ||
private boolean idempotent(String source, IRegion[] regions, List<Replacement> replacements) { | ||
// This implementation only checks for single replacement. | ||
if (replacements.size() == 1) { | ||
Replacement replacement = replacements.get(0); | ||
String output = replacement.getReplacementString(); | ||
// Entire source case: input = output, nothing changed. | ||
if (output.equals(source)) { | ||
return true; | ||
} | ||
// Single region and single replacement case: if they are equal, | ||
// nothing changed. | ||
if (regions.length == 1) { | ||
Range<Integer> range = replacement.getReplaceRange(); | ||
String snippet = source.substring(range.lowerEndpoint(), range.upperEndpoint()); | ||
if (output.equals(snippet)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private TextEdit editFromReplacements(List<Replacement> replacements) { | ||
// Split the replacements that cross line boundaries. | ||
TextEdit edit = new MultiTextEdit(); | ||
for (Replacement replacement : replacements) { | ||
Range<Integer> replaceRange = replacement.getReplaceRange(); | ||
edit.addChild( | ||
new ReplaceEdit( | ||
replaceRange.lowerEndpoint(), | ||
replaceRange.upperEndpoint() - replaceRange.lowerEndpoint(), | ||
replacement.getReplacementString())); | ||
} | ||
return edit; | ||
} | ||
|
||
/** | ||
* Create an instance of the formatter that will be used. This method should be aligned with | ||
* {@link #indentationSize()} such that the indentation size corresponds with the indent level of | ||
* the format options used to create the snippet formatter. | ||
*/ | ||
protected abstract SnippetFormatter createSnippetFormatter(); | ||
|
||
/** | ||
* The number of spaces per indent level. This method should be aligned with {@link | ||
* #createSnippetFormatter()} such that the indentation size corresponds with the indent level of | ||
* the format options used to create the snippet formatter. | ||
*/ | ||
protected abstract int indentationSize(); | ||
} |
30 changes: 30 additions & 0 deletions
30
eclipse_plugin/src/com/google/googlejavaformat/java/AospJavaFormatter.java
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.google.googlejavaformat.java; | ||
|
||
import com.google.googlejavaformat.java.JavaFormatterOptions.Style; | ||
|
||
/** Runs the AOSP Java formatter on the given code. */ | ||
public class AospJavaFormatter extends AbstractJavaFormatter { | ||
|
||
@Override | ||
protected SnippetFormatter createSnippetFormatter() { | ||
return new SnippetFormatter(JavaFormatterOptions.builder().style(Style.AOSP).build()); | ||
} | ||
|
||
@Override | ||
protected int indentationSize() { | ||
return 4; | ||
} | ||
} |
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.
🤔 maybe
SnippetFormatter#indentationSize()
can be madepublic
instead? That does mean a modification to its public API, but since this caller could use it, maybe so can others.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.
I can make it public, though I tend to prefer more restrictive access until a need arises. That said, I'll make change if you prefer. Please advise.
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.
Well, it seems a need arose ;).
But in all seriousness: I'm not a maintainer of this project, so my advice carries little weight. Let's see what the maintainers say.