diff --git a/idea/testData/libraries/usercode/library.marked.kt b/idea/testData/libraries/usercode/library.marked.kt new file mode 100644 index 00000000000..11f06b0684c --- /dev/null +++ b/idea/testData/libraries/usercode/library.marked.kt @@ -0,0 +1,92 @@ +package testData.libraries + +trait SimpleTrait { +} + +class SimpleClass { +} + +class SimpleTraitImpl : SimpleTrait { +} + +class WithInnerAndObject { + class object { + fun foo() { + } + } + + class MyInner { + trait MyInnerInner { + fun innerInnerMethod() + } + } +} + +class WithTraitClassObject { + class object : SimpleTrait +} + +abstract class AbstractClass { +} + +enum class <4><5>Color(val <6>rgb : Int) { + RED : Color(0xFF0000) + GREEN : Color(0x00FF00) + BLUE : Color(0x0000FF) +} + +abstract class <1>ClassWithAbstractAndOpenMembers { + abstract fun abstractFun() + open fun openFun() { + } + + abstract val abstractVal : String + open val openVal : String = "" + open val openValWithGetter : String + get() { + return "239" + } + + abstract var <2><3>abstractVar : String + open var openVar : String = "" + open var openVarWithGetter : String + get() { + return "239" + } + set(value) { + } +} + +fun main(args : Array) { +} + +val <7>globalVal : #(Int, String) = #(239, "239") + +val <8>globalValWithGetter : Long +get() { + return System.currentTimeMillis() +} + +val String.<9>exProp : String +get() { + return this +} + +val Int.exProp : Int +get() { + return this +} + +val #(T, T).<10>exProp : String +get() { + return "${this._1} : ${this._2}" +} + +fun <11><12>func(a : Int, b : String = "55") { +} + +fun <13>func(a : Int, b : Int) { +} + +fun <14>func() { +} \ No newline at end of file diff --git a/idea/testData/libraries/usercode/usercode.kt b/idea/testData/libraries/usercode/usercode.kt new file mode 100644 index 00000000000..f531e235c5c --- /dev/null +++ b/idea/testData/libraries/usercode/usercode.kt @@ -0,0 +1,22 @@ +package userpackage + +import testData.libraries.* + +fun foo(a : ClassWithAbstractAndOpenMembers) { + a.abstractVar = "v" + println(a.abstractVar) +} + +fun main(args : Array) : Unit { + val color: Color? = Color.RED + color?.rgb + + println(testData.libraries.globalVal) + println(testData.libraries.globalValWithGetter) + println("".exProp) + println(#(1, 2).exProp) + func(5) + func(5, "5") + func(5, 5) + func() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/libraries/LibrariesWithSourcesTest.java b/idea/tests/org/jetbrains/jet/plugin/libraries/LibrariesWithSourcesTest.java new file mode 100644 index 00000000000..c2ecf3e5a83 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/libraries/LibrariesWithSourcesTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.libraries; + +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.fileEditor.FileDocumentManager; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiReference; +import jet.Tuple2; +import org.jetbrains.jet.plugin.references.JetPsiReference; + +import java.util.*; + +/** + * @author Evgeny Gerashchenko + * @since 3/23/12 + */ +public class LibrariesWithSourcesTest extends AbstractLibrariesTest { + + private VirtualFile myLibrarySourceFile; + + public void test() { + myLibrarySourceFile = LocalFileSystem.getInstance().findFileByPath(TEST_DATA_PATH + "/library/library.kt"); + assertNotNull(myLibrarySourceFile); + + String annotatedLibrary = getAnnotatedLibrarySourceText(); + assertSameLinesWithFile(TEST_DATA_PATH + "/usercode/library.marked.kt", annotatedLibrary); + } + + private Collection collectInterestingReferences() { + VirtualFile userFile = LocalFileSystem.getInstance().findFileByPath(TEST_DATA_PATH + "/usercode/usercode.kt"); + assert userFile != null; + + PsiFile psiFile = getPsiManager().findFile(userFile); + assert psiFile != null; + Map referenceContainersToReferences = new LinkedHashMap(); + for (int offset = 0; offset < psiFile.getTextLength(); offset++) { + PsiReference ref = psiFile.findReferenceAt(offset); + if (ref instanceof JetPsiReference && !referenceContainersToReferences.containsKey(ref.getElement())) { + PsiElement target = ref.resolve(); + if (target == null) { + continue; + } + PsiFile targetNavPsiFile = target.getNavigationElement().getContainingFile(); + if (targetNavPsiFile == null) { + continue; + } + if (myLibrarySourceFile.equals(targetNavPsiFile.getVirtualFile())) { + referenceContainersToReferences.put(ref.getElement(), (JetPsiReference)ref); + } + } + } + return referenceContainersToReferences.values(); + } + + private String getAnnotatedLibrarySourceText() { + List> numbersAndOffsets = new ArrayList>(); + for (JetPsiReference ref : collectInterestingReferences()) { + PsiElement target = ref.resolve(); + assertNotNull(target); + PsiElement navigationElement = target.getNavigationElement(); + numbersAndOffsets.add(new Tuple2(numbersAndOffsets.size() + 1, navigationElement.getTextOffset())); + } + + Collections.sort(numbersAndOffsets, Collections.reverseOrder(new Comparator>() { + @Override + public int compare(Tuple2 t1, Tuple2 t2) { + int offsets = t1._2.compareTo(t2._2); + return offsets == 0 ? t1._1.compareTo(t2._1) : offsets; + } + })); + + Document document = FileDocumentManager.getInstance().getDocument(myLibrarySourceFile); + assertNotNull(document); + StringBuilder result = new StringBuilder(document.getText()); + for (Tuple2 numberOffset : numbersAndOffsets) { + result.insert(numberOffset._2, String.format("<%d>", numberOffset._1)); + } + + return result.toString(); + } + + @Override + protected boolean isWithSources() { + return true; + } +}