Skip to content

Commit

Permalink
字符串常量和数字常量在hover时会直接显示
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Apr 19, 2022
1 parent be1760d commit eb2bae6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import com.tang.intellij.lua.stubs.index.LuaConstIndex

object LuaConst {

fun isConst(className: String, fieldName: String, context: SearchContext): Boolean{
fun isConstField(className: String, fieldName: String, context: SearchContext): Boolean{
return LuaConstIndex.instance.isConst(className, fieldName, context)
}

fun isConst(name: String, context: SearchContext): Boolean{
fun isConstGlobal(name: String, context: SearchContext): Boolean{
return LuaConstIndex.instance.isConst(Constants.WORD_G, name, context)
}


fun isConstLocal(filePath: String, name: String, context: SearchContext): Boolean{
return LuaConstIndex.instance.isConstLocal(filePath, name, context)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ abstract class IndexSink {
LuaSuperClassIndex.instance.removeStubs(file)
LuaShortNameIndex.removeStubs(file)
LuaAliasIndex.instance.removeStubs(file)
LuaConstIndex.instance.removeStubs(file)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class LuaConstIndex: StubIndex<Int, LuaPsiElement>() {

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
return get(key.hashCode(), context.project, context.scope).size == 1
}

fun isConstLocal(filePath: String, name: String, context: SearchContext): Boolean{
val key = "$filePath*$name"
return get(key.hashCode(), context.project, context.scope).size == 0
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ private fun index(luaNameExpr: LuaNameExpr, sink: IndexSink) {
sink.occurrence(StubKeys.SHORT_NAME, name, luaNameExpr)
sink.occurrence(StubKeys.CONST, "${Constants.WORD_G}*$name".hashCode(), luaNameExpr)
}
else{
sink.occurrence(StubKeys.CONST, "${luaNameExpr.containingFile.virtualFile.path}*$name".hashCode(), luaNameExpr)
}
}

private fun index(funcDef: LuaFuncDef, sink: IndexSink) {
Expand All @@ -205,4 +208,4 @@ private fun index(funcDef: LuaFuncDef, sink: IndexSink) {
sink.occurrence(StubKeys.CLASS_MEMBER, moduleName.hashCode(), funcDef)
sink.occurrence(StubKeys.CLASS_MEMBER, "$moduleName*${nameRef.text}".hashCode(), funcDef)
sink.occurrence(StubKeys.SHORT_NAME, nameRef.text, funcDef)
}
}
3 changes: 3 additions & 0 deletions EmmyLua-LS/src/main/kotlin/com/tang/vscode/VSCodeSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ object VSCodeSettings : IVSCodeSettings {
path("emmylua.inspections.assignValidation")?.asString?.let {
DiagnosticsOptions.assignValidation = InspectionsLevel.valueOf(it)
}
path("emmylua.inspections.deprecated")?.asBoolean?.let {
DiagnosticsOptions.deprecated = InspectionsLevel.Warning
}

return SettingsUpdateResult(associationChanged)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ object DiagnosticsOptions {

var assignValidation = InspectionsLevel.None

var deprecated = InspectionsLevel.Warning
var deprecated = InspectionsLevel.None
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.intellij.psi.util.PsiTreeUtil
import com.tang.intellij.lua.comment.psi.LuaDocTagClass
import com.tang.intellij.lua.comment.psi.LuaDocTagField
import com.tang.intellij.lua.psi.*
import com.tang.intellij.lua.psi.search.LuaShortNamesManager
import com.tang.intellij.lua.reference.ReferencesSearch
import com.tang.intellij.lua.search.SearchContext
import com.tang.intellij.lua.stubs.index.LuaClassIndex
Expand Down Expand Up @@ -128,14 +127,9 @@ class LuaDocumentationProvider : DocumentationProvider {

return@wrapLanguage
}
is TyClass -> {
sb.append("class ")
}
is TyUnion -> {
sb.append("union ")
}
else -> {
if (classMember.name != null && LuaConst.isConst(
sb.append("field ")
if (classMember.name != null && LuaConst.isConstField(
parentType.className,
classMember.name!!,
context
Expand All @@ -158,7 +152,7 @@ class LuaDocumentationProvider : DocumentationProvider {
val values = assignStat.valueExprList?.exprList ?: listOf()

for (i in 0 until assignees.size) {
if (assignees[i] == classMember && i < values.size) {
if (assignees[i] == classMember && i < values.size && isConstLiteral(values[i])) {
renderTy(sb, parentType)
sb.append(".${classMember.name} = ${values[i].text}")
sb.append("\n")
Expand All @@ -172,7 +166,6 @@ class LuaDocumentationProvider : DocumentationProvider {
}
}
}
sb.append("property ")
}
}
renderTy(sb, parentType)
Expand All @@ -187,6 +180,25 @@ class LuaDocumentationProvider : DocumentationProvider {
sb.append("global ")
with(sb) {
append(nameExpr.name)
if(LuaConst.isConstGlobal(nameExpr.name, context)
&& (Ty.STRING.subTypeOf(ty, context, true) || Ty.NUMBER.subTypeOf(ty, context, true)) ){
val assignStat = nameExpr.assignStat

if(assignStat != null) {
val assignees = assignStat.varExprList.exprList
val values = assignStat.valueExprList?.exprList ?: listOf()

for (i in 0 until assignees.size) {
if (assignees[i] == nameExpr && i < values.size && isConstLiteral(values[i])) {
sb.append(" = ${values[i].text}")
sb.append("\n")
return@wrapLanguage
}
}

}
}

when (ty) {
is TyFunction -> renderSignature(sb, ty.mainSignature)
else -> {
Expand Down Expand Up @@ -235,15 +247,15 @@ class LuaDocumentationProvider : DocumentationProvider {
val context = SearchContext.get(element.project)
val ty = element.guessType(context)
if (Ty.STRING.subTypeOf(ty, context, true) || Ty.NUMBER.subTypeOf(ty, context, true)) {
if (isConstLocalVariable(element)) {
if (LuaConst.isConstLocal(element.containingFile.virtualFile.path, element.name, context)) {
val localDef = PsiTreeUtil.getParentOfType(element, LuaLocalDef::class.java)
if (localDef != null) {
val nameList = localDef.nameList
val exprList = localDef.exprList
if (nameList != null && exprList != null) {
val index = localDef.getIndexFor(element)
val expr = exprList.getExprAt(index)
if (expr != null) {
if (expr != null && isConstLiteral(expr) ) {
sb.append("local ${element.name} = ${expr.text}")
sb.append("\n")
return@wrapLanguage
Expand All @@ -261,14 +273,8 @@ class LuaDocumentationProvider : DocumentationProvider {
owner?.let { renderComment(sb, owner.comment) }
}

private fun isConstLocalVariable(element: PsiElement): Boolean {
val refs = ReferencesSearch.search(element)
for (ref in refs) {
if (ref.element.parent is LuaVarList) {
return false
}
}
return true
private fun isConstLiteral(element: PsiElement): Boolean{
return element.node.elementType == LuaTypes.LITERAL_EXPR
}

}

0 comments on commit eb2bae6

Please sign in to comment.