Skip to content

Commit

Permalink
Integrated Iulian's suggestions from pull #96
Browse files Browse the repository at this point in the history
  • Loading branch information
dotta committed May 3, 2012
1 parent f4c72db commit a59e692
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ private[hyperlink] object Hyperlink {
type Factory = (Openable, Int, Int, String, IRegion) => IHyperlink

def toDeclaration(file: Openable, pos: Int, len: Int, label: String, wordRegion: IRegion): IHyperlink =
new OpenDeclaration(file, pos, len, label, wordRegion)
new ScalaHyperlink(file, pos, len, label, text = "Open Declaration", wordRegion)

def toImplicit(file: Openable, pos: Int, len: Int, text: String, wordRegion: IRegion): IHyperlink =
new OpenImplicit(file, pos, len, text, wordRegion)
def toImplicit(file: Openable, pos: Int, len: Int, label: String, wordRegion: IRegion): IHyperlink =
new ScalaHyperlink(file, pos, len, label, text = "Open Implicit", wordRegion)

abstract class Hyperlink(file: Openable, pos: Int, len: Int, label: String, text: String, wordRegion: IRegion) extends IHyperlink {
private class ScalaHyperlink(file: Openable, pos: Int, len: Int, label: String, text: String, wordRegion: IRegion) extends IHyperlink {
def getHyperlinkRegion = wordRegion
def getTypeLabel = label
def getHyperlinkText = text
Expand All @@ -27,11 +27,5 @@ private[hyperlink] object Hyperlink {
}
}
}

class OpenDeclaration(file: Openable, pos: Int, len: Int, label: String, region: IRegion)
extends Hyperlink(file, pos, len, label, text = "Open Declaration", region)

class OpenImplicit(file: Openable, pos: Int, len: Int, label: String, region: IRegion)
extends Hyperlink(file, pos, len, label, text = "Open Implicit", region)
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import scala.tools.eclipse.javaelements.ScalaCompilationUnit
import scala.tools.eclipse.javaelements.ScalaSelectionEngine
import scala.tools.eclipse.javaelements.ScalaSelectionRequestor
import scala.tools.eclipse.semantichighlighting.implicits.ImplicitConversionAnnotation
import scala.tools.eclipse.ui.EditorUtils.getAnnotationsAtOffset
import scala.tools.eclipse.ui.EditorUtils.withEditor
import scala.tools.eclipse.ScalaWordFinder

import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility
Expand Down Expand Up @@ -64,11 +62,11 @@ class HyperlinksDetector extends AbstractHyperlinkDetector {
// 2) Because we use the editor's annotation model, this functionality cannot be tested in a UI-less environment.
private def findHyperlinkToImplicit(scu: ScalaCompilationUnit, offset: Int): List[IHyperlink] = {
import scala.tools.eclipse.semantichighlighting.implicits.ImplicitConversionAnnotation
import scala.tools.eclipse.ui.EditorUtils.{ withEditor, getAnnotationsAtOffset }
import scala.tools.eclipse.util.EditorUtils.{ openEditorAndApply, getAnnotationsAtOffset }

var hyperlinks = List[IHyperlink]()

withEditor(scu) { editor =>
openEditorAndApply(scu) { editor =>
for ((ann, pos) <- getAnnotationsAtOffset(editor, offset)) ann match {
case a: ImplicitConversionAnnotation if a.sourceLink.isDefined =>
hyperlinks = a.sourceLink.get :: hyperlinks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class ScalaQuickAssistProcessor extends org.eclipse.jdt.ui.text.java.IQuickAssis
override def getAssists(context: IInvocationContext, locations: Array[IProblemLocation]): Array[IJavaCompletionProposal] =
context.getCompilationUnit match {
case ssf: ScalaSourceFile => {
import scala.tools.eclipse.ui.EditorUtils.{withEditor, getAnnotationsAtOffset}
withEditor(ssf) { editor =>
import scala.tools.eclipse.util.EditorUtils.{openEditorAndApply, getAnnotationsAtOffset}
openEditorAndApply(ssf) { editor =>
val corrections = {
for ((ann, pos) <- getAnnotationsAtOffset(editor, context.getSelectionOffset())) yield {
suggestAssist(context.getCompilationUnit(), ann.getText, pos)
}
}.flatten
}.flatten.toList
corrections match {
case Nil => null
case correction => correction.distinct.toArray
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package scala.tools.eclipse.ui
package scala.tools.eclipse.util

import org.eclipse.ui.IEditorPart
import org.eclipse.jface.text.source.Annotation
import org.eclipse.jface.text.Position
import org.eclipse.jdt.internal.core.JavaElement
import org.eclipse.jface.text.Position
import org.eclipse.jface.text.source.Annotation
import org.eclipse.ui.IEditorPart
import scala.collection.JavaConverters._
import org.eclipse.jface.text.source.IAnnotationModelExtension2
import scala.tools.eclipse.ScalaWordFinder

object EditorUtils {

def withEditor[T](element: JavaElement)(editor: IEditorPart => T): T =
def openEditorAndApply[T](element: JavaElement)(editor: IEditorPart => T): T =
editor(org.eclipse.jdt.ui.JavaUI.openInEditor(element))

def getAnnotationsAtOffset(part: org.eclipse.ui.IEditorPart, offset: Int): List[(Annotation, Position)] = {
def getAnnotationsAtOffset(part: org.eclipse.ui.IEditorPart, offset: Int): Iterator[(Annotation, Position)] = {
val model = org.eclipse.jdt.ui.JavaUI.getDocumentProvider.getAnnotationModel(part.getEditorInput)
import scala.collection.JavaConversions._

val annotationsWithPositions = model.getAnnotationIterator collect {
val annotations = model match {
case am2: IAnnotationModelExtension2 => am2.getAnnotationIterator(offset, 1, true, true)
case _ => model.getAnnotationIterator
}

val annotationsWithPositions = annotations.asScala collect {
case ann: Annotation => (ann, model.getPosition(ann))
}

val annotationsAtOffset = annotationsWithPositions filter {
case (_, pos) => pos.includes(offset)
}
annotationsAtOffset.toList

annotationsAtOffset
}
}

0 comments on commit a59e692

Please sign in to comment.