Skip to content

Commit 760a13c

Browse files
sormurascushon
authored andcommitted
Eclipse plugin (google#118)
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 google#52 google#52 (comment)
1 parent 4ae067a commit 760a13c

File tree

12 files changed

+354
-0
lines changed

12 files changed

+354
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
.classpath
66
.project
7+
.factorypath
78
.settings/
9+
.apt_generated/
810

911
target/
1012

core/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,51 @@
147147
</archive>
148148
</configuration>
149149
</plugin>
150+
<plugin>
151+
<artifactId>maven-resources-plugin</artifactId>
152+
<version>3.0.1</version>
153+
<executions>
154+
<execution>
155+
<id>copy-resources</id>
156+
<phase>package</phase>
157+
<goals>
158+
<goal>copy-resources</goal>
159+
</goals>
160+
<configuration>
161+
<outputDirectory>../eclipse_plugin/lib</outputDirectory>
162+
<resources>
163+
<resource>
164+
<directory>target</directory>
165+
<include>${project.artifactId}-${project.version}.jar</include>
166+
</resource>
167+
</resources>
168+
</configuration>
169+
</execution>
170+
</executions>
171+
</plugin>
172+
<plugin>
173+
<groupId>org.apache.maven.plugins</groupId>
174+
<artifactId>maven-dependency-plugin</artifactId>
175+
<version>2.10</version>
176+
<executions>
177+
<execution>
178+
<id>copy-dependencies</id>
179+
<phase>package</phase>
180+
<goals>
181+
<goal>copy-dependencies</goal>
182+
</goals>
183+
<configuration>
184+
<outputDirectory>../eclipse_plugin/lib</outputDirectory>
185+
<overWriteReleases>true</overWriteReleases>
186+
<overWriteSnapshots>true</overWriteSnapshots>
187+
<excludeTransitive>true</excludeTransitive>
188+
<excludeArtifactIds>org.eclipse.jdt.core</excludeArtifactIds>
189+
<excludeScope>compile</excludeScope>
190+
<excludeScope>provided</excludeScope>
191+
</configuration>
192+
</execution>
193+
</executions>
194+
</plugin>
150195
</plugins>
151196
</build>
152197
</project>

eclipse_plugin/.classpath

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5+
<classpathentry kind="src" path="src"/>
6+
<classpathentry exported="true" kind="lib" path="lib/guava-19.0.jar"/>
7+
<classpathentry exported="true" kind="lib" path="lib/google-java-format-1.3-SNAPSHOT.jar"/>
8+
<classpathentry exported="true" kind="lib" path="lib/javac-9-dev-r3297-1-shaded.jar"/>
9+
<classpathentry kind="output" path="target/classes"/>
10+
</classpath>

eclipse_plugin/.project

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>google-java-format-eclipse</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.pde.ManifestBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.pde.SchemaBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.m2e.core.maven2Builder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
31+
<nature>org.eclipse.pde.PluginNature</nature>
32+
<nature>org.eclipse.jdt.core.javanature</nature>
33+
</natures>
34+
</projectDescription>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.compliance=1.8
5+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
6+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
7+
org.eclipse.jdt.core.compiler.source=1.8

eclipse_plugin/META-INF/MANIFEST.MF

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Manifest-Version: 1.0
2+
Bundle-ManifestVersion: 2
3+
Bundle-Name: Google Java Formatter
4+
Bundle-SymbolicName: google-java-format-eclipse-plugin;singleton:=true
5+
Bundle-Version: 0.0.1
6+
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
7+
Require-Bundle: org.eclipse.jdt.core;bundle-version="3.10.0",
8+
org.eclipse.jface,
9+
org.eclipse.text,
10+
org.eclipse.ui,
11+
org.eclipse.equinox.common
12+
Bundle-ClassPath: .,
13+
lib/guava-19.0.jar,
14+
lib/javac-9-dev-r3297-1-shaded.jar,
15+
lib/google-java-format-1.3-SNAPSHOT.jar

eclipse_plugin/build.properties

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source.. = src/
2+
output.. = target/classes
3+
bin.includes = META-INF/,\
4+
.,\
5+
plugin.xml,\
6+
lib/google-java-format-1.3-SNAPSHOT.jar,\
7+
lib/javac-9-dev-r3297-1-shaded.jar,\
8+
lib/guava-19.0.jar

eclipse_plugin/lib/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

eclipse_plugin/plugin.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<?eclipse version="3.4"?>
3+
<plugin>
4+
<extension point="org.eclipse.jdt.core.javaFormatter">
5+
<javaFormatter
6+
class="com.google.googlejavaformat.java.GoogleJavaFormatter"
7+
id="com.google.googlejavaformat.java.GoogleJavaFormatter"
8+
name="Google Java Formatter (1.3-SNAPSHOT)">
9+
</javaFormatter>
10+
</extension>
11+
</plugin>

eclipse_plugin/pom.xml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<!--
2+
~ Copyright 2015 Google Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<parent>
21+
<groupId>com.google.googlejavaformat</groupId>
22+
<artifactId>google-java-format-parent</artifactId>
23+
<version>1.3-SNAPSHOT</version>
24+
</parent>
25+
26+
<artifactId>google-java-format-eclipse-plugin</artifactId>
27+
<version>0.0.1</version>
28+
<packaging>eclipse-plugin</packaging>
29+
<name>Google Java Format Plugin for Eclipse 4.5+</name>
30+
31+
<description>
32+
A Java source code formatter that follows Google Java Style.
33+
</description>
34+
35+
<properties>
36+
<tycho-version>0.26.0</tycho-version>
37+
</properties>
38+
39+
<repositories>
40+
<repository>
41+
<id>mars</id>
42+
<layout>p2</layout>
43+
<url>http://download.eclipse.org/releases/mars</url>
44+
</repository>
45+
</repositories>
46+
47+
<dependencies>
48+
<dependency>
49+
<groupId>com.google.googlejavaformat</groupId>
50+
<artifactId>google-java-format</artifactId>
51+
<version>1.3-SNAPSHOT</version>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
58+
<plugin>
59+
<groupId>org.eclipse.tycho</groupId>
60+
<artifactId>tycho-maven-plugin</artifactId>
61+
<version>${tycho-version}</version>
62+
<extensions>true</extensions>
63+
</plugin>
64+
65+
<plugin>
66+
<groupId>org.eclipse.tycho</groupId>
67+
<artifactId>target-platform-configuration</artifactId>
68+
<version>${tycho-version}</version>
69+
<configuration>
70+
<environments>
71+
<environment>
72+
<os>linux</os>
73+
<ws>gtk</ws>
74+
<arch>x86</arch>
75+
</environment>
76+
<environment>
77+
<os>linux</os>
78+
<ws>gtk</ws>
79+
<arch>x86_64</arch>
80+
</environment>
81+
<environment>
82+
<os>win32</os>
83+
<ws>win32</ws>
84+
<arch>x86</arch>
85+
</environment>
86+
<environment>
87+
<os>win32</os>
88+
<ws>win32</ws>
89+
<arch>x86_64</arch>
90+
</environment>
91+
<environment>
92+
<os>macosx</os>
93+
<ws>cocoa</ws>
94+
<arch>x86_64</arch>
95+
</environment>
96+
</environments>
97+
</configuration>
98+
</plugin>
99+
100+
</plugins>
101+
</build>
102+
103+
</project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.googlejavaformat.java;
16+
17+
import com.google.common.base.Preconditions;
18+
import com.google.common.collect.Range;
19+
import com.google.googlejavaformat.java.SnippetFormatter.SnippetKind;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
import org.eclipse.jdt.core.dom.ASTParser;
23+
import org.eclipse.jdt.core.formatter.CodeFormatter;
24+
import org.eclipse.jface.text.IRegion;
25+
import org.eclipse.jface.text.Region;
26+
import org.eclipse.text.edits.MultiTextEdit;
27+
import org.eclipse.text.edits.ReplaceEdit;
28+
import org.eclipse.text.edits.TextEdit;
29+
30+
/** Runs the Google Java formatter on the given code. */
31+
public class GoogleJavaFormatter extends CodeFormatter {
32+
33+
private static final int INDENTATION_SIZE = 2;
34+
35+
@Override
36+
public TextEdit format(
37+
int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) {
38+
IRegion[] regions = new IRegion[] {new Region(offset, length)};
39+
return formatInternal(kind, source, regions, indentationLevel);
40+
}
41+
42+
@Override
43+
public TextEdit format(
44+
int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) {
45+
return formatInternal(kind, source, regions, indentationLevel);
46+
}
47+
48+
@Override
49+
public String createIndentationString(int indentationLevel) {
50+
Preconditions.checkArgument(
51+
indentationLevel >= 0,
52+
"Indentation level cannot be less than zero. Given: %s",
53+
indentationLevel);
54+
int spaces = indentationLevel * INDENTATION_SIZE;
55+
StringBuilder buf = new StringBuilder(spaces);
56+
for (int i = 0; i < spaces; i++) {
57+
buf.append(' ');
58+
}
59+
return buf.toString();
60+
}
61+
62+
/** Runs the Google Java formatter on the given source, with only the given ranges specified. */
63+
private TextEdit formatInternal(int kind, String source, IRegion[] regions, int initialIndent) {
64+
try {
65+
boolean includeComments =
66+
(kind & CodeFormatter.F_INCLUDE_COMMENTS) == CodeFormatter.F_INCLUDE_COMMENTS;
67+
kind &= ~CodeFormatter.F_INCLUDE_COMMENTS;
68+
SnippetKind snippetKind;
69+
switch (kind) {
70+
case ASTParser.K_EXPRESSION:
71+
snippetKind = SnippetKind.EXPRESSION;
72+
break;
73+
case ASTParser.K_STATEMENTS:
74+
snippetKind = SnippetKind.STATEMENTS;
75+
break;
76+
case ASTParser.K_CLASS_BODY_DECLARATIONS:
77+
snippetKind = SnippetKind.CLASS_BODY_DECLARATIONS;
78+
break;
79+
case ASTParser.K_COMPILATION_UNIT:
80+
snippetKind = SnippetKind.COMPILATION_UNIT;
81+
break;
82+
default:
83+
throw new IllegalArgumentException(String.format("Unknown snippet kind: %d", kind));
84+
}
85+
return editFromReplacements(
86+
new SnippetFormatter()
87+
.format(
88+
snippetKind, source, rangesFromRegions(regions), initialIndent, includeComments));
89+
} catch (IllegalArgumentException | FormatterException exception) {
90+
// Do not format on errors.
91+
return null;
92+
}
93+
}
94+
95+
private List<Range<Integer>> rangesFromRegions(IRegion[] regions) {
96+
List<Range<Integer>> ranges = new ArrayList<>();
97+
for (IRegion region : regions) {
98+
ranges.add(Range.closedOpen(region.getOffset(), region.getOffset() + region.getLength()));
99+
}
100+
return ranges;
101+
}
102+
103+
private TextEdit editFromReplacements(List<Replacement> replacements) {
104+
// Split the replacements that cross line boundaries.
105+
TextEdit edit = new MultiTextEdit();
106+
for (Replacement replacement : replacements) {
107+
Range<Integer> replaceRange = replacement.getReplaceRange();
108+
edit.addChild(
109+
new ReplaceEdit(
110+
replaceRange.lowerEndpoint(),
111+
replaceRange.upperEndpoint() - replaceRange.lowerEndpoint(),
112+
replacement.getReplacementString()));
113+
}
114+
return edit;
115+
}
116+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
<modules>
3434
<module>core</module>
35+
<module>eclipse_plugin</module>
3536
<!-- google-java-format#24
3637
<module>idea_plugin</module>
3738
-->

0 commit comments

Comments
 (0)