Skip to content

Commit

Permalink
Merge pull request #635 from dingyi222666/lsp
Browse files Browse the repository at this point in the history
refactor(language-lsp): improve signature help trigger, basic diagnostics support
  • Loading branch information
Rosemoe authored Dec 14, 2024
2 parents 93885d3 + fb20092 commit b13130a
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ open class DefaultLanguageClient(protected val context: ClientContext) :
}

override fun registerCapability(params: RegistrationParams): CompletableFuture<Void> {
//Not prepared to support this feature
// Not prepared to support this feature
return CompletableFuture.completedFuture(null)
}

Expand All @@ -83,8 +83,6 @@ open class DefaultLanguageClient(protected val context: ClientContext) :
}

override fun publishDiagnostics(publishDiagnosticsParams: PublishDiagnosticsParams) {
// FIXME: support it.

val diagnosticsContainer = context.project.diagnosticsContainer
val uri = URI(publishDiagnosticsParams.uri).toFileUri()

Expand All @@ -96,7 +94,6 @@ open class DefaultLanguageClient(protected val context: ClientContext) :
val editor = context.getEditor(uri)

editor?.onDiagnosticsUpdate()

}

override fun refreshDiagnostics(): CompletableFuture<Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class CustomConnectProvider(private val streamProvider: StreamProvider) : Stream
/**
* Provider of language server connection
*/
interface StreamProvider {
fun interface StreamProvider {
@WorkerThread
fun getStreams(): Pair<InputStream, OutputStream>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import org.eclipse.lsp4j.DidCloseTextDocumentParams
import org.eclipse.lsp4j.DidOpenTextDocumentParams
import org.eclipse.lsp4j.DidSaveTextDocumentParams
import org.eclipse.lsp4j.DocumentColorParams
import org.eclipse.lsp4j.DocumentDiagnosticParams
import org.eclipse.lsp4j.DocumentDiagnosticReport
import org.eclipse.lsp4j.DocumentFormattingParams
import org.eclipse.lsp4j.DocumentHighlight
import org.eclipse.lsp4j.DocumentHighlightParams
Expand Down Expand Up @@ -491,6 +493,19 @@ class DefaultRequestManager(
} else null
}

override fun diagnostic(params: DocumentDiagnosticParams?): CompletableFuture<DocumentDiagnosticReport?>? {
return if (checkStatus()) {
try {
if (serverCapabilities.diagnosticProvider != null) textDocumentService.diagnostic(
params
) else null
} catch (e: Exception) {
crashed(e)
null
}
} else null
}

override fun definition(params: TextDocumentPositionParams): CompletableFuture<Either<List<Location>, List<LocationLink>>>? {
return definition(DefinitionParams(params.textDocument, params.position))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ package io.github.rosemoe.sora.lsp.editor

import androidx.annotation.WorkerThread
import io.github.rosemoe.sora.event.ContentChangeEvent
import io.github.rosemoe.sora.event.SelectionChangeEvent
import io.github.rosemoe.sora.lang.Language
import io.github.rosemoe.sora.lsp.client.languageserver.requestmanager.RequestManager
import io.github.rosemoe.sora.lsp.client.languageserver.serverdefinition.LanguageServerDefinition
import io.github.rosemoe.sora.lsp.client.languageserver.wrapper.LanguageServerWrapper
import io.github.rosemoe.sora.lsp.editor.event.LspEditorContentChangeEventReceiver
import io.github.rosemoe.sora.lsp.editor.event.LspEditorSelectionChangeEventReceiver
import io.github.rosemoe.sora.lsp.editor.signature.SignatureHelpWindow
import io.github.rosemoe.sora.lsp.events.EventType
import io.github.rosemoe.sora.lsp.events.diagnostics.publishDiagnostics
Expand All @@ -44,6 +46,7 @@ import io.github.rosemoe.sora.lsp.utils.clearVersions
import io.github.rosemoe.sora.widget.CodeEditor
import io.github.rosemoe.sora.widget.subscribeEvent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.future.future
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
Expand All @@ -67,6 +70,8 @@ class LspEditor(

private lateinit var editorContentChangeEventReceiver: LspEditorContentChangeEventReceiver

private lateinit var editorSelectionChangeEventReceiver: LspEditorSelectionChangeEventReceiver

private var currentLanguage: LspLanguage? = null

private var isClose = false
Expand Down Expand Up @@ -102,12 +107,25 @@ class LspEditor(

editorContentChangeEventReceiver = LspEditorContentChangeEventReceiver(this)

val subscriptionReceipt =
currentEditor.subscribeEvent<ContentChangeEvent>(
editorContentChangeEventReceiver
editorSelectionChangeEventReceiver = LspEditorSelectionChangeEventReceiver(this)

val subscriptionReceipts =
mutableListOf(
currentEditor.subscribeEvent<ContentChangeEvent>(
editorContentChangeEventReceiver
),
currentEditor.subscribeEvent<SelectionChangeEvent>(
editorSelectionChangeEventReceiver
)
)

unsubscribeFunction = Runnable { subscriptionReceipt.unsubscribe() }
unsubscribeFunction =
Runnable {
subscriptionReceipts.removeAll {
it.unsubscribe()
true
}
}
}
get() {
return _currentEditor.get()
Expand Down Expand Up @@ -207,7 +225,7 @@ class LspEditor(
exception.printStackTrace();
}
start = System.currentTimeMillis()
Thread.sleep((retryTime / 10).toLong())
delay((retryTime / 10).toLong())
}

if (!isConnected && start > maxRetryTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class LspLanguage(var editor: LspEditor) : Language {
}*/

val prefix = computePrefix(content, position)
Log.d("prefix", prefix);

val prefixLength = prefix.length

val documentChangeEvent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package io.github.rosemoe.sora.lsp.editor.completion

import android.util.Log
import io.github.rosemoe.sora.lang.completion.CompletionItemKind
import io.github.rosemoe.sora.lang.completion.SimpleCompletionIconDrawer.draw
import io.github.rosemoe.sora.lang.completion.snippet.parser.CodeSnippetParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@

package io.github.rosemoe.sora.lsp.editor.event

import android.util.Log
import io.github.rosemoe.sora.event.ContentChangeEvent
import io.github.rosemoe.sora.event.EventReceiver
import io.github.rosemoe.sora.event.SelectionChangeEvent
import io.github.rosemoe.sora.event.Unsubscribe
import io.github.rosemoe.sora.lsp.editor.LspEditor
import io.github.rosemoe.sora.lsp.events.EventType
import io.github.rosemoe.sora.lsp.events.diagnostics.publishDiagnostics
import io.github.rosemoe.sora.lsp.events.diagnostics.queryDocumentDiagnostics
import io.github.rosemoe.sora.lsp.events.document.documentChange
import io.github.rosemoe.sora.lsp.events.signature.signatureHelp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.eclipse.lsp4j.DocumentDiagnosticReport
import org.eclipse.lsp4j.FullDocumentDiagnosticReport
import org.eclipse.lsp4j.UnchangedDocumentDiagnosticReport
import org.eclipse.lsp4j.jsonrpc.messages.Either


class LspEditorContentChangeEventReceiver(private val editor: LspEditor) :
Expand All @@ -49,9 +57,41 @@ class LspEditorContentChangeEventReceiver(private val editor: LspEditor) :
}

editor.eventManager.emitAsync(EventType.signatureHelp, event.changeStart)

val diagnostics =
editor.eventManager.emitAsync(EventType.queryDocumentDiagnostics)
.getOrNull<DocumentDiagnosticReport>("diagnostics") ?: return@launch

if (diagnostics.isRelatedUnchangedDocumentDiagnosticReport) {
// no-op
return@launch
}

if (diagnostics.isRelatedFullDocumentDiagnosticReport) {
editor.eventManager.emit(EventType.publishDiagnostics) {
put("data", diagnostics.left.items)
}
}
}


}
}

class LspEditorSelectionChangeEventReceiver(private val editor: LspEditor) :
EventReceiver<SelectionChangeEvent> {
override fun onReceive(event: SelectionChangeEvent, unsubscribe: Unsubscribe) {

editor.coroutineScope.launch(Dispatchers.IO) {

if (editor.hitReTrigger(event.editor.text[event.left.index].toString())) {
editor.showSignatureHelp(null)
return@launch
}

editor.eventManager.emitAsync(EventType.signatureHelp, event.left)
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class EventContext {
}

fun <T : Any> getOrNull(key: String): T? {
return data[key] as T?
return data[key] as? T?
}

fun put(key: String, value: Any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PublishDiagnosticsEvent : EventListener {

val lspEditor = context.get<LspEditor>("lsp-editor")
val originEditor = lspEditor.editor ?: return
val data = context.get<List<Diagnostic>>("data")
val data = context.getOrNull<List<Diagnostic>>("data") ?: return

val diagnosticsContainer =
originEditor.diagnostics ?: DiagnosticsContainer()
Expand All @@ -51,7 +51,10 @@ class PublishDiagnosticsEvent : EventListener {
data.transformToEditorDiagnostics(originEditor)
)

originEditor.diagnostics = diagnosticsContainer
// run on ui thread
originEditor.post {
originEditor.diagnostics = diagnosticsContainer
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,10 @@ class QueryDocumentDiagnosticsEvent : AsyncEventListener() {
.diagnostic(
editor.uri.createDocumentDiagnosticParams()
)
.thenApply { either: DocumentDiagnosticReport ->
if (either.isRelatedFullDocumentDiagnosticReport)
either.relatedFullDocumentDiagnosticReport
.relatedDocuments
else
either.relatedUnchangedDocumentDiagnosticReport
.relatedDocuments
}

this.future = future.thenAccept { }

val result = future.await()

if (result.isEmpty()) {
return
}
val result = future.await() ?: return

context.put("diagnostics", result)
}
Expand Down

1 comment on commit b13130a

@HanzoDev1375
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

شرکت اکلیپس اگر بفهمه کد هاشو تبدیل کردی به kt بهت توهین میکنه

Please sign in to comment.