Skip to content

Commit 237739e

Browse files
committed
Filter pure expression warning in worksheets
We now hide the warning the pure expression in statement position when the pure expression is an immediate children of the worksheet wrapper.
1 parent b55d17e commit 237739e

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import ast.{Trees, tpd}
2121
import core._, core.Decorators.{sourcePos => _, _}
2222
import Comments._, Contexts._, Flags._, Names._, NameOps._, Symbols._, SymDenotations._, Trees._, Types._
2323
import classpath.ClassPathEntries
24-
import reporting._, reporting.diagnostic.MessageContainer
24+
import reporting._, reporting.diagnostic.{Message, MessageContainer, messages}
2525
import typer.Typer
2626
import util._
2727
import interactive._, interactive.InteractiveDriver._
@@ -193,7 +193,7 @@ class DottyLanguageServer extends LanguageServer
193193

194194
client.publishDiagnostics(new PublishDiagnosticsParams(
195195
document.getUri,
196-
diags.flatMap(diagnostic(_, positionMapper)).asJava))
196+
diags.flatMap(diagnostic(_, positionMapper)(driver.currentCtx)).asJava))
197197
}
198198

199199
override def didChange(params: DidChangeTextDocumentParams): Unit = {
@@ -221,7 +221,7 @@ class DottyLanguageServer extends LanguageServer
221221

222222
client.publishDiagnostics(new PublishDiagnosticsParams(
223223
document.getUri,
224-
diags.flatMap(diagnostic(_, positionMapper)).asJava))
224+
diags.flatMap(diagnostic(_, positionMapper)(driver.currentCtx)).asJava))
225225
}
226226
}
227227

@@ -475,7 +475,9 @@ object DottyLanguageServer {
475475
* Convert a MessageContainer to an lsp4j.Diagnostic. The positions are transformed vy
476476
* `positionMapper`.
477477
*/
478-
def diagnostic(mc: MessageContainer, positionMapper: Option[SourcePosition => SourcePosition] = None): Option[lsp4j.Diagnostic] =
478+
def diagnostic(mc: MessageContainer,
479+
positionMapper: Option[SourcePosition => SourcePosition] = None
480+
)(implicit ctx: Context): Option[lsp4j.Diagnostic] =
479481
if (!mc.pos.exists)
480482
None // diagnostics without positions are not supported: https://github.com/Microsoft/language-server-protocol/issues/249
481483
else {
@@ -493,11 +495,40 @@ object DottyLanguageServer {
493495
}
494496
}
495497

496-
val code = mc.contained().errorId.errorNumber.toString
497-
range(mc.pos, positionMapper).map(r =>
498-
new lsp4j.Diagnostic(
499-
r, mc.message, severity(mc.level), /*source =*/ "", code))
498+
val message = mc.contained()
499+
if (displayMessage(message, mc.pos.source)) {
500+
val code = message.errorId.errorNumber.toString
501+
range(mc.pos, positionMapper).map(r =>
502+
new lsp4j.Diagnostic(
503+
r, mc.message, severity(mc.level), /*source =*/ "", code))
504+
} else {
505+
None
506+
}
507+
}
508+
509+
/**
510+
* Check whether `message` should be displayed in the IDE.
511+
*
512+
* Currently we only filter out the warning about pure expressions in statement position when they
513+
* are immediate children of the worksheet wrapper.
514+
*
515+
* @param message The message to filter.
516+
* @param sourceFile The sourcefile from which `message` originates.
517+
* @return true if the message should be displayed in the IDE, false otherwise.
518+
*/
519+
private def displayMessage(message: Message, sourceFile: SourceFile)(implicit ctx: Context): Boolean = {
520+
if (isWorksheet(sourceFile)) {
521+
message match {
522+
case messages.PureExpressionInStatementPosition(_, exprOwner) =>
523+
val ownerSym = if (exprOwner.isLocalDummy) exprOwner.owner else exprOwner
524+
!isWorksheetWrapper(ownerSym)
525+
case _ =>
526+
true
527+
}
528+
} else {
529+
true
500530
}
531+
}
501532

502533
/** Does this URI represent a worksheet? */
503534
private def isWorksheet(uri: URI): Boolean =
@@ -559,8 +590,15 @@ object DottyLanguageServer {
559590
* @see wrapWorksheet
560591
*/
561592
def isWorksheetWrapper(sourceTree: SourceTree)(implicit ctx: Context): Boolean = {
562-
val symbol = sourceTree.tree.symbol
563-
isWorksheet(sourceTree.source) &&
593+
isWorksheet(sourceTree.source) && isWorksheetWrapper(sourceTree.tree.symbol)
594+
}
595+
596+
/**
597+
* Is this symbol the wrapper object that we put around worksheet sources?
598+
*
599+
* @see wrapWorksheet
600+
*/
601+
def isWorksheetWrapper(symbol: Symbol)(implicit ctx: Context): Boolean = {
564602
symbol.name.toString == "Worksheet$" &&
565603
symbol.owner == ctx.definitions.EmptyPackageClass
566604
}

0 commit comments

Comments
 (0)