Skip to content

Commit

Permalink
暂存
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Apr 18, 2022
1 parent 30bc80e commit be1760d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 18 deletions.
19 changes: 19 additions & 0 deletions EmmyLua-Common/src/main/ext/com/tang/intellij/lua/psi/LuaConst.kt
Original file line number Diff line number Diff line change
@@ -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)
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Int, LuaPsiElement>() {
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())
}*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ object StubKeys {
val CLASS: IndexId<String, LuaDocTagClass> = IndexId.create<String, LuaDocTagClass>("lua.index.class")
val SUPER_CLASS: IndexId<String, LuaDocTagClass> = IndexId.create<String, LuaDocTagClass>("lua.index.super_class")
val ALIAS: IndexId<String, LuaDocTagAlias> = IndexId.create<String, LuaDocTagAlias>("lua.index.alias")
val CONST: IndexId<Int, LuaPsiElement> = IndexId.create<Int, LuaPsiElement>("lua.index.const")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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? {
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down Expand Up @@ -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") {
Expand All @@ -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)
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
}
}

0 comments on commit be1760d

Please sign in to comment.