44 lines
1.7 KiB
Java
44 lines
1.7 KiB
Java
package p.studio.app.lsp;
|
|
|
|
import p.studio.api.project.ProjectReference;
|
|
import p.studio.core.session.LspLifecyclePort;
|
|
import p.studio.lsp.api.LspServerLifecycle;
|
|
import p.studio.lsp.messages.LspProjectContext;
|
|
import p.studio.lsp.messages.LspServerConfiguration;
|
|
|
|
import java.util.Objects;
|
|
|
|
public final class LspLifecycleAdapter implements LspLifecyclePort {
|
|
private final LspServerLifecycle lifecycle;
|
|
private final java.util.Map<java.nio.file.Path, LspProjectContext> bindings = new java.util.concurrent.ConcurrentHashMap<>();
|
|
|
|
public LspLifecycleAdapter(LspServerLifecycle lifecycle) {
|
|
this.lifecycle = Objects.requireNonNull(lifecycle, "lifecycle");
|
|
}
|
|
|
|
@Override
|
|
public void boot(ProjectReference project) {
|
|
var binding = context(project);
|
|
lifecycle.bootServer(LspServerConfiguration.builder().endpoint(
|
|
new p.studio.lsp.messages.LspServerEndpoint("127.0.0.1", 0)).build(), binding);
|
|
bindings.put(project.rootPath().toAbsolutePath().normalize(), binding);
|
|
}
|
|
|
|
@Override
|
|
public void shutdown(ProjectReference project) {
|
|
var binding = bindings.remove(project.rootPath().toAbsolutePath().normalize());
|
|
if (binding != null) lifecycle.shutdownServer(binding);
|
|
}
|
|
|
|
static LspProjectContext context(ProjectReference project) {
|
|
final var safeProject = Objects.requireNonNull(project, "project");
|
|
final java.nio.file.Path root;
|
|
try { root = safeProject.rootPath().toRealPath(); }
|
|
catch (java.io.IOException failure) { throw new IllegalStateException("Project root unavailable", failure); }
|
|
return new LspProjectContext(
|
|
root.toString(),
|
|
safeProject.languageId(),
|
|
root);
|
|
}
|
|
}
|