Skip to content

Commit

Permalink
add LanguageServerWrapper#getProcessHandle()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Aug 10, 2023
1 parent 7c2c002 commit cf359f8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.lsp4e.debug/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Debug Adapter client for Eclipse IDE (Incubation)
Bundle-SymbolicName: org.eclipse.lsp4e.debug;singleton:=true
Bundle-Version: 0.15.2.qualifier
Bundle-Version: 0.15.3.qualifier
Bundle-Activator: org.eclipse.lsp4e.debug.DSPPlugin
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public DSPProcess(DSPDebugTarget dspDebugTarget, ProcessEventArguments args) {
}

@Override
@SuppressWarnings("unchecked")
public <T> T getAdapter(Class<T> adapter) {
if (adapter.isInstance(handle)) {
return (T) handle.orElse(null);
}
return target.getAdapter(adapter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
package org.eclipse.lsp4e.test;

import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Collection;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void testConnect() throws Exception {
waitForAndAssertCondition(2_000, () -> wrapper.isActive());

// e.g. LanguageServerWrapper@69fe8c75 [serverId=org.eclipse.lsp4e.test.server-with-multi-root-support, initialPath=null, initialProject=P/LanguageServerWrapperTestProject11691664858710, isActive=true]
assertTrue(wrapper.toString().matches("LanguageServerWrapper@[0-9a-f]+ \\[serverId=org.eclipse.lsp4e.test.server-with-multi-root-support, initialPath=null, initialProject=P\\/LanguageServerWrapperTestProject1[0-9]+, isActive=true\\]"));
assertTrue(wrapper.toString().matches("LanguageServerWrapper@[0-9a-f]+ \\[serverId=org.eclipse.lsp4e.test.server-with-multi-root-support, initialPath=null, initialProject=P\\/LanguageServerWrapperTestProject1[0-9]+, isActive=true, pid=(null|[0-9])+\\]"));

assertTrue(wrapper.isConnectedTo(testFile1.getLocationURI()));
assertTrue(wrapper.isConnectedTo(testFile2.getLocationURI()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
Expand Down Expand Up @@ -346,6 +347,11 @@ private CompletableFuture<InitializeResult> initServer(final URI rootURI) {
return languageServer.initialize(initParams);
}

@Nullable
public ProcessHandle getProcessHandle() {
return Adapters.adapt(lspStreamProvider, ProcessHandle.class);
}

private ClientInfo getClientInfo(String name) {
String pluginVersion = Platform.getBundle(LanguageServerPlugin.PLUGIN_ID).getVersion().toString();
final var clientInfo = new ClientInfo(name, pluginVersion);
Expand Down Expand Up @@ -1011,11 +1017,13 @@ public boolean canOperate(@NonNull IDocument document) {

@Override
public String toString() {
final var ph = getProcessHandle();
return getClass().getSimpleName() + '@' + Integer.toHexString(System.identityHashCode(this)) //
+ " [serverId=" + serverDefinition.id //$NON-NLS-1$
+ ", initialPath=" + initialPath //$NON-NLS-1$
+ ", initialProject=" + initialProject //$NON-NLS-1$
+ ", isActive=" + isActive() //$NON-NLS-1$
+ ", pid=" + (ph == null ? null : ph.pid()) //$NON-NLS-1$
+ ']';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.core.DebugException;
Expand All @@ -44,7 +45,7 @@
* Access and control IO streams from a Launch Configuration to connect
* them to language server protocol client.
*/
public class LaunchConfigurationStreamProvider implements StreamConnectionProvider {
public class LaunchConfigurationStreamProvider implements StreamConnectionProvider, IAdaptable {

private StreamProxyInputStream inputStream;
private StreamProxyInputStream errorStream;
Expand Down Expand Up @@ -206,6 +207,14 @@ private void setStatusHandler(boolean enabled) {
IInternalDebugCoreConstants.PREF_ENABLE_STATUS_HANDLERS, enabled, null);
}

@Override
public <T> T getAdapter(Class<T> adapter) {
if(adapter == ProcessHandle.class) {
return process.getAdapter(adapter);
}
return null;
}

@Override
public InputStream getInputStream() {
return this.inputStream;
Expand Down Expand Up @@ -254,5 +263,4 @@ public void stop() {
this.outputStream = null;
this.errorStream = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.time.OffsetDateTime;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jface.preference.IPreferenceStore;
Expand All @@ -37,7 +39,7 @@
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class LoggingStreamConnectionProviderProxy implements StreamConnectionProvider {
public class LoggingStreamConnectionProviderProxy implements StreamConnectionProvider, IAdaptable {

public static File getLogDirectory() {
IPath root = ResourcesPlugin.getWorkspace().getRoot().getLocation();
Expand Down Expand Up @@ -159,6 +161,14 @@ public int read(byte[] b, int off, int len) throws IOException {
return inputStream;
}

@Override
public <T> T getAdapter(Class<T> adapter) {
if(adapter == ProcessHandle.class) {
return Adapters.adapt(provider, adapter);
}
return null;
}

@Override
public InputStream getErrorStream() {
if (errorStream != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import java.util.List;
import java.util.Objects;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.annotation.Nullable;

/**
*
* @since 0.1.0
*/
public abstract class ProcessStreamConnectionProvider implements StreamConnectionProvider {
public abstract class ProcessStreamConnectionProvider implements StreamConnectionProvider, IAdaptable {

private @Nullable Process process;
private List<String> commands;
Expand Down Expand Up @@ -93,6 +94,20 @@ public void stop() {
}
}

@Override
@SuppressWarnings("unchecked")
public <T> T getAdapter(Class<T> adapter) {
final var process = this.process;
if(adapter == ProcessHandle.class) {
try {
return process == null ? null : (T) process.toHandle();
} catch(UnsupportedOperationException ex) {
// ignore
}
}
return null;
}

protected List<String> getCommands() {
return commands;
}
Expand Down Expand Up @@ -129,5 +144,4 @@ public String toString() {
return "ProcessStreamConnectionProvider [commands=" + this.getCommands() + ", workingDir=" //$NON-NLS-1$//$NON-NLS-2$
+ this.getWorkingDirectory() + "]"; //$NON-NLS-1$
}

}

0 comments on commit cf359f8

Please sign in to comment.