@@ -21,7 +21,7 @@ import ast.{Trees, tpd}
21
21
import core ._ , core .Decorators .{sourcePos => _ , _ }
22
22
import Comments ._ , Contexts ._ , Flags ._ , Names ._ , NameOps ._ , Symbols ._ , SymDenotations ._ , Trees ._ , Types ._
23
23
import classpath .ClassPathEntries
24
- import reporting ._ , reporting .diagnostic .MessageContainer
24
+ import reporting ._ , reporting .diagnostic .{ Message , MessageContainer , messages }
25
25
import typer .Typer
26
26
import util ._
27
27
import interactive ._ , interactive .InteractiveDriver ._
@@ -193,7 +193,7 @@ class DottyLanguageServer extends LanguageServer
193
193
194
194
client.publishDiagnostics(new PublishDiagnosticsParams (
195
195
document.getUri,
196
- diags.flatMap(diagnostic(_, positionMapper)).asJava))
196
+ diags.flatMap(diagnostic(_, positionMapper)(driver.currentCtx) ).asJava))
197
197
}
198
198
199
199
override def didChange (params : DidChangeTextDocumentParams ): Unit = {
@@ -221,7 +221,7 @@ class DottyLanguageServer extends LanguageServer
221
221
222
222
client.publishDiagnostics(new PublishDiagnosticsParams (
223
223
document.getUri,
224
- diags.flatMap(diagnostic(_, positionMapper)).asJava))
224
+ diags.flatMap(diagnostic(_, positionMapper)(driver.currentCtx) ).asJava))
225
225
}
226
226
}
227
227
@@ -475,7 +475,9 @@ object DottyLanguageServer {
475
475
* Convert a MessageContainer to an lsp4j.Diagnostic. The positions are transformed vy
476
476
* `positionMapper`.
477
477
*/
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 ] =
479
481
if (! mc.pos.exists)
480
482
None // diagnostics without positions are not supported: https://github.com/Microsoft/language-server-protocol/issues/249
481
483
else {
@@ -493,11 +495,40 @@ object DottyLanguageServer {
493
495
}
494
496
}
495
497
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
500
530
}
531
+ }
501
532
502
533
/** Does this URI represent a worksheet? */
503
534
private def isWorksheet (uri : URI ): Boolean =
@@ -559,8 +590,15 @@ object DottyLanguageServer {
559
590
* @see wrapWorksheet
560
591
*/
561
592
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 = {
564
602
symbol.name.toString == " Worksheet$" &&
565
603
symbol.owner == ctx.definitions.EmptyPackageClass
566
604
}
0 commit comments