Skip to content

Commit

Permalink
Resources are now resolved also relatively to the module source roots
Browse files Browse the repository at this point in the history
  • Loading branch information
AmailP committed Jan 10, 2016
1 parent 57c437b commit 8e5b9de
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# IntelliJ
/.idea/workspace.xml
lib
9 changes: 9 additions & 0 deletions .idea/libraries/python.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ sourceSets.test.java.srcDirs = []

dependencies {
compile 'org.scala-lang:scala-library:2.11.7'
ideaPlugins files("${buildDir}/plugins/python/lib/python.jar")
ideaPlugins files("${projectDir}/lib/python.jar")
testCompile 'junit:junit:4.12'
testCompile 'org.scalatest:scalatest_2.11:2.2.5'
}
Expand All @@ -49,7 +49,6 @@ intellij {

import de.undercouch.gradle.tasks.download.Download
task downloadPython(type: Download) {
println("${intellij.version}")
src pythonPluginForVersion["${intellij.type}-${intellij.version}"]
dest "${buildDir}/downloads/python.zip"
onlyIfNewer true
Expand All @@ -65,7 +64,12 @@ task copyPythonToSandbox(dependsOn: unzipPython, type: Copy) {
into "${intellij.sandboxDirectory}/plugins/python"
}

compileScala.dependsOn(unzipPython)
task copyPythonJarToLib(dependsOn: unzipPython, type: Copy) {
from "${buildDir}/plugins/python/lib/python.jar"
into "${projectDir}/lib"
}

compileScala.dependsOn(copyPythonJarToLib)

afterEvaluate {
tasks.getByPath('prepareSandbox').dependsOn copyPythonToSandbox
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jan 08 23:31:54 CET 2016
#Sun Jan 10 21:24:06 CET 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
7 changes: 6 additions & 1 deletion robot-plugin.iml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/build/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/downloads" />
<excludeFolder url="file://$MODULE_DIR$/build/idea-sandbox" />
<excludeFolder url="file://$MODULE_DIR$/build/resources" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down Expand Up @@ -128,7 +133,7 @@
<orderEntry type="module-library">
<library name="Gradle: python">
<CLASSES>
<root url="jar://$MODULE_DIR$/build/plugins/python/lib/python.jar!/" />
<root url="jar://$MODULE_DIR$/lib/python.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
Expand Down
9 changes: 7 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@
]]></description>

<change-notes><![CDATA[
0.13 -
0.14 -
<ul>
<li>Syntax highlighting support added for list and dictionary variables</li>
<li>Resources are now resolved also relatively to the module source roots
</ul>
0.13 - 16 December 2015
<ul>
<li>Updated to support Idea 15</li>
</ul>
0.12 - 24 March 2015
<ul>
Expand Down Expand Up @@ -112,7 +117,7 @@
</change-notes>

<!-- http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges -->
<idea-version since-build="141.1" until-build="143.*"/>
<idea-version since-build="143.1"/>

<!-- http://confluence.jetbrains.net/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products -->
<depends>com.intellij.modules.lang</depends>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.intellij.psi._
import amailp.intellij.robot.psi.reference.ResourceValueReference

class ResourceValue(node: ASTNode) extends ASTWrapperPsiElement(node) {
override def getReference: PsiReference = new ResourceValueReference(this)
override def getReference: ResourceValueReference = new ResourceValueReference(this)
}


Expand Down
24 changes: 13 additions & 11 deletions src/main/scala/amailp/intellij/robot/psi/RobotPsiFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ class RobotPsiFile(viewProvider: FileViewProvider)
override def getIcon(flags: Int): Icon = super.getIcon(flags)

def getImportedRobotFiles: Stream[RobotPsiFile] = {
val currentDir = getOriginalFile.getVirtualFile.getParent
for (
resourceValue: ResourceValue <- PsiTreeUtil.findChildrenOfType(getNode.getPsi,classOf[ResourceValue]).toStream;
linkedFile = currentDir.findFileByRelativePath(resourceValue.getText) if !(linkedFile == null);
robotFile = PsiManager.getInstance(getProject).findFile(linkedFile)
if robotFile.getFileType == robot.file.FileType
) yield robotFile.asInstanceOf[RobotPsiFile]
PsiTreeUtil.findChildrenOfType(getNode.getPsi, classOf[ResourceValue])
.toStream
.flatMap(_.getReference.resolveReferenceValue())
}

def getImportedLibraries: Iterable[Library] =
Expand All @@ -49,16 +45,22 @@ class RobotPsiFile(viewProvider: FileViewProvider)

def getRecursivelyImportedRobotFiles: Stream[RobotPsiFile] = {
@tailrec
def visit(toVisit: Stream[RobotPsiFile], visited: Set[RobotPsiFile], accumulator: Stream[RobotPsiFile]): Stream[RobotPsiFile] = {
def visit(toVisit: Stream[RobotPsiFile],
visited: Set[RobotPsiFile],
cumulated: Set[RobotPsiFile],
accumulator: Stream[RobotPsiFile]): Stream[RobotPsiFile] = {
toVisit match {
case head #:: tail if visited.contains(head) =>
visit(toVisit.tail, visited, accumulator)
visit(toVisit.tail, visited, cumulated, accumulator)
case head #:: tail if !visited.contains(head) =>
val importedFromHead = head.getImportedRobotFiles
visit(toVisit.tail #::: importedFromHead, visited + head, accumulator #::: importedFromHead )
visit(toVisit.tail #::: importedFromHead,
visited + head,
cumulated ++ importedFromHead,
accumulator #::: importedFromHead.filterNot(cumulated.contains) )
case Empty => accumulator
}
}
visit(Stream(this), Set(), Stream())
visit(Stream(this), Set(), Set(), Stream())
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
package amailp.intellij.robot.psi.reference

import com.intellij.psi.{PsiElement, PsiReferenceBase}
import amailp.intellij.robot.psi.ResourceValue
import com.intellij.openapi.vfs.VirtualFile
import amailp.intellij.robot.psi.utils.ExtRobotPsiUtils
import amailp.intellij.robot.psi.{ResourceValue, RobotPsiFile}
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.{PsiElement, PsiReferenceBase}

class ResourceValueReference(element: ResourceValue) extends PsiReferenceBase[ResourceValue](element) with ExtRobotPsiUtils {
override def resolve() = {
Option[VirtualFile](currentDirectory.findFileByRelativePath(getElement.getText)) match {
case Some(targetFile) => psiManager.findFile(targetFile)
case None => null
}
}

override def resolve() = resolveReferenceValue().orNull
override def getVariants: Array[AnyRef] = Array()
override def utilsPsiElement: PsiElement = getElement

def resolveReferenceValue(): Option[RobotPsiFile] =
resolveLocalFile orElse resolveAbsoluteFile orElse resolveFromSourceRoots

private def resolveLocalFile: Option[RobotPsiFile] =
maybeToRobotPsiFile(Option(currentDirectory.findFileByRelativePath(getElement.getText)))

private def resolveAbsoluteFile: Option[RobotPsiFile] =
maybeToRobotPsiFile(Option(currentFile.getFileSystem.findFileByPath(getElement.getText)))

private def resolveFromSourceRoots: Option[RobotPsiFile] = {
def sourceRoots = ModuleRootManager.getInstance(ModuleUtilCore.findModuleForPsiElement(getElement)).getSourceRoots(true).toList
sourceRoots.map(s => maybeToRobotPsiFile(Option(s.findFileByRelativePath(getElement.getText))))
.foldLeft(None: Option[RobotPsiFile])((a, b) => a orElse b)
}

private def maybeToRobotPsiFile(file: Option[VirtualFile]): Option[RobotPsiFile] =
file.flatMap(f =>
Option(psiManager.findFile(f))
.filter(_.isInstanceOf[RobotPsiFile])
.map(_.asInstanceOf[RobotPsiFile])
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import scala.collection.JavaConversions._
trait ExtRobotPsiUtils {
def utilsPsiElement: PsiElement
def currentRobotFile = PsiTreeUtil.getParentOfType(utilsPsiElement, classOf[RobotPsiFile])
def currentFile = currentRobotFile.getVirtualFile
def originalRobotFile = currentRobotFile.getOriginalFile.asInstanceOf[RobotPsiFile]
def currentFile = originalRobotFile.getVirtualFile
def currentDirectory = currentFile.getParent
def psiManager = PsiManager.getInstance(utilsPsiElement.getProject)

Expand Down

0 comments on commit 8e5b9de

Please sign in to comment.