diff --git a/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/psi/LuaConst.kt b/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/psi/LuaConst.kt new file mode 100644 index 00000000..5ee8be43 --- /dev/null +++ b/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/psi/LuaConst.kt @@ -0,0 +1,19 @@ +package com.tang.intellij.lua.psi + +import com.tang.intellij.lua.Constants +import com.tang.intellij.lua.search.SearchContext +import com.tang.intellij.lua.stubs.index.LuaConstIndex + +object LuaConst { + + fun isConst(className: String, fieldName: String, context: SearchContext): Boolean{ + return LuaConstIndex.instance.isConst(className, fieldName, context) + } + + fun isConst(name: String, context: SearchContext): Boolean{ + return LuaConstIndex.instance.isConst(Constants.WORD_G, name, context) + } + + + +} \ No newline at end of file diff --git a/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/IndexSink.kt b/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/IndexSink.kt index 882b9a33..af840939 100644 --- a/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/IndexSink.kt +++ b/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/IndexSink.kt @@ -27,6 +27,7 @@ class IndexSinkImpl(val file: LuaPsiFile) : IndexSink() { StubKeys.SUPER_CLASS -> LuaSuperClassIndex.instance.occurrence(file, key, value) StubKeys.SHORT_NAME -> LuaShortNameIndex.occurrence(file, key, value) StubKeys.ALIAS -> LuaAliasIndex.instance.occurrence(file, key, value) + StubKeys.CONST -> LuaConstIndex.instance.occurrence(file, key, value) } } } \ No newline at end of file diff --git a/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/index/LuaConstIndex.kt b/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/index/LuaConstIndex.kt new file mode 100644 index 00000000..68a1d5f1 --- /dev/null +++ b/EmmyLua-Common/src/main/ext/com/tang/intellij/lua/stubs/index/LuaConstIndex.kt @@ -0,0 +1,32 @@ +package com.tang.intellij.lua.stubs.index + +import com.intellij.util.Processor +import com.intellij.util.containers.ContainerUtil +import com.tang.intellij.lua.comment.psi.LuaDocTagField +import com.tang.intellij.lua.psi.LuaClassMember +import com.tang.intellij.lua.psi.LuaClassMethod +import com.tang.intellij.lua.psi.LuaPsiElement +import com.tang.intellij.lua.psi.LuaTableField +import com.tang.intellij.lua.search.SearchContext +import com.tang.intellij.lua.stubs.StubKeys +import com.tang.intellij.lua.ty.ITyClass +import com.tang.intellij.lua.ty.TyParameter + +class LuaConstIndex: StubIndex() { + override fun getKey() = StubKeys.CONST + + fun isConst(className: String, fieldName: String, context: SearchContext): Boolean { + val key = "$className*$fieldName" + return LuaClassMemberIndex.instance.get(key.hashCode(), context.project, context.scope).size == 1 + } + + companion object { + val instance = LuaConstIndex() + + /*fun indexStub(indexSink: IndexSink, className: String, memberName: String) { + indexSink.occurrence(StubKeys.CLASS_MEMBER, className.hashCode()) + indexSink.occurrence(StubKeys.CLASS_MEMBER, "$className*$memberName".hashCode()) + }*/ + } + +} \ No newline at end of file diff --git a/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/StubKeys.kt b/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/StubKeys.kt index 07fba58f..9eca5032 100644 --- a/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/StubKeys.kt +++ b/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/StubKeys.kt @@ -28,4 +28,5 @@ object StubKeys { val CLASS: IndexId = IndexId.create("lua.index.class") val SUPER_CLASS: IndexId = IndexId.create("lua.index.super_class") val ALIAS: IndexId = IndexId.create("lua.index.alias") + val CONST: IndexId = IndexId.create("lua.index.const") } diff --git a/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/index.kt b/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/index.kt index 5a3b3349..ffba2047 100644 --- a/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/index.kt +++ b/EmmyLua-Common/src/main/java/com/tang/intellij/lua/stubs/index.kt @@ -144,7 +144,7 @@ private fun index(indexExpr: LuaIndexExpr, sink: IndexSink) { classNameSet.forEach { className -> sink.occurrence(StubKeys.CLASS_MEMBER, className.hashCode(), indexExpr) sink.occurrence(StubKeys.CLASS_MEMBER, "$className*$name".hashCode(), indexExpr) - + sink.occurrence(StubKeys.CONST, "$className*$name".hashCode(), indexExpr) sink.occurrence(StubKeys.SHORT_NAME, name, indexExpr) } } @@ -161,6 +161,7 @@ private fun index(tableField: LuaTableField, sink: IndexSink) { sink.occurrence(StubKeys.CLASS_MEMBER, className.hashCode(), tableField) sink.occurrence(StubKeys.CLASS_MEMBER, "$className*$name".hashCode(), tableField) sink.occurrence(StubKeys.SHORT_NAME, name, tableField) + sink.occurrence(StubKeys.CONST, "$className*$name".hashCode(), tableField) } private fun findTableExprTypeName(field: LuaTableField): String? { @@ -188,6 +189,7 @@ private fun index(luaNameExpr: LuaNameExpr, sink: IndexSink) { sink.occurrence(StubKeys.CLASS_MEMBER, Constants.WORD_G.hashCode(), luaNameExpr) sink.occurrence(StubKeys.CLASS_MEMBER, "${Constants.WORD_G}*$name".hashCode(), luaNameExpr) sink.occurrence(StubKeys.SHORT_NAME, name, luaNameExpr) + sink.occurrence(StubKeys.CONST, "${Constants.WORD_G}*$name".hashCode(), luaNameExpr) } } diff --git a/EmmyLua-LS/src/main/kotlin/com/tang/vscode/LuaTextDocumentService.kt b/EmmyLua-LS/src/main/kotlin/com/tang/vscode/LuaTextDocumentService.kt index 715b145b..e68ffb89 100644 --- a/EmmyLua-LS/src/main/kotlin/com/tang/vscode/LuaTextDocumentService.kt +++ b/EmmyLua-LS/src/main/kotlin/com/tang/vscode/LuaTextDocumentService.kt @@ -354,7 +354,7 @@ class LuaTextDocumentService(private val workspace: LuaWorkspaceService) : TextD val position = arr[1].toInt() file.psi?.findElementAt(position)?.let { psi -> PsiTreeUtil.getParentOfType(psi, LuaClassMember::class.java)?.let { member -> - val doc = documentProvider.generateDoc(member, false) + val doc = documentProvider.generateDoc(member) val content = MarkupContent() content.kind = "markdown" content.value = doc @@ -378,7 +378,7 @@ class LuaTextDocumentService(private val workspace: LuaWorkspaceService) : TextD val element = TargetElementUtil.findTarget(file.psi, pos) if (element != null) { val ref = element.reference?.resolve() ?: element - val doc = documentProvider.generateDoc(ref, true) + val doc = documentProvider.generateDoc(ref) if (doc != null) hover = Hover(listOf(Either.forLeft(doc))) } diff --git a/EmmyLua-LS/src/main/kotlin/com/tang/vscode/documentation/LuaDocumentationProvider.kt b/EmmyLua-LS/src/main/kotlin/com/tang/vscode/documentation/LuaDocumentationProvider.kt index 06b38943..9ded27b1 100644 --- a/EmmyLua-LS/src/main/kotlin/com/tang/vscode/documentation/LuaDocumentationProvider.kt +++ b/EmmyLua-LS/src/main/kotlin/com/tang/vscode/documentation/LuaDocumentationProvider.kt @@ -39,7 +39,7 @@ class LuaDocumentationProvider : DocumentationProvider { } override fun generateDoc(element: PsiElement, originalElement: PsiElement?): String? { - return generateDoc(element, false) + return generateDoc(element) } override fun getQuickNavigateInfo(element: PsiElement?, originalElement: PsiElement?): String? { @@ -73,12 +73,12 @@ class LuaDocumentationProvider : DocumentationProvider { } - fun generateDoc(element: PsiElement, hover: Boolean): String? { + fun generateDoc(element: PsiElement): String? { val sb = StringBuilder() when (element) { is LuaParamNameDef -> renderParamNameDef(sb, element) is LuaDocTagClass -> renderClassDef(sb, element) - is LuaClassMember -> renderClassMember(sb, element, hover) + is LuaClassMember -> renderClassMember(sb, element) is LuaNameDef -> renderNamDef(sb, element) is LuaLocalFuncDef -> { sb.wrapLanguage("lua") { @@ -98,7 +98,7 @@ class LuaDocumentationProvider : DocumentationProvider { return null } - private fun renderClassMember(sb: StringBuilder, classMember: LuaClassMember, hover: Boolean) { + private fun renderClassMember(sb: StringBuilder, classMember: LuaClassMember) { val context = SearchContext.get(classMember.project) val parentType = classMember.guessClassType(context) val ty = classMember.guessType(context) @@ -135,8 +135,12 @@ class LuaDocumentationProvider : DocumentationProvider { sb.append("union ") } else -> { - if (hover && isConstField(classMember)) { - + if (classMember.name != null && LuaConst.isConst( + parentType.className, + classMember.name!!, + context + ) + ) { when (classMember) { is LuaTableField -> { if (classMember.exprList.isNotEmpty()) { @@ -267,13 +271,4 @@ class LuaDocumentationProvider : DocumentationProvider { return true } - private fun isConstField(element: PsiElement): Boolean { - val refs = ReferencesSearch.search(element) - for (ref in refs) { - if (ref.element.parent is LuaVarList) { - return false - } - } - return true - } }