implements PLN-0065

This commit is contained in:
2026-05-05 15:50:55 +01:00
parent e5342e34f7
commit b0073cd7bd
32 changed files with 1445 additions and 277 deletions
@@ -1,3 +1,6 @@
plugins {
id("gradle.java-library-conventions")
}
}
dependencies {
}
@@ -0,0 +1,26 @@
package p.studio.lsp.api;
import java.nio.file.Path;
import java.util.Objects;
public record LspProjectContext(
String projectKey,
String languageId,
Path projectRoot) {
public LspProjectContext {
projectKey = requireText(projectKey, "projectKey");
languageId = requireText(languageId, "languageId");
projectRoot = Objects.requireNonNull(projectRoot, "projectRoot").toAbsolutePath().normalize();
}
private static String requireText(
final String value,
final String field) {
final String candidate = Objects.requireNonNull(value, field).trim();
if (candidate.isEmpty()) {
throw new IllegalArgumentException(field + " must not be blank");
}
return candidate;
}
}
@@ -0,0 +1,17 @@
package p.studio.lsp.api;
import java.util.Objects;
public record LspServerBootRequest(
LspProjectContext project,
LspServerConfiguration configuration) {
public LspServerBootRequest {
project = Objects.requireNonNull(project, "project");
configuration = Objects.requireNonNull(configuration, "configuration");
}
public static LspServerBootRequest defaults(final LspProjectContext project) {
return new LspServerBootRequest(project, LspServerConfiguration.defaults());
}
}
@@ -0,0 +1,29 @@
package p.studio.lsp.api;
import java.util.Objects;
public record LspServerConfiguration(
String host,
int port) {
public static final String DEFAULT_HOST = "127.0.0.1";
public static final int DEFAULT_PORT = 7777;
public LspServerConfiguration {
host = requireHost(host);
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("port must be between 0 and 65535");
}
}
public static LspServerConfiguration defaults() {
return new LspServerConfiguration(DEFAULT_HOST, DEFAULT_PORT);
}
private static String requireHost(final String value) {
final String candidate = Objects.requireNonNull(value, "host").trim();
if (candidate.isEmpty()) {
throw new IllegalArgumentException("host must not be blank");
}
return candidate;
}
}
@@ -0,0 +1,19 @@
package p.studio.lsp.api;
import java.util.Objects;
public record LspServerEndpoint(
String host,
int port) {
public LspServerEndpoint {
final String candidate = Objects.requireNonNull(host, "host").trim();
if (candidate.isEmpty()) {
throw new IllegalArgumentException("host must not be blank");
}
if (port < 0 || port > 65535) {
throw new IllegalArgumentException("port must be between 0 and 65535");
}
host = candidate;
}
}
@@ -0,0 +1,13 @@
package p.studio.lsp.api;
import java.util.Objects;
public record LspServerHandle(
LspProjectContext project,
LspServerEndpoint endpoint) {
public LspServerHandle {
project = Objects.requireNonNull(project, "project");
endpoint = Objects.requireNonNull(endpoint, "endpoint");
}
}
@@ -0,0 +1,7 @@
package p.studio.lsp.api;
public interface LspServerLifecycle {
LspServerHandle bootServer(LspServerBootRequest request);
void shutdownServer(LspProjectContext project);
}
@@ -0,0 +1,32 @@
package p.studio.lsp.api;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertTrue;
class LspApiBoundaryTest {
@Test
void apiSourceMustRemainFreeOfLsp4jAndProtocolPackages() throws IOException {
final Path sourceRoot = Path.of("src/main/java");
final List<Path> javaFiles;
try (var stream = Files.walk(sourceRoot)) {
javaFiles = stream
.filter(path -> path.toString().endsWith(".java"))
.toList();
}
for (final Path javaFile : javaFiles) {
final String source = Files.readString(javaFile);
assertTrue(!source.contains("org.eclipse.lsp4j"),
() -> "LSP4J leaked into lsp-api: " + javaFile);
assertTrue(!source.contains(".protocol."),
() -> "Protocol package leaked into lsp-api: " + javaFile);
}
}
}
@@ -0,0 +1,41 @@
package p.studio.lsp.api;
import org.junit.jupiter.api.Test;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class LspServerBootRequestTest {
@Test
void defaultsUseDeterministicLoopbackConfiguration() {
final LspProjectContext project = new LspProjectContext("demo", "pbs", Path.of("."));
final LspServerBootRequest request = LspServerBootRequest.defaults(project);
assertEquals(project, request.project());
assertEquals(LspServerConfiguration.DEFAULT_HOST, request.configuration().host());
assertEquals(LspServerConfiguration.DEFAULT_PORT, request.configuration().port());
}
@Test
void configurationRejectsInvalidValues() {
assertThrows(NullPointerException.class, () -> new LspServerConfiguration(null, 7777));
assertThrows(IllegalArgumentException.class, () -> new LspServerConfiguration(" ", 7777));
assertThrows(IllegalArgumentException.class, () -> new LspServerConfiguration("127.0.0.1", -1));
}
@Test
void projectContextNormalizesAndRejectsBlankFields() {
final LspProjectContext context = new LspProjectContext(" demo ", " pbs ", Path.of("."));
assertEquals("demo", context.projectKey());
assertEquals("pbs", context.languageId());
assertEquals(Path.of(".").toAbsolutePath().normalize(), context.projectRoot());
assertThrows(IllegalArgumentException.class, () -> new LspProjectContext(" ", "pbs", Path.of(".")));
assertThrows(IllegalArgumentException.class, () -> new LspProjectContext("demo", " ", Path.of(".")));
}
}