Skip to content

Commit

Permalink
fix: fixes #73 (and some ktlint violations)
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Addazi <[email protected]>
  • Loading branch information
loradd committed Mar 11, 2024
1 parent 8d8e85d commit e7c474a
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 96 deletions.
7 changes: 6 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
version=1.0.4-SNAPSHOT
version=1.0.4-SNAPSHOT
kotlinVersion=1.8.22
kolasuVersion=1.5.45
lsp4jVersion=0.21.1
luceneVersion=9.8.0
junitVersion=5.7.1
17 changes: 13 additions & 4 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ repositories {
mavenCentral()
}

val kotlinVersion: String by project
val kolasuVersion: String by project
val lsp4jVersion: String by project
val luceneVersion: String by project

dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.5.21")
implementation("com.strumenta.kolasu:kolasu-core:1.5.45")
implementation("org.eclipse.lsp4j:org.eclipse.lsp4j:0.21.1")
implementation("org.apache.lucene:lucene-core:9.8.0")
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
implementation("com.strumenta.kolasu:kolasu-core:$kolasuVersion")
implementation("org.eclipse.lsp4j:org.eclipse.lsp4j:$lsp4jVersion")
implementation("org.apache.lucene:lucene-core:$luceneVersion")
}

java {
Expand Down Expand Up @@ -78,3 +83,7 @@ publishing {
signing {
sign(publishing.publications.getByName("language-server-library"))
}

ktlint {
version = "1.2.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ open class KolasuServer<T : Node>(
protected open val extensions: List<String> = listOf(),
protected open val enableDefinitionCapability: Boolean = false,
protected open val enableReferencesCapability: Boolean = false,
protected open val generator: CodeGenerator<T>? = null
protected open val generator: CodeGenerator<T>? = null,
) : LanguageServer, TextDocumentService, WorkspaceService, LanguageClientAware {

protected open lateinit var client: LanguageClient
protected open var configuration: JsonObject = JsonObject()
protected open var traceLevel: String = "off"
Expand All @@ -112,9 +111,13 @@ open class KolasuServer<T : Node>(
protected open val uuid = mutableMapOf<Node, String>()

override fun getTextDocumentService() = this

override fun getWorkspaceService() = this

open fun startCommunication(inputStream: InputStream = System.`in`, outputStream: OutputStream = System.out) {
open fun startCommunication(
inputStream: InputStream = System.`in`,
outputStream: OutputStream = System.out,
) {
val launcher = LSPLauncher.createServerLauncher(this, inputStream, outputStream)
connect(launcher.remoteProxy)
launcher.startListening()
Expand All @@ -134,9 +137,20 @@ open class KolasuServer<T : Node>(

val capabilities = ServerCapabilities()

capabilities.workspace = WorkspaceServerCapabilities(WorkspaceFoldersOptions().apply { supported = true; changeNotifications = Either.forLeft("didChangeWorkspaceFoldersRegistration"); })

capabilities.setTextDocumentSync(TextDocumentSyncOptions().apply { openClose = true; change = TextDocumentSyncKind.Full })
capabilities.workspace =
WorkspaceServerCapabilities(
WorkspaceFoldersOptions().apply {
supported = true
changeNotifications = Either.forLeft("didChangeWorkspaceFoldersRegistration")
},
)

capabilities.setTextDocumentSync(
TextDocumentSyncOptions().apply {
openClose = true
change = TextDocumentSyncKind.Full
},
)
capabilities.setDocumentSymbolProvider(true)
capabilities.setDefinitionProvider(this.enableDefinitionCapability)
capabilities.setReferencesProvider(this.enableReferencesCapability)
Expand All @@ -149,9 +163,31 @@ open class KolasuServer<T : Node>(
for (folder in folders) {
watchers.add(FileSystemWatcher(Either.forLeft(URI(folder).path + """/**/*{${extensions.joinToString(","){".$it"}}}""")))
}
client.registerCapability(RegistrationParams(listOf(Registration("workspace/didChangeWatchedFiles", "workspace/didChangeWatchedFiles", DidChangeWatchedFilesRegistrationOptions(watchers)))))

client.registerCapability(RegistrationParams(listOf(Registration("workspace/didChangeConfiguration", "workspace/didChangeConfiguration", object { val section = language }))))
client.registerCapability(
RegistrationParams(
listOf(
Registration(
"workspace/didChangeWatchedFiles",
"workspace/didChangeWatchedFiles",
DidChangeWatchedFilesRegistrationOptions(watchers),
),
),
),
)

client.registerCapability(
RegistrationParams(
listOf(
Registration(
"workspace/didChangeConfiguration",
"workspace/didChangeConfiguration",
object {
val section = language
},
),
),
),
)
}

override fun didChangeConfiguration(params: DidChangeConfigurationParams?) {
Expand All @@ -167,7 +203,9 @@ open class KolasuServer<T : Node>(
commitIndex()

client.createProgress(WorkDoneProgressCreateParams(Either.forLeft("indexing")))
client.notifyProgress(ProgressParams(Either.forLeft("indexing"), Either.forLeft(WorkDoneProgressBegin().apply { title = "indexing" })))
client.notifyProgress(
ProgressParams(Either.forLeft("indexing"), Either.forLeft(WorkDoneProgressBegin().apply { title = "indexing" })),
)
for (folder in folders) {
val projectFiles = File(URI(folder)).walk().filter { extensions.contains(it.extension) }.toList()
val totalBytes = projectFiles.sumOf { it.length() }
Expand All @@ -176,7 +214,16 @@ open class KolasuServer<T : Node>(
parse(file.toURI().toString(), Files.readString(file.toPath()))
parsedBytes += file.length()
val percentage = (parsedBytes * 100 / totalBytes).toInt()
client.notifyProgress(ProgressParams(Either.forLeft("indexing"), Either.forLeft(WorkDoneProgressReport().apply { this.percentage = percentage })))
client.notifyProgress(
ProgressParams(
Either.forLeft("indexing"),
Either.forLeft(
WorkDoneProgressReport().apply {
this.percentage = percentage
},
),
),
)
}
}
client.notifyProgress(ProgressParams(Either.forLeft("indexing"), Either.forLeft(WorkDoneProgressEnd())))
Expand Down Expand Up @@ -208,7 +255,10 @@ open class KolasuServer<T : Node>(
parse(uri, text)
}

open fun parse(uri: String, text: String) {
open fun parse(
uri: String,
text: String,
) {
if (!::indexWriter.isInitialized) return

val parsingResult = parser?.parse(text) ?: return
Expand Down Expand Up @@ -270,11 +320,27 @@ open class KolasuServer<T : Node>(
if (node.children.isNotEmpty() || node.position == null) continue

if (showASTWarnings && tree.findByPosition(node.position!!) != node) {
diagnostics.add(Diagnostic(toLSPRange(node.position!!), "Leaf type: ${node.simpleNodeType} but findByPositionType: ${tree.findByPosition(node.position!!)?.simpleNodeType}").apply { severity = DiagnosticSeverity.Warning })
diagnostics.add(
Diagnostic(
toLSPRange(node.position!!),
"Leaf type: ${node.simpleNodeType} but findByPositionType: ${tree.findByPosition(
node.position!!,
)?.simpleNodeType}",
).apply {
severity = DiagnosticSeverity.Warning
},
)
}

if (showLeafPositions) {
diagnostics.add(Diagnostic(toLSPRange(node.position!!), "Leaf position: ${node.position}, Source text: ${node.sourceText}").apply { severity = DiagnosticSeverity.Information })
diagnostics.add(
Diagnostic(
toLSPRange(node.position!!),
"Leaf position: ${node.position}, Source text: ${node.sourceText}",
).apply {
severity = DiagnosticSeverity.Information
},
)
}
}
}
Expand All @@ -301,7 +367,10 @@ open class KolasuServer<T : Node>(
return CompletableFuture.completedFuture(mutableListOf(Either.forRight(namedTree)))
}

protected open fun appendNamedChildren(node: Node, parent: DocumentSymbol) {
protected open fun appendNamedChildren(
node: Node,
parent: DocumentSymbol,
) {
var nextParent = parent
if (node is PossiblyNamed && node.name != null) {
val range = toLSPRange(node.position!!)
Expand All @@ -322,11 +391,17 @@ open class KolasuServer<T : Node>(
return SymbolKind.Variable
}

override fun definition(params: DefinitionParams?): CompletableFuture<Either<MutableList<out Location>, MutableList<out LocationLink>>> {
override fun definition(
params: DefinitionParams?,
): CompletableFuture<Either<MutableList<out Location>, MutableList<out LocationLink>>> {
val document = getDocument(params) ?: return CompletableFuture.completedFuture(null)

val symbolID = document.fields.find { it.name() == "reference" }?.stringValue() ?: return CompletableFuture.completedFuture(null)
val result = indexSearcher.search(TermQuery(Term("uuid", symbolID)), 1).scoreDocs.firstOrNull() ?: return CompletableFuture.completedFuture(null)
val result =
indexSearcher.search(
TermQuery(Term("uuid", symbolID)),
1,
).scoreDocs.firstOrNull() ?: return CompletableFuture.completedFuture(null)
val definition = indexSearcher.storedFields().document(result.doc)

if (definition.fields.none { it.name() == "startLine" }) return CompletableFuture.completedFuture(null)
Expand All @@ -348,7 +423,11 @@ open class KolasuServer<T : Node>(
}

if (params?.context?.isIncludeDeclaration == true) {
val result = indexSearcher.search(TermQuery(Term("uuid", symbolID)), 1).scoreDocs.firstOrNull() ?: return CompletableFuture.completedFuture(null)
val result =
indexSearcher.search(
TermQuery(Term("uuid", symbolID)),
1,
).scoreDocs.firstOrNull() ?: return CompletableFuture.completedFuture(null)
val definition = indexSearcher.storedFields().document(result.doc)

list.add(toLSPLocation(definition))
Expand All @@ -362,13 +441,14 @@ open class KolasuServer<T : Node>(
val uri = params?.textDocument?.uri ?: return null
val position = params.position

val query = BooleanQuery.Builder()
.add(TermQuery(Term("uri", uri)), BooleanClause.Occur.MUST)
.add(IntPoint.newExactQuery("startLine", position.line + 1), BooleanClause.Occur.MUST)
.add(IntPoint.newExactQuery("endLine", position.line + 1), BooleanClause.Occur.MUST)
.add(IntPoint.newRangeQuery("startColumn", Int.MIN_VALUE, position.character), BooleanClause.Occur.MUST)
.add(IntPoint.newRangeQuery("endColumn", position.character, Int.MAX_VALUE), BooleanClause.Occur.MUST)
.build()
val query =
BooleanQuery.Builder()
.add(TermQuery(Term("uri", uri)), BooleanClause.Occur.MUST)
.add(IntPoint.newExactQuery("startLine", position.line + 1), BooleanClause.Occur.MUST)
.add(IntPoint.newExactQuery("endLine", position.line + 1), BooleanClause.Occur.MUST)
.add(IntPoint.newRangeQuery("startColumn", Int.MIN_VALUE, position.character), BooleanClause.Occur.MUST)
.add(IntPoint.newRangeQuery("endColumn", position.character, Int.MAX_VALUE), BooleanClause.Occur.MUST)
.build()

val sortingField = SortedNumericSortField("size", SortField.Type.INT, true)
val results = indexSearcher.search(query, 100, Sort(sortingField))
Expand All @@ -386,7 +466,11 @@ open class KolasuServer<T : Node>(

protected open fun toLSPLocation(document: Document): Location {
val uri = document.get("uri")
val range = Range(Position(document.get("startLine").toInt() - 1, document.get("startColumn").toInt()), Position(document.get("endLine").toInt() - 1, document.get("endColumn").toInt()))
val range =
Range(
Position(document.get("startLine").toInt() - 1, document.get("startColumn").toInt()),
Position(document.get("endLine").toInt() - 1, document.get("endColumn").toInt()),
)
return Location(uri, range)
}

Expand Down Expand Up @@ -446,21 +530,32 @@ open class KolasuServer<T : Node>(
exitProcess(0)
}

protected open fun log(text: String, verboseExplanation: String? = null) {
protected open fun log(
text: String,
verboseExplanation: String? = null,
) {
client.logTrace(LogTraceParams(text, verboseExplanation))
}

protected open fun showNotification(text: String, messageType: MessageType = MessageType.Info) {
protected open fun showNotification(
text: String,
messageType: MessageType = MessageType.Info,
) {
client.showMessage(MessageParams(messageType, text))
}

protected open fun askClient(messageText: String, options: List<String> = listOf("Yes", "No"), messageType: MessageType = MessageType.Info): CompletableFuture<String> {
protected open fun askClient(
messageText: String,
options: List<String> = listOf("Yes", "No"),
messageType: MessageType = MessageType.Info,
): CompletableFuture<String> {
val future = CompletableFuture<String>()

val request = ShowMessageRequestParams(options.map { MessageActionItem(it) }).apply {
type = messageType
message = messageText
}
val request =
ShowMessageRequestParams(options.map { MessageActionItem(it) }).apply {
type = messageType
message = messageText
}

client.showMessageRequest(request).thenApply { item -> future.complete(item.title) }

Expand All @@ -475,9 +570,15 @@ open class KolasuServer<T : Node>(
}

interface CodeGenerator<T : Node> {
fun generate(tree: T, uri: String)
fun generate(
tree: T,
uri: String,
)
}

interface SymbolResolver {
fun resolveSymbols(tree: Node?, uri: String)
fun resolveSymbols(
tree: Node?,
uri: String,
)
}
25 changes: 24 additions & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@ plugins {
id("maven-publish")
id("java-gradle-plugin")
id("com.gradle.plugin-publish") version "1.2.1"
id("com.github.gmazzo.buildconfig") version "5.3.5"
}

repositories {
mavenCentral()
gradlePluginPortal()
}

val kotlinVersion: String by project
val kolasuVersion: String by project
val luceneVersion: String by project
val lsp4jVersion: String by project
val junitVersion: String by project

dependencies {
implementation("com.github.johnrengelman.shadow:com.github.johnrengelman.shadow.gradle.plugin:7.1.2")
implementation("org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.6.0-M1")
implementation("org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:$kotlinVersion")
}

gradlePlugin {
Expand All @@ -30,3 +37,19 @@ gradlePlugin {
}
}
}

buildConfig {
packageName = "com.strumenta.kolasu.languageserver.plugin"
buildConfigField("KOLASU_LSP_VERSION", "${project.version}")
buildConfigField("KOLASU_VERSION", kolasuVersion)
buildConfigField("LUCENE_VERSION", luceneVersion)
buildConfigField("LSP4J_VERSION", lsp4jVersion)
buildConfigField("JUNIT_VERSION", junitVersion)
}

ktlint {
version = "1.2.1"
filter {
exclude("**/generated/**")
}
}
Loading

0 comments on commit e7c474a

Please sign in to comment.