Skip to content

Commit a608add

Browse files
committed
优化hint
1 parent caba5de commit a608add

File tree

6 files changed

+73
-42
lines changed

6 files changed

+73
-42
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ class LuaLanguageServer : LanguageServer, LanguageClientAware {
103103

104104
capabilities.textDocumentSync = Either.forLeft(TextDocumentSyncKind.Full)
105105

106-
capabilities.inlayHintProvider = Either.forLeft(true)
106+
val inlayHintOptions = InlayHintRegistrationOptions()
107+
inlayHintOptions.resolveProvider = true
108+
capabilities.inlayHintProvider = Either.forRight(inlayHintOptions)
107109

108110
capabilities.diagnosticProvider = DiagnosticRegistrationOptions(false, true)
109111

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package com.tang.vscode
22

33
import com.google.gson.JsonPrimitive
44
import com.intellij.openapi.util.TextRange
5-
import com.intellij.psi.PsiComment
6-
import com.intellij.psi.PsiElement
7-
import com.intellij.psi.PsiErrorElement
5+
import com.intellij.psi.*
86
import com.intellij.psi.search.GlobalSearchScope
97
import com.intellij.psi.util.PsiTreeUtil
108
import com.intellij.util.Consumer
@@ -18,6 +16,7 @@ import com.tang.intellij.lua.editor.completion.asCompletionItem
1816
import com.tang.intellij.lua.psi.*
1917
import com.tang.intellij.lua.reference.ReferencesSearch
2018
import com.tang.intellij.lua.search.SearchContext
19+
import com.tang.intellij.lua.stubs.index.LuaClassMemberIndex
2120
import com.tang.intellij.lua.ty.*
2221
import com.tang.lsp.ILuaFile
2322
import com.tang.lsp.getRangeInFile
@@ -440,8 +439,7 @@ class LuaTextDocumentService(private val workspace: LuaWorkspaceService) : TextD
440439
if (file == null) {
441440
val u = URI(uri)
442441
workspace.addFile(File(u.path), params.textDocument.text, true)
443-
}
444-
else if(file is LuaFile){
442+
} else if (file is LuaFile) {
445443
file.text = params.textDocument.text
446444
}
447445
}
@@ -1032,6 +1030,41 @@ class LuaTextDocumentService(private val workspace: LuaWorkspaceService) : TextD
10321030
}
10331031
}
10341032

1033+
override fun resolveInlayHint(unresolved: InlayHint): CompletableFuture<InlayHint> {
1034+
return computeAsync {
1035+
if (unresolved.data != null && unresolved.data is JsonPrimitive) {
1036+
val data = (unresolved.data as JsonPrimitive).asString
1037+
val texts = data.split("#")
1038+
val labelParts = mutableListOf<InlayHintLabelPart>()
1039+
if (texts.size == 2) {
1040+
val className = texts[0]
1041+
val fieldName = texts[1]
1042+
val context = SearchContext.get(workspace.getProject())
1043+
val resolveList = mutableListOf<ResolveResult>()
1044+
LuaClassMemberIndex.process(className, fieldName, context, Processor {
1045+
resolveList.add(PsiElementResolveResult(it))
1046+
false
1047+
})
1048+
1049+
val resolve = resolveList.firstOrNull()?.element
1050+
if (resolve != null) {
1051+
val sourceFile = resolve.containingFile?.virtualFile as? LuaFile
1052+
val range = resolve.nameRange
1053+
if (range != null && sourceFile != null) {
1054+
val labelPart = InlayHintLabelPart(unresolved.label.left)
1055+
labelPart.location = Location(sourceFile.uri.toString(), range.toRange(sourceFile))
1056+
labelParts.add(labelPart)
1057+
}
1058+
}
1059+
1060+
}
1061+
1062+
unresolved.label = Either.forRight(labelParts)
1063+
}
1064+
unresolved
1065+
}
1066+
}
1067+
10351068
private fun withPsiFile(
10361069
textDocument: TextDocumentIdentifier,
10371070
position: Position,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,8 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
430430
project.putUserData(IConfigurationManager.KEY, configurationManager)
431431
project.putUserData(IFileManager.KEY, fileManager)
432432
}
433+
434+
fun getProject(): Project {
435+
return project
436+
}
433437
}

EmmyLua-LS/src/main/kotlin/com/tang/vscode/api/impl/LuaFile.kt

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -256,26 +256,15 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
256256
var fchild = funcBody.firstChild
257257
while (fchild != funcBody.lastChild) {
258258
if (fchild.text == ")") {
259-
// var location: Location? = null
260-
// val resolve = member.reference?.resolve()
261-
// if (resolve != null) {
262-
// val sourceFile = resolve.containingFile?.virtualFile as? LuaFile
263-
// val range = resolve.nameRange
264-
// if (range != null && sourceFile != null)
265-
// location = Location(
266-
// sourceFile.uri.toString(),
267-
// range.toRange(sourceFile)
268-
// )
269-
//
270-
// }
271259
overrideHints.add(
272260
RenderRange(
273261
fchild.textRange.toRange(file),
274-
" override "
262+
"override",
263+
"${sup.className}#${member.name}"
275264
)
276265
)
277266

278-
return@processSuperClass true
267+
return@processSuperClass false
279268
}
280269

281270
fchild = fchild.nextSibling
@@ -384,8 +373,8 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
384373
}
385374
}
386375
})
387-
val inlayHints = mutableListOf<InlayHint>()
388376

377+
val inlayHints = mutableListOf<InlayHint>()
389378
if (paramHints.isNotEmpty()) {
390379
for (paramHint in paramHints) {
391380
val hint = InlayHint(paramHint.range.start, Either.forLeft("${paramHint.hint}:"))
@@ -404,18 +393,13 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
404393
}
405394
if (overrideHints.isNotEmpty()) {
406395
for (overrideHint in overrideHints) {
407-
if(overrideHint.location == null) {
408-
val hint = InlayHint(overrideHint.range.end, Either.forLeft(overrideHint.hint))
409-
hint.paddingLeft = true
410-
inlayHints.add(hint)
411-
}
412-
else{
413-
val hintPart = InlayHintLabelPart(overrideHint.hint)
414-
hintPart.location = overrideHint.location
415-
val hint = InlayHint(overrideHint.range.end, Either.forRight(listOf(hintPart)))
416-
hint.paddingLeft = true
417-
inlayHints.add(hint)
396+
val hint = InlayHint(overrideHint.range.end, Either.forLeft(overrideHint.hint))
397+
hint.paddingLeft = true
398+
if(overrideHint.data != null){
399+
hint.data = overrideHint.data
418400
}
401+
402+
inlayHints.add(hint)
419403
}
420404
}
421405
return inlayHints

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ class LuaDocumentationProvider : DocumentationProvider {
9696
return null
9797
}
9898

99+
fun generateCommentDoc(element: PsiElement): String {
100+
val sb = StringBuilder()
101+
if (element is LuaCommentOwner) {
102+
renderComment(sb, element.comment)
103+
}
104+
if (sb.isNotEmpty()) return sb.toString()
105+
return ""
106+
}
107+
99108
private fun renderClassMember(sb: StringBuilder, classMember: LuaClassMember) {
100109
val context = SearchContext.get(classMember.project)
101110
val parentType = classMember.guessClassType(context)
@@ -179,16 +188,17 @@ class LuaDocumentationProvider : DocumentationProvider {
179188
sb.append("global ")
180189
with(sb) {
181190
append(nameExpr.name)
182-
if(LuaConst.isConstGlobal(nameExpr.name, context)
183-
&& (Ty.STRING.subTypeOf(ty, context, true) || Ty.NUMBER.subTypeOf(ty, context, true)) ){
191+
if (LuaConst.isConstGlobal(nameExpr.name, context)
192+
&& (Ty.STRING.subTypeOf(ty, context, true) || Ty.NUMBER.subTypeOf(ty, context, true))
193+
) {
184194
val assignStat = nameExpr.assignStat
185195

186-
if(assignStat != null) {
196+
if (assignStat != null) {
187197
val assignees = assignStat.varExprList.exprList
188198
val values = assignStat.valueExprList?.exprList ?: listOf()
189199

190200
for (i in 0 until assignees.size) {
191-
if (assignees[i] == nameExpr && i < values.size && isConstLiteral(values[i])) {
201+
if (assignees[i] == nameExpr && i < values.size && isConstLiteral(values[i])) {
192202
sb.append(" = ${values[i].text}")
193203
sb.append("\n")
194204
return@wrapLanguage
@@ -254,7 +264,7 @@ class LuaDocumentationProvider : DocumentationProvider {
254264
if (nameList != null && exprList != null) {
255265
val index = localDef.getIndexFor(element)
256266
val expr = exprList.getExprAt(index)
257-
if (expr != null && isConstLiteral(expr) ) {
267+
if (expr != null && isConstLiteral(expr)) {
258268
sb.append("local ${element.name} = ${expr.text}")
259269
sb.append("\n")
260270
return@wrapLanguage
@@ -272,7 +282,7 @@ class LuaDocumentationProvider : DocumentationProvider {
272282
owner?.let { renderComment(sb, owner.comment) }
273283
}
274284

275-
private fun isConstLiteral(element: PsiElement): Boolean{
285+
private fun isConstLiteral(element: PsiElement): Boolean {
276286
return element.node.elementType == LuaTypes.LITERAL_EXPR
277287
}
278288

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.tang.vscode
22

33
import com.google.gson.JsonArray
44
import com.google.gson.JsonObject
5+
import com.intellij.psi.PsiElement
56
import com.tang.lsp.FileURI
67
import org.eclipse.lsp4j.Location
78
import org.eclipse.lsp4j.Range
@@ -12,14 +13,11 @@ enum class AnnotatorType {
1213
DocName,
1314
Upvalue,
1415
NotUse,
15-
ParamHint,
16-
LocalHint,
17-
OverrideHint
1816
}
1917

2018
data class AnnotatorParams(val uri: String)
2119

22-
data class RenderRange(val range: Range, var hint: String?, val location: Location? = null)
20+
data class RenderRange(val range: Range, var hint: String?, val data: String? = null)
2321

2422
data class Annotator(val uri: String, val ranges: List<RenderRange>, val type: AnnotatorType)
2523

0 commit comments

Comments
 (0)