Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language Server Experiments #821

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions features/org.eclipse.pde-feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,11 @@
version="0.0.0"
unpack="false"/>

<plugin
id="org.eclipse.pde.ls"
download-size="0"
install-size="0"
version="0.0.0"
unpack="false"/>

</feature>
7 changes: 7 additions & 0 deletions ui/org.eclipse.pde.ls/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
28 changes: 28 additions & 0 deletions ui/org.eclipse.pde.ls/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.eclipse.pde.ls</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
9 changes: 9 additions & 0 deletions ui/org.eclipse.pde.ls/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
12 changes: 12 additions & 0 deletions ui/org.eclipse.pde.ls/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: PDE Language Server
Bundle-SymbolicName: org.eclipse.pde.ls;singleton:=true
Bundle-Version: 1.0.0.qualifier
Import-Package: aQute.bnd.help;version="2.0.0",
aQute.bnd.properties
Require-Bundle: org.eclipse.lsp4e;bundle-version="0.18.0",
org.eclipse.lsp4j;bundle-version="0.21.1",
org.eclipse.lsp4j.jsonrpc;bundle-version="0.21.1"
Automatic-Module-Name: org.eclipse.pde.ls
Bundle-RequiredExecutionEnvironment: JavaSE-17
5 changes: 5 additions & 0 deletions ui/org.eclipse.pde.ls/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml
17 changes: 17 additions & 0 deletions ui/org.eclipse.pde.ls/plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.lsp4e.languageServer">
<server
class="org.eclipse.pde.ls.BNDLanguageServerClient"
id="org.eclipse.pde.ls.bnd"
label="BND Instructions">
</server>
<contentTypeMapping
contentType="org.eclipse.pde.bndInstructions"
id="org.eclipse.pde.ls.bnd">
</contentTypeMapping>
</extension>

</plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.ls;

import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;

import org.eclipse.lsp4e.server.StreamConnectionProvider;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.launch.LSPLauncher;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.pde.ls.bnd.BNDLanguageServer;

public class BNDLanguageServerClient implements StreamConnectionProvider {

private InputStream input;
private OutputStream output;
private Future<Void> listening;

@Override
public InputStream getErrorStream() {
return null;
}

@Override
public InputStream getInputStream() {
return input;
}

@Override
public OutputStream getOutputStream() {
return output;
}

@Override
public void start() throws IOException {
System.out.println("BNDLanguageServerClient.start()");
BNDLanguageServer server = new BNDLanguageServer();
LinkedBlockingQueue<Integer> inqueue = new LinkedBlockingQueue<>(10 * 1024 * 104);
LinkedBlockingQueue<Integer> outqueue = new LinkedBlockingQueue<>(10 * 1024 * 104);
Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(server, new QueueInputStream(inqueue),
new QueueOutputStream(outqueue));
input = new QueueInputStream(outqueue);
output = new QueueOutputStream(inqueue);
listening = launcher.startListening();
}

@Override
public void stop() {
System.out.println("BNDLanguageServerClient.stop()");
// TODO how to properly shutdown?!?
listening.cancel(true);
try {
input.close();
} catch (IOException e) {
}
try {
output.close();
} catch (IOException e) {
}
}

private static final class QueueInputStream extends InputStream {

private final BlockingQueue<Integer> byteSource;
private boolean closed;

public QueueInputStream(BlockingQueue<Integer> byteSource) {
this.byteSource = byteSource;
}

@Override
public int read() throws IOException {
if (closed) {
return -1;
}
try {
Integer take = byteSource.take();
if (take < 0) {
closed = true;
}
return take;
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
}

@Override
public void close() throws IOException {
closed = true;
}

}

private static final class QueueOutputStream extends OutputStream {

private final BlockingQueue<Integer> byteSink;
private boolean closed;

public QueueOutputStream(BlockingQueue<Integer> byteSink) {
this.byteSink = byteSink;
}

@Override
public void write(int b) throws IOException {
if (closed) {
throw new IOException("closed");
}
try {
byteSink.put(b);
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
}

@Override
public void close() throws IOException {
closed = true;
try {
byteSink.put(-1);
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*******************************************************************************
* Copyright (c) 2023 Christoph Läubrich and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.pde.ls.bnd;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.SemanticTokenModifiers;
import org.eclipse.lsp4j.SemanticTokenTypes;
import org.eclipse.lsp4j.SemanticTokensLegend;
import org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.eclipse.lsp4j.services.WorkspaceService;

//See
// https://github.com/eclipse-lsp4j/lsp4j/blob/main/documentation/README.md#implement-your-language-server
// https://github.com/AlloyTools/org.alloytools.alloy/tree/master/org.alloytools.alloy.lsp
// https://github.com/bndtools/bnd/issues/5833
// https://www.vogella.com/tutorials/EclipseLanguageServer/article.html
// https://github.com/mickaelistria/eclipse-languageserver-demo/tree/master/Le%20LanguageServer%20de%20Chamrousse
public class BNDLanguageServer implements LanguageServer {

private BNDTextDocumentService documentService;
private BNDWorkspaceService workspaceService;
private ExecutorService executorService;

public BNDLanguageServer() {
executorService = Executors.newWorkStealingPool();
documentService = new BNDTextDocumentService(executorService);
workspaceService = new BNDWorkspaceService();
}

@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
final InitializeResult res = new InitializeResult(new ServerCapabilities());
res.getCapabilities().setCompletionProvider(new CompletionOptions(false, List.of()));
res.getCapabilities().setTextDocumentSync(TextDocumentSyncKind.Full);
res.getCapabilities().setSemanticTokensProvider(
new SemanticTokensWithRegistrationOptions(
new SemanticTokensLegend(List.of(SemanticTokenTypes.Property, SemanticTokenTypes.Comment),
List.of(SemanticTokenModifiers.Readonly)),
Boolean.TRUE));
return CompletableFuture.completedFuture(res);
}

@Override
public CompletableFuture<Object> shutdown() {
return CompletableFuture.supplyAsync(() -> {
executorService.shutdown();
return Boolean.TRUE;
});
}

@Override
public void exit() {
executorService.shutdownNow();
try {
executorService.awaitTermination(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

@Override
public TextDocumentService getTextDocumentService() {
return documentService;
}

@Override
public WorkspaceService getWorkspaceService() {
return workspaceService;
}

}
Loading
Loading