Skip to content

Commit e01333b

Browse files
committed
提高诊断表现性能
1 parent 97a94f6 commit e01333b

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ class LuaLanguageServer : LanguageServer, LanguageClientAware {
120120
val didChangeWorkspaceFolders = Registration(WORKSPACE_FOLDERS_CAPABILITY_ID, WORKSPACE_FOLDERS_CAPABILITY_NAME)
121121
client?.registerCapability(RegistrationParams(listOf(didChangeWorkspaceFolders)))
122122

123-
if(VSCodeSettings.clientType != "vsc"){
124-
workspaceService.loadWorkspace()
125-
}
123+
workspaceService.loadWorkspace()
124+
125+
workspaceService.diagnoseWorkspace()
126126
}
127127

128128
override fun getWorkspaceService(): WorkspaceService {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ class LuaTextDocumentService(private val workspace: LuaWorkspaceService) : TextD
144144
while (fchild != funcBody.lastChild) {
145145
if (fchild.text == ")") {
146146
overrideHint.add(RenderRange(fchild.nameRange!!.toRange(file), null))
147+
return@processSuperClass true
147148
}
148149

149150
fchild = fchild.nextSibling

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.tang.vscode.configuration.ConfigurationManager
2222
import com.tang.vscode.utils.computeAsync
2323
import com.tang.vscode.utils.getSymbol
2424
import org.eclipse.lsp4j.*
25+
import org.eclipse.lsp4j.jsonrpc.CompletableFutures
2526
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
2627
import org.eclipse.lsp4j.services.WorkspaceService
2728
import java.io.File
@@ -37,6 +38,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
3738
private val schemeMap = mutableMapOf<String, IFolder>()
3839
private val configurationManager = ConfigurationManager()
3940
private var client: LuaLanguageClient? = null
41+
private var workspaceDiagnoseFuture: CompletableFuture<Unit>? = null
4042

4143
inner class WProject : UserDataHolderBase(), Project {
4244
override fun process(processor: Processor<PsiFile>) {
@@ -68,13 +70,13 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
6870
FileChangeType.Created -> addFile(change.uri)
6971
FileChangeType.Deleted -> removeFile(change.uri)
7072
FileChangeType.Changed -> {
71-
if(change.uri.endsWith("globalStorage")){
73+
if (change.uri.endsWith("globalStorage")) {
7274
return
7375
}
7476
removeFile(change.uri)
7577
addFile(change.uri)
7678
}
77-
else -> { }
79+
else -> {}
7880
}
7981
}
8082
}
@@ -83,7 +85,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
8385
val settings = params.settings as? JsonObject ?: return
8486
val ret = VSCodeSettings.update(settings)
8587
if (ret.associationChanged) {
86-
loadWorkspace()
88+
diagnoseWorkspace()
8789
}
8890
}
8991

@@ -95,14 +97,15 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
9597
fun updateConfig(params: UpdateConfigParams): CompletableFuture<Void> {
9698
configurationManager.updateConfiguration(params)
9799
loadWorkspace()
100+
diagnoseWorkspace()
98101
return CompletableFuture()
99102
}
100103

101104
override fun symbol(params: WorkspaceSymbolParams): CompletableFuture<MutableList<out SymbolInformation>> {
102105
if (params.query.isBlank())
103106
return CompletableFuture.completedFuture(mutableListOf())
104107
val matcher = CamelHumpMatcher(params.query, false)
105-
return computeAsync { cancel->
108+
return computeAsync { cancel ->
106109
val list = mutableListOf<SymbolInformation>()
107110
LuaShortNameIndex.processValues(project, GlobalSearchScope.projectScope(project), Processor {
108111
cancel.checkCanceled()
@@ -123,8 +126,10 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
123126
params.event.removed.forEach {
124127
removeRoot(it.uri)
125128
}
126-
if (params.event.added.isNotEmpty())
129+
if (params.event.added.isNotEmpty()) {
127130
loadWorkspace()
131+
diagnoseWorkspace()
132+
}
128133
}
129134

130135
override fun eachRoot(processor: (ws: IFolder) -> Boolean) {
@@ -226,6 +231,10 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
226231
})
227232
}
228233

234+
fun diagnoseWorkspace() {
235+
sendAllDiagnostics()
236+
}
237+
229238
private fun loadWorkspace(monitor: IProgressMonitor) {
230239
monitor.setProgress("load workspace folders", 0f)
231240
val collections = fileManager.findAllFiles()
@@ -238,29 +247,39 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
238247
processedCount++
239248
val file = uri.toFile()
240249
if (file != null) {
241-
monitor.setProgress("Emmy parse file[${(processedCount / totalFileCount * 100).toInt()}%]: ${file.canonicalPath}",
242-
processedCount / totalFileCount)
250+
monitor.setProgress(
251+
"Emmy parse file[${(processedCount / totalFileCount * 100).toInt()}%]: ${file.canonicalPath}",
252+
processedCount / totalFileCount
253+
)
243254
}
244255
addFile(uri, null)
245256
}
246257
}
247258
monitor.done()
248-
sendAllDiagnostics()
249259
}
250260

251261
/**
252262
* send all diagnostics of the workspace
253263
*/
254264
private fun sendAllDiagnostics() {
255-
project.process {
256-
val file = it.virtualFile
257-
if (file is LuaFile) {
258-
file.diagnose()
259-
if(file.diagnostics.isNotEmpty()) {
260-
client?.publishDiagnostics(PublishDiagnosticsParams(file.uri.toString(), file.diagnostics))
265+
workspaceDiagnoseFuture?.cancel(true)
266+
267+
workspaceDiagnoseFuture = CompletableFutures.computeAsync { cancel ->
268+
project.process {
269+
if (cancel.isCanceled) {
270+
return@process false
271+
}
272+
273+
val file = it.virtualFile
274+
if (file is LuaFile) {
275+
file.diagnose()
276+
if (file.diagnostics.isNotEmpty()) {
277+
client?.publishDiagnostics(PublishDiagnosticsParams(file.uri.toString(), file.diagnostics))
278+
}
261279
}
280+
true
262281
}
263-
true
282+
workspaceDiagnoseFuture = null
264283
}
265284
}
266285

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,20 @@ import com.intellij.lang.cacheBuilder.DefaultWordsScanner
55
import com.intellij.lexer.FlexAdapter
66
import com.intellij.openapi.util.text.StringUtil
77
import com.intellij.openapi.vfs.VirtualFile
8-
import com.intellij.psi.PsiErrorElement
98
import com.intellij.psi.PsiFile
109
import com.intellij.psi.tree.TokenSet
11-
import com.intellij.psi.util.PsiTreeUtil
12-
import com.tang.intellij.lua.comment.psi.LuaDocPsiElement
1310
import com.tang.intellij.lua.lang.LuaLanguageLevel
1411
import com.tang.intellij.lua.lang.LuaParserDefinition
1512
import com.tang.intellij.lua.lexer.LuaLexer
1613
import com.tang.intellij.lua.lexer._LuaLexer
1714
import com.tang.intellij.lua.parser.LuaParser
18-
import com.tang.intellij.lua.psi.LuaCallExpr
19-
import com.tang.intellij.lua.psi.LuaExprStat
2015
import com.tang.intellij.lua.psi.LuaPsiFile
2116
import com.tang.intellij.lua.stubs.IndexSink
2217
import com.tang.lsp.FileURI
2318
import com.tang.lsp.ILuaFile
2419
import com.tang.lsp.Word
25-
import com.tang.lsp.toRange
2620
import com.tang.vscode.diagnostics.DiagnosticsService
2721
import org.eclipse.lsp4j.Diagnostic
28-
import org.eclipse.lsp4j.DiagnosticSeverity
2922
import org.eclipse.lsp4j.DidChangeTextDocumentParams
3023

3124
internal data class Line(val line: Int, val startOffset: Int, val stopOffset: Int)
@@ -35,6 +28,7 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
3528
private var _lines = mutableListOf<Line>()
3629
private var _myPsi: LuaPsiFile? = null
3730
private var _words: List<Word>? = null
31+
private var _completeDiagnose = false;
3832

3933
override val diagnostics = mutableListOf<Diagnostic>()
4034

@@ -84,9 +78,10 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
8478
}
8579

8680
override fun diagnose(){
87-
if(diagnostics.isEmpty()) {
81+
if(!_completeDiagnose) {
8882
// 索引建立完之后再诊断
8983
DiagnosticsService.diagnosticFile(this, diagnostics)
84+
_completeDiagnose = true
9085
}
9186
}
9287

@@ -124,6 +119,7 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
124119

125120
private fun doParser() {
126121
_words = null
122+
_completeDiagnose = false
127123
diagnostics.clear()
128124
unindex()
129125
val parser = LuaParser()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ object DiagnosticsService {
146146
if (prefixType !is TyUnknown && res == null) {
147147
o.id?.let { id ->
148148
val diagnostic = Diagnostic()
149-
diagnostic.message = "undefined property '${id.text}'"
149+
diagnostic.message = "Undefined property '${id.text}'"
150150
diagnostic.severity = makeSeverity(DiagnosticsOptions.fieldValidation)
151151
diagnostic.range = id.textRange.toRange(file)
152152
diagnostics.add(diagnostic)

0 commit comments

Comments
 (0)