Skip to content

Commit

Permalink
improvement: Don't publish diagnostics for dependencies
Browse files Browse the repository at this point in the history
Previously, we would publish dependency diagnsotics as info diagnostics, but that has never proved useful and it might be false positive as we don't have the full build definition of dependencies. Now, we don't publish them at all.
  • Loading branch information
tgodzik committed Dec 5, 2023
1 parent bf1bb31 commit ab529da
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@ package scala.meta.internal.metals

import java.nio.charset.Charset
import java.util.Collections
import java.util.concurrent.atomic.AtomicReference

import scala.util.Success
import scala.util.Try

import scala.meta.internal.builds.SbtBuildTool
import scala.meta.internal.metals.Messages._
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.mtags.MD5
import scala.meta.internal.mtags.Semanticdbs
import scala.meta.internal.mtags.TextDocumentLookup
import scala.meta.internal.{semanticdb => s}
import scala.meta.io.AbsolutePath

import org.eclipse.lsp4j.DiagnosticSeverity
import org.eclipse.lsp4j.PublishDiagnosticsParams
import org.eclipse.{lsp4j => l}

/**
* Produces SemanticDBs on-demand by using the presentation compiler.
*
Expand All @@ -34,9 +27,7 @@ final class InteractiveSemanticdbs(
workspace: AbsolutePath,
buildTargets: BuildTargets,
charset: Charset,
client: MetalsLanguageClient,
tables: Tables,
statusBar: StatusBar,
compilers: () => Compilers,
clientConfig: ClientConfiguration,
semanticdbIndexer: () => SemanticdbIndexer,
Expand All @@ -45,7 +36,6 @@ final class InteractiveSemanticdbs(
) extends Cancelable
with Semanticdbs {

private val activeDocument = new AtomicReference[Option[String]](None)
private val textDocumentCache = Collections.synchronizedMap(
new java.util.HashMap[AbsolutePath, s.TextDocument]()
)
Expand Down Expand Up @@ -134,42 +124,6 @@ final class InteractiveSemanticdbs(
}
}

/**
* Unpublish diagnostics for un-focused dependency source, if any, and publish diagnostics
* for the currently focused source, if any.
*/
def didFocus(path: AbsolutePath): Unit = {
activeDocument.get().foreach { uri =>
client.publishDiagnostics(
new PublishDiagnosticsParams(uri, Collections.emptyList())
)
}
if (path.isDependencySource(workspace)) {
textDocument(path).toOption.foreach { doc =>
val uri = path.toURI.toString()
activeDocument.set(Some(uri))
val diagnostics = for {
diag <- doc.diagnostics
if diag.severity.isError
range <- diag.range
} yield {
// Use INFO instead of ERROR severity because these diagnostics are published for readonly
// files of external dependencies so the user cannot fix them.
val severity = DiagnosticSeverity.Information
new l.Diagnostic(range.toLsp, diag.message, severity, "scala")
}
if (diagnostics.nonEmpty) {
statusBar.addMessage(partialNavigation(clientConfig.icons))
client.publishDiagnostics(
new PublishDiagnosticsParams(uri, diagnostics.asJava)
)
}
}
} else {
activeDocument.set(None)
}
}

private def compile(source: AbsolutePath, text: String): s.TextDocument = {
if (source.isJavaFilename)
javaInteractiveSemanticdb.fold(s.TextDocument())(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,7 @@ class MetalsLspService(
folder,
buildTargets,
charset,
languageClient,
tables,
statusBar,
() => compilers,
clientConfig,
() => semanticDBIndexer,
Expand Down Expand Up @@ -1076,11 +1074,7 @@ class MetalsLspService(
} yield ()

if (path.isDependencySource(folder)) {
CancelTokens { _ =>
// publish diagnostics
interactiveSemanticdbs.didFocus(path)
()
}
CompletableFuture.completedFuture(())
} else {
buildServerPromise.future.flatMap { _ =>
def load(): Future[Unit] = {
Expand Down Expand Up @@ -1135,8 +1129,6 @@ class MetalsLspService(
buildTargets
.inverseSources(path)
.foreach(focusedDocumentBuildTarget.set)
// unpublish diagnostic for dependencies
interactiveSemanticdbs.didFocus(path)
// Don't trigger compilation on didFocus events under cascade compilation
// because save events already trigger compile in inverse dependencies.
if (path.isDependencySource(folder)) {
Expand Down

0 comments on commit ab529da

Please sign in to comment.