Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Implement code select. #76

Closed
wants to merge 1 commit into from

2 participants

Iulian Dragos Mirco Dotta
Iulian Dragos
Owner

This allows the Javadoc view to track the cursor, and searching for references of the current method under the cursor.

It's part of my clean-up effort of branches that hang around my checkout. I think this one is generally useful and adds some convenience when working with Java APIs, plus it cleans up something that never worked.

Iulian Dragos Implement code select.
This allows the Javadoc view to track the cursor, and searching for references of the current method under the cursor.
1d78669
Mirco Dotta dotta commented on the diff
org.scala-ide.sdt.core/src/scala/tools/eclipse/javaelements/ScalaJavaMapper.scala
((21 lines not shown))
  31
+      askOption { () =>
  32
+        ((meth.getElementName == sym.name.toString)
  33
+          && meth.getParameterTypes.map(tp => getTypeErasure(getElementType(tp)))
  34
+                                   .sameElements(sym.tpe.paramTypes.map(mapParamTypeSignature)))
  35
+      }.getOrElse(false)
  36
+    }
  37
+
  38
+    if (sym.isPackage) {
  39
+      val fullName = sym.fullName
  40
+      val results = projects.map(p => Option(p.findPackageFragment(new Path(fullName))))
  41
+      results.flatten.headOption
  42
+    } else if (sym.isClass || sym.isModule) {
  43
+      val fullClassName = mapType(sym)
  44
+      val results = projects.map(p => Option(p.findType(fullClassName)))
  45
+      results.find(_.isDefined).flatten.headOption
  46
+    } else getJavaElement(sym.owner) match {
3
Mirco Dotta Owner
dotta added a note

isn't sym.owner risky? (I mean executing it outside of the PC)

Iulian Dragos Owner
dragos added a note

It shouldn't be. The scala compiler uses laziness only in types, not in symbols... Every symbol has an owner, since they are always created through factory methods, like owner.newValue or owner.newClass. That being said, to my great surprise TypeTree.symbol is delegating to its type to retrieve the symbol. However, that looks like a bug or an exception and I'm willing to take a risk here for the owner. As soon as we get the API (yeah, I know, it sounds like empty promisses), this can be switched to the 'safe' API.

Mirco Dotta Owner
dotta added a note

In the compiler I remember seeing phases changing the owner in some cases, that's what made me thinking. But I guess that owners don't change if only phases up to typer are run.

Looking forward to the API, we will finally be able to fix quite a bit of code. We really have way too many places were we can suffer visibility issues because we access compiler's data structures that are not thread safe...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Iulian Dragos dragos closed this
Mirko Stocker misto referenced this pull request from a commit
Mirko Stocker Create fatal errors when a refactoring's initial check yields an error.
Until now, we created normal errors, which were shown in a wizard page,
and because they were non-fatal, the refactoring still tried to execute,
resulting in exceptions in the log file. Now we use fatal errors, which
immediately abort a refactoring and show the error message in a window.

Fixes scala-refactoring #76
0ae24d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 29, 2012
Iulian Dragos Implement code select.
This allows the Javadoc view to track the cursor, and searching for references of the current method under the cursor.
1d78669
This page is out of date. Refresh to see the latest.
25  org.scala-ide.sdt.core/src/scala/tools/eclipse/javaelements/ScalaCompilationUnit.scala
@@ -27,10 +27,7 @@ import scala.tools.nsc.util.{ BatchSourceFile, SourceFile }
27 27
 import scala.tools.eclipse.contribution.weaving.jdt.{ IScalaCompilationUnit, IScalaWordFinder }
28 28
 import scala.tools.eclipse.{ ScalaImages, ScalaPlugin, ScalaPresentationCompiler, ScalaSourceIndexer, ScalaWordFinder }
29 29
 import scala.tools.eclipse.util.ReflectionUtils
30  
-import org.eclipse.jdt.core.IField
31  
-import org.eclipse.jdt.core.IMethod
32  
-import org.eclipse.jdt.core.ISourceReference
33  
-import org.eclipse.jdt.core.IParent
  30
+import org.eclipse.jdt.core._
34 31
 import org.eclipse.jdt.internal.core.JavaElement
35 32
 import org.eclipse.jdt.internal.core.SourceRefElement
36 33
 import scala.tools.eclipse.logging.HasLogger
@@ -187,9 +184,23 @@ trait ScalaCompilationUnit extends Openable with env.ICompilationUnit with Scala
187 184
       case elem => elem
188 185
     }
189 186
   }
190  
-    
191  
-  override def codeSelect(cu : env.ICompilationUnit, offset : Int, length : Int, workingCopyOwner : WorkingCopyOwner) : Array[IJavaElement] = {
192  
-    Array.empty
  187
+
  188
+  override def codeSelect(cu: env.ICompilationUnit, offset: Int, length: Int, workingCopyOwner: WorkingCopyOwner): Array[IJavaElement] = {
  189
+    withSourceFile { (srcFile, compiler) =>
  190
+      val pos = compiler.rangePos(srcFile, offset, offset, offset)
  191
+
  192
+      val typed = new compiler.Response[compiler.Tree]
  193
+      compiler.askTypeAt(pos, typed)
  194
+      val typedRes = typed.get
  195
+      val element = for {
  196
+       t <- typedRes.left.toOption
  197
+       if t.hasSymbol
  198
+       element <- compiler.getJavaElement(t.symbol)
  199
+      } yield Array(element: IJavaElement)
  200
+      
  201
+      val res = element.getOrElse(Array.empty[IJavaElement])
  202
+      res
  203
+    }(Array.empty[IJavaElement])
193 204
   }
194 205
 
195 206
   def codeComplete
47  org.scala-ide.sdt.core/src/scala/tools/eclipse/javaelements/ScalaJavaMapper.scala
@@ -11,9 +11,56 @@ import scala.tools.nsc.symtab.Flags
11 11
 import scala.tools.eclipse.ScalaPresentationCompiler
12 12
 import ch.epfl.lamp.fjbg.{ JObjectType, JType }
13 13
 import scala.tools.eclipse.logging.HasLogger
  14
+import org.eclipse.jdt.core._
  15
+import org.eclipse.jdt.internal.core.JavaModelManager
  16
+import org.eclipse.core.runtime.Path
14 17
 
15 18
 trait ScalaJavaMapper extends ScalaAnnotationHelper with HasLogger { self : ScalaPresentationCompiler => 
16 19
 
  20
+  /** Return the Java Element corresponding to the given Scala Symbol, looking in the
  21
+   *  given project list
  22
+   * 
  23
+   *  If the symbol exists in several projects, it returns one of them.
  24
+   */
  25
+  def getJavaElement(sym: Symbol, projects: IJavaProject*): Option[IJavaElement] = {
  26
+    assert(sym ne null)
  27
+    if (sym == NoSymbol) return None
  28
+    
  29
+    def matchesMethod(meth: IMethod): Boolean = {
  30
+      import Signature._
  31
+      askOption { () =>
  32
+        ((meth.getElementName == sym.name.toString)
  33
+          && meth.getParameterTypes.map(tp => getTypeErasure(getElementType(tp)))
  34
+                                   .sameElements(sym.tpe.paramTypes.map(mapParamTypeSignature)))
  35
+      }.getOrElse(false)
  36
+    }
  37
+
  38
+    if (sym.isPackage) {
  39
+      val fullName = sym.fullName
  40
+      val results = projects.map(p => Option(p.findPackageFragment(new Path(fullName))))
  41
+      results.flatten.headOption
  42
+    } else if (sym.isClass || sym.isModule) {
  43
+      val fullClassName = mapType(sym)
  44
+      val results = projects.map(p => Option(p.findType(fullClassName)))
  45
+      results.find(_.isDefined).flatten.headOption
  46
+    } else getJavaElement(sym.owner) match {
  47
+        case Some(ownerClass: IType) => 
  48
+          if (sym.isMethod) ownerClass.getMethods.find(matchesMethod)
  49
+          else ownerClass.getFields.find(_.getElementName == sym.name.toString)
  50
+        case _ => None
  51
+    }
  52
+  }
  53
+  
  54
+  /** Return the Java Element corresponding to the given Scala Symbol, looking in the
  55
+   *  all existing Java projects.
  56
+   *  
  57
+   *  If the symbol exists in several projects, it returns one of them.
  58
+   */
  59
+  def getJavaElement(sym: Symbol): Option[IJavaElement] = {
  60
+    val javaModel = JavaModelManager.getJavaModelManager.getJavaModel
  61
+    getJavaElement(sym, javaModel.getJavaProjects(): _*)
  62
+  }
  63
+  
17 64
   def mapType(t : Tree) : String = {
18 65
     (t match {
19 66
       case tt : TypeTree => {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.