Skip to content

Commit eb2bae6

Browse files
committed
字符串常量和数字常量在hover时会直接显示
1 parent be1760d commit eb2bae6

File tree

7 files changed

+46
-26
lines changed

7 files changed

+46
-26
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import com.tang.intellij.lua.stubs.index.LuaConstIndex
66

77
object LuaConst {
88

9-
fun isConst(className: String, fieldName: String, context: SearchContext): Boolean{
9+
fun isConstField(className: String, fieldName: String, context: SearchContext): Boolean{
1010
return LuaConstIndex.instance.isConst(className, fieldName, context)
1111
}
1212

13-
fun isConst(name: String, context: SearchContext): Boolean{
13+
fun isConstGlobal(name: String, context: SearchContext): Boolean{
1414
return LuaConstIndex.instance.isConst(Constants.WORD_G, name, context)
1515
}
1616

17-
17+
fun isConstLocal(filePath: String, name: String, context: SearchContext): Boolean{
18+
return LuaConstIndex.instance.isConstLocal(filePath, name, context)
19+
}
1820

1921
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ abstract class IndexSink {
1414
LuaSuperClassIndex.instance.removeStubs(file)
1515
LuaShortNameIndex.removeStubs(file)
1616
LuaAliasIndex.instance.removeStubs(file)
17+
LuaConstIndex.instance.removeStubs(file)
1718
}
1819
}
1920
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ class LuaConstIndex: StubIndex<Int, LuaPsiElement>() {
1717

1818
fun isConst(className: String, fieldName: String, context: SearchContext): Boolean {
1919
val key = "$className*$fieldName"
20-
return LuaClassMemberIndex.instance.get(key.hashCode(), context.project, context.scope).size == 1
20+
return get(key.hashCode(), context.project, context.scope).size == 1
21+
}
22+
23+
fun isConstLocal(filePath: String, name: String, context: SearchContext): Boolean{
24+
val key = "$filePath*$name"
25+
return get(key.hashCode(), context.project, context.scope).size == 0
2126
}
2227

2328
companion object {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ private fun index(luaNameExpr: LuaNameExpr, sink: IndexSink) {
191191
sink.occurrence(StubKeys.SHORT_NAME, name, luaNameExpr)
192192
sink.occurrence(StubKeys.CONST, "${Constants.WORD_G}*$name".hashCode(), luaNameExpr)
193193
}
194+
else{
195+
sink.occurrence(StubKeys.CONST, "${luaNameExpr.containingFile.virtualFile.path}*$name".hashCode(), luaNameExpr)
196+
}
194197
}
195198

196199
private fun index(funcDef: LuaFuncDef, sink: IndexSink) {
@@ -205,4 +208,4 @@ private fun index(funcDef: LuaFuncDef, sink: IndexSink) {
205208
sink.occurrence(StubKeys.CLASS_MEMBER, moduleName.hashCode(), funcDef)
206209
sink.occurrence(StubKeys.CLASS_MEMBER, "$moduleName*${nameRef.text}".hashCode(), funcDef)
207210
sink.occurrence(StubKeys.SHORT_NAME, nameRef.text, funcDef)
208-
}
211+
}

EmmyLua-LS/src/main/kotlin/com/tang/vscode/VSCodeSettings.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ object VSCodeSettings : IVSCodeSettings {
120120
path("emmylua.inspections.assignValidation")?.asString?.let {
121121
DiagnosticsOptions.assignValidation = InspectionsLevel.valueOf(it)
122122
}
123+
path("emmylua.inspections.deprecated")?.asBoolean?.let {
124+
DiagnosticsOptions.deprecated = InspectionsLevel.Warning
125+
}
123126

124127
return SettingsUpdateResult(associationChanged)
125128
}

EmmyLua-LS/src/main/kotlin/com/tang/vscode/diagnostics/DiagnosticsOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ object DiagnosticsOptions {
2525

2626
var assignValidation = InspectionsLevel.None
2727

28-
var deprecated = InspectionsLevel.Warning
28+
var deprecated = InspectionsLevel.None
2929
}

EmmyLua-LS/src/main/kotlin/com/tang/vscode/documentation/LuaDocumentationProvider.kt

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import com.intellij.psi.util.PsiTreeUtil
2323
import com.tang.intellij.lua.comment.psi.LuaDocTagClass
2424
import com.tang.intellij.lua.comment.psi.LuaDocTagField
2525
import com.tang.intellij.lua.psi.*
26-
import com.tang.intellij.lua.psi.search.LuaShortNamesManager
2726
import com.tang.intellij.lua.reference.ReferencesSearch
2827
import com.tang.intellij.lua.search.SearchContext
2928
import com.tang.intellij.lua.stubs.index.LuaClassIndex
@@ -128,14 +127,9 @@ class LuaDocumentationProvider : DocumentationProvider {
128127

129128
return@wrapLanguage
130129
}
131-
is TyClass -> {
132-
sb.append("class ")
133-
}
134-
is TyUnion -> {
135-
sb.append("union ")
136-
}
137130
else -> {
138-
if (classMember.name != null && LuaConst.isConst(
131+
sb.append("field ")
132+
if (classMember.name != null && LuaConst.isConstField(
139133
parentType.className,
140134
classMember.name!!,
141135
context
@@ -158,7 +152,7 @@ class LuaDocumentationProvider : DocumentationProvider {
158152
val values = assignStat.valueExprList?.exprList ?: listOf()
159153

160154
for (i in 0 until assignees.size) {
161-
if (assignees[i] == classMember && i < values.size) {
155+
if (assignees[i] == classMember && i < values.size && isConstLiteral(values[i])) {
162156
renderTy(sb, parentType)
163157
sb.append(".${classMember.name} = ${values[i].text}")
164158
sb.append("\n")
@@ -172,7 +166,6 @@ class LuaDocumentationProvider : DocumentationProvider {
172166
}
173167
}
174168
}
175-
sb.append("property ")
176169
}
177170
}
178171
renderTy(sb, parentType)
@@ -187,6 +180,25 @@ class LuaDocumentationProvider : DocumentationProvider {
187180
sb.append("global ")
188181
with(sb) {
189182
append(nameExpr.name)
183+
if(LuaConst.isConstGlobal(nameExpr.name, context)
184+
&& (Ty.STRING.subTypeOf(ty, context, true) || Ty.NUMBER.subTypeOf(ty, context, true)) ){
185+
val assignStat = nameExpr.assignStat
186+
187+
if(assignStat != null) {
188+
val assignees = assignStat.varExprList.exprList
189+
val values = assignStat.valueExprList?.exprList ?: listOf()
190+
191+
for (i in 0 until assignees.size) {
192+
if (assignees[i] == nameExpr && i < values.size && isConstLiteral(values[i])) {
193+
sb.append(" = ${values[i].text}")
194+
sb.append("\n")
195+
return@wrapLanguage
196+
}
197+
}
198+
199+
}
200+
}
201+
190202
when (ty) {
191203
is TyFunction -> renderSignature(sb, ty.mainSignature)
192204
else -> {
@@ -235,15 +247,15 @@ class LuaDocumentationProvider : DocumentationProvider {
235247
val context = SearchContext.get(element.project)
236248
val ty = element.guessType(context)
237249
if (Ty.STRING.subTypeOf(ty, context, true) || Ty.NUMBER.subTypeOf(ty, context, true)) {
238-
if (isConstLocalVariable(element)) {
250+
if (LuaConst.isConstLocal(element.containingFile.virtualFile.path, element.name, context)) {
239251
val localDef = PsiTreeUtil.getParentOfType(element, LuaLocalDef::class.java)
240252
if (localDef != null) {
241253
val nameList = localDef.nameList
242254
val exprList = localDef.exprList
243255
if (nameList != null && exprList != null) {
244256
val index = localDef.getIndexFor(element)
245257
val expr = exprList.getExprAt(index)
246-
if (expr != null) {
258+
if (expr != null && isConstLiteral(expr) ) {
247259
sb.append("local ${element.name} = ${expr.text}")
248260
sb.append("\n")
249261
return@wrapLanguage
@@ -261,14 +273,8 @@ class LuaDocumentationProvider : DocumentationProvider {
261273
owner?.let { renderComment(sb, owner.comment) }
262274
}
263275

264-
private fun isConstLocalVariable(element: PsiElement): Boolean {
265-
val refs = ReferencesSearch.search(element)
266-
for (ref in refs) {
267-
if (ref.element.parent is LuaVarList) {
268-
return false
269-
}
270-
}
271-
return true
276+
private fun isConstLiteral(element: PsiElement): Boolean{
277+
return element.node.elementType == LuaTypes.LITERAL_EXPR
272278
}
273279

274280
}

0 commit comments

Comments
 (0)