Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.2</version>
<configuration>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution>
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -85,6 +107,12 @@
<artifactId>maven-plugin-api</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
Expand Down
102 changes: 32 additions & 70 deletions src/main/java/org/scalariform/ScalariformMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,92 @@

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
* Goal which formats scala source files
*
* @goal format
*
* @phase process-sources
*/
@Mojo(name = "format", defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true)
public class ScalariformMojo extends AbstractMojo {

/**
* Base directory of the project
*
* @parameter expression="${basedir}"
* @required
*/
@Parameter(property = "basedir", required = true)
protected String baseDir;

/**
* @parameter default-value=false
* Source file encoding, e.g. UTF-8. If not set, defaults to the platform default encoding.
*/
@Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
protected String encoding;

@Parameter(defaultValue = "false")
protected boolean alignParameters;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean alignSingleLineCaseStatements;

/**
* @parameter default-value=40
*/
@Parameter(defaultValue = "40")
protected int alignSingleLineCaseStatements_maxArrowIndent;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean compactControlReadability;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean compactStringConcatenation;

/**
* @parameter default-value=true
*/
@Parameter(defaultValue = "true")
protected boolean doubleIndentClassDeclaration;

/**
* @parameter default-value=true
*/
@Parameter(defaultValue = "true")
protected boolean formatXml;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean indentLocalDefs;

/**
* @parameter default-value=true
*/
@Parameter(defaultValue = "true")
protected boolean indentPackageBlocks;

/**
* @parameter default-value=2
*/
@Parameter(defaultValue = "2")
protected int indentSpaces;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean indentWithTabs;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean multilineScaladocCommentsStartOnFirstLine;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean placeScaladocAsterisksBeneathSecondAsterisk;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean preserveDanglingCloseParenthesis;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean preserveSpaceBeforeArguments;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean rewriteArrowSymbols;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean spaceBeforeColon;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean spaceInsideBrackets;

/**
* @parameter default-value=false
*/
@Parameter(defaultValue = "false")
protected boolean spaceInsideParentheses;

/**
* @parameter default-value=true
*/

@Parameter(defaultValue = "true")
protected boolean spacesWithinPatternBinders;

public void execute() throws MojoExecutionException {

MojoFormatter.format(baseDir, this.getLog(),
alignParameters,
MojoFormatter.format(baseDir, encoding, this.getLog(),
alignParameters,
alignSingleLineCaseStatements,
alignSingleLineCaseStatements_maxArrowIndent,
compactControlReadability,
Expand All @@ -144,6 +108,4 @@ public void execute() throws MojoExecutionException {
spaceInsideParentheses,
spacesWithinPatternBinders);
}

}

9 changes: 6 additions & 3 deletions src/main/scala/org/scalariform/MojoFormatter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.io.{ File, FilenameFilter, FileFilter }

import scala.collection.JavaConversions._

import scala.io.Source
import scala.io.{Codec, Source}

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

Expand Down Expand Up @@ -41,6 +41,7 @@ object MojoFormatter {
}

def format(path: String,
sourceEncoding: String,
log: Log,
alignParameters: Boolean,
alignSingleLineCaseStatements: Boolean,
Expand Down Expand Up @@ -87,14 +88,16 @@ object MojoFormatter {

val files = findScalaFiles(path)

val sourceCodec = Option(sourceEncoding).map(Codec.apply)

var count = 0

files.foreach { file ⇒
try {
val original = Source.fromFile(file).mkString
val original = Source.fromFile(file)(sourceCodec getOrElse implicitly[Codec]).mkString
val formatted = ScalaFormatter.format(original, preferences)
if (original != formatted) {
writeText(file, formatted)
writeText(file, formatted, sourceCodec map (_.name))
count += 1
}
} catch {
Expand Down