Skip to content

Commit 036cf99

Browse files
committed
Fix and enable expression chain recursion detection
Also remove duplicate types from resolution result
1 parent c63c7d0 commit 036cf99

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/org/klesun/deep_js_completion/contexts/SearchCtx.scala

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ import com.intellij.lang.javascript.psi.{JSCallExpression, JSExpression, JSRefer
55
import com.intellij.openapi.project.Project
66
import com.intellij.psi.PsiElement
77
import org.klesun.deep_js_completion.completion_providers.PropNamePvdr
8+
import org.klesun.deep_js_completion.helpers.Mt
89
import org.klesun.deep_js_completion.resolvers.MainRes
910
import org.klesun.lang.DeepJsLang._
1011

1112
import scala.collection.{GenTraversableOnce, mutable}
1213

1314
object SearchCtx {
1415
val DEBUG = false
16+
val DEBUG_OBJ = new {
17+
val PRINT_PSI_TREE = true
18+
}
19+
def formatPsi(element: PsiElement): String = {
20+
val line = element.getContainingFile.getText
21+
.slice(0, element.getTextOffset).split("\n").length
22+
element.getText.replace("\n", " \\n ") + ":" + line
23+
}
1524
}
1625

1726
class SearchCtx(
@@ -82,11 +91,11 @@ class SearchCtx(
8291
var chain: List[PsiElement] = List()
8392
while (ctx != null) {
8493
if (!chain.lastOption.contains(ctx.expr)) {
85-
chain = List(ctx.expr) ++ chain
94+
chain = chain ++ List(ctx.expr)
8695
}
8796
ctx = ctx.parent.orNull
8897
}
89-
chain
98+
chain.reverse
9099
}
91100

92101
private def endsWith[T](superList: List[T], subList: List[T]): Boolean = {
@@ -141,16 +150,17 @@ class SearchCtx(
141150
None
142151
} else if (expressionsResolved >= 7500) {
143152
None
144-
// } else if (isRecursion(exprCtx)) {
145-
// None
153+
} else if (isRecursion(exprCtx)) {
154+
//Console.println("ololo recursion")
155+
None
146156
} else {
147157
putToCache(exprCtx, expr, Iterator.empty.mem())
148158
val resolved = MainRes.resolveIn(expr, exprCtx).itr
149159
// no point getting built-in type here, IDEA will show it itself
150160
val isAtCaret = exprCtx.parent.isEmpty
151161
val builtIn = getWsType(expr).filter(t => !isAtCaret)
152162
var result = frs(resolved, builtIn)
153-
val mit = result.mem()
163+
val mit = result.flatMap(t => Mt.flattenTypes(t)).unq().mem()
154164
if (SearchCtx.DEBUG) {
155165
val postfix = " ||| " + singleLine(expr.getText, 350)
156166
// TODO: one of types happens to be null sometimes - fix!

src/org/klesun/lang/DeepJsLang.scala

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.klesun.lang
22

33
import java.io.{PrintWriter, StringWriter}
4+
import java.util
45

56
import com.intellij.psi.PsiElement
67
import org.klesun.deep_js_completion.contexts.SearchCtx
@@ -276,6 +277,18 @@ object DeepJsLang {
276277
if (hasNext) Some(next()) else None
277278
}
278279

280+
def unq(): It[T] = {
281+
val met = new util.HashSet[T]
282+
filter(el => {
283+
if (met.contains(el)) {
284+
false
285+
} else {
286+
met.add(el)
287+
true
288+
}
289+
})
290+
}
291+
279292
/**
280293
* should not be needed in deep-js code, but deep-assoc, which uses it, has
281294
* MemIt implementation that may ping hasNext() multiple times for same value

0 commit comments

Comments
 (0)