Correctly handle boundary conditions in the ScalaWordFinder

I reorganized and simplified the logic in the ScalaWordFinder,
and added a few tests. It seems to fix at least the exception
and a couple of other boundary conditions (revealed by pressing
F3 at the very beginning or very end of an identifier, or even
an identifier between `{}`).

Fixed #1001526