Skip to content

Commit

Permalink
reports for chosen errors
Browse files Browse the repository at this point in the history
The errors are chosen based on what reoccurs in `metals.log`.
  • Loading branch information
kasiaMarek committed Feb 17, 2023
1 parent e2997ce commit 84d0290
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import scala.meta.internal.implementation.Supermethods.formatMethodSymbolForQuic
import scala.meta.internal.metals.ClientCommands
import scala.meta.internal.metals.DefinitionProvider
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.Reports
import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.metals.clients.language.MetalsQuickPickItem
import scala.meta.internal.metals.clients.language.MetalsQuickPickParams
Expand All @@ -23,7 +24,8 @@ class Supermethods(
definitionProvider: DefinitionProvider,
implementationProvider: ImplementationProvider,
)(implicit
ec: ExecutionContext
ec: ExecutionContext,
reports: Reports,
) {

def getGoToSuperMethodCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ object Directories {
RelativePath(".metals").resolve("readonly")
def tmp: RelativePath =
RelativePath(".metals").resolve(".tmp")
def reports: RelativePath =
RelativePath(".metals").resolve(".reports")
def dependencies: RelativePath =
readonly.resolve(dependenciesName)
def log: RelativePath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,6 @@ object MetalsEnrichments
else Files.move(path.toNIO, newPath.toNIO)
}

def createDirectories(): AbsolutePath =
AbsolutePath(Files.createDirectories(path.dealias.toNIO))

def createAndGetDirectories(): Seq[AbsolutePath] = {
def createDirectoriesRec(
absolutePath: AbsolutePath,
Expand All @@ -513,31 +510,6 @@ object MetalsEnrichments
path.listRecursive.toList.reverse.foreach(_.delete())
}

def writeText(text: String): Unit = {
path.parent.createDirectories()
val tmp = Files.createTempFile("metals", path.filename)
// Write contents first to a temporary file and then try to
// atomically move the file to the destination. The atomic move
// reduces the risk that another tool will concurrently read the
// file contents during a half-complete file write.
Files.write(
tmp,
text.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.TRUNCATE_EXISTING,
)
try {
Files.move(
tmp,
path.toNIO,
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE,
)
} catch {
case NonFatal(_) =>
Files.move(tmp, path.toNIO, StandardCopyOption.REPLACE_EXISTING)
}
}

def appendText(text: String): Unit = {
path.parent.createDirectories()
Files.write(
Expand Down Expand Up @@ -601,7 +573,19 @@ object MetalsEnrichments
case _ => None
}

def toAbsolutePathSafe: Option[AbsolutePath] = Try(toAbsolutePath).toOption
def toAbsolutePathSafe(implicit reports: Reports): Option[AbsolutePath] =
try {
Some(toAbsolutePath)
} catch {
case NonFatal(e) =>
reports.incognito.createReport(
"absolute-path",
s"""|Uri: $value
|""".stripMargin,
e,
)
None
}

def toAbsolutePath: AbsolutePath = toAbsolutePath(followSymlink = true)

Expand Down Expand Up @@ -1086,11 +1070,4 @@ object MetalsEnrichments
}
}

implicit class XtensionAny[T](v: T) {
def withExec[R](toExec: T => R): T = {
toExec(v)
v
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import scala.meta.internal.metals.BuildInfo
import scala.meta.internal.metals.Messages.AmmoniteJvmParametersChange
import scala.meta.internal.metals.Messages.IncompatibleBloopVersion
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.Reports
import scala.meta.internal.metals.ammonite.Ammonite
import scala.meta.internal.metals.callHierarchy.CallHierarchyProvider
import scala.meta.internal.metals.clients.language.ConfiguredLanguageClient
Expand Down Expand Up @@ -189,7 +190,7 @@ class MetalsLspService(

val tables: Tables = register(new Tables(workspace, time))

private val reports = new Reports(workspace)
implicit val reports: Reports = new Reports(workspace)

private val buildTools: BuildTools = new BuildTools(
workspace,
Expand Down Expand Up @@ -290,7 +291,7 @@ class MetalsLspService(
)

private val timerProvider: TimerProvider = new TimerProvider(time)
private val trees = new Trees(buffers, scalaVersionSelector, reports)
private val trees = new Trees(buffers, scalaVersionSelector)

private val documentSymbolProvider = new DocumentSymbolProvider(
trees,
Expand Down Expand Up @@ -1170,9 +1171,10 @@ class MetalsLspService(
None
}

uriOpt match {
case Some(uri) => {
val path = uri.toAbsolutePath
val pathOpt = uriOpt.flatMap(_.toAbsolutePathSafe)

pathOpt match {
case Some(path) => {
focusedDocument = Some(path)
buildTargets
.inverseSources(path)
Expand Down Expand Up @@ -2736,6 +2738,11 @@ class MetalsLspService(
case e: IndexingExceptions.PathIndexingException =>
scribe.error(s"issues while parsing: ${e.path}", e.underlying)
case e: IndexingExceptions.InvalidSymbolException =>
reports.incognito.createReport(
"invalid-symbol",
s"""Symbol: ${e.symbol}""".stripMargin,
e,
)
scribe.error(s"searching for `${e.symbol}` failed", e.underlying)
case _: NoSuchFileException =>
// only comes for badly configured jar with `/Users` path added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ final class JavaFoldingRangeExtractor(
def extract(): List[FoldingRange] = {
val scanner = ToolFactory.createScanner(true, true, false, true)
scanner.setSource(text.toCharArray())

@tailrec
def swallowUntilSemicolon(token: Int, line: Int): Int = {
val addLine = scanner.getCurrentTokenSource.count(_ == '\n')
if (token != ITerminalSymbols.TokenNameSEMICOLON) {
swallowUntilSemicolon(scanner.getNextToken(), line + addLine)
} else {
line + addLine
if (token == ITerminalSymbols.TokenNameEOF) line
else {
val addLine = scanner.getCurrentTokenSource.count(_ == '\n')
if (token != ITerminalSymbols.TokenNameSEMICOLON) {
swallowUntilSemicolon(scanner.getNextToken(), line + addLine)
} else {
line + addLine
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ import org.eclipse.{lsp4j => l}
final class Trees(
buffers: Buffers,
scalaVersionSelector: ScalaVersionSelector,
reports: Reports,
) {
)(implicit reports: Reports) {

private val trees = TrieMap.empty[AbsolutePath, Tree]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ abstract class PcCollector[T](
def isForComprehensionOwner(named: NameTree) =
soughtNames(named.name) &&
named.symbol.owner.isAnonymousFunction && owners.exists(
_.pos.point == named.symbol.owner.pos.point
pos.isDefined && _.pos.point == named.symbol.owner.pos.point
)

def soughtOrOverride(sym: Symbol) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.{util as ju}
import scala.collection.mutable
import scala.jdk.CollectionConverters.*

import scala.meta.internal.metals.Reports
import scala.meta.internal.mtags.MtagsEnrichments.*
import scala.meta.internal.pc.AutoImports.*
import scala.meta.internal.pc.completions.CompletionPos
Expand All @@ -31,7 +32,7 @@ final class AutoImportsProvider(
params: OffsetParams,
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
):
)(using Option[Reports]):

def autoImports(isExtension: Boolean): List[AutoImportsResult] =
val uri = params.uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.util.logging.Logger

import scala.util.control.NonFatal

import scala.meta.internal.metals.Reports
import scala.meta.pc.*

import dotty.tools.dotc.core.Contexts.*
Expand All @@ -16,7 +17,7 @@ import dotty.tools.dotc.core.Symbols.*
class CompilerSearchVisitor(
query: String,
visitSymbol: Symbol => Boolean,
)(using ctx: Context)
)(using ctx: Context, reports: Option[Reports])
extends SymbolSearchVisitor:

val logger: Logger = Logger.getLogger(classOf[CompilerSearchVisitor].getName)
Expand All @@ -25,6 +26,13 @@ class CompilerSearchVisitor(
sym != NoSymbol && sym.isPublic
catch
case NonFatal(e) =>
reports.foreach(
_.incognito.createReport(
"is_public",
s"""Symbol: $sym""".stripMargin,
e,
)
)
logger.log(Level.SEVERE, e.getMessage(), e)
false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContextExecutor

import scala.meta.internal.metals.EmptyCancelToken
import scala.meta.internal.metals.Reports
import scala.meta.internal.mtags.BuildInfo
import scala.meta.internal.mtags.MtagsEnrichments.*
import scala.meta.internal.pc.AutoImports.*
Expand Down Expand Up @@ -57,6 +58,7 @@ case class ScalaPresentationCompiler(

private val forbiddenOptions = Set("-print-lines", "-print-tasty")
private val forbiddenDoubleOptions = Set("-release")
given Option[Reports] = workspace.map(Reports(_))

val compilerAccess: CompilerAccess[StoreReporter, MetalsDriver] =
Scala3CompilerAccess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.nio.file.Path
import scala.annotation.tailrec
import scala.collection.JavaConverters.*

import scala.meta.internal.metals.Reports
import scala.meta.internal.mtags.MtagsEnrichments.*
import scala.meta.internal.pc.AutoImports.AutoImportEdits
import scala.meta.internal.pc.AutoImports.AutoImportsGenerator
Expand Down Expand Up @@ -38,7 +39,7 @@ class CompletionProvider(
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
workspace: Option[Path],
):
)(using reports: Option[Reports]):
def completions(): CompletionList =
val uri = params.uri

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.nio.file.Paths
import scala.collection.mutable

import scala.meta.internal.metals.Fuzzy
import scala.meta.internal.metals.Reports
import scala.meta.internal.mtags.BuildInfo
import scala.meta.internal.mtags.MtagsEnrichments.*
import scala.meta.internal.pc.AutoImports.AutoImportsGenerator
Expand Down Expand Up @@ -51,7 +52,7 @@ class Completions(
workspace: Option[Path],
autoImports: AutoImportsGenerator,
options: List[String],
):
)(using Option[Reports]):

implicit val context: Context = ctx

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scala.meta.internal.pc.completions
import scala.collection.mutable.ListBuffer

import scala.meta.internal.metals.Fuzzy
import scala.meta.internal.metals.Reports
import scala.meta.internal.mtags.MtagsEnrichments.*
import scala.meta.internal.mtags.MtagsEnrichments.given
import scala.meta.internal.pc.AutoImports.AutoImport
Expand Down Expand Up @@ -41,7 +42,7 @@ object InterpolatorCompletions:
search: SymbolSearch,
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
)(using Context) =
)(using Context, Option[Reports]) =
InterpolationSplice(pos.span.point, text.toCharArray(), text) match
case Some(interpolator) =>
InterpolatorCompletions.contributeScope(
Expand Down Expand Up @@ -122,7 +123,7 @@ object InterpolatorCompletions:
areSnippetsSupported: Boolean,
search: SymbolSearch,
buildTargetIdentifier: String,
)(using Context): List[CompletionValue] =
)(using Context, Option[Reports]): List[CompletionValue] =
def newText(
name: String,
suffix: Option[String],
Expand Down Expand Up @@ -241,7 +242,7 @@ object InterpolatorCompletions:
search: SymbolSearch,
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
)(using ctx: Context): List[CompletionValue] =
)(using ctx: Context, reports: Option[Reports]): List[CompletionValue] =
val litStartPos = lit.span.start
val litEndPos = lit.span.end - Cursor.value.length()
val span = position.span
Expand Down
Loading

0 comments on commit 84d0290

Please sign in to comment.