Skip to content

Commit

Permalink
Merge pull request #5742 from sdedic/lsp/dap-lsp-commonlookup
Browse files Browse the repository at this point in the history
DAP and LSP use a common Lookup.
  • Loading branch information
sdedic authored Mar 31, 2023
2 parents d5c444f + 8a59dc3 commit b3cf9b9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.eclipse.lsp4j.debug.launch.DSPLauncher;
Expand All @@ -33,7 +32,6 @@
import org.netbeans.modules.java.lsp.server.LspSession;
import org.netbeans.modules.java.lsp.server.progress.OperationContext;

import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.Pair;
import org.openide.util.lookup.AbstractLookup;
Expand All @@ -55,7 +53,7 @@ public static NbProtocolServer startDebugger(Pair<InputStream, OutputStream> io,
NbProtocolServer server = new NbProtocolServer(context);

Launcher<IDebugProtocolClient> serverLauncher = DSPLauncher.createServerLauncher(
server, io.first(), io.second(), null, ConsumeWithLookup::new);
server, io.first(), io.second(), null, (d) -> new ConsumeWithLookup(d, session));
context.setClient(serverLauncher.getRemoteProxy());
Future<Void> runningServer = serverLauncher.startListening();
server.setRunningFuture(runningServer);
Expand All @@ -64,22 +62,32 @@ public static NbProtocolServer startDebugger(Pair<InputStream, OutputStream> io,

private static class ConsumeWithLookup implements MessageConsumer {
private final MessageConsumer delegate;
private final LspSession lspSession;
private OperationContext topContext;
private Lookup debugSessionLookup;

public ConsumeWithLookup(MessageConsumer delegate) {
public ConsumeWithLookup(MessageConsumer delegate, LspSession session) {
this.delegate = delegate;
this.lspSession = session;
}

@Override
public void consume(Message message) throws MessageIssueException, JsonRpcException {
InstanceContent ic = new InstanceContent();
ProxyLookup ll = new ProxyLookup(new AbstractLookup(ic), Lookup.getDefault());
// HACK: piggyback on LSP's client.
Lookup ll = debugSessionLookup;
final OperationContext ctx;
if (ll == null) {
ll = new ProxyLookup(new AbstractLookup(ic), lspSession.getLookup());
synchronized (this) {
if (debugSessionLookup == null) {
debugSessionLookup = ll;
}
}
// HACK: piggyback on LSP's client.
}
if (topContext == null) {
topContext = OperationContext.find(null);
topContext = OperationContext.find(lspSession.getLookup());
}
final OperationContext ctx;

if (topContext != null) {
ctx = topContext.operationContext();
ctx.disableCancels();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import org.netbeans.spi.debugger.ui.DebuggingView;
import org.netbeans.spi.debugger.ui.DebuggingView.DVFrame;
import org.netbeans.spi.debugger.ui.DebuggingView.DVThread;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class NbLspServer implements LspSession.ScheduledServer {

@Override
public Lookup getServerLookup() {
return impl.getSessionLookup();
return impl.getSessionOnlyLookup();
}

public TextDocumentService getTextDocumentService() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static boolean isClientResponseThread(NbCodeLanguageClient client) {
}

public static NbLspServer launchServer(Pair<InputStream, OutputStream> io, LspSession session) {
LanguageServerImpl server = new LanguageServerImpl();
LanguageServerImpl server = new LanguageServerImpl(session);
ConsumeWithLookup msgProcessor = new ConsumeWithLookup(server.getSessionLookup());
Launcher<NbCodeLanguageClient> serverLauncher = createLauncher(server, io, msgProcessor::attachLookup);
NbCodeLanguageClient remote = serverLauncher.getRemoteProxy();
Expand Down Expand Up @@ -357,8 +357,9 @@ public static class LanguageServerImpl implements LanguageServer, LanguageClient
private final TextDocumentServiceImpl textDocumentService = new TextDocumentServiceImpl(this);
private final WorkspaceServiceImpl workspaceService = new WorkspaceServiceImpl(this);
private final InstanceContent sessionServices = new InstanceContent();
private final AbstractLookup sessionOnly = new AbstractLookup(sessionServices);
private final Lookup sessionLookup = new ProxyLookup(
new AbstractLookup(sessionServices),
sessionOnly,
Lookup.getDefault()
);

Expand Down Expand Up @@ -401,9 +402,24 @@ public static class LanguageServerImpl implements LanguageServer, LanguageClient
private final List<FileObject> acceptedWorkspaceFolders = new ArrayList<>();

private final OpenedDocuments openedDocuments = new OpenedDocuments();

private final LspSession lspSession;

LanguageServerImpl(LspSession session) {
this.lspSession = session;
}

Lookup getSessionLookup() {
return sessionLookup;
private Lookup getSessionLookup() {
return lspSession.getLookup();
}

/**
* Returns a Lookup specific for this LSP server's session. Does not include the default Lookup contents,
* it is suitable for composing into a ProxyLookup with other parts + the default one.
* @return
*/
Lookup getSessionOnlyLookup() {
return sessionOnly;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
Expand Down Expand Up @@ -188,6 +189,7 @@
import org.netbeans.spi.project.ProjectFactory;
import org.netbeans.spi.project.ProjectState;
import org.netbeans.spi.project.ui.ProjectOpenedHook;
import org.openide.DialogDisplayer;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.LineCookie;
import org.openide.filesystems.FileObject;
Expand Down Expand Up @@ -219,6 +221,40 @@ public ServerTest(String name) {
super(name);
}

private static final String COMMAND_EXTRACT_LOOKUP = "test.lookup.extract"; // NOI8N

static volatile ServerLookupExtractionCommand extractCommand = null;

@ServiceProvider(service = CodeActionsProvider.class)
public static class ServerLookupExtractionCommand extends CodeActionsProvider {
volatile Lookup serverLookup;
volatile Lookup commandLookup;

public ServerLookupExtractionCommand() {
extractCommand = this;
}

@Override
public List<CodeAction> getCodeActions(ResultIterator resultIterator, CodeActionParams params) throws Exception {
return Collections.emptyList();
}

@Override
public Set<String> getCommands() {
// this is called during server's initialization.
serverLookup = Lookup.getDefault();
return Collections.singleton(COMMAND_EXTRACT_LOOKUP);
}

@Override
public CompletableFuture<Object> processCommand(NbCodeLanguageClient client, String command, List<Object> arguments) {
if (COMMAND_EXTRACT_LOOKUP.equals(command)) {
commandLookup = Lookup.getDefault();
return CompletableFuture.completedFuture(true);
}
return null;
}
}
@Override
protected void setUp() throws Exception {
System.setProperty("java.awt.headless", Boolean.TRUE.toString());
Expand Down Expand Up @@ -4362,6 +4398,7 @@ public CompletableFuture<List<QuickPickItem>> showQuickPick(ShowQuickPickParams
}

private Launcher<LanguageServer> createClientLauncherWithLogging(LanguageClient client, InputStream input, OutputStream output) {
System.err.println("Creating a client for testcase: " + getName());
Launcher.Builder<LanguageServer> builder = new LSPLauncher.Builder<LanguageServer>()
.setLocalService(client)
.setExceptionHandler((t) -> {
Expand Down Expand Up @@ -5551,6 +5588,29 @@ public void testDeclarativeHints() throws Exception {
assertEquals(Arrays.asList("Method:length() : int"), actualItems);
}

/**
* Checks that the default Lookup contents is present just once in Lookup.getDefault() during server invocation in general,
* and specifically during command invocation.
*/
public void testDefaultLookupJustOnce() throws Exception {
Launcher<LanguageServer> serverLauncher = createClientLauncherWithLogging(new LspClient(), client.getInputStream(), client.getOutputStream());
serverLauncher.startListening();
LanguageServer server = serverLauncher.getRemoteProxy();
CompletableFuture<Object> o = server.getWorkspaceService().executeCommand(new ExecuteCommandParams(COMMAND_EXTRACT_LOOKUP, Collections.emptyList()));
o.get();
Collection<? extends NbCodeLanguageClient> mm1 = extractCommand.serverLookup.lookupAll(NbCodeLanguageClient.class);
Collection<? extends NbCodeLanguageClient> mm2 = extractCommand.commandLookup.lookupAll(NbCodeLanguageClient.class);

assertEquals(1, mm1.size());
assertEquals(1, mm2.size());

Collection<? extends DialogDisplayer> mm3 = extractCommand.serverLookup.lookupAll(DialogDisplayer.class);
Collection<? extends DialogDisplayer> mm4 = extractCommand.commandLookup.lookupAll(DialogDisplayer.class);

assertEquals(1, mm3.size());
assertEquals(1, mm4.size());
}

static {
System.setProperty("SourcePath.no.source.filter", "true");
JavacParser.DISABLE_SOURCE_LEVEL_DOWNGRADE = true;
Expand Down

0 comments on commit b3cf9b9

Please sign in to comment.