Skip to content

Commit b09fd3a

Browse files
committed
正确实现工作区诊断,工作区诊断现在不会占用过多性能
1 parent 2c804af commit b09fd3a

File tree

4 files changed

+197
-176
lines changed

4 files changed

+197
-176
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.tang.vscode.api.impl.LuaFile
2626
import com.tang.vscode.documentation.LuaDocumentationProvider
2727
import com.tang.vscode.formatter.FormattingFormatter
2828
import com.tang.vscode.formatter.FormattingType
29+
import com.tang.vscode.inlayHint.InlayHintService
2930
import com.tang.vscode.utils.TargetElementUtil
3031
import com.tang.vscode.utils.computeAsync
3132
import com.tang.vscode.utils.getDocumentSymbols
@@ -960,7 +961,6 @@ class LuaTextDocumentService(private val workspace: LuaWorkspaceService) : TextD
960961
})
961962
}
962963

963-
964964
if (callExpr is LuaCallExpr) {
965965
if (callExpr.firstChild.text == "require") {
966966
val lines = file.getLine(callExpr.textOffset)
@@ -1009,7 +1009,7 @@ class LuaTextDocumentService(private val workspace: LuaWorkspaceService) : TextD
10091009
if (file is LuaFile) {
10101010
file.lock {
10111011
// 认为所有local名称定义一开始都是未使用的
1012-
list = file.getInlayHint()
1012+
list = InlayHintService.getInlayHint(file)
10131013
}
10141014
}
10151015
list

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,10 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
162162
true
163163
}
164164

165+
val workspaceConfigVersion = configVersion
166+
165167
return computeAsync { checker ->
166168
for (luaFile in files) {
167-
// Thread.sleep(200)
168169
luaFile.lock {
169170
val documentReport = diagnoseFile(luaFile, luaFile.workspaceDiagnosticResultId, checker)
170171
if (documentReport.isRelatedFullDocumentDiagnosticReport) {
@@ -191,6 +192,12 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
191192
}
192193
}
193194
}
195+
196+
// 如果配置版本发生变更则中断无限循环
197+
while (workspaceConfigVersion == configVersion) {
198+
Thread.sleep(1000)
199+
}
200+
194201
val empty = WorkspaceDiagnosticReport(emptyList())
195202
empty
196203
}

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

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -233,177 +233,4 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
233233
}
234234
}
235235
}
236-
237-
fun getInlayHint(): MutableList<InlayHint> {
238-
val paramHints = mutableListOf<RenderRange>()
239-
val localHints = mutableListOf<RenderRange>()
240-
val overrideHints = mutableListOf<RenderRange>()
241-
val file = this
242-
243-
psi?.acceptChildren(object : LuaRecursiveVisitor() {
244-
override fun visitClassMethodDef(o: LuaClassMethodDef) {
245-
if (LuaSettings.instance.overrideHint) {
246-
val context = SearchContext.get(o.project)
247-
val classType = o.guessClassType(context)
248-
if (classType != null) {
249-
TyClass.processSuperClass(classType, context) { sup ->
250-
val id = o.classMethodName.id
251-
if (id != null) {
252-
val member = sup.findMember(id.text, context)
253-
if (member != null) {
254-
val funcBody = o.children.find { it is LuaFuncBody }
255-
if (funcBody is LuaFuncBody) {
256-
var fchild = funcBody.firstChild
257-
while (fchild != funcBody.lastChild) {
258-
if (fchild.text == ")") {
259-
overrideHints.add(
260-
RenderRange(
261-
fchild.textRange.toRange(file),
262-
"override",
263-
"${sup.className}#${member.name}"
264-
)
265-
)
266-
267-
return@processSuperClass false
268-
}
269-
270-
fchild = fchild.nextSibling
271-
}
272-
}
273-
}
274-
}
275-
true
276-
}
277-
}
278-
}
279-
o.acceptChildren(this)
280-
}
281-
282-
override fun visitLocalDef(o: LuaLocalDef) {
283-
if (o.parent is LuaExprStat) // non-complete stat
284-
return
285-
if (LuaSettings.instance.localHint) {
286-
val nameList = o.nameList
287-
o.exprList?.exprList.let { _ ->
288-
nameList?.nameDefList?.forEach {
289-
it.nameRange?.let { nameRange ->
290-
// 这个类型联合的名字太长对大多数情况都不是必要的,将进行必要的裁剪
291-
val gussType = it.guessType(SearchContext.get(o.project))
292-
val displayName = gussType.displayName
293-
when {
294-
displayName.startsWith("fun") -> {
295-
localHints.add(RenderRange(nameRange.toRange(file), "function"))
296-
}
297-
displayName.startsWith('[') -> {
298-
// ignore
299-
}
300-
else -> {
301-
val unexpectedNameIndex = displayName.indexOf("|[")
302-
when (unexpectedNameIndex) {
303-
-1 -> {
304-
localHints.add(RenderRange(nameRange.toRange(file), displayName))
305-
}
306-
else -> {
307-
localHints.add(
308-
RenderRange(
309-
nameRange.toRange(file),
310-
displayName.substring(0, unexpectedNameIndex)
311-
)
312-
)
313-
}
314-
}
315-
}
316-
}
317-
}
318-
}
319-
}
320-
}
321-
o.acceptChildren(this)
322-
}
323-
324-
override fun visitCallExpr(callExpr: LuaCallExpr) {
325-
if (LuaSettings.instance.paramHint) {
326-
var activeParameter = 0
327-
var nCommas = 0
328-
val literalMap = mutableMapOf<Int, Int>()
329-
callExpr.args.firstChild?.let { firstChild ->
330-
var child: PsiElement? = firstChild
331-
while (child != null) {
332-
if (child.node.elementType == LuaTypes.COMMA) {
333-
activeParameter++
334-
nCommas++
335-
} else if (child.node.elementType == LuaTypes.LITERAL_EXPR
336-
|| child.node.elementType == LuaTypes.TABLE_EXPR
337-
|| child.node.elementType == LuaTypes.CLOSURE_EXPR
338-
|| child.node.elementType == LuaTypes.BINARY_EXPR
339-
) {
340-
paramHints.add(RenderRange(child.textRange.toRange(file), null))
341-
literalMap[activeParameter] = paramHints.size - 1;
342-
}
343-
344-
child = child.nextSibling
345-
}
346-
}
347-
348-
callExpr.guessParentType(SearchContext.get(callExpr.project)).let { parentType ->
349-
parentType.each { ty ->
350-
if (ty is ITyFunction) {
351-
val sig = ty.findPerfectSignature(callExpr)
352-
var index = 0;
353-
354-
sig.params.forEach { pi ->
355-
literalMap[index]?.let {
356-
paramHints[it].hint = pi.name
357-
}
358-
index++
359-
}
360-
361-
if (sig.hasVarargs() && LuaSettings.instance.varargHint) {
362-
for (paramIndex in literalMap.keys) {
363-
if (paramIndex >= index) {
364-
literalMap[paramIndex]?.let {
365-
paramHints[it].hint = "var" + (paramIndex - index).toString()
366-
}
367-
}
368-
}
369-
}
370-
}
371-
}
372-
}
373-
}
374-
}
375-
})
376-
377-
val inlayHints = mutableListOf<InlayHint>()
378-
if (paramHints.isNotEmpty()) {
379-
for (paramHint in paramHints) {
380-
if(paramHint.hint != null) {
381-
val hint = InlayHint(paramHint.range.start, Either.forLeft("${paramHint.hint}:"))
382-
hint.kind = InlayHintKind.Parameter
383-
hint.paddingRight = true
384-
inlayHints.add(hint)
385-
}
386-
}
387-
}
388-
if (localHints.isNotEmpty()) {
389-
for (localHint in localHints) {
390-
val hint = InlayHint(localHint.range.end, Either.forLeft(":${localHint.hint}"))
391-
hint.kind = InlayHintKind.Type
392-
hint.paddingLeft = true
393-
inlayHints.add(hint)
394-
}
395-
}
396-
if (overrideHints.isNotEmpty()) {
397-
for (overrideHint in overrideHints) {
398-
val hint = InlayHint(overrideHint.range.end, Either.forLeft(overrideHint.hint))
399-
hint.paddingLeft = true
400-
if(overrideHint.data != null){
401-
hint.data = overrideHint.data
402-
}
403-
404-
inlayHints.add(hint)
405-
}
406-
}
407-
return inlayHints
408-
}
409236
}

0 commit comments

Comments
 (0)