Skip to content

Commit

Permalink
ScalafmtSession: add formatOrError()
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 2, 2021
1 parent 493cf44 commit 96a6a5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ final case class ScalafmtDynamicSession(
override def format(file: Path, code: String): String =
tryFormat(file, code).getOrElse(code)

override def formatOrError(file: Path, code: String): ScalafmtResult =
tryFormat(file, code).fold(new ScalafmtResult(_), new ScalafmtResult(_))

override def matchesProjectFilters(file: Path): Boolean =
cfg.isIncludedInProject(file)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.scalafmt.interfaces;

public final class ScalafmtResult {
public final String value;
public final Throwable exception;

public ScalafmtResult(String value) {
this.value = value;
this.exception = null;
}

public ScalafmtResult(Throwable exception) {
this.exception = exception;
this.value = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public interface ScalafmtSession {
String format(Path file, String code);


/**
* Format a single file with the given configuration.
*
* @param file relative or absolute path to the file being formatted. Used only for the path
* name, the file does not have to exist on disk.
* @param code the text contents to format.
* @return the formatted contents if formatting was successful, otherwise an error.
*/
ScalafmtResult formatOrError(Path file, String code);

/**
* Whether the path matches the 'project.{excludeFilters,includeFilters}' setting.
* @param file path to match
Expand Down

0 comments on commit 96a6a5d

Please sign in to comment.