Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.
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
4 changes: 1 addition & 3 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

version = "2.6.4"
align = some
align {
Expand All @@ -17,10 +16,9 @@ align {
assumeStandardLibraryStripMargin = true
continuationIndent.defnSite = 2
continuationIndent.callSite = 2
danglingParentheses = true
docstrings = JavaDoc
indentOperator = spray
maxColumn = 110
newlines.implicitParamListModifierPrefer = before
project {
git = true
excludeFilters = ["target"]
Expand Down
28 changes: 13 additions & 15 deletions src/main/scala/org/scalastyle/Checker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ case class Lines(lines: Array[Line], lastChar: Char) {

lines.foreach { l =>
i = i + 1
if (position >= l.start && position < l.end) {
if (position >= l.start && position < l.end)
return Some((l, i))
}
}

None
Expand Down Expand Up @@ -78,7 +77,7 @@ class ScalastyleChecker[T <: FileSpec](classLoader: Option[ClassLoader] = None)
StartWork() :: files
.flatMap(file =>
StartFile(file) :: checkerUtils.verifyFile(configuration, checks, file) :::
List(EndFile(file))
List(EndFile(file))
)
.toList ::: List(EndWork())
}
Expand Down Expand Up @@ -131,17 +130,17 @@ class CheckerUtils(classLoader: Option[ClassLoader] = None) {
file: T,
source: String
): List[Message[T]] = {
if (source.isEmpty) {
if (source.isEmpty)
Nil
} else {
else {
val lines = Checker.parseLines(source)
val scalariformAst = parseScalariform(source)

val commentFilters = if (configuration.commentFilter) {
CommentFilter.findCommentFilters(scalariformAst.comments, lines)
} else {
Nil
}
val commentFilters =
if (configuration.commentFilter)
CommentFilter.findCommentFilters(scalariformAst.comments, lines)
else
Nil

classes
.flatMap(cc => newInstance(cc.className, cc.level, cc.parameters, cc.customMessage, cc.customId))
Expand Down Expand Up @@ -188,13 +187,12 @@ class CheckerUtils(classLoader: Option[ClassLoader] = None) {
def readFile(file: String, encoding: Option[String])(implicit codec: Codec): String = {
@tailrec
def readFileWithEncoding(file: String, encodings: List[String]): Option[String] = {
if (encodings.isEmpty) {
if (encodings.isEmpty)
None
} else {
else {
val encoding = encodings.head
try {
Some(Source.fromFile(file)(encoding).mkString)
} catch {
try Some(Source.fromFile(file)(encoding).mkString)
catch {
case _: MalformedInputException =>
// printxxln("caught MalFormedInputException with " + (if (encoding.isDefined) encoding.get else "default (" + codec.charSet + ")") + " encoding")
readFileWithEncoding(file, encodings.tail)
Expand Down
13 changes: 5 additions & 8 deletions src/main/scala/org/scalastyle/CommentFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,21 @@ object CommentFilter {

it.foreach { ci =>
(inMap.getOrElse(ci.id, false), ci.off) match {
case (true, false) => { // off then on, add a new CommentFilter
case (true, false) => // off then on, add a new CommentFilter
list += CommentFilter(ci.id, start.getOrElse(ci.id, None), lines.toLineColumn(ci.position))
inMap.put(ci.id, false)
start.remove(ci.id)
}
case (true, true) => // off then off, do nothing
case (false, false) => // on then on, do nothing
case (false, true) => { // on then off, reset start
case (false, true) => // on then off, reset start
start.put(ci.id, lines.toLineColumn(ci.position))
inMap.put(ci.id, true)
}
}
}

inMap.foreach { e =>
if (e._2) {
if (e._2)
list += CommentFilter(e._1, start.getOrElse(e._1, None), None)
}
}

list.toList
Expand All @@ -100,9 +97,9 @@ object CommentFilter {
private def idMatches(key: String)(cf: CommentFilter) = cf.id.isEmpty || cf.id.get == key

private def filterApplies[T <: FileSpec](se: StyleError[_])(cf: CommentFilter): Boolean = {
if (se.lineNumber.isEmpty) {
if (se.lineNumber.isEmpty)
true
} else {
else {
val m = se.lineNumber.get
(cf.start, cf.end) match {
case (Some(s), Some(e)) => m >= s.line && m < e.line
Expand Down
11 changes: 5 additions & 6 deletions src/main/scala/org/scalastyle/Directory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ object Directory {
}

private[this] def createFileExclusionFilter(excludedFiles: Seq[String]): Option[FileFilter] = {
if (excludedFiles.isEmpty) {
if (excludedFiles.isEmpty)
None
} else {
else {
val exclusionPatterns = excludedFiles.map(_.r)
Some(new FileFilter {
def accept(file: File): Boolean = {
Expand All @@ -68,17 +68,16 @@ object Directory {
def getFilesHelper(currentFiles: Iterable[File], acc: Set[File]): Set[File] = {
currentFiles.headOption match {
case Some(f) =>
if (excludeFilter.exists(_.accept(f))) {
if (excludeFilter.exists(_.accept(f)))
getFilesHelper(currentFiles.tail, acc)
} else if (f.isDirectory) {
else if (f.isDirectory) {
val newCurrentFiles = currentFiles.tail ++ f.listFiles
getFilesHelper(newCurrentFiles, acc)
} else if (scalaFileFilter.accept(f) && !acc(f)) {
val newAcc = acc + f
getFilesHelper(currentFiles.tail, newAcc)
} else {
} else
getFilesHelper(currentFiles.tail, acc)
}
case None => acc
}
}
Expand Down
11 changes: 4 additions & 7 deletions src/main/scala/org/scalastyle/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ object Main {
}
}

if (config.config.isEmpty || config.directories.isEmpty) {
if (config.config.isEmpty || config.directories.isEmpty)
config = config.copy(error = true)
}

config
}
Expand All @@ -100,9 +99,8 @@ object Main {
if (config.error) {
usage(BuildInfo.version)
1
} else {
if (execute(config)) 1 else 0
}
} else if (execute(config)) 1
else 0
}

System.exit(exitVal)
Expand All @@ -122,10 +120,9 @@ object Main {
val config = ConfigFactory.load(cl.getOrElse(this.getClass.getClassLoader))
val outputResult = new TextOutput(config, mc.verbose, mc.quiet).output(messages)
mc.xmlFile match {
case Some(x) => {
case Some(x) =>
val encoding = mc.xmlEncoding.getOrElse(codec.charSet).toString
XmlOutput.save(config, x, encoding, messages)
}
case None =>
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/org/scalastyle/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ case class StyleError[+T <: FileSpec](
) extends Message[T] {
override def toString(): String =
"StyleError key=" + key + " args=" + args + " lineNumber=" + lineNumber +
" column=" + column + " customMessage=" + customMessage
" column=" + column + " customMessage=" + customMessage
}
case class StyleException[+T <: FileSpec](
fileSpec: T,
Expand Down
101 changes: 51 additions & 50 deletions src/main/scala/org/scalastyle/Output.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@ trait Output[T <: FileSpec] {
OutputResult(files, errors, warnings, infos)
}

def eachMessage(m: Message[T]): Unit = m match {
case StartWork() =>
case EndWork() =>
case StartFile(file) => files += 1
case EndFile(file) =>
case StyleError(file, clazz, key, level, args, line, column, customMessage) =>
level match {
case WarningLevel => warnings += 1
case InfoLevel => infos += 1
case _ => errors += 1
}
case StyleException(file, clazz, message, stacktrace, line, column) => errors += 1
}
def eachMessage(m: Message[T]): Unit =
m match {
case StartWork() =>
case EndWork() =>
case StartFile(file) => files += 1
case EndFile(file) =>
case StyleError(file, clazz, key, level, args, line, column, customMessage) =>
level match {
case WarningLevel => warnings += 1
case InfoLevel => infos += 1
case _ => errors += 1
}
case StyleException(file, clazz, message, stacktrace, line, column) => errors += 1
}

def message(m: Message[T]): Unit
}
Expand All @@ -74,29 +75,30 @@ class TextOutput[T <: FileSpec](config: Config, verbose: Boolean = false, quiet:
private val messageHelper = new MessageHelper(config)

// scalastyle:off regex multiple.string.literals
override def message(m: Message[T]): Unit = m match {
case StartWork() => if (verbose) println("Starting scalastyle")
case EndWork() =>
case StartFile(file) => if (verbose) println("start file " + file)
case EndFile(file) => if (verbose) println("end file " + file)
case StyleError(file, clazz, key, level, args, line, column, customMessage) =>
if (!quiet || verbose) {
println(
messageHelper.text(level.name) + print("file", file.name) +
print("message", Output.findMessage(messageHelper, key, args, customMessage)) +
print("line", line) + print("column", column)
)
}
case StyleException(file, clazz, message, stacktrace, line, column) =>
if (!quiet || verbose) {
println(
"error" + print("file", file.name) + print("message", message) + print("line", line) + print(
"column",
column
override def message(m: Message[T]): Unit =
m match {
case StartWork() => if (verbose) println("Starting scalastyle")
case EndWork() =>
case StartFile(file) => if (verbose) println("start file " + file)
case EndFile(file) => if (verbose) println("end file " + file)
case StyleError(file, clazz, key, level, args, line, column, customMessage) =>
if (!quiet || verbose) {
println(
messageHelper.text(level.name) + print("file", file.name) +
print("message", Output.findMessage(messageHelper, key, args, customMessage)) +
print("line", line) + print("column", column)
)
)
}
}
}
case StyleException(file, clazz, message, stacktrace, line, column) =>
if (!quiet || verbose) {
println(
"error" + print("file", file.name) + print("message", message) + print("line", line) + print(
"column",
column
)
)
}
}

// scalastyle:on regex

Expand Down Expand Up @@ -137,18 +139,14 @@ object XmlOutput {
private def printToFile(f: java.io.File, encoding: String)(op: java.io.PrintWriter => Unit): Unit = {
val parent = f.getParentFile
// sometimes f.getParentFile returns null - don't know why, but protect anyway
if (parent != null && !parent.exists() && !parent.mkdirs()) { // scalastyle:ignore null
if (parent != null && !parent.exists() && !parent.mkdirs()) // scalastyle:ignore null
throw new IllegalStateException("Couldn't create dir: " + parent)
}

val p = new java.io.PrintWriter(f, encoding)
try {
op(p)
} catch {
try op(p)
catch {
case e: Throwable => throw e
} finally {
p.close()
}
} finally p.close()
}

case class Alert(
Expand Down Expand Up @@ -188,15 +186,17 @@ object XmlOutput {
<file name={filename}>
{
alerts.map {
case Alert(fn, severity, message, source, line, column) => {
case Alert(fn, severity, message, source, line, column) =>
val s = source.collect {
case x: Class[_] => x.getName
}
<error severity={severity} message={message}/> % attr("source", s) % attr("line", line) % attr(
<error severity={severity} message={message}/> % attr("source", s) % attr(
"line",
line
) % attr(
"column",
column
)
}
}
}
</file>
Expand All @@ -205,8 +205,9 @@ object XmlOutput {
</checkstyle>
}

private[this] def attr(name: String, value: Option[Any]): xml.MetaData = value match {
case Some(x) => xml.Attribute("", name, x.toString, xml.Null)
case None => xml.Null
}
private[this] def attr(name: String, value: Option[Any]): xml.MetaData =
value match {
case Some(x) => xml.Attribute("", name, x.toString, xml.Null)
case None => xml.Null
}
}
Loading