Skip to content

Commit a29b2b0

Browse files
committed
重构语言服务,升级到3.17版本
1 parent 40e5191 commit a29b2b0

File tree

9 files changed

+515
-402
lines changed

9 files changed

+515
-402
lines changed

EmmyLua-Common/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ sourceSets {
1616
dependencies {
1717
compile project(":IntelliJ-Core")
1818
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
19+
// compile fileTree(dir: "libs", include: "*.jar")
1920
}
2021
buildscript {
21-
ext.kotlin_version = '1.6.0'
22+
ext.kotlin_version = '1.6.10'
2223
repositories {
2324
mavenCentral()
2425
}

EmmyLua-Common/src/main/ext/com/tang/lsp/workspace.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.tang.lsp
33
import com.intellij.openapi.project.Project
44
import com.intellij.openapi.util.Key
55
import com.intellij.psi.PsiFile
6-
import org.eclipse.lsp4j.Diagnostic
76
import org.eclipse.lsp4j.DidChangeTextDocumentParams
87
import java.io.File
98
import java.net.URI
@@ -48,12 +47,12 @@ data class Word(val hashCode: Int, val start: Int, val end: Int)
4847

4948
interface ILuaFile : IVirtualFile {
5049
fun getText(): CharSequence
51-
val diagnostics: List<Diagnostic>
5250
val psi: PsiFile?
5351
fun unindex()
5452
fun getLine(offset: Int): Pair<Int, Int>
5553
fun didChange(params: DidChangeTextDocumentParams)
56-
fun diagnose();
5754
fun getPosition(line:Int, char: Int): Int
5855
fun processWords(processor: (w: Word) -> Boolean)
56+
fun getVersion(): Int
57+
fun lock(code: () -> Unit)
5958
}

EmmyLua-LS/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = '1.6.0'
2+
ext.kotlin_version = '1.6.10'
33
repositories {
44
mavenCentral()
55
}
@@ -9,7 +9,7 @@ buildscript {
99
}
1010

1111
plugins {
12-
id "org.jetbrains.kotlin.jvm" version "1.6.0"
12+
id "org.jetbrains.kotlin.jvm" version "1.6.10"
1313
id 'com.github.johnrengelman.shadow' version '6.1.0'
1414
}
1515

@@ -21,10 +21,11 @@ repositories {
2121
}
2222
dependencies {
2323
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
24-
compile 'org.eclipse.lsp4j:org.eclipse.lsp4j:0.12.0'
24+
compile 'org.eclipse.lsp4j:org.eclipse.lsp4j:0.13.0'
2525
compile group: 'org.picocontainer', name: 'picocontainer', version: '2.15'
2626
compile project(":EmmyLua-Common")
2727
compile 'com.yevdo:jwildcard:1.4'
28+
// compile fileTree(dir: "libs", include: "*.jar")
2829
}
2930

3031
sourceSets {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ class LuaLanguageServer : LanguageServer, LanguageClientAware {
102102
capabilities.foldingRangeProvider = Either.forLeft(true)
103103

104104
capabilities.textDocumentSync = Either.forLeft(TextDocumentSyncKind.Full)
105+
106+
capabilities.inlayHintProvider = Either.forLeft(true)
107+
108+
capabilities.diagnosticProvider = DiagnosticRegistrationOptions(false, true)
109+
105110
// capabilities.semanticTokensProvider = SemanticTokensWithRegistrationOptions(
106111
// SemanticTokensLegend(
107112
// listOf(),
@@ -121,8 +126,6 @@ class LuaLanguageServer : LanguageServer, LanguageClientAware {
121126
client?.registerCapability(RegistrationParams(listOf(didChangeWorkspaceFolders)))
122127

123128
workspaceService.loadWorkspace()
124-
125-
workspaceService.diagnoseWorkspace()
126129
}
127130

128131
override fun getWorkspaceService(): WorkspaceService {

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

Lines changed: 213 additions & 320 deletions
Large diffs are not rendered by default.

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

Lines changed: 83 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import com.tang.lsp.*
1919
import com.tang.vscode.api.impl.Folder
2020
import com.tang.vscode.api.impl.LuaFile
2121
import com.tang.vscode.configuration.ConfigurationManager
22+
import com.tang.vscode.diagnostics.DiagnosticsService
2223
import com.tang.vscode.utils.computeAsync
2324
import com.tang.vscode.utils.getSymbol
2425
import org.eclipse.lsp4j.*
25-
import org.eclipse.lsp4j.jsonrpc.CompletableFutures
26+
import org.eclipse.lsp4j.jsonrpc.CancelChecker
27+
import org.eclipse.lsp4j.jsonrpc.messages.Either
2628
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
2729
import org.eclipse.lsp4j.services.WorkspaceService
2830
import java.io.File
@@ -38,7 +40,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
3840
private val schemeMap = mutableMapOf<String, IFolder>()
3941
private val configurationManager = ConfigurationManager()
4042
private var client: LuaLanguageClient? = null
41-
private var workspaceDiagnoseFuture: CompletableFuture<Unit>? = null
43+
private var configVersion = 0
4244

4345
inner class WProject : UserDataHolderBase(), Project {
4446
override fun process(processor: Processor<PsiFile>) {
@@ -84,10 +86,12 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
8486
override fun didChangeConfiguration(params: DidChangeConfigurationParams) {
8587
val settings = params.settings as? JsonObject ?: return
8688
val ret = VSCodeSettings.update(settings)
89+
++configVersion
8790
if (ret.associationChanged) {
8891
loadWorkspace()
89-
diagnoseWorkspace()
92+
refreshWorkspace()
9093
}
94+
9195
}
9296

9397
fun initConfigFiles(files: Array<EmmyConfigurationSource>) {
@@ -96,15 +100,16 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
96100

97101
@JsonRequest("emmy/updateConfig")
98102
fun updateConfig(params: UpdateConfigParams): CompletableFuture<Void> {
103+
++configVersion
99104
configurationManager.updateConfiguration(params)
100105
loadWorkspace()
101-
diagnoseWorkspace()
106+
refreshWorkspace()
102107
return CompletableFuture()
103108
}
104109

105-
override fun symbol(params: WorkspaceSymbolParams): CompletableFuture<MutableList<out SymbolInformation>> {
110+
override fun symbol(params: WorkspaceSymbolParams): CompletableFuture<Either<MutableList<out SymbolInformation>, MutableList<out WorkspaceSymbol>>> {
106111
if (params.query.isBlank())
107-
return CompletableFuture.completedFuture(mutableListOf())
112+
return CompletableFuture.completedFuture(Either.forRight(mutableListOf()))
108113
val matcher = CamelHumpMatcher(params.query, false)
109114
return computeAsync { cancel ->
110115
val list = mutableListOf<SymbolInformation>()
@@ -116,7 +121,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
116121
}
117122
true
118123
})
119-
list
124+
Either.forLeft(list)
120125
}
121126
}
122127

@@ -129,7 +134,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
129134
}
130135
if (params.event.added.isNotEmpty()) {
131136
loadWorkspace()
132-
diagnoseWorkspace()
137+
refreshWorkspace()
133138
}
134139
}
135140

@@ -140,6 +145,56 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
140145
}
141146
}
142147

148+
override fun diagnostic(params: WorkspaceDiagnosticParams): CompletableFuture<WorkspaceDiagnosticReport> {
149+
for(prev in params.previousResultIds){
150+
val file = findFile(prev.uri)
151+
if(file is LuaFile){
152+
file.workspaceDiagnosticResultId = prev.value
153+
}
154+
}
155+
156+
val files = mutableListOf<LuaFile>()
157+
project.process {
158+
val file = it.virtualFile
159+
if (file is LuaFile) {
160+
files.add(file)
161+
}
162+
true
163+
}
164+
165+
return computeAsync { checker ->
166+
for(luaFile in files){
167+
Thread.sleep(200)
168+
val documentReport = diagnoseFile(luaFile, luaFile.workspaceDiagnosticResultId, checker)
169+
if (documentReport.isRelatedFullDocumentDiagnosticReport) {
170+
val workspaceItemReport = WorkspaceFullDocumentDiagnosticReport(
171+
documentReport.relatedFullDocumentDiagnosticReport.items,
172+
luaFile.uri.toString(),
173+
luaFile.getVersion()
174+
)
175+
workspaceItemReport.resultId = documentReport.relatedFullDocumentDiagnosticReport.resultId
176+
val report = WorkspaceDiagnosticReportPartialResult(
177+
listOf(WorkspaceDocumentDiagnosticReport(workspaceItemReport))
178+
)
179+
client?.notifyProgress(ProgressParams(params.partialResultToken, Either.forRight(report)))
180+
}
181+
else if(documentReport.isRelatedUnchangedDocumentDiagnosticReport){
182+
val workspaceItemReport = WorkspaceUnchangedDocumentDiagnosticReport(
183+
documentReport.relatedUnchangedDocumentDiagnosticReport.resultId,
184+
luaFile.uri.toString(),
185+
luaFile.getVersion()
186+
)
187+
val report = WorkspaceDiagnosticReportPartialResult(
188+
listOf(WorkspaceDocumentDiagnosticReport(workspaceItemReport))
189+
)
190+
client?.notifyProgress(ProgressParams(params.partialResultToken, Either.forRight(report)))
191+
}
192+
}
193+
val empty = WorkspaceDiagnosticReport(emptyList())
194+
empty
195+
}
196+
}
197+
143198
private fun getSchemeFolder(path: FileURI, autoCreate: Boolean): IFolder? {
144199
var folder: IFolder? = schemeMap[path.scheme]
145200
if (folder == null && autoCreate) {
@@ -232,8 +287,26 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
232287
})
233288
}
234289

235-
fun diagnoseWorkspace() {
236-
sendAllDiagnostics()
290+
private fun refreshWorkspace() {
291+
client?.refreshDiagnostics()
292+
}
293+
294+
fun diagnoseFile(file: ILuaFile, previousId: String?, checker: CancelChecker?): DocumentDiagnosticReport {
295+
val fileVersion = file.getVersion()
296+
val resultId = "$fileVersion|$configVersion"
297+
if (previousId != null && resultId == previousId) {
298+
return DocumentDiagnosticReport(RelatedUnchangedDocumentDiagnosticReport(resultId))
299+
}
300+
301+
val diagnostics = mutableListOf<Diagnostic>()
302+
// DiagnosticsService.diagnosticFile(file, diagnostics, checker)
303+
if(file is LuaFile){
304+
file.diagnostic(diagnostics, checker)
305+
}
306+
307+
val report = DocumentDiagnosticReport(RelatedFullDocumentDiagnosticReport(diagnostics))
308+
report.relatedFullDocumentDiagnosticReport.resultId = resultId
309+
return report
237310
}
238311

239312
private fun loadWorkspace(monitor: IProgressMonitor) {
@@ -259,36 +332,6 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
259332
monitor.done()
260333
}
261334

262-
/**
263-
* send all diagnostics of the workspace
264-
*/
265-
private fun sendAllDiagnostics() {
266-
workspaceDiagnoseFuture?.cancel(true)
267-
val files = mutableListOf<LuaFile>()
268-
project.process {
269-
val file = it.virtualFile
270-
if (file is LuaFile) {
271-
files.add(file)
272-
}
273-
true
274-
}
275-
276-
workspaceDiagnoseFuture = CompletableFutures.computeAsync { cancel ->
277-
files.forEach { file ->
278-
if (cancel.isCanceled) {
279-
return@forEach
280-
}
281-
282-
file.diagnose()
283-
val diagnostics = file.diagnostics
284-
if (diagnostics.isNotEmpty()) {
285-
client?.publishDiagnostics(PublishDiagnosticsParams(file.uri.toString(), diagnostics))
286-
}
287-
}
288-
workspaceDiagnoseFuture = null
289-
}
290-
}
291-
292335
override fun findFile(uri: String): IVirtualFile? {
293336
val fileURI = FileURI(uri, false)
294337
return findFile(fileURI)

0 commit comments

Comments
 (0)