Re: askTypeAt Question

25 views
Skip to first unread message

Chee Seng Chua

unread,
Aug 2, 2012, 3:22:50 AM8/2/12
to Mirco Dotta, scala-...@googlegroups.com, Luc Bourlier, Iulian Dragos
Hi Mirco, 

Sure, I have added scala-ide-dev into the list.

Iulian:  Let me try to see if I can work out something based on your guide.

Thanks!

Best Regards, 
Chee Seng

On Thu, Aug 2, 2012 at 3:19 PM, Mirco Dotta <mirco...@typesafe.com> wrote:
Hi Chee Seng,

May I suggest to move this discussion in the scala-ide-dev ML, I'm sure this topic can be of interest for others.

-- Mirco

On Aug 2, 2012, at 8:55 AM, Iulian Dragoș wrote:

Hi Chee Seng,

I'm on vacation, and probably won't have a place to check my email later. I added Mirco and Luc, and they will be able to help you.

I think starting with an IType, you are limited in what you can do. One way around it would be to simply retrieve the whole compilation unit. If you need it to be typed, you can use `loadedType` -- may be slow, so don't do this if you run your code on every keystroke -- then you can use 'askParsedEntered', which won't fully type check, but you will have symbols in the tree.

Then, you can iterate through the trees and retrieve the one you need:

val myClassTree = trees.collect {
  case t: ClassDef if t.name == "..." => t
}

(I'm not sure trees implement 'collect' as Sequences do, but if they don't, you get the idea how to filter...).

Otherwise, maybe there's a better way without going through IType?

On Wed, Aug 1, 2012 at 6:16 PM, Chee Seng Chua <chee...@amaseng.com> wrote:
Hi Iulian, 

I have a problem using askTypeAt to get the correct class's Tree object.  Given a IType, I use the following code to get the class position using the following code:-

val scu = iType.getCompilationUnit.asInstanceOf[ScalaCompilationUnit]
val classPosition = new OffsetPosition(scu.createSourceFile, iType.getSourceRange.getOffset)

I then use the following code to get the Tree of the class:-

val response = new Response[Tree]
compiler.askTypeAt(classPosition, response)
val rootTree = response.get match {
    case Left(tree) => tree 
    case Right(thr) => throw thr
}

For a class without annotation, e.g. normal ScalaTest style traits, it works correctly.  However, if there's annotation, e.g.:-

@WrapWith(classOf[Spec1Runner])
class ExampleSpec1 extends Specification {
...
}

classPosition that I got starts at the @WrapWith line, but when I call compiler.askTypeAt(classPosition, response), it returns the package Tree instead of the class tree.  Is that the intended behavior?  Or is there a better way for me to get the related Tree, given a IType/IJavaElement?

Thanks!

Best Regards, 
Chee Seng





--

Iulian Dragos
Head of Lausanne Office

Typesafe - The software stack for applications that scale
tel: +41 21 691 4965
twitter: @jaguarul


Chee Seng Chua

unread,
Aug 2, 2012, 9:35:08 AM8/2/12
to Mirco Dotta, scala-...@googlegroups.com, Luc Bourlier, Iulian Dragos
Hi all, 

I can't go through IType/IJavaElement, as it is what I can get from property tester (when user right click in the code, the element which is clicked on).  However, the compilation unit workaround does work, the only different is instead of:-

t.name == "..."

I'll need to use:-

t.symbol.fullName == "..."

I have checked in the changes:-


The goal of the ScalaTestLaunchShortcut.isScalaTestSuite is to determine whether a particular IType is a runnable suite, which could be a sub-class of org.scalatest.Suite or class annotated with @WrapWith (which is used to run non-ScalaTest suite, such as specs, cucumber, scalachecks etc.).  While in ScalaTestFinder, it needs to find out the finder class, which is from @Style annotation.  For ScalaTest style traits (Suite, FunSpec, FeatureSpec etc.) it is annotated at the style traits level, so it only needs to find it from the super classes/traits @Style.  For the @WrapWith case, the @Style is available at the runner class, for example:-

import org.specs.Specification
import org.scalatest.WrapWith
import org.scalatest.specs.Spec1Runner

@WrapWith(classOf[Spec1Runner])
class ExampleSpec1 extends Specification {
...
}

The @Style will be available at Spec1Runner class.  Previously the lookup was done with reflection, but it has a problem where the information will not be available until the class is compiled, which these changes should fix.

Do feel free to review and comment on the code.

Thanks a lot for the guidance!

P/S Iulian: Sorry to disturb your vacation, I wasn't aware of that.  Have a good one!

Best Regards, 
Chee Seng
Reply all
Reply to author
Forward
0 new messages