Skip to content

Commit

Permalink
Refactor Report Context to java interface, extend PresentationCompile…
Browse files Browse the repository at this point in the history
…r with additional reports
  • Loading branch information
rochala committed Jan 26, 2024
1 parent 8f0a4c9 commit 07ed784
Show file tree
Hide file tree
Showing 70 changed files with 622 additions and 478 deletions.
25 changes: 15 additions & 10 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,14 @@ def lintingOptions(scalaVersion: String) = {
}

val sharedJavacOptions = List(
packageDoc / publishArtifact := true,
packageSrc / publishArtifact := true,
Compile / javacOptions ++= {
if (sys.props("java.version").startsWith("1.8"))
Nil
else
Seq("--release", "8")
}
},
)

val sharedScalacOptions = List(
Expand All @@ -210,7 +212,7 @@ val sharedScalacOptions = List(
isScala212(partialVersion) && V.scala212 != scalaVersion.value =>
List("-target:jvm-1.8", "-Yrangepos", "-Xexperimental")
case partialVersion if isScala3(partialVersion) =>
List("-Xtarget:8", "-language:implicitConversions", "-Xsemanticdb")
List("-Xtarget:8", "-language:implicitConversions")
case _ =>
List("-target:jvm-1.8", "-Yrangepos")
}
Expand Down Expand Up @@ -255,9 +257,8 @@ lazy val interfaces = project

lazy val telemetryInterfaces = project
.in(file("telemetry-interfaces"))
.settings(sharedSettings)
.settings(sharedJavacOptions)
.settings(
sharedJavacOptions,
moduleName := "telemetry-interfaces",
autoScalaLibrary := false,
crossVersion := CrossVersion.disabled,
Expand All @@ -279,17 +280,21 @@ lazy val mtagsShared = project
Compile / packageSrc / publishArtifact := true,
Compile / scalacOptions ++= {
if (scalaVersion.value == V.scala3)
List("-Yexplicit-nulls", "-language:unsafeNulls")
List(
"-Yexplicit-nulls",
"-language:unsafeNulls",
"-Xfatal-warnings",
"-deprecation",
)
else Nil
},
libraryDependencies ++= List(
"org.lz4" % "lz4-java" % "1.8.0",
"com.google.protobuf" % "protobuf-java" % "3.25.2",
"io.get-coursier" % "interface" % V.coursierInterfaces,
"com.lihaoyi" %% "requests" % V.requests,
),
)
.dependsOn(interfaces, telemetryInterfaces)
.dependsOn(interfaces)

def multiScalaDirectories(root: File, scalaVersion: String) = {
val base = root / "src" / "main"
Expand Down Expand Up @@ -407,7 +412,7 @@ lazy val mtags3 = project
(ThisBuild / baseDirectory).value / ".scalafix3.conf"
),
)
.dependsOn(interfaces, telemetryInterfaces)
.dependsOn(interfaces)
.enablePlugins(BuildInfoPlugin)

lazy val mtags3WithPresentationCompiler = project
Expand All @@ -425,7 +430,7 @@ lazy val mtags3WithPresentationCompiler = project
(ThisBuild / baseDirectory).value / ".scalafix3.conf"
),
)
.dependsOn(interfaces, telemetryInterfaces)
.dependsOn(interfaces)
.enablePlugins(BuildInfoPlugin)

lazy val mtags = project
Expand Down Expand Up @@ -572,7 +577,7 @@ lazy val metals = project
"lastSupportedSemanticdb" -> SemanticDbSupport.last,
),
)
.dependsOn(mtags, `mtags-java`)
.dependsOn(mtags, `mtags-java`, telemetryInterfaces)
.enablePlugins(BuildInfoPlugin)

lazy val `sbt-metals` = project
Expand Down
2 changes: 1 addition & 1 deletion metals-bench/src/main/scala/bench/MetalsBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import scala.meta.dialects
import scala.meta.interactive.InteractiveSemanticdb
import scala.meta.internal.metals.EmptyReportContext
import scala.meta.internal.metals.JdkSources
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.logging.MetalsLogger
import scala.meta.internal.mtags.JavaMtags
import scala.meta.internal.mtags.Mtags
Expand All @@ -22,6 +21,7 @@ import scala.meta.internal.tokenizers.LegacyScanner
import scala.meta.internal.tokenizers.LegacyToken
import scala.meta.io.AbsolutePath
import scala.meta.io.Classpath
import scala.meta.pc.ReportContext

import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import scala.meta.internal.metals.BspStatus
import scala.meta.internal.metals.ClientCommands
import scala.meta.internal.metals.Icons
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.clients.language.MetalsStatusParams
import scala.meta.internal.metals.clients.language.StatusType
import scala.meta.io.AbsolutePath
import scala.meta.pc.ReportContext

class ConnectionBspStatus(
bspStatus: BspStatus,
Expand Down Expand Up @@ -102,7 +102,7 @@ class ConnectionBspStatus(
*/
private def syncWithReportContext(errorReports: Set[String]) =
errorReports.intersect(
rc.bloop.getReports().map(_.toPath.toUri().toString()).toSet
rc.bloop.getReports().map(_.toPath.toUri().toString()).asScala.toSet
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import java.security.MessageDigest
import scala.meta.internal.bsp.BspSession
import scala.meta.internal.bsp.ConnectionBspStatus
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.Report
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.Tables
import scala.meta.internal.pc.StandardReport
import scala.meta.pc.ReportContext

import com.google.common.io.BaseEncoding

Expand All @@ -19,7 +19,7 @@ class BspErrorHandler(
def onError(message: String): Unit = {
if (shouldShowBspError) {
for {
report <- createReport(message)
report <- createReport(message).asScala
if !tables.dismissedNotifications.BspErrors.isDismissed
} bspStatus.showError(message, report)
} else logError(message)
Expand All @@ -36,7 +36,7 @@ class BspErrorHandler(
val id = BaseEncoding.base64().encode(digest)
val sanitized = reportContext.bloop.sanitize(message)
reportContext.bloop.create(
Report(
StandardReport(
sanitized.trimTo(20),
s"""|### Bloop error:
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import scala.meta.internal.metals.Buffers
import scala.meta.internal.metals.BuildTargets
import scala.meta.internal.metals.DefinitionProvider
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ReportContext
import scala.meta.internal.metals.ScalaVersionSelector
import scala.meta.internal.metals.SemanticdbFeatureProvider
import scala.meta.internal.mtags.GlobalSymbolIndex
Expand All @@ -33,6 +32,7 @@ import scala.meta.internal.semanticdb.TypeRef
import scala.meta.internal.semanticdb.TypeSignature
import scala.meta.internal.symtab.GlobalSymbolTable
import scala.meta.io.AbsolutePath
import scala.meta.pc.ReportContext

import org.eclipse.lsp4j.Location
import org.eclipse.lsp4j.TextDocumentPositionParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ 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.ReportContext
import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.metals.clients.language.MetalsQuickPickItem
import scala.meta.internal.metals.clients.language.MetalsQuickPickParams
import scala.meta.internal.semanticdb.SymbolInformation
import scala.meta.io.AbsolutePath
import scala.meta.pc.ReportContext

import org.eclipse.lsp4j.ExecuteCommandParams
import org.eclipse.lsp4j.Location
Expand Down
84 changes: 66 additions & 18 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import scala.meta.internal.pc.EmptySymbolSearch
import scala.meta.internal.pc.JavaPresentationCompiler
import scala.meta.internal.pc.LogMessages
import scala.meta.internal.pc.ScalaPresentationCompiler
import scala.meta.internal.telemetry
import scala.meta.internal.telemetry.ReporterContext
import scala.meta.internal.telemetry.ScalaPresentationCompilerContext
import scala.meta.internal.telemetry.TelemetryReportContext
import scala.meta.internal.worksheets.WorksheetPcData
import scala.meta.internal.worksheets.WorksheetProvider
import scala.meta.io.AbsolutePath
Expand All @@ -34,6 +38,8 @@ import scala.meta.pc.CancelToken
import scala.meta.pc.HoverSignature
import scala.meta.pc.OffsetParams
import scala.meta.pc.PresentationCompiler
import scala.meta.pc.PresentationCompilerConfig
import scala.meta.pc.ReportContext
import scala.meta.pc.SymbolSearch
import scala.meta.pc.SyntheticDecoration

Expand Down Expand Up @@ -1004,7 +1010,7 @@ class Compilers(
): Option[PresentationCompiler] = {
val pc = JavaPresentationCompiler()
Some(
configure(pc, search)
configure(pc, search, log)
.newInstance(
targetUri,
classpath.toAbsoluteClasspath.map(_.toNIO).toSeq.asJava,
Expand Down Expand Up @@ -1133,29 +1139,70 @@ class Compilers(
sourceMapper.pcMapping(path, scalaVersion)
}

private def createTelemetryReporterContext(
scalaVersion: String,
config: PresentationCompilerConfig,
options: List[String],
): ReporterContext =
new ScalaPresentationCompilerContext(
scalaVersion,
options.asJava,
telemetry.conversion.PresentationCompilerConfig(config),
)

private def remoteReporting(
createTelemetryReporterContext: () => ReporterContext
): ReportContext = {
val logger =
ju.logging.Logger.getLogger(classOf[TelemetryReportContext].getName)
new TelemetryReportContext(
telemetryLevel = () => userConfig().telemetryLevel,
reporterContext = createTelemetryReporterContext,
sanitizers = new TelemetryReportContext.Sanitizers(
workspace = Some(workspace.toNIO),
sourceCodeTransformer = Some(ScalametaSourceCodeTransformer),
),
logger = LoggerAccess(
debug = logger.fine(_),
info = logger.info(_),
warning = logger.warning(_),
error = logger.severe(_),
),
)
}

private def getUserConfiguration(): PresentationCompilerConfig = {
val options =
InitializationOptions.from(initializeParams).compilerOptions
config.initialConfig.compilers
.update(options)
.copy(
_symbolPrefixes = userConfig().symbolPrefixes,
isCompletionSnippetsEnabled =
initializeParams.supportsCompletionSnippets,
_isStripMarginOnTypeFormattingEnabled =
() => userConfig().enableStripMarginOnTypeFormatting,
)
}

private def configure(
pc: PresentationCompiler,
search: SymbolSearch,
): PresentationCompiler =
options: List[String],
): PresentationCompiler = {
val config = getUserConfiguration()
val remoteReportContext = remoteReporting(() =>
createTelemetryReporterContext(pc.scalaVersion, config, options)
)

pc.withSearch(search)
.withExecutorService(ec)
.withWorkspace(workspace.toNIO)
.withScheduledExecutorService(sh)
.withReportsLoggerLevel(MetalsServerConfig.default.loglevel)
.withTelemetryLevel(userConfig().telemetryLevel.stringValue)
.withConfiguration {
val options =
InitializationOptions.from(initializeParams).compilerOptions
config.initialConfig.compilers
.update(options)
.copy(
_symbolPrefixes = userConfig().symbolPrefixes,
isCompletionSnippetsEnabled =
initializeParams.supportsCompletionSnippets,
_isStripMarginOnTypeFormattingEnabled =
() => userConfig().enableStripMarginOnTypeFormatting,
)
}
.withConfiguration(config)
.withAdditionalReportContexts(List(remoteReportContext).asJava)
}

def newCompiler(
target: ScalaTarget,
Expand Down Expand Up @@ -1197,11 +1244,12 @@ class Compilers(
}

val filteredOptions = plugins.filterSupportedOptions(options)
configure(pc, search)
val allOptions = log ++ filteredOptions
configure(pc, search, allOptions)
.newInstance(
name,
classpath.asJava,
(log ++ filteredOptions).asJava,
(allOptions).asJava,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import scala.meta.internal.semanticdb.Synthetic
import scala.meta.internal.semanticdb.TextDocument
import scala.meta.io.AbsolutePath
import scala.meta.pc.CancelToken
import scala.meta.pc.ReportContext
import scala.meta.tokens.Token

import ch.epfl.scala.bsp4j.BuildTargetIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import scala.meta.internal.semanticdb.Scala._
import scala.meta.internal.tvp.FolderTreeViewProvider
import scala.meta.internal.worksheets.WorksheetProvider
import scala.meta.io.AbsolutePath
import scala.meta.pc.ReportContext

import ch.epfl.scala.{bsp4j => b}
import org.eclipse.lsp4j.Position
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package scala.meta.internal.metals

import java.nio.file.Path
import java.{util => ju}

import scala.meta.internal.metals.utils.TimestampedFile
import scala.meta.pc.Report
import scala.meta.pc.ReportContext
import scala.meta.pc.Reporter
import scala.meta.pc.TimestampedFile

object LoggerReporter extends Reporter {

override def name: String = "logger-report"
override def create(report: => Report, ifVerbose: Boolean): Option[Path] = {
override def create(report: Report, ifVerbose: Boolean): ju.Optional[Path] = {
scribe.info(
s"Report ${report.name}: ${report.fullText(withIdAndSummary = false)}"
s"Report ${report.name}: ${report.fullText(false)}"
)
None
ju.Optional.empty()
}

override def cleanUpOldReports(maxReportsNumber: Int): List[TimestampedFile] =
List()
override def cleanUpOldReports(
maxReportsNumber: Int
): ju.List[TimestampedFile] =
ju.Collections.emptyList()

override def getReports(): List[TimestampedFile] = List()
override def getReports(): ju.List[TimestampedFile] =
ju.Collections.emptyList()

override def deleteAll(): Unit = {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ import scala.meta.inputs.Input
import scala.meta.internal.io.FileIO
import scala.meta.internal.mtags.MtagsEnrichments
import scala.meta.internal.parsing.EmptyResult
import scala.meta.internal.pc.StandardReport
import scala.meta.internal.semanticdb.Scala.Descriptor
import scala.meta.internal.semanticdb.Scala.Symbols
import scala.meta.internal.trees.Origin
import scala.meta.internal.trees.Origin.Parsed
import scala.meta.internal.{semanticdb => s}
import scala.meta.io.AbsolutePath
import scala.meta.io.RelativePath
import scala.meta.pc.ReportContext

import ch.epfl.scala.{bsp4j => b}
import com.google.gson.Gson
Expand Down Expand Up @@ -673,7 +675,7 @@ object MetalsEnrichments
} catch {
case NonFatal(error) =>
reports.incognito.create(
Report(
StandardReport(
"absolute-path",
s"""|Uri: $value
|""".stripMargin,
Expand Down
Loading

0 comments on commit 07ed784

Please sign in to comment.