是否可以限制编译器显示的错误数量? [英] Is it possible to limit the number of errors the compiler shows?

查看:42
本文介绍了是否可以限制编译器显示的错误数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时在进行某些类型的重构时,编译器可能会产生数百个错误.我喜欢从顶部开始解决这些错误,但由于错误的数量之多,要一直向上滚动会非常麻烦.

Sometimes when doing certain kinds of refactorings, the compiler may generate hundreds of errors. I like to start resolving these errors from the top, but the sheer number of errors can make it quite cumbersome to scroll all the way back up.

是否可以让 Scala 编译器限制它显示的错误数量,以便从顶部开始修复它们变得更容易?显然,代价是可能不得不多次运行编译器.

Is it possible to make the Scala compiler limit the number of errors it shows so that it becomes easier to start fixing them from the top? At the expense of possibly having to run the compiler multiple times, obviously.

推荐答案

是的,因为 2.12 可以使用自定义报告器.

Yes, since 2.12 it's possible to use a custom reporter.

这是一个示例记者:

package myrep

import scala.tools.nsc.Settings
import scala.tools.nsc.reporters.ConsoleReporter

import scala.reflect.internal.util._

class MyReporter(ss: Settings) extends ConsoleReporter(ss) {
  var deprecationCount = 0
  override def warning(pos: Position, msg: String): Unit = {
    if (msg contains "is deprecated") deprecationCount += 1
    super.warning(pos, msg)
  }
  override def hasWarnings: Boolean = count(WARNING) - deprecationCount > 0
  override def reset() = { deprecationCount = 0 ; super.reset() }

  // limit total
  var limit = 5
  override def display(pos: Position, msg: String, severity: Severity): Unit =
    if (severity != ERROR || severity.count <= limit) print(pos, msg, severity)
}

你的记者类必须在工具类路径上:

Your reporter class has to be on the tool class path:

$ ~/scala-2.12.0-M3/bin/scalac -toolcp . -Xreporter myrep.MyReporter test.scala

您可能会选择以某种方式配置限制,也许使用系统属性,但配置不是内置的.

You might choose to configure the limit somehow, perhaps with a system property, but configuration is not built-in.

对于这个示例文件,有六个错误,但报告了五个错误:

For this sample file, there are six errors but five are reported:

package tester

@deprecated("Don't use me", since="2.12.0")
class C

object Test extends App {
  Console println s"${new C}"

  val x: String = 42

  val y: Int = "42"

  val z: Int = 2.0

  Console println (42 drop 1)
  Console println (42 take 1)
  Console println (42 shift 1)
}

这篇关于是否可以限制编译器显示的错误数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆