Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputMethod: use nio.Path instead of String #2869

Merged
merged 1 commit into from
Nov 8, 2021
Merged
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
12 changes: 7 additions & 5 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/InputMethod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package org.scalafmt.cli

import scala.io.Source

import java.io.File
import java.io.InputStream
import java.nio.file.{Path, Paths}

import org.scalafmt.Error.MisformattedFile
import org.scalafmt.util.AbsoluteFile

sealed abstract class InputMethod {
def readInput(options: CliOptions): String
def filename: String
def path: Path

protected def print(text: String, options: CliOptions): Unit
protected def list(options: CliOptions): Unit
Expand All @@ -26,8 +26,8 @@ sealed abstract class InputMethod {
else if (codeChanged)
options.writeMode match {
case WriteMode.Test =>
val diff = InputMethod.unifiedDiff(filename, original, formatted)
throw MisformattedFile(new File(filename), diff)
val diff = InputMethod.unifiedDiff(path.toString, original, formatted)
throw MisformattedFile(path, diff)
case WriteMode.Override => overwrite(formatted, options)
case WriteMode.List => list(options)
case _ =>
Expand All @@ -48,6 +48,8 @@ object InputMethod {
}
}
case class StdinCode(filename: String, input: String) extends InputMethod {
override def path: Path = Paths.get(filename)

def readInput(options: CliOptions): String = input

override protected def print(text: String, options: CliOptions): Unit =
Expand All @@ -61,7 +63,7 @@ object InputMethod {
}

case class FileContents(file: AbsoluteFile) extends InputMethod {
override def filename = file.toString()
override def path = file.path
def readInput(options: CliOptions): String =
file.readFile(options.encoding)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,20 @@ object ScalafmtCoreRunner extends ScalafmtRunner {
scalafmtConfig: ScalafmtConfig
): ExitCode = {
val input = inputMethod.readInput(options)
val formatResult = Scalafmt.formatCode(
input,
scalafmtConfig,
options.range,
inputMethod.filename
)
val filename = inputMethod.path.toString
val formatResult =
Scalafmt.formatCode(input, scalafmtConfig, options.range, filename)
formatResult.formatted match {
case Formatted.Success(formatted) =>
inputMethod.write(formatted, input, options)
case _: Formatted.Failure if scalafmtConfig.runner.ignoreWarnings =>
ExitCode.Ok // do nothing
case Formatted.Failure(e @ (_: ParseException | _: TokenizeException)) =>
options.common.err.println(e.toString)
ExitCode.ParseError
case Formatted.Failure(e) =>
if (scalafmtConfig.runner.ignoreWarnings) {
ExitCode.Ok // do nothing
} else {
e match {
case e @ (_: ParseException | _: TokenizeException) =>
options.common.err.println(e.toString)
ExitCode.ParseError
case _ =>
new FailedToFormat(inputMethod.filename, e)
.printStackTrace(options.common.out)
ExitCode.UnexpectedError
}
}
new FailedToFormat(filename, e).printStackTrace(options.common.out)
ExitCode.UnexpectedError
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.scalafmt.cli

import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.atomic.{AtomicInteger, AtomicReference}

import org.scalafmt.CompatCollections.ParConverters._
Expand Down Expand Up @@ -61,7 +60,7 @@ object ScalafmtDynamicRunner extends ScalafmtRunner {
exitCode.getAndUpdate(ExitCode.merge(code, _))
} catch {
case e: MisformattedFile =>
reporter.error(e.file.toPath, e)
reporter.error(e.file, e)
if (options.check) break
}
PlatformTokenizerCache.megaCache.clear()
Expand Down Expand Up @@ -106,11 +105,7 @@ object ScalafmtDynamicRunner extends ScalafmtRunner {
): ExitCode = {
val input = inputMethod.readInput(options)

val formatResult = session
.format(
Paths.get(inputMethod.filename),
input
)
val formatResult = session.format(inputMethod.path, input)
inputMethod.write(formatResult, input, options)
}

Expand Down
6 changes: 3 additions & 3 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Error.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.meta.Case
import scala.meta.Tree
import scala.reflect.ClassTag
import scala.reflect.classTag
import java.io.File
import java.nio.file.Path
import scala.meta.inputs.Position
import org.scalafmt.internal.Decision
import org.scalafmt.internal.FormatToken
Expand Down Expand Up @@ -70,8 +70,8 @@ object Error {
case class UnableToFindStyle(filename: String, e: Throwable)
extends Error(s"Unable to find style for file $filename. $e")

case class MisformattedFile(file: File, customMessage: String)
extends Error(s"${file.getPath} is mis-formatted. $customMessage")
case class MisformattedFile(file: Path, customMessage: String)
extends Error(s"${file} is mis-formatted. $customMessage")

case class SearchStateExploded(
deepestState: State,
Expand Down