Skip to content

Commit

Permalink
Merge pull request #26 from SergeevPavel/master
Browse files Browse the repository at this point in the history
treat .edn as clojure file and fix resolve in java hierarchies
  • Loading branch information
gregsh authored Dec 11, 2018
2 parents b1c913b + 2192c46 commit 3022ea4
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/clojure-constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ object ClojureConstants {
@JvmStatic val CLJS = "cljs"
@JvmStatic val CLJ = "clj"
@JvmStatic val CLJC = "cljc"
@JvmStatic val EDN = "edn"

@JvmStatic val NS_USER = "user"
@JvmStatic val NS_SPEC = "clojure.spec"
Expand Down
2 changes: 1 addition & 1 deletion src/lang/clojure-language.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import org.intellij.clojure.util.thisForm
*/
class ClojureFileTypeFactory : FileTypeFactory() {
override fun createFileTypes(consumer: FileTypeConsumer) {
consumer.consume(ClojureFileType, "${ClojureConstants.CLJ};${ClojureConstants.CLJS};${ClojureConstants.CLJC}")
consumer.consume(ClojureFileType, "${ClojureConstants.CLJ};${ClojureConstants.CLJS};${ClojureConstants.CLJC};${ClojureConstants.EDN}")
consumer.consume(ClojureFileType, WildcardFileNameMatcher(ClojureConstants.BOOT_CONFIG))
}
}
Expand Down
69 changes: 48 additions & 21 deletions src/lang/clojure-psi-java.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.intellij.util.ObjectUtils
import com.intellij.util.containers.ContainerUtil
import com.intellij.util.containers.ContainerUtilRt
import com.intellij.util.containers.JBIterable
import com.intellij.util.containers.JBTreeTraverser
import org.intellij.clojure.ClojureConstants
import org.intellij.clojure.psi.impl.ClojureDefinitionService
import org.intellij.clojure.util.EachNth
Expand Down Expand Up @@ -142,30 +143,49 @@ abstract class JavaHelper {
override fun findPackage(packageName: String?, withClass: String?): NavigatablePsiElement? =
myFacade.findPackage(packageName!!) as? NavigatablePsiElement ?: asm.findPackage(packageName, withClass)

internal fun superclasses(className: String?) = JBTreeTraverser<PsiClass> { o ->
JBIterable.of(o.superClass).append(o.interfaces)
}
.withRoot(findClass(className) as? PsiClass)
.unique()
.traverse()


override fun findClassMethods(className: String?,
scope: Scope,
name: String?,
paramCount: Int,
vararg paramTypes: String): List<NavigatablePsiElement> {
val aClass = findClass(className) as? PsiClass ?: return asm.findClassMethods(className, scope, name, paramCount, *paramTypes)
val methods = if (scope == Scope.INIT) aClass.constructors.jbIt()
else JBIterable.generate(aClass) { it.superClass }.flatMap { it!!.methods.jbIt() }
return methods.map { o ->
if (acceptsName(name, o.name) &&
acceptsMethod(o, scope == Scope.STATIC) &&
acceptsMethod(myElementFactory, o, paramCount, *paramTypes)) o else null
}.notNulls().toList()
}
vararg paramTypes: String): List<NavigatablePsiElement> =
if (findClass(className) !is PsiClass) {
asm.findClassMethods(className, scope, name, paramCount, *paramTypes)
}
else {
superclasses(className)
.flatten {
if (scope == Scope.INIT) it.constructors.jbIt() else it.methods.jbIt()
}
.filter {
acceptsName(name, it.name) &&
acceptsMethod(it, scope == Scope.STATIC) &&
acceptsMethod(myElementFactory, it, paramCount, *paramTypes)
}
.notNulls()
.toList()
}

override fun findClassFields(className: String?, scope: Scope, name: String?): List<NavigatablePsiElement> {
val aClass = findClass(className) as? PsiClass ?: return asm.findClassFields(className, scope, name)
val fields = JBIterable.generate(aClass) { it.superClass }.flatMap { it!!.fields.jbIt() }
return fields.map { o ->
if (acceptsName(name, o.name) &&
acceptsMethod(o, scope == Scope.STATIC)) o
else null
}.notNulls().toList()
}
override fun findClassFields(className: String?, scope: Scope, name: String?): List<NavigatablePsiElement> =
if (findClass(className) !is PsiClass) {
asm.findClassFields(className, scope, name)
}
else superclasses(className)
.flatten {
it.fields.jbIt()
}
.filter {
acceptsName(name, it.name) &&
acceptsMethod(it, scope == Scope.STATIC)
}
.notNulls().toList()

override fun getSuperClassName(className: String?): String? {
val aClass = findClass(className) as? PsiClass ?: return asm.getSuperClassName(className)
Expand Down Expand Up @@ -309,8 +329,15 @@ abstract class JavaHelper {
internal fun cached(id: String, info: Any) =
map[id] ?: ConcurrencyUtil.cacheOrGet(map, id, MyElement(project, info))

internal fun superclasses(name: String?) = JBIterable.generate(findClass(name)) {
findClass((it?.delegate as? ClassInfo)?.superClass)}.notNulls()
internal fun superclasses(name: String?) = JBTreeTraverser<MyElement<*>> { o ->
JBIterable.of((o.delegate as? ClassInfo)?.superClass)
.append((o.delegate as? ClassInfo)?.interfaces)
.map(this@AsmHelper::findClass)
.notNulls()
}
.withRoot(findClass(name))
.unique()
.traverse()

override fun findClassMethods(className: String?,
scope: Scope,
Expand Down
6 changes: 3 additions & 3 deletions testData/highlighting/ClojureLang.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/clojure/core.clj 0 errors, 8 warnings, 19 dynamic
/clojure/core.clj 0 errors, 8 warnings, 13 dynamic
123975: unable to resolve 'setInt'
124114: unable to resolve 'setLong'
124261: unable to resolve 'setBoolean'
Expand All @@ -13,7 +13,7 @@
2113: unable to resolve 'jsr166y.ForkJoinTask'
2178: unable to resolve 'jsr166y.ForkJoinTask'
/clojure/core/server.clj 0 errors, 0 warnings, 4 dynamic
/clojure/core_deftype.clj 0 errors, 0 warnings, 13 dynamic
/clojure/core_deftype.clj 0 errors, 0 warnings, 11 dynamic
/clojure/core_instant18.clj 0 errors, 0 warnings, 0 dynamic
/clojure/core_print.clj 0 errors, 0 warnings, 0 dynamic
/clojure/core_proxy.clj 0 errors, 3 warnings, 0 dynamic
Expand All @@ -25,7 +25,7 @@
2620: unable to resolve 'isArray'
/clojure/edn.clj 0 errors, 0 warnings, 0 dynamic
/clojure/genclass.clj 0 errors, 0 warnings, 2 dynamic
/clojure/gvec.clj 0 errors, 0 warnings, 47 dynamic
/clojure/gvec.clj 0 errors, 0 warnings, 45 dynamic
/clojure/inspector.clj 0 errors, 0 warnings, 11 dynamic
/clojure/instant.clj 0 errors, 0 warnings, 5 dynamic
/clojure/java/browse.clj 0 errors, 0 warnings, 2 dynamic
Expand Down
2 changes: 1 addition & 1 deletion testData/highlighting/ClojureScript.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/cljs/analyzer/macros.clj 0 errors, 0 warnings, 1 dynamic
/cljs/analyzer/utils.clj 0 errors, 0 warnings, 0 dynamic
/cljs/build/api.clj 0 errors, 0 warnings, 0 dynamic
/cljs/closure.clj 0 errors, 14 warnings, 53 dynamic
/cljs/closure.clj 0 errors, 14 warnings, 52 dynamic
13343: unable to resolve 'setExtraAnnotationNames'
55993: unable to resolve 'read-str'
61410: unable to resolve 'write-str'
Expand Down
2 changes: 2 additions & 0 deletions tests/lang/completion-tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class ClojureCompletionTest : LightPlatformCodeInsightFixtureTestCase() {
fun testInsideImport3() = doTest("(require [clojure.string :refer [<caret>]])", "blank?")
fun testInsideImport4() = doNegTest("(require [clojure.string :refer [<caret>]])", "def")

fun testJava1() = doPosTest("(defn a [^java.util.SortedSet a] (.<caret> a))", "comparator", "size", "contains", "add")


private fun doTest(text: String, select: String,
expected: String = text.replace("<caret>", select),
Expand Down

0 comments on commit 3022ea4

Please sign in to comment.