Skip to content

Commit

Permalink
roll back on making the output of ScalaWordFinder an Option
Browse files Browse the repository at this point in the history
  • Loading branch information
François Garillot authored and huitseeker committed Sep 27, 2014
1 parent dda90aa commit 31f58f4
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class CompletionTests {
val completions: List[ICompletionProposal] = completion.computeCompletionProposals(context, monitor).map(_.asInstanceOf[ICompletionProposal]).toList
*/

val completions = wordRegion.map(completion.findCompletions(_)(pos + 1, unit)(src, compiler)).getOrElse(Nil)
val completions = completion.findCompletions(wordRegion)(pos + 1, unit)(src, compiler)
val sortedCompletions = completions.sortWith((x,y) => x.relevance >= y.relevance)
body(i, position, sortedCompletions)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class FindReferencesTests extends FindReferencesTester with HasLogger {

val offset = offsets.head

val wordRegion = ScalaWordFinder.findWordOrEmptyRegion(unit.getContents, offset)
val wordRegion = ScalaWordFinder.findWord(unit.getContents, offset)
val word = new String(unit.getContents.slice(wordRegion.getOffset, wordRegion.getOffset + wordRegion.getLength))

if (word.trim.isEmpty) fail("No word found at offset: " + offset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ trait HyperlinkTester extends TestProjectSetup {
if(forceTypeChecking) waitUntilTypechecked(unit)
new VerifyHyperlink {
/** @param expectations A collection of expected `Link` (test's oracle). */
override def andCheckAgainst(expectations: List[Link], checker: (InteractiveCompilationUnit, IRegion, String, Link) => Unit = checkScalaLinks) = {
def andCheckAgainst(expectations: List[Link], checker: (InteractiveCompilationUnit, IRegion, String, Link) => Unit = checkScalaLinks) = {
val positions = findMarker(HyperlinkMarker).in(unit)

println("checking %d positions".format(positions.size))
assertEquals(positions.size, expectations.size)
for ((pos, oracle) <- positions.zip(expectations)) {
val wordRegion = ScalaWordFinder.findWordOrEmptyRegion(unit.getContents, pos)
val wordRegion = ScalaWordFinder.findWord(unit.getContents, pos)
val word = new String(unit.getContents.slice(wordRegion.getOffset, wordRegion.getOffset + wordRegion.getLength))
println("hyperlinking at position %d (%s)".format(pos, word))
checker(unit, wordRegion, word, oracle)
Expand Down Expand Up @@ -79,4 +79,4 @@ trait HyperlinkTester extends TestProjectSetup {
} yield tpe.getFullyQualifiedName() + "." + e.getElementName()
assertEquals("text", oracle.text.toString, linkResults.toString)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ScalaWordFinderTest {
private def findWord(doc: String): IRegion = {
val pos = doc.indexOf('|')
val filteredDoc = doc.filterNot(_ == '|')
ScalaWordFinder.findWord(filteredDoc.toIndexedSeq, pos).orNull
ScalaWordFinder.findWord(filteredDoc.toIndexedSeq, pos)
}

private def test(doc: String, r: (Int, Int)) {
Expand Down Expand Up @@ -75,4 +75,4 @@ class ScalaWordFinderTest {
test("foo(<<<=|) ", (4, 4))
test("foo(<<<=|", (4, 4))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class OccurrencesFinderTest {

for ((pos, count) <- positions) {
println("looking at position %d for %d occurrences".format(pos, count))
val region = ScalaWordFinder.findWordOrEmptyRegion(contents, pos - 1)
val region = ScalaWordFinder.findWord(contents, pos - 1)
val word = new String(contents.slice(region.getOffset(), region.getOffset() + region.getLength()))
println("using word region: " + region)
val occurrences = new ScalaOccurrencesFinder(unit).findOccurrences(region, 1)
assertTrue("No occurrences of %s".format(word), occurrences.isDefined)
assertEquals("Not enough occurrences (%s): expected: %d, found: %d".format(word, count, occurrences.get.locations.size), count, occurrences.get.locations.size)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ abstract class CompletionTests extends TextEditTests with CompilerSupport {
override def execute() = withCompiler { compiler =>
val unit = mkScalaCompilationUnit(doc.get())
val src = unit.sourceFile()
val w = ScalaWordFinder.findWord(doc, caretOffset)
val completions = w.map(new ScalaCompletions().findCompletions(_)(caretOffset, unit)(src, compiler)).getOrElse(Nil)
val completions = new ScalaCompletions().findCompletions(ScalaWordFinder.findWord(doc, caretOffset))(caretOffset, unit)(src, compiler)

def findCompletion(rawCompletion: String) =
if (!rawCompletion.contains("-"))
Expand Down Expand Up @@ -94,4 +93,4 @@ abstract class CompletionTests extends TextEditTests with CompilerSupport {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DeclarationHyperlinkDetector extends BaseHyperlinkDetector with HasLogger
val doc = textEditor.getDocumentProvider.getDocument(input)
val wordRegion = ScalaWordFinder.findWord(doc.get, currentSelection.getOffset)

wordRegion.map(findHyperlinks(textEditor, icu, _)).getOrElse(Nil)
findHyperlinks(textEditor, icu, wordRegion)
}

protected def findHyperlinks(textEditor: ITextEditor, icu: InteractiveCompilationUnit, wordRegion: IRegion): List[IHyperlink] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import org.scalaide.core.compiler.IScalaPresentationCompiler._
import scala.tools.nsc.interactive.InteractiveReporter
import scala.tools.nsc.interactive.CommentPreservingTypers
import org.scalaide.ui.internal.editor.hover.ScalaDocHtmlProducer
import scala.util.Try

class ScalaPresentationCompiler(name: String, _settings: Settings) extends {
/*
Expand Down Expand Up @@ -453,13 +454,13 @@ object ScalaPresentationCompiler {
import prob._
if (pos.isDefined) {
val source = pos.source
val reducedPos =
val reducedPos: Option[Position]=
if (pos.isRange)
Option(toSingleLine(pos))
else
ScalaWordFinder.findWord(source.content, pos.start) map { w
new RangePosition(source, pos.point, pos.point, pos.point + w.getLength())
}
Some(toSingleLine(pos))
else{
val wordPos = Try(ScalaWordFinder.findWord(source.content, pos.start).getLength).toOption
wordPos map ((p) => new RangePosition(pos.source, pos.point, pos.point, pos.point + p))
}

reducedPos flatMap { reducedPos
val fileName =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import org.scalaide.util.internal.ScalaWordFinder
import org.scalaide.core.internal.jdt.model.ScalaLocalVariableElement
import org.scalaide.core.compiler.IScalaPresentationCompiler.Implicits._

class ScalaSelectionEngine(nameEnvironment: SearchableEnvironment, requestor: ScalaSelectionRequestor, settings: ju.Map[_, _])
extends Engine(settings) with ISearchRequestor with HasLogger {
class ScalaSelectionEngine(nameEnvironment: SearchableEnvironment, requestor: ScalaSelectionRequestor, settings: ju.Map[_, _]) extends Engine(settings) with ISearchRequestor with HasLogger {

var actualSelectionStart: Int = _
var actualSelectionEnd: Int = _
Expand All @@ -41,7 +40,7 @@ class ScalaSelectionEngine(nameEnvironment: SearchableEnvironment, requestor: Sc
import compiler.{ log => _, _ }

val source = icu.getContents
val region = ScalaWordFinder.findWordOrEmptyRegion(source, selectionStart0)
val region = ScalaWordFinder.findWord(source, selectionStart0)

val (selectionStart, selectionEnd) =
if (selectionStart0 <= selectionEnd0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ScalaCompletionProposalComputer extends ScalaCompletions with IJavaComplet
val chars = context.getDocument
val region = ScalaWordFinder.findCompletionPoint(chars, position)

val res = region.map(findCompletions(_)(position, scu)(scu.sourceFile, compiler)).getOrElse(Nil)
val res = findCompletions(region)(position, scu)(scu.sourceFile, compiler)

import collection.JavaConverters._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class ScalaHover(val icu: InteractiveCompilationUnit) extends ITextHover with IT
override def getHoverInfo(viewer: ITextViewer, region: IRegion) = null

override def getHoverRegion(viewer: ITextViewer, offset: Int) = {
ScalaWordFinder.findWord(viewer.getDocument, offset).orNull
ScalaWordFinder.findWord(viewer.getDocument, offset)
}

override def getHoverControlCreator(): IInformationControlCreator =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ object ScalaWordFinder extends IScalaWordFinder {
}

override def getWord(document: IDocument, offset: Int): IRegion =
findWord(document, offset).orNull
findWord(document, offset)

/** See [[findWord(IndexedSeq[Char],Int):Option[IRegion]]. */
def findWord(document: IDocument, offset: Int): Option[IRegion] =
/** See [[findWord(IndexedSeq[Char],Int):IRegion]]. */
def findWord(document: IDocument, offset: Int): IRegion =
findWord(docToSeq(document), offset)

/** See [[findWord(IndexedSeq[Char],Int):Option[IRegion]]. */
def findWord(buffer: IBuffer, offset: Int): Option[IRegion] =
/** See [[findWord(IndexedSeq[Char],Int):IRegion]]. */
def findWord(buffer: IBuffer, offset: Int): IRegion =
findWord(bufferToSeq(buffer), offset)

/**
Expand All @@ -41,56 +41,45 @@ object ScalaWordFinder extends IScalaWordFinder {
* {{{ s"Hello, $name" }}}
*
* Here, the identifier is only `name`.
*
* Returns `None` if offset is invalid.
*/
def findWord(document: IndexedSeq[Char], offset: Int): Option[IRegion] = {
if (offset < 0 || offset > document.length)
None
else {
def find(p: Char => Boolean) = {
var start = -2
var end = -1
def findWord(document: IndexedSeq[Char], offset: Int): IRegion = {
if (offset < 0 || offset > document.length) throw new IndexOutOfBoundsException("Received an invalid offset for word finding.")

var pos = Math.min(offset - 1, document.size - 1)
def find(p: Char => Boolean): IRegion = {
var start = -2
var end = -1

while (pos >= 0 && p(document(pos)))
pos -= 1
var pos = Math.min(offset - 1, document.size - 1)

start = pos
while (pos >= 0 && p(document(pos)))
pos -= 1

pos = offset
val len = document.length
while (pos < len && p(document(pos)))
pos += 1
start = pos

end = pos
pos = offset
val len = document.length
while (pos < len && p(document(pos)))
pos += 1

new Region(start + 1, end - start - 1)
}
end = pos

val idRegion = find(ch => isIdentifierPart(ch) && ch != '$')
if (idRegion.getLength == 0)
Some(find(isOperatorPart))
else
Some(idRegion)
new Region(start + 1, end - start - 1)
}
}

/**
* Calls [[findWord(IndexedSeq[Char],Int):Option[IRegion]] and if that returns
* `None` returns a region whose offset and length is 0.
*/
def findWordOrEmptyRegion(document: IndexedSeq[Char], offset: Int): IRegion =
findWord(document, offset).getOrElse(new Region(0, 0))
val idRegion = find(ch => isIdentifierPart(ch) && ch != '$')
if (idRegion.getLength == 0)
find(isOperatorPart)
else
idRegion
}

def findCompletionPoint(document: IDocument, offset: Int): Option[IRegion] =
def findCompletionPoint(document: IDocument, offset: Int): IRegion =
findCompletionPoint(docToSeq(document), offset)

def findCompletionPoint(buffer: IBuffer, offset: Int): Option[IRegion] =
def findCompletionPoint(buffer: IBuffer, offset: Int): IRegion =
findCompletionPoint(bufferToSeq(buffer), offset)

def findCompletionPoint(document: IndexedSeq[Char], offset0: Int): Option[IRegion] = {
def findCompletionPoint(document: IndexedSeq[Char], offset0: Int): IRegion = {
def isWordPart(ch: Char) = isIdentifierPart(ch) || isOperatorPart(ch)

val offset = if (offset0 >= document.length) (document.length - 1) else offset0
Expand All @@ -100,10 +89,10 @@ object ScalaWordFinder extends IScalaWordFinder {
else if (offset > 0 && isWordPart(document(offset - 1)))
findWord(document, offset - 1)
else
Some(new Region(offset, 0))
new Region(offset, 0)
}

/** Returns the length of the identifier which is located at the offset position. */
def identLenAtOffset(doc: IDocument, offset: Int): Int =
ScalaWordFinder.findWord(doc, offset).fold(0)(_.getLength())
ScalaWordFinder.findWord(doc, offset).getLength()
}

0 comments on commit 31f58f4

Please sign in to comment.