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 15, 2023
1 parent e2997ce commit 4cd8917
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import scala.concurrent.Promise
import scala.concurrent.duration.FiniteDuration
import scala.util.Properties
import scala.util.Try
import scala.util.control.NonFatal
import scala.{meta => m}

import scala.meta.Template
Expand Down Expand Up @@ -488,9 +487,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 +509,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 @@ -1086,11 +1057,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 @@ -1170,9 +1171,25 @@ class MetalsLspService(
None
}

uriOpt match {
case Some(uri) => {
val path = uri.toAbsolutePath
val pathOpt = uriOpt.flatMap { uri =>
try {
Some(uri.toAbsolutePath)
} catch {
case NonFatal(e) =>
reports.incognito.createReport(
"did-focus-absolute-path",
s"""|metals/didFocusTextDocument
|Uri: $uri
|Error message: ${e.getMessage()}
|Error: ${e}
|""".stripMargin,
)
None
}
}

pathOpt match {
case Some(path) => {
focusedDocument = Some(path)
buildTargets
.inverseSources(path)
Expand Down Expand Up @@ -2736,6 +2753,13 @@ 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}
|Error message: ${e.getMessage()}
|Error: ${e.underlying}
|""".stripMargin,
)
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 @@ -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 @@ -20,6 +20,7 @@ import scala.tools.nsc.reporters.StoreReporter

import scala.meta.internal.jdk.CollectionConverters._
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.pc.AutoImportsResult
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,6 +32,7 @@ final class AutoImportsProvider(
params: OffsetParams,
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
reports: Option[Reports],
):

def autoImports(isExtension: Boolean): List[AutoImportsResult] =
Expand Down Expand Up @@ -66,7 +68,7 @@ final class AutoImportsProvider(
def isExactMatch(sym: Symbol, query: String): Boolean =
sym.name.show == query

val visitor = new CompilerSearchVisitor(name, visit)
val visitor = new CompilerSearchVisitor(name, visit, reports)
if isExtension then
search.searchMethods(name, buildTargetIdentifier, visitor)
else search.search(name, buildTargetIdentifier, visitor)
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,6 +17,7 @@ import dotty.tools.dotc.core.Symbols.*
class CompilerSearchVisitor(
query: String,
visitSymbol: Symbol => Boolean,
reports: Option[Reports],
)(using ctx: Context)
extends SymbolSearchVisitor:

Expand All @@ -25,6 +27,14 @@ class CompilerSearchVisitor(
sym != NoSymbol && sym.isPublic
catch
case NonFatal(e) =>
reports.foreach(
_.incognito.createReport(
"is_public",
s"""|Symbol: $sym
|Error message: ${e.getMessage()}
|""".stripMargin,
)
)
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")
private val optReports = workspace.map(Reports(_))

val compilerAccess: CompilerAccess[StoreReporter, MetalsDriver] =
Scala3CompilerAccess(
Expand Down Expand Up @@ -115,6 +117,7 @@ case class ScalaPresentationCompiler(
config,
buildTargetIdentifier,
workspace,
optReports,
).completions()

}
Expand Down Expand Up @@ -205,6 +208,7 @@ case class ScalaPresentationCompiler(
params,
config,
buildTargetIdentifier,
optReports,
)
.autoImports(isExtension)
.asJava
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,6 +39,7 @@ class CompletionProvider(
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
workspace: Option[Path],
reports: Option[Reports],
):
def completions(): CompletionList =
val uri = params.uri
Expand Down Expand Up @@ -84,6 +86,7 @@ class CompletionProvider(
workspace,
autoImportsGen,
driver.settings,
reports,
).completions()

val items = completions.zipWithIndex.map { case (item, idx) =>
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,6 +52,7 @@ class Completions(
workspace: Option[Path],
autoImports: AutoImportsGenerator,
options: List[String],
reports: Option[Reports],
):

implicit val context: Context = ctx
Expand Down Expand Up @@ -469,6 +471,7 @@ class Completions(
search,
config,
buildTargetIdentifier,
reports,
)
.filterInteresting(enrich = false)
._1
Expand Down Expand Up @@ -579,7 +582,9 @@ class Completions(
sym,
sym.decodedName,
CompletionValue.Workspace(_, _, _, sym),
).forall(visit),
).forall(visit)
,
reports,
)
Some(search.search(query, buildTargetIdentifier, visitor))
case CompletionKind.Members if query.nonEmpty =>
Expand All @@ -595,6 +600,7 @@ class Completions(
CompletionValue.Extension(_, _, _),
).forall(visit)
else false,
reports,
)
Some(search.searchMethods(query, buildTargetIdentifier, visitor))
case CompletionKind.Members => // query.isEmpry
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,6 +42,7 @@ object InterpolatorCompletions:
search: SymbolSearch,
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
reports: Option[Reports],
)(using Context) =
InterpolationSplice(pos.span.point, text.toCharArray(), text) match
case Some(interpolator) =>
Expand All @@ -58,6 +60,7 @@ object InterpolatorCompletions:
search,
config,
buildTargetIdentifier,
reports,
)
case None =>
InterpolatorCompletions.contributeMember(
Expand All @@ -70,6 +73,7 @@ object InterpolatorCompletions:
snippetsEnabled,
search,
buildTargetIdentifier,
reports,
)
end match
end contribute
Expand Down Expand Up @@ -122,6 +126,7 @@ object InterpolatorCompletions:
areSnippetsSupported: Boolean,
search: SymbolSearch,
buildTargetIdentifier: String,
reports: Option[Reports],
)(using Context): List[CompletionValue] =
def newText(
name: String,
Expand Down Expand Up @@ -152,6 +157,7 @@ object InterpolatorCompletions:
buffer.append(sym)
true
else false,
reports,
)
search.searchMethods(completionPos.query, buildTargetIdentifier, visitor)
buffer.toList
Expand Down Expand Up @@ -241,6 +247,7 @@ object InterpolatorCompletions:
search: SymbolSearch,
config: PresentationCompilerConfig,
buildTargetIdentifier: String,
reports: Option[Reports],
)(using ctx: Context): List[CompletionValue] =
val litStartPos = lit.span.start
val litEndPos = lit.span.end - Cursor.value.length()
Expand Down Expand Up @@ -289,7 +296,9 @@ object InterpolatorCompletions:
case IndexedContext.Result.InScope => false
case _ =>
if sym.is(Flags.Module) then workspaceSymbols += sym
true,
true
,
reports,
)
if interpolator.name.nonEmpty then
search.search(interpolator.name, buildTargetIdentifier, visitor)
Expand Down
Loading

0 comments on commit 4cd8917

Please sign in to comment.