Skip to content

Commit

Permalink
Support dependencies.json config
Browse files Browse the repository at this point in the history
  • Loading branch information
Enaium committed Dec 22, 2024
1 parent 6b2fd10 commit 3962e67
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 18 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
- [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=enaium.jimmer-dto-lsp-vscode): Install the
extension from the marketplace
- [IntelliJ IDEA](https://plugins.jetbrains.com/plugin/26045-jimmer-dto-lsp): Install the plugin from the marketplace
- Eclipse: First install new software [LSP4E](https://download.eclipse.org/lsp4e/releases/latest/) and then move the plugin
- Eclipse: First install new software [LSP4E](https://download.eclipse.org/lsp4e/releases/latest/) and then move the
plugin
to the `dropins` folder
- Other IDEs: Install the LSP server from the release page

Expand All @@ -64,4 +65,30 @@
- `target/classes` Maven Java or Kotlin
- `build/tmp/kotlin-classes/debug` Gradle Android Kotlin
- `build/intermediates/javac/debug/classes` Gradle Android Java
- `build/intermediates/javac/debug/compileDebugJavaWithJavac/classes` Gradle Android Java
- `build/intermediates/javac/debug/compileDebugJavaWithJavac/classes` Gradle Android Java

If you want to add a new jar classpath, you can add it to the `dependencies.json` file in the LSP server
directory(`<user>/jimmer-dto-lsp`).

In Windows your path should be like this:

```json
{
"x:\\path\\to\\your\\jar": [
"C:\\path\\to\\your\\jar"
]
}
```

In Linux or MacOS your path should be like this:

```json
{
"/path/to/your/jar": [
"/path/to/your/jar"
]
}
```

If you want to add a new jar classpath automatically, you can use
the [jimmer-gradle](https://github.com/Enaium/jimmer-gradle) plugin.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
version=1.3.1
version=1.4.0
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ intellij = "2.1.0"
lsp4j = "0.23.1"
jimmer = "0.9.34"
shadow = "9.0.0-beta2"
jakartaValidation = "3.1.0"
jackson = "2.18.2"

[libraries]
lsp4j = { module = "org.eclipse.lsp4j:org.eclipse.lsp4j", version.ref = "lsp4j" }
Expand All @@ -13,7 +13,7 @@ jimmer-core = { module = "org.babyfish.jimmer:jimmer-core", version.ref = "jimme
jimmer-coreKotlin = { module = "org.babyfish.jimmer:jimmer-core-kotlin", version.ref = "jimmer" }
jimmer-sql = { module = "org.babyfish.jimmer:jimmer-sql", version.ref = "jimmer" }
jimmer-sqlKotlin = { module = "org.babyfish.jimmer:jimmer-sql-kotlin", version.ref = "jimmer" }
jakarta-validation = { module = "jakarta.validation:jakarta.validation-api", version.ref = "jakartaValidation" }
jackson = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
Expand Down
2 changes: 2 additions & 0 deletions intellij/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</description>
<change-notes>
<![CDATA[<h1>Change notes</h1>
<p><b>1.4.0</b></p>
<ul><li>Support 'dependencies.json' config</li></ul>
<p><b>1.3.1</b></p>
<ul>
<li>Fix completion in the parenthesis</li>
Expand Down
7 changes: 7 additions & 0 deletions scripts/embed.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
param ($Jar = $( throw "Jar is required" ))

$Location = Get-Location

Copy-Item $Jar "$Location/vscode/out/server.jar"
Copy-Item $Jar "$Location/intellij/src/main/resources/server.jar"
Copy-Item $Jar "$Location/eclipse/src/main/resources/server.jar"
18 changes: 18 additions & 0 deletions scripts/vscode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
param ($Jar = $( throw "Jar is required" ), $Version = $( throw "Version is required" ))

Copy-Item $Jar "${env:USERPROFILE}/.vscode/extensions/enaium.jimmer-dto-lsp-vscode-$Version/out/server.jar"

$VSCode

try {
$VSCode = Get-Process code -ErrorAction Stop
} catch {
$VSCode = $null
}

if ($null -ne $VSCode)
{
Stop-Process $VSCode
}

Start-Process code -NoNewWindow
23 changes: 22 additions & 1 deletion server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies {
implementation(libs.jimmer.coreKotlin)
implementation(libs.jimmer.sql)
implementation(libs.jimmer.sqlKotlin)
implementation(libs.jakarta.validation)
implementation(libs.jackson)

testImplementation(kotlin("test"))
implementation(kotlin("reflect"))
Expand All @@ -33,4 +33,25 @@ tasks.shadowJar {
tasks.jar {
archiveBaseName = archiveName
dependsOn(tasks.shadowJar)
}

tasks.register<Exec>("runVSCode") {
workingDir = rootProject.rootDir
commandLine(
"powershell",
"scripts/vscode.ps1",
tasks.shadowJar.get().archiveFile.get().asFile.absolutePath,
version
)
dependsOn(tasks.shadowJar)
}

tasks.register<Exec>("embed") {
workingDir = rootProject.rootDir
commandLine(
"powershell",
"scripts/embed.ps1",
tasks.shadowJar.get().archiveFile.get().asFile.absolutePath
)
dependsOn(tasks.shadowJar)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package cn.enaium.jimmer.dto.lsp.service

import cn.enaium.jimmer.dto.lsp.DocumentManager
import cn.enaium.jimmer.dto.lsp.Main
import cn.enaium.jimmer.dto.lsp.compiler.Context
import cn.enaium.jimmer.dto.lsp.compiler.ImmutableProp
import cn.enaium.jimmer.dto.lsp.compiler.ImmutableType
Expand All @@ -36,7 +35,6 @@ import org.eclipse.lsp4j.jsonrpc.messages.Either
import java.nio.file.Path
import java.util.concurrent.CompletableFuture
import kotlin.collections.set
import kotlin.io.path.toPath

/**
* @author Enaium
Expand Down Expand Up @@ -356,11 +354,7 @@ class DocumentCompletionService(documentManager: DocumentManager) : DocumentServ

private fun findAnnotationNames(context: Context, classpath: List<Path> = emptyList()): List<String> {
val results = mutableListOf<String>()
findClassNames(
listOf(
Main::class.java.protectionDomain.codeSource.location.toURI().toPath()
) + classpath
).forEach { name ->
findClassNames(classpath).forEach { name ->
context.loader[name]?.run {
if (this.isAnnotation) {
results.add(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import cn.enaium.jimmer.dto.lsp.compiler.Context
import cn.enaium.jimmer.dto.lsp.compiler.DocumentDtoCompiler
import cn.enaium.jimmer.dto.lsp.compiler.ImmutableType
import cn.enaium.jimmer.dto.lsp.utility.findClasspath
import cn.enaium.jimmer.dto.lsp.utility.findDependencies
import cn.enaium.jimmer.dto.lsp.utility.findProjectDir
import cn.enaium.jimmer.dto.lsp.utility.toFile
import org.antlr.v4.runtime.*
Expand Down Expand Up @@ -66,12 +67,14 @@ class DocumentSyncService(private val workspaceFolders: MutableSet<String>, docu
val projectDir = findProjectDir(URI.create(uri).toPath())
val classpath = mutableListOf<Path>()

projectDir?.run {
findClasspath(this, classpath)
projectDir?.also {
findClasspath(it, classpath)
classpath += findDependencies(it)
} ?: run {
workspaceFolders.forEach workspaceFolder@{ workspaceFolder ->
val path = URI.create(workspaceFolder).toPath()
findClasspath(path, classpath)
classpath += findDependencies(path)
}
}
val context = Context(URLClassLoader(classpath.map { it.toUri().toURL() }.toTypedArray()))
Expand Down
24 changes: 24 additions & 0 deletions server/src/main/kotlin/cn/enaium/jimmer/dto/lsp/utility/utility.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package cn.enaium.jimmer.dto.lsp.utility

import cn.enaium.jimmer.dto.lsp.Main
import com.fasterxml.jackson.databind.ObjectMapper
import org.antlr.v4.runtime.Token
import org.babyfish.jimmer.dto.compiler.Constants
import org.babyfish.jimmer.dto.compiler.DtoLexer
Expand Down Expand Up @@ -76,6 +78,28 @@ fun findClasspath(path: Path, results: MutableList<Path>) {
}
}

fun findDependencies(project: Path): MutableList<Path> {
val results = mutableListOf<Path>()
val lspHome = Main::class.java.protectionDomain.codeSource.location.toURI().toPath().parent
lspHome.resolve("dependencies.json").takeIf { it.exists() }?.also {
val dependenciesJson = ObjectMapper().readTree(it.toFile())
dependenciesJson[project.absolutePathString().let { projectPath ->
if (projectPath.first() != '/') {
projectPath.first().uppercase() + projectPath.substring(1)
} else {
projectPath
}
}]?.forEach { dependency ->
Path(dependency.asText()).also { path ->
if (path.exists()) {
results.add(path)
}
}
}
}
return results
}

fun findProjectDir(dtoPath: Path): Path? {
var parent = dtoPath.parent
while (parent != null) {
Expand Down
2 changes: 1 addition & 1 deletion vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Jimmer DTO LSP",
"publisher": "enaium",
"author": "Enaium",
"version": "1.3.1",
"version": "1.4.0",
"description": "A language server for Jimmer DTO",
"icon": "icons/logo.png",
"main": "out/extensions",
Expand Down
16 changes: 14 additions & 2 deletions vscode/src/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/

import path = require("path");
import * as path from "path";
import * as fs from "fs";
import * as os from "os";
import { ExtensionContext, window } from "vscode";

import {
Expand All @@ -24,10 +26,20 @@ import {
TransportKind,
} from "vscode-languageclient/node";

const serverJar = path.join(__dirname, "server.jar");
const serverJar = path.join(os.homedir(), "jimmer-dto-lsp", "server.jar");
const embedJar = path.join(__dirname, "server.jar");
let client: LanguageClient;

export function activate(context: ExtensionContext) {
if (fs.existsSync(serverJar)) {
try {
fs.unlinkSync(serverJar);
} catch (e) {}
}

fs.mkdirSync(path.join(os.homedir(), "jimmer-dto-lsp"), { recursive: true });
fs.copyFileSync(embedJar, serverJar);

const serverOptions: ServerOptions = {
command: "java",
args: ["-cp", serverJar, "cn.enaium.jimmer.dto.lsp.MainKt"],
Expand Down

0 comments on commit 3962e67

Please sign in to comment.