Skip to content

Commit 3f6f576

Browse files
committed
upgrade emmy core
1 parent 58ac70f commit 3f6f576

File tree

15 files changed

+111
-87
lines changed

15 files changed

+111
-87
lines changed

EmmyLua-Common/src/main/ext/com/tang/intellij/lua/psi/search/LuaShortNamesManager.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package com.tang.intellij.lua.psi.search
1818

1919
import com.intellij.openapi.project.Project
2020
import com.intellij.openapi.util.Key
21-
import com.intellij.psi.search.GlobalSearchScope
2221
import com.intellij.util.Processor
2322
import com.tang.intellij.lua.ext.ExtensionPointName
2423
import com.tang.intellij.lua.psi.LuaClass
@@ -46,27 +45,25 @@ abstract class LuaShortNamesManager {
4645

4746
abstract fun findClass(name: String, context: SearchContext): LuaClass?
4847

49-
abstract fun findClass(name: String, project: Project, scope: GlobalSearchScope): LuaClass?
50-
5148
abstract fun findMember(type: ITyClass, fieldName: String, context: SearchContext): LuaClassMember?
5249

5350
abstract fun processAllClassNames(project: Project, processor: Processor<String>): Boolean
5451

55-
abstract fun processClassesWithName(name: String, project: Project, scope: GlobalSearchScope, processor: Processor<LuaClass>): Boolean
52+
abstract fun processClassesWithName(name: String, context: SearchContext, processor: Processor<LuaClass>): Boolean
5653

57-
abstract fun getClassMembers(clazzName: String, project: Project, scope: GlobalSearchScope): MutableCollection<LuaClassMember>
54+
abstract fun getClassMembers(clazzName: String, context: SearchContext): Collection<LuaClassMember>
5855

5956
abstract fun processAllMembers(type: ITyClass, fieldName: String, context: SearchContext, processor: Processor<LuaClassMember>): Boolean
6057

61-
open fun findAlias(name: String, project: Project, scope: GlobalSearchScope): LuaTypeAlias? {
58+
open fun findAlias(name: String, context: SearchContext): LuaTypeAlias? {
6259
return null
6360
}
6461

6562
open fun processAllAlias(project: Project, processor: Processor<String>): Boolean {
6663
return true
6764
}
6865

69-
open fun findTypeDef(name: String, project: Project, scope: GlobalSearchScope): LuaTypeDef? {
70-
return findClass(name, project, scope) ?: findAlias(name, project, scope)
66+
open fun findTypeDef(name: String, context: SearchContext): LuaTypeDef? {
67+
return findClass(name, context) ?: findAlias(name, context)
7168
}
7269
}

EmmyLua-Common/src/main/ext/com/tang/intellij/lua/psi/search/LuaShortNamesManagerImpl.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ import com.tang.intellij.lua.stubs.index.LuaClassMemberIndex
2929
import com.tang.intellij.lua.ty.ITyClass
3030

3131
class LuaShortNamesManagerImpl : LuaShortNamesManager() {
32-
override fun findClass(name: String, project: Project, scope: GlobalSearchScope): LuaClass? {
33-
return LuaClassIndex.find(name, project, scope)
34-
}
3532

3633
override fun findClass(name: String, context: SearchContext): LuaClass? {
3734
return LuaClassIndex.find(name, context)
@@ -45,21 +42,21 @@ class LuaShortNamesManagerImpl : LuaShortNamesManager() {
4542
return LuaClassIndex.processKeys(project, processor)
4643
}
4744

48-
override fun processClassesWithName(name: String, project: Project, scope: GlobalSearchScope, processor: Processor<LuaClass>): Boolean {
49-
return LuaClassIndex.process(name, project, scope, Processor { processor.process(it) })
45+
override fun processClassesWithName(name: String, context: SearchContext, processor: Processor<LuaClass>): Boolean {
46+
return LuaClassIndex.process(name, context.project, context.scope, Processor { processor.process(it) })
5047
}
5148

52-
override fun getClassMembers(clazzName: String, project: Project, scope: GlobalSearchScope): MutableCollection<LuaClassMember> {
53-
return LuaClassMemberIndex.instance.get(clazzName.hashCode(), project, scope)
49+
override fun getClassMembers(clazzName: String, context: SearchContext): Collection<LuaClassMember> {
50+
return LuaClassMemberIndex.instance.get(clazzName.hashCode(), context.project, context.scope)
5451
}
5552

5653
override fun processAllMembers(type: ITyClass, fieldName: String, context: SearchContext, processor: Processor<LuaClassMember>): Boolean {
5754
LuaClassMemberIndex.processAll(type, fieldName, context, processor)
5855
return true
5956
}
6057

61-
override fun findAlias(name: String, project: Project, scope: GlobalSearchScope): LuaTypeAlias? {
62-
return LuaAliasIndex.instance.get(name, project, scope).firstOrNull()
58+
override fun findAlias(name: String, context: SearchContext): LuaTypeAlias? {
59+
return LuaAliasIndex.instance.get(name, context.project, context.scope).firstOrNull()
6360
}
6461

6562
override fun processAllAlias(project: Project, processor: Processor<String>): Boolean {

EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/index/LuaClassIndex.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.intellij.openapi.project.Project
44
import com.intellij.psi.search.GlobalSearchScope
55
import com.intellij.util.Processor
66
import com.intellij.util.containers.ContainerUtil
7-
import com.intellij.util.indexing.IndexId
87
import com.tang.intellij.lua.comment.psi.LuaDocTagClass
98
import com.tang.intellij.lua.search.SearchContext
109
import com.tang.intellij.lua.stubs.StubKeys
@@ -17,7 +16,7 @@ class LuaClassIndex : StubIndex<String, LuaDocTagClass>() {
1716
val instance = LuaClassIndex()
1817

1918
fun find(name: String, context: SearchContext): LuaDocTagClass? {
20-
return find(name, context.project, context.getScope())
19+
return find(name, context.project, context.scope)
2120
}
2221

2322
fun find(name: String, project: Project, scope: GlobalSearchScope): LuaDocTagClass? {

EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/index/LuaClassMemberIndex.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class LuaClassMemberIndex : StubIndex<Int, LuaClassMember>() {
3636
fun process(key: String, context: SearchContext, processor: Processor<LuaClassMember>): Boolean {
3737
if (context.isDumb)
3838
return false
39-
val all = LuaClassMemberIndex.instance.get(key.hashCode(), context.project, context.getScope())
39+
val all = LuaClassMemberIndex.instance.get(key.hashCode(), context.project, context.scope)
4040
return ContainerUtil.process(all, processor)
4141
}
4242

EmmyLua-Common/src/main/java/com/tang/intellij/lua/comment/reference/LuaClassNameReference.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ package com.tang.intellij.lua.comment.reference
1919
import com.intellij.openapi.util.TextRange
2020
import com.intellij.psi.PsiElement
2121
import com.intellij.psi.PsiReferenceBase
22-
import com.intellij.psi.search.ProjectAndLibrariesScope
2322
import com.intellij.psi.util.PsiTreeUtil
2423
import com.tang.intellij.lua.comment.LuaCommentUtil
2524
import com.tang.intellij.lua.comment.psi.LuaDocClassNameRef
2625
import com.tang.intellij.lua.comment.psi.LuaDocFunctionTy
2726
import com.tang.intellij.lua.comment.psi.LuaDocGenericDef
2827
import com.tang.intellij.lua.psi.LuaElementFactory
2928
import com.tang.intellij.lua.psi.search.LuaShortNamesManager
29+
import com.tang.intellij.lua.search.SearchContext
3030

3131
/**
3232
@@ -62,7 +62,7 @@ class LuaClassNameReference(element: LuaDocClassNameRef) : PsiReferenceBase<LuaD
6262
return genericDef
6363
}
6464

65-
return LuaShortNamesManager.getInstance(myElement.project).findTypeDef(name, myElement.project, ProjectAndLibrariesScope(myElement.project))
65+
return LuaShortNamesManager.getInstance(myElement.project).findTypeDef(name, SearchContext.get(myElement.project))
6666
}
6767

6868
override fun getVariants(): Array<Any> = emptyArray()

EmmyLua-Common/src/main/java/com/tang/intellij/lua/editor/completion/ClassMemberCompletionProvider.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616

1717
package com.tang.intellij.lua.editor.completion
1818

19+
import com.intellij.codeInsight.completion.CompletionInitializationContext
1920
import com.intellij.codeInsight.completion.CompletionResultSet
2021
import com.intellij.codeInsight.completion.PrefixMatcher
22+
import com.intellij.codeInsight.completion.PrioritizedLookupElement
2123
import com.intellij.codeInsight.lookup.LookupElement
2224
import com.intellij.openapi.progress.ProgressManager
2325
import com.intellij.openapi.project.Project
2426
import com.intellij.util.Processor
2527
import com.tang.intellij.lua.lang.LuaIcons
26-
import com.tang.intellij.lua.psi.LuaClassField
27-
import com.tang.intellij.lua.psi.LuaClassMember
28-
import com.tang.intellij.lua.psi.LuaIndexExpr
29-
import com.tang.intellij.lua.psi.LuaPsiTreeUtil
28+
import com.tang.intellij.lua.psi.*
3029
import com.tang.intellij.lua.search.SearchContext
3130
import com.tang.intellij.lua.ty.*
3231

@@ -57,7 +56,8 @@ open class ClassMemberCompletionProvider : LuaCompletionProvider() {
5756
val isColon = indexExpr.colon != null
5857
val project = indexExpr.project
5958
val contextTy = LuaPsiTreeUtil.findContextClass(indexExpr)
60-
val prefixType = SearchContext.with(project) { indexExpr.guessParentType(it) }
59+
val context = SearchContext.get(project)
60+
val prefixType = indexExpr.guessParentType(context)
6161
if (!Ty.isInvalid(prefixType)) {
6262
complete(isColon, project, contextTy, prefixType, completionResultSet, completionResultSet.prefixMatcher, null)
6363
}
@@ -73,7 +73,7 @@ open class ClassMemberCompletionProvider : LuaCompletionProvider() {
7373
val it = d.firstDeclaration.psi
7474
val txt = it.name
7575
if (it is LuaTypeGuessable && txt != null && prefixName != txt && matcher.prefixMatches(txt)) {
76-
val type = SearchContext.infer(it)
76+
val type = it.guessType(context)
7777
if (!Ty.isInvalid(prefixType)) {
7878
val prefixMatcher = completionResultSet.prefixMatcher
7979
val resultSet = completionResultSet.withPrefixMatcher("$prefixName*$postfixName")

EmmyLua-Common/src/main/java/com/tang/intellij/lua/editor/completion/LocalAndGlobalCompletionProvider.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ class LocalAndGlobalCompletionProvider(private val mask: Int) : ClassMemberCompl
4949
true
5050
})
5151
}
52+
val ctx = SearchContext.get(psi.project)
5253
when (psi) {
5354
is LuaFuncBodyOwner -> {
54-
addTy(SearchContext.infer(psi) as ITyFunction)
55+
addTy(psi.guessType(ctx) as ITyFunction)
5556
}
5657
is LuaTypeGuessable -> {
57-
val type = SearchContext.infer(psi)
58+
val type = psi.guessType(ctx)
5859
var isFn = false
5960
TyUnion.each(type) {
6061
if (it is ITyFunction) {

EmmyLua-Common/src/main/java/com/tang/intellij/lua/psi/LuaPsiImplUtil.kt

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,16 @@ package com.tang.intellij.lua.psi
2121
import com.intellij.extapi.psi.StubBasedPsiElementBase
2222
import com.intellij.icons.AllIcons
2323
import com.intellij.navigation.ItemPresentation
24-
import com.intellij.openapi.util.Computable
25-
import com.intellij.openapi.util.Key
2624
import com.intellij.psi.*
2725
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry
2826
import com.intellij.psi.search.GlobalSearchScope
2927
import com.intellij.psi.search.SearchScope
3028
import com.intellij.psi.stubs.StubElement
31-
import com.intellij.psi.util.CachedValueProvider
32-
import com.intellij.psi.util.CachedValuesManager
33-
import com.intellij.psi.util.ParameterizedCachedValue
3429
import com.intellij.psi.util.PsiTreeUtil
3530
import com.tang.intellij.lua.comment.LuaCommentUtil
3631
import com.tang.intellij.lua.comment.psi.LuaDocAccessModifier
3732
import com.tang.intellij.lua.comment.psi.LuaDocTagVararg
3833
import com.tang.intellij.lua.comment.psi.api.LuaComment
39-
import com.tang.intellij.lua.ext.recursionGuard
4034
import com.tang.intellij.lua.lang.LuaIcons
4135
import com.tang.intellij.lua.lang.type.LuaString
4236
import com.tang.intellij.lua.search.SearchContext
@@ -124,35 +118,30 @@ fun getPresentation(classMethodDef: LuaClassMethodDef): ItemPresentation {
124118
}
125119
}
126120

127-
private val GET_CLASS_METHOD = Key.create<ParameterizedCachedValue<ITy, SearchContext>>("GET_CLASS_METHOD")
128-
129121
/**
130122
* 寻找对应的类
131123
* @param classMethodDef def
132124
* *
133125
* @return LuaType
134126
*/
135127
fun guessParentType(classMethodDef: LuaClassMethodDef, context: SearchContext): ITy {
136-
return CachedValuesManager.getManager(classMethodDef.project).getParameterizedCachedValue(classMethodDef, GET_CLASS_METHOD, { ctx ->
137-
/*val stub = classMethodDef.stub
138-
var type: ITy = Ty.UNKNOWN
139-
if (stub != null) {
140-
stub.classes.forEach {
141-
type = type.union(it)
142-
}
143-
} else {
144-
val expr = classMethodDef.classMethodName.expr
145-
val ty = expr.guessType(ctx)
146-
val perfect = TyUnion.getPerfectClass(ty)
147-
if (perfect is ITyClass)
148-
type = perfect
149-
}*/
150-
128+
/*val stub = classMethodDef.stub
129+
var type: ITy = Ty.UNKNOWN
130+
if (stub != null) {
131+
stub.classes.forEach {
132+
type = type.union(it)
133+
}
134+
} else {
151135
val expr = classMethodDef.classMethodName.expr
152136
val ty = expr.guessType(ctx)
153-
val type = TyUnion.getPerfectClass(ty)
154-
CachedValueProvider.Result.create(type, classMethodDef)
155-
}, false, context) ?: Ty.UNKNOWN
137+
val perfect = TyUnion.getPerfectClass(ty)
138+
if (perfect is ITyClass)
139+
type = perfect
140+
}*/
141+
142+
val expr = classMethodDef.classMethodName.expr
143+
val ty = expr.guessType(context)
144+
return TyUnion.getPerfectClass(ty) ?: Ty.UNKNOWN
156145
}
157146

158147
fun getNameIdentifier(funcDef: LuaFuncDef): PsiElement? {

EmmyLua-Common/src/main/java/com/tang/intellij/lua/psi/LuaTypeGuessable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
public interface LuaTypeGuessable extends LuaPsiElement {
2828
default ITy guessType(SearchContext context) {
29-
ITy ty = SearchContext.Companion.infer(this);
29+
ITy ty = SearchContext.Companion.infer(this, context);
3030
ty = TyAliasSubstitutor.Companion.substitute(ty, context);
3131
return ty;
3232
}

EmmyLua-Common/src/main/java/com/tang/intellij/lua/psi/parser/LuaExpressionParser.kt

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.tang.intellij.lua.psi.parser
1818

1919
import com.intellij.lang.PsiBuilder
20+
import com.intellij.psi.tree.IElementType
2021
import com.intellij.psi.tree.TokenSet
2122
import com.tang.intellij.lua.psi.LuaParserUtil.MY_LEFT_COMMENT_BINDER
2223
import com.tang.intellij.lua.psi.LuaParserUtil.MY_RIGHT_COMMENT_BINDER
@@ -120,10 +121,28 @@ object LuaExpressionParser {
120121
}
121122

122123
private fun parsePrimaryExpr(b: PsiBuilder, l: Int): PsiBuilder.Marker? {
123-
var prefix = parsePrefixExpr(b, l + 1)
124+
val pair = parsePrefixExpr(b, l + 1)
125+
val prefixType = pair?.second
126+
var prefix = pair?.first
127+
val validatePrefixForCall = prefixType != TABLE_EXPR && prefixType != LITERAL_EXPR
128+
/**
129+
* ok:
130+
* {
131+
* ({}){},
132+
* ("")""
133+
* }
134+
* not ok:
135+
* {
136+
* {}{},
137+
* """",
138+
* {}""
139+
* }
140+
*/
124141
while (prefix != null) {
125-
val suffix = parseIndexExpr(prefix, b, l + 1)
126-
?: parseCallExpr(prefix, b, l + 1)
142+
var suffix = parseIndexExpr(prefix, b, l + 1)
143+
if (suffix == null && validatePrefixForCall) {
144+
suffix = parseCallExpr(prefix, b, l + 1)
145+
}
127146
if (suffix == null) break
128147
else prefix = suffix
129148
}
@@ -203,7 +222,7 @@ object LuaExpressionParser {
203222
}
204223
}
205224

206-
private fun parsePrefixExpr(b: PsiBuilder, l: Int): PsiBuilder.Marker? {
225+
private fun parsePrefixExpr(b: PsiBuilder, l: Int): Pair<PsiBuilder.Marker, IElementType>? {
207226
when (b.tokenType) {
208227
LPAREN -> { // parenExpr ::= '(' expr ')'
209228
val m = b.mark()
@@ -214,22 +233,25 @@ object LuaExpressionParser {
214233
expectError(b, RPAREN) { "')'" } // )
215234

216235
m.done(PAREN_EXPR)
217-
return m
236+
return Pair(m, PAREN_EXPR)
218237
}
219238
ID -> { // nameExpr ::= ID
220239
val m = b.mark()
221240
b.advanceLexer()
222241
m.done(NAME_EXPR)
223-
return m
242+
return Pair(m, NAME_EXPR)
224243
}
225244
NUMBER, STRING, NIL, TRUE, FALSE, ELLIPSIS -> { //literalExpr ::= nil | false | true | NUMBER | STRING | "..."
226245
val m = b.mark()
227246
b.advanceLexer()
228247
m.done(LITERAL_EXPR)
229-
return m
248+
return Pair(m, LITERAL_EXPR)
230249
}
231250
LCURLY -> { // table expr
232-
return parseTableExpr(b, l)
251+
val tableExpr = parseTableExpr(b, l)
252+
return if (tableExpr != null)
253+
Pair(tableExpr, LITERAL_EXPR)
254+
else null
233255
}
234256
}
235257
return null

0 commit comments

Comments
 (0)