Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #39 from velo/groovy
Browse files Browse the repository at this point in the history
Prepare for groovy support
  • Loading branch information
velo authored Dec 20, 2017
2 parents 8bdb0ec + d08ccba commit eb5f935
Show file tree
Hide file tree
Showing 43 changed files with 737 additions and 183 deletions.
8 changes: 8 additions & 0 deletions formatters/formatter-api/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: formatter-api
Bundle-Version: 1.9.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.junit;bundle-version="4.12.0",
com.google.guava;bundle-version="21.0.0"
Export-Package: com.marvinformatics.formatter
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
<version>1.9.0-SNAPSHOT</version>
</parent>

<artifactId>jsdt-core</artifactId>
<artifactId>formatter-api</artifactId>
<packaging>eclipse-plugin</packaging>

<properties>
<formatter.relocation>formatter.java.</formatter.relocation>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.util.Map;
import java.util.Random;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -37,6 +35,7 @@

public abstract class AbstractFormatterTest {

private static final String OUTPUT_DIR = "target/formatter-files/";
private Formatter formatter;

@Before
Expand All @@ -51,10 +50,6 @@ public File getTargetDirectory() {
return targetDir;
}

public Log getLog() {
return new SystemStreamLog();
}

public Charset getEncoding() {
return Charsets.UTF_8;
}
Expand All @@ -80,6 +75,27 @@ public LineEnding lineEnding() {
public boolean isDryRun() {
return false;
}

@Override
public void info(String message) {
System.out.println("INFO " + message);
}

@Override
public void error(String message) {
System.out.println("ERROR " + message);
}

@Override
public void debug(String message) {
System.out.println("DEBUG " + message);
}

@Override
public void warn(String message, File sourceFile, Exception e) {
System.out.println("WARN " + message);
throw new RuntimeException(e);
}
});
}

Expand All @@ -90,32 +106,35 @@ public void tuneDefaultConfigs(Map<String, String> options) {

@Test
public void doTestFormat() throws IOException, NoSuchAlgorithmException {
File originalSourceFile = new File("src/test/resources/", fileUnderTest());
File sourceFile = createUnformatedFile(originalSourceFile);
new File(OUTPUT_DIR, fileUnderTest()).mkdirs();

Result r = formatter.formatFile(sourceFile.toPath());
assertEquals(Result.SUCCESS, r);
File originalFile = new File(OUTPUT_DIR, fileUnderTest() + "/original");
Files.copy(new File("src/test/resources/sample/", fileUnderTest()), originalFile);

String originalContent = Files.toString(originalSourceFile, Charsets.UTF_8);
String formattedContent = Files.toString(sourceFile, Charsets.UTF_8);
File unformattedFile = createUnformatedFile(originalFile);
String unformattedContent = Files.toString(unformattedFile, Charsets.UTF_8);

String msg = "Files: \n-" + originalSourceFile.getAbsolutePath() + "\n-" + sourceFile.getAbsolutePath();
String originalContent = Files.toString(originalFile, Charsets.UTF_8);
String formattedContent = formatter.format(unformattedContent);

File formattedFile = new File(OUTPUT_DIR, fileUnderTest() + "/formatted");
Files.write(formattedContent, formattedFile, Charsets.UTF_8);

String msg = "Files: \n-" + originalFile.getAbsolutePath() + "\n-" + formattedFile.getAbsolutePath();
assertEquals(msg, originalContent, formattedContent);

String expectedSha1 = Files.hash(originalSourceFile, Hashing.sha1()).toString();
String sha1 = Files.hash(sourceFile, Hashing.sha1()).toString();
String expectedSha1 = Files.hash(originalFile, Hashing.sha1()).toString();
String sha1 = Files.hash(formattedFile, Hashing.sha1()).toString();

assertEquals(msg, expectedSha1, sha1);
}

public abstract String fileUnderTest();

private File createUnformatedFile(File originalSourceFile) throws IOException {
File unformatedFile = new File("target/test-classes/", fileUnderTest());
File unformatedFile = new File(OUTPUT_DIR, fileUnderTest() + "/unformatted");

Files.copy(originalSourceFile, unformatedFile);

List<String> content = Files.readLines(unformatedFile, Charsets.UTF_8);
List<String> content = Files.readLines(originalSourceFile, Charsets.UTF_8);

StringBuilder messedContent = new StringBuilder();
for (String line : content)
Expand All @@ -136,7 +155,10 @@ private StringBuilder randomSeparator() {

StringBuilder separator = new StringBuilder();
for (int i = 0; i < spaces; i++)
separator.append(' ');
if (i % 3 == 0)
separator.append('\t');
else
separator.append(' ');

return separator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
import java.io.File;
import java.nio.charset.Charset;

import org.apache.maven.plugin.logging.Log;

/**
* @author marvin.froeder
*/
public interface ConfigurationSource {

Log getLog();

String getCompilerSources();

String getCompilerCompliance();
Expand All @@ -41,4 +37,12 @@ public interface ConfigurationSource {

boolean isDryRun();

void info(String message);

void error(String message);

void debug(String message);

void warn(String message, File sourceFile, Exception e);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
*/
package com.marvinformatics.formatter;

import java.nio.file.Path;

/**
* @author marvin.froeder
*/
public interface Formatter {

/**
* Format individual file.
*
* @param file
* @return
*
* @param originalCode code to be formatted
* @return formatted code
*/
public abstract Result formatFile(Path file);
String format(String originalCode);

}
59 changes: 59 additions & 0 deletions formatters/groovy-formatter-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com)
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.marvinformatics.formatter</groupId>
<artifactId>formatters</artifactId>
<version>1.9.0-SNAPSHOT</version>
</parent>

<artifactId>groovy-formatter-test</artifactId>
<packaging>jar</packaging>

<repositories>
<repository>
<id>greclipse</id>
<url>http://dist.springsource.org/snapshot/GRECLIPSE/e4.7/</url>
<layout>p2</layout>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.marvinformatics.formatter</groupId>
<artifactId>formatter-api</artifactId>
<version>1.9.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.marvinformatics.formatter</groupId>
<artifactId>groovy-formatter</artifactId>
<version>1.9.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com)
*
* 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.marvinformatics.formatter.groovy;

import java.util.Map;

import com.marvinformatics.formatter.AbstractFormatterTest;
import com.marvinformatics.formatter.ConfigurationSource;
import com.marvinformatics.formatter.Formatter;
import com.marvinformatics.formatter.groovy.GroovyFormatter;

/**
* @author marvin.froeder
*/
public class GroovyFormatterTest extends AbstractFormatterTest {

@Override
public Formatter createFormatter(Map<String, String> options, ConfigurationSource configurationSource) {
return new GroovyFormatter(options);
}

@Override
public void tuneDefaultConfigs(Map<String, String> options) {
options.put("groovy.formatter.remove.unnecessary.semicolons", "false");
}

@Override
public String fileUnderTest() {
return "AnyClass.groovy";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2010 Marvin Herman Froeder (marvin@marvinformatics.com)
*
* 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.
*/
/**
* Class description
*/
class Foo
{
String foo

/* Method */
def callBar()
{
new Bar().bar(foo) }

/** Inner class */
class Bar
{

def bar(foo)
{
println "${foo}Bar" }}}

def foo = new Foo(foo: 'Foo')
foo.callBar()
15 changes: 15 additions & 0 deletions formatters/groovy-formatter/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: groovy-formatter
Bundle-Version: 1.9.0.qualifier
Require-Bundle: formatter-api,
org.codehaus.groovy.eclipse.core;bundle-version="[2.9.0,10.0.0)",
org.codehaus.groovy.eclipse.refactoring;bundle-version="[2.9.0,10.0.0)",
org.eclipse.jdt.groovy.core,
org.eclipse.text,
org.eclipse.jface.text,
org.eclipse.jdt.core,
org.codehaus.groovy,
org.codehaus.groovy.eclipse
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: com.marvinformatics.formatter.groovy
Loading

0 comments on commit eb5f935

Please sign in to comment.