Skip to content

Commit 04304ee

Browse files
committed
去掉同步锁,改为读写锁,inlay hint修改为语言服务协议定义的方式
1 parent a29b2b0 commit 04304ee

File tree

2 files changed

+79
-77
lines changed

2 files changed

+79
-77
lines changed

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
146146
}
147147

148148
override fun diagnostic(params: WorkspaceDiagnosticParams): CompletableFuture<WorkspaceDiagnosticReport> {
149-
for(prev in params.previousResultIds){
149+
for (prev in params.previousResultIds) {
150150
val file = findFile(prev.uri)
151-
if(file is LuaFile){
151+
if (file is LuaFile) {
152152
file.workspaceDiagnosticResultId = prev.value
153153
}
154154
}
@@ -163,31 +163,32 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
163163
}
164164

165165
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)))
166+
for (luaFile in files) {
167+
// Thread.sleep(200)
168+
luaFile.lock {
169+
val documentReport = diagnoseFile(luaFile, luaFile.workspaceDiagnosticResultId, checker)
170+
if (documentReport.isRelatedFullDocumentDiagnosticReport) {
171+
val workspaceItemReport = WorkspaceFullDocumentDiagnosticReport(
172+
documentReport.relatedFullDocumentDiagnosticReport.items,
173+
luaFile.uri.toString(),
174+
luaFile.getVersion()
175+
)
176+
workspaceItemReport.resultId = documentReport.relatedFullDocumentDiagnosticReport.resultId
177+
val report = WorkspaceDiagnosticReportPartialResult(
178+
listOf(WorkspaceDocumentDiagnosticReport(workspaceItemReport))
179+
)
180+
client?.notifyProgress(ProgressParams(params.partialResultToken, Either.forRight(report)))
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+
}
191192
}
192193
}
193194
val empty = WorkspaceDiagnosticReport(emptyList())
@@ -300,7 +301,7 @@ class LuaWorkspaceService : WorkspaceService, IWorkspace {
300301

301302
val diagnostics = mutableListOf<Diagnostic>()
302303
// DiagnosticsService.diagnosticFile(file, diagnostics, checker)
303-
if(file is LuaFile){
304+
if (file is LuaFile) {
304305
file.diagnostic(diagnostics, checker)
305306
}
306307

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

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import org.eclipse.lsp4j.InlayHint
3030
import org.eclipse.lsp4j.InlayHintKind
3131
import org.eclipse.lsp4j.jsonrpc.CancelChecker
3232
import org.eclipse.lsp4j.jsonrpc.messages.Either
33+
import java.util.concurrent.locks.ReentrantReadWriteLock
34+
import kotlin.concurrent.read
35+
import kotlin.concurrent.write
3336

3437
internal data class Line(val line: Int, val startOffset: Int, val stopOffset: Int)
3538

@@ -38,41 +41,42 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
3841
private var _lines = mutableListOf<Line>()
3942
private var _myPsi: LuaPsiFile? = null
4043
private var _words: List<Word>? = null
41-
private var _inlayHints = mutableListOf<InlayHint>()
4244
private var _version: Int = 0
45+
private var _rwl = ReentrantReadWriteLock()
4346

4447
var workspaceDiagnosticResultId: String? = null
4548

46-
@Synchronized
4749
override fun didChange(params: DidChangeTextDocumentParams) {
48-
if (params.contentChanges.isEmpty())
49-
return
50-
51-
var sb = _text.toString()
52-
var offset = 0
53-
params.contentChanges.forEach {
54-
when {
55-
// for TextDocumentSyncKind.Full
56-
it.range == null -> sb = it.text
57-
// incremental updating
58-
it.range.start.line >= _lines.size -> {
59-
sb += it.text
60-
_lines.add(Line(it.range.start.line, it.range.start.character, it.range.end.character))
61-
}
62-
else -> {
63-
val sline = _lines[it.range.start.line]
64-
val eline = _lines[it.range.end.line]
65-
val spos = sline.startOffset + it.range.start.character
66-
val epos = eline.startOffset + it.range.end.character
67-
sb = sb.replaceRange(spos, epos, it.text)
68-
69-
val textSize = it.text.length
70-
offset += textSize - it.rangeLength
50+
_rwl.write {
51+
if (params.contentChanges.isEmpty())
52+
return
53+
54+
var sb = _text.toString()
55+
var offset = 0
56+
params.contentChanges.forEach {
57+
when {
58+
// for TextDocumentSyncKind.Full
59+
it.range == null -> sb = it.text
60+
// incremental updating
61+
it.range.start.line >= _lines.size -> {
62+
sb += it.text
63+
_lines.add(Line(it.range.start.line, it.range.start.character, it.range.end.character))
64+
}
65+
else -> {
66+
val sline = _lines[it.range.start.line]
67+
val eline = _lines[it.range.end.line]
68+
val spos = sline.startOffset + it.range.start.character
69+
val epos = eline.startOffset + it.range.end.character
70+
sb = sb.replaceRange(spos, epos, it.text)
71+
72+
val textSize = it.text.length
73+
offset += textSize - it.rangeLength
74+
}
7175
}
7276
}
77+
_text = sb
78+
onChanged()
7379
}
74-
_text = sb
75-
onChanged()
7680
}
7781

7882
override fun getText(): CharSequence {
@@ -84,11 +88,12 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
8488
}
8589

8690
fun setText(str: CharSequence) {
87-
_text = str
88-
onChanged()
91+
_rwl.write {
92+
_text = str
93+
onChanged()
94+
}
8995
}
9096

91-
@Synchronized
9297
private fun updateLines() {
9398
_lines.clear()
9499
var pos = 0
@@ -143,7 +148,6 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
143148
return _lines.firstOrNull { it.line == line } ?.startOffset ?: 0
144149
}*/
145150

146-
@Synchronized
147151
override fun getLine(offset: Int): Pair<Int, Int> {
148152
if (_lines.size <= 1) {
149153
return Pair(0, offset)
@@ -174,28 +178,26 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
174178
}
175179
}
176180

177-
@Synchronized
178181
override fun getPosition(line: Int, char: Int): Int {
179182
val lineData = _lines.firstOrNull { it.line == line }
180183
var pos = if (lineData != null) lineData.startOffset + char else char
181-
if(pos >= _text.length){
184+
if (pos >= _text.length) {
182185
pos = _text.length
183186
}
184187
return pos
185188
}
186189

187-
@Synchronized
188-
override fun getVersion(): Int{
190+
override fun getVersion(): Int {
189191
return _version
190192
}
191193

192-
@Synchronized
193194
override fun lock(code: () -> Unit) {
194-
code()
195+
_rwl.read {
196+
code()
197+
}
195198
}
196199

197-
@Synchronized
198-
fun diagnostic(diagnostics: MutableList<Diagnostic>, checker: CancelChecker?){
200+
fun diagnostic(diagnostics: MutableList<Diagnostic>, checker: CancelChecker?) {
199201
DiagnosticsService.diagnosticFile(this, diagnostics, checker)
200202
}
201203

@@ -236,10 +238,6 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
236238
}
237239

238240
fun getInlayHint(): MutableList<InlayHint> {
239-
return _inlayHints
240-
}
241-
242-
private fun calculateInlayHint() {
243241
val paramHints = mutableListOf<RenderRange>()
244242
val localHints = mutableListOf<RenderRange>()
245243
val overrideHints = mutableListOf<RenderRange>()
@@ -360,7 +358,7 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
360358
for (paramIndex in literalMap.keys) {
361359
if (paramIndex >= index) {
362360
literalMap[paramIndex]?.let {
363-
paramHints[it].hint = "var:" + (paramIndex - index).toString()
361+
paramHints[it].hint = "var" + (paramIndex - index).toString()
364362
}
365363
}
366364
}
@@ -371,25 +369,28 @@ class LuaFile(override val uri: FileURI) : VirtualFileBase(uri), ILuaFile, Virtu
371369
}
372370
}
373371
})
374-
_inlayHints = mutableListOf<InlayHint>()
372+
val inlayHints = mutableListOf<InlayHint>()
375373

376374
if (paramHints.isNotEmpty()) {
377375
for (paramHint in paramHints) {
378-
val hint = InlayHint(paramHint.range.start, Either.forLeft(" ${paramHint.hint}: "))
376+
val hint = InlayHint(paramHint.range.start, Either.forLeft("${paramHint.hint}:"))
379377
hint.kind = InlayHintKind.Parameter
380378
hint.paddingRight = true
381-
_inlayHints.add(hint)
379+
inlayHints.add(hint)
382380
}
383381
}
384382
if (localHints.isNotEmpty()) {
385383
for (localHint in localHints) {
386-
_inlayHints.add(InlayHint(localHint.range.end, Either.forLeft(localHint.hint)))
384+
val hint = InlayHint(localHint.range.end, Either.forLeft(":${localHint.hint}"))
385+
hint.kind = InlayHintKind.Type
386+
inlayHints.add(hint)
387387
}
388388
}
389389
if (overrideHints.isNotEmpty()) {
390390
for (overrideHint in overrideHints) {
391-
_inlayHints.add(InlayHint(overrideHint.range.end, Either.forLeft(overrideHint.hint)))
391+
inlayHints.add(InlayHint(overrideHint.range.end, Either.forLeft(" override ")))
392392
}
393393
}
394+
return inlayHints
394395
}
395396
}

0 commit comments

Comments
 (0)