Skip to content

Commit

Permalink
Remove the hard-coded InteractiveCompilationUnit from ScalaHover
Browse files Browse the repository at this point in the history
The associated compilation unit may change (see #1002251) when the editor
is reused. On top of that, the structured editor in the Play plugin needs
a no-arg constructor (it’s instantiated through an extension point).

This change allows the Play plugin to use plain Scala hovers, including
all the goodies (configured font and CSS, scaladoc, etc).
  • Loading branch information
dragos committed Oct 2, 2014
1 parent dac08df commit 46b7ba9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Expand Up @@ -188,7 +188,7 @@ class ScalaSourceViewerConfiguration(

override def getInformationPresenter(sourceViewer: ISourceViewer) = {
val p = new InformationPresenter(getInformationControlCreator(sourceViewer))
val ip = new HoverInformationProvider(compilationUnit map (new ScalaHover(_)))
val ip = new HoverInformationProvider(Some(new ScalaHover()))

p.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer))
getConfiguredContentTypes(sourceViewer) foreach (p.setInformationProvider(ip, _))
Expand Down Expand Up @@ -236,7 +236,7 @@ class ScalaSourceViewerConfiguration(

override def getTextHover(sv: ISourceViewer, contentType: String, stateMask: Int): ITextHover =
compilationUnit.map(scu =>
ScalaHoverDebugOverrideExtensionPoint.hoverFor(scu).getOrElse(new ScalaHover(scu))
ScalaHoverDebugOverrideExtensionPoint.hoverFor(scu).getOrElse(new ScalaHover())
).getOrElse(new DefaultTextHover(sv))

override def getHyperlinkDetectors(sv: ISourceViewer): Array[IHyperlinkDetector] = {
Expand Down
Expand Up @@ -27,6 +27,9 @@ import scala.tools.nsc.interactive.CompilerControl
import scala.tools.nsc.symtab.Flags
import org.scalaide.core.internal.compiler.ScalaPresentationCompiler
import org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover
import org.scalaide.ui.internal.editor.InteractiveCompilationUnitEditor
import org.eclipse.core.filebuffers.FileBuffers
import org.scalaide.core.extensions.SourceFileProviderRegistry

object ScalaHover extends HasLogger {
/** could return null, but prefer to return empty (see API of ITextHover). */
Expand Down Expand Up @@ -90,9 +93,21 @@ object ScalaHover extends HasLogger {
}
}

class ScalaHover(val icu: InteractiveCompilationUnit) extends ITextHover with ITextHoverExtension with ITextHoverExtension2 with HtmlHover {
class ScalaHover extends ITextHover with ITextHoverExtension with ITextHoverExtension2 with HtmlHover {
import ScalaHover._

protected def getCompilationUnit(textViewer: ITextViewer): InteractiveCompilationUnit = {
val doc = textViewer.getDocument
val icu = for {
buffer <- Option(FileBuffers.getTextFileBufferManager.getTextFileBuffer(doc))
location = buffer.getLocation
provider <- Option(SourceFileProviderRegistry.getProvider(location))
icu <- provider.createFrom(location)
} yield icu
icu.getOrElse(null)
}


/**
* Returns the focused control creator, which is known as the presenter
* control creator in the Eclipse API.
Expand All @@ -105,6 +120,7 @@ class ScalaHover(val icu: InteractiveCompilationUnit) extends ITextHover with IT
new FocusedControlCreator(HoverFontId)

override def getHoverInfo2(viewer: ITextViewer, region: IRegion): AnyRef = {
val icu = getCompilationUnit(viewer)
icu.withSourceFile({ (src, compiler) =>
import compiler.{ stringToTermName => _, stringToTypeName => _, _ }
import RegionUtils.RichRegion
Expand Down
Expand Up @@ -24,10 +24,11 @@ import org.scalaide.core.compiler.IScalaPresentationCompiler.Implicits._
class TextHoverFactory extends TextHoverFactoryInterface {
import IScalaPresentationCompiler.Implicits._

def createFor(scu: ScalaCompilationUnit): ITextHover = new ScalaHover(scu) with ITextHoverExtension with ITextHoverExtension2 {
def createFor(scu: ScalaCompilationUnit): ITextHover = new ScalaHover with ITextHoverExtension with ITextHoverExtension2 {
var stringWasReturnedAtGetHoverInfo2 = false

override def getHoverInfo2(viewer: ITextViewer, region: IRegion): AnyRef = {
val icu = getCompilationUnit(viewer)
icu.withSourceFile{(src, compiler) =>
import compiler._
import org.scalaide.util.eclipse.RegionUtils.RichRegion
Expand Down

0 comments on commit 46b7ba9

Please sign in to comment.