From af9753f113262e24914810862dc5e26cc9b6d1a9 Mon Sep 17 00:00:00 2001 From: kasiaMarek Date: Wed, 18 Dec 2024 15:39:02 +0100 Subject: [PATCH] catch `NullPointerException` in `typedTreeAt` in signature help --- .../internal/pc/SignatureHelpProvider.scala | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/SignatureHelpProvider.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/SignatureHelpProvider.scala index d2e020b99a7..a63dc555de2 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/SignatureHelpProvider.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/SignatureHelpProvider.scala @@ -20,15 +20,23 @@ class SignatureHelpProvider(val compiler: MetalsGlobal) { cursor = cursor(params.offset(), params.text()) ) val pos = unit.position(params.offset()) - typedTreeAt(pos) - val enclosingApply = new EnclosingApply(pos).find(unit.body) - val typedEnclosing = typedTreeAt(enclosingApply.pos) - new MethodCallTraverser(unit, pos) - .fromTree(typedEnclosing) - .map(toSignatureHelp) - .getOrElse(new SignatureHelp()) + (for { + _ <- safeTypedTreeAt(pos) + enclosingApply = new EnclosingApply(pos).find(unit.body) + typedEnclosing <- safeTypedTreeAt(enclosingApply.pos) + encolsingCall <- new MethodCallTraverser(unit, pos).fromTree( + typedEnclosing + ) + } yield toSignatureHelp(encolsingCall)) getOrElse new SignatureHelp() } + private def safeTypedTreeAt(pos: Position): Option[Tree] = + try { + Some(typedTreeAt(pos)) + } catch { + case _: NullPointerException => None + } + class EnclosingApply(pos: Position) extends Traverser { var last: Tree = EmptyTree def find(tree: Tree): Tree = {