-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: - declare maven dependency in plugin module (using manual copy for now) - publish packaged jar (copy google-java-format-eclipse-plugin-0.0.1.jar to your Eclipse dropins folder) - support other (older/newer) Eclipse versions - documentation #52 #52 (comment)
- Loading branch information
Showing
12 changed files
with
354 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,9 @@ | |
|
||
.classpath | ||
.project | ||
.factorypath | ||
.settings/ | ||
.apt_generated/ | ||
|
||
target/ | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry exported="true" kind="lib" path="lib/guava-19.0.jar"/> | ||
<classpathentry exported="true" kind="lib" path="lib/google-java-format-1.3-SNAPSHOT.jar"/> | ||
<classpathentry exported="true" kind="lib" path="lib/javac-9-dev-r3297-1-shaded.jar"/> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
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,34 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>google-java-format-eclipse</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.pde.ManifestBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.pde.SchemaBuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
<nature>org.eclipse.pde.PluginNature</nature> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
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,7 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
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,15 @@ | ||
Manifest-Version: 1.0 | ||
Bundle-ManifestVersion: 2 | ||
Bundle-Name: Google Java Formatter | ||
Bundle-SymbolicName: google-java-format-eclipse-plugin;singleton:=true | ||
Bundle-Version: 0.0.1 | ||
Bundle-RequiredExecutionEnvironment: JavaSE-1.8 | ||
Require-Bundle: org.eclipse.jdt.core;bundle-version="3.10.0", | ||
org.eclipse.jface, | ||
org.eclipse.text, | ||
org.eclipse.ui, | ||
org.eclipse.equinox.common | ||
Bundle-ClassPath: ., | ||
lib/guava-19.0.jar, | ||
lib/javac-9-dev-r3297-1-shaded.jar, | ||
lib/google-java-format-1.3-SNAPSHOT.jar |
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,8 @@ | ||
source.. = src/ | ||
output.. = target/classes | ||
bin.includes = META-INF/,\ | ||
.,\ | ||
plugin.xml,\ | ||
lib/google-java-format-1.3-SNAPSHOT.jar,\ | ||
lib/javac-9-dev-r3297-1-shaded.jar,\ | ||
lib/guava-19.0.jar |
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,2 @@ | ||
* | ||
!.gitignore |
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<?eclipse version="3.4"?> | ||
<plugin> | ||
<extension point="org.eclipse.jdt.core.javaFormatter"> | ||
<javaFormatter | ||
class="com.google.googlejavaformat.java.GoogleJavaFormatter" | ||
id="com.google.googlejavaformat.java.GoogleJavaFormatter" | ||
name="Google Java Formatter (1.3-SNAPSHOT)"> | ||
</javaFormatter> | ||
</extension> | ||
</plugin> |
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,103 @@ | ||
<!-- | ||
~ Copyright 2015 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. | ||
--> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.google.googlejavaformat</groupId> | ||
<artifactId>google-java-format-parent</artifactId> | ||
<version>1.3-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>google-java-format-eclipse-plugin</artifactId> | ||
<version>0.0.1</version> | ||
<packaging>eclipse-plugin</packaging> | ||
<name>Google Java Format Plugin for Eclipse 4.5+</name> | ||
|
||
<description> | ||
A Java source code formatter that follows Google Java Style. | ||
</description> | ||
|
||
<properties> | ||
<tycho-version>0.26.0</tycho-version> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>mars</id> | ||
<layout>p2</layout> | ||
<url>http://download.eclipse.org/releases/mars</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.googlejavaformat</groupId> | ||
<artifactId>google-java-format</artifactId> | ||
<version>1.3-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
|
||
<plugin> | ||
<groupId>org.eclipse.tycho</groupId> | ||
<artifactId>tycho-maven-plugin</artifactId> | ||
<version>${tycho-version}</version> | ||
<extensions>true</extensions> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.eclipse.tycho</groupId> | ||
<artifactId>target-platform-configuration</artifactId> | ||
<version>${tycho-version}</version> | ||
<configuration> | ||
<environments> | ||
<environment> | ||
<os>linux</os> | ||
<ws>gtk</ws> | ||
<arch>x86</arch> | ||
</environment> | ||
<environment> | ||
<os>linux</os> | ||
<ws>gtk</ws> | ||
<arch>x86_64</arch> | ||
</environment> | ||
<environment> | ||
<os>win32</os> | ||
<ws>win32</ws> | ||
<arch>x86</arch> | ||
</environment> | ||
<environment> | ||
<os>win32</os> | ||
<ws>win32</ws> | ||
<arch>x86_64</arch> | ||
</environment> | ||
<environment> | ||
<os>macosx</os> | ||
<ws>cocoa</ws> | ||
<arch>x86_64</arch> | ||
</environment> | ||
</environments> | ||
</configuration> | ||
</plugin> | ||
|
||
</plugins> | ||
</build> | ||
|
||
</project> |
116 changes: 116 additions & 0 deletions
116
eclipse_plugin/src/com/google/googlejavaformat/java/GoogleJavaFormatter.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,116 @@ | ||
/* | ||
* 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 com.google.common.base.Preconditions; | ||
import com.google.common.collect.Range; | ||
import com.google.googlejavaformat.java.SnippetFormatter.SnippetKind; | ||
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; | ||
|
||
/** Runs the Google Java formatter on the given code. */ | ||
public class GoogleJavaFormatter extends CodeFormatter { | ||
|
||
private static final int INDENTATION_SIZE = 2; | ||
|
||
@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 * INDENTATION_SIZE; | ||
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)); | ||
} | ||
return editFromReplacements( | ||
new SnippetFormatter() | ||
.format( | ||
snippetKind, source, rangesFromRegions(regions), initialIndent, includeComments)); | ||
} 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; | ||
} | ||
|
||
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; | ||
} | ||
} |
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