Skip to content

Commit

Permalink
Moved EclipseAcceleoLanguageServerContext from server to plugin start.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Dec 20, 2023
1 parent 356e9f4 commit 75a8620
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import java.net.InetAddress;
import java.net.Socket;

import org.eclipse.acceleo.aql.ls.IAcceleoLanguageServerContext;
import org.eclipse.acceleo.aql.ls.AcceleoSocketServer;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.lsp4e.server.StreamConnectionProvider;

/**
Expand Down Expand Up @@ -48,9 +46,7 @@ public class AcceleoConnectionProvider implements StreamConnectionProvider {
* Constructor.
*/
public AcceleoConnectionProvider() {
IAcceleoLanguageServerContext acceleoContext = new EclipseAcceleoLanguageServerContext(ResourcesPlugin
.getWorkspace());
this.acceleoSocketServer = new AcceleoSocketServer(acceleoContext);
this.acceleoSocketServer = new AcceleoSocketServer(Activator.getDefault().getServiceContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
Expand Down Expand Up @@ -46,31 +47,39 @@ public class Activator extends AbstractUIPlugin {
*/
private static Activator plugin;

/**
* The {@link EclipseAcceleoLanguageServerContext}.
*/
private EclipseAcceleoLanguageServerContext serviceContext;

/**
* The constructor.
*/
public Activator() {
}

/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework. BundleContext)
*/
@Override
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
serviceContext = new EclipseAcceleoLanguageServerContext(ResourcesPlugin.getWorkspace());
}

/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework. BundleContext)
*/
@Override
public void stop(BundleContext context) throws Exception {
serviceContext.dispose();
serviceContext = null;
plugin = null;
super.stop(context);
}

/**
* Gets the {@link EclipseAcceleoLanguageServerContext}.
*
* @return the {@link EclipseAcceleoLanguageServerContext}
*/
public EclipseAcceleoLanguageServerContext getServiceContext() {
return serviceContext;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.eclipse.acceleo.aql.ide.AcceleoPlugin;
Expand Down Expand Up @@ -45,13 +43,13 @@
public class EclipseAcceleoLanguageServerContext implements IAcceleoLanguageServerContext {

/**
* the {@link Synchronizer} for Acceleo.
* The {@link Synchronizer} for Acceleo.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
private static class AcceleSynchronizer extends Synchronizer<AcceleoProject> {
private static class AcceleoSynchronizer extends Synchronizer<AcceleoProject> {

AcceleSynchronizer(IWorkspace eclipseWorkspace, IQueryWorkspace<AcceleoProject> queryWorkspace) {
AcceleoSynchronizer(IWorkspace eclipseWorkspace, IQueryWorkspace<AcceleoProject> queryWorkspace) {
super(eclipseWorkspace, queryWorkspace);
}

Expand All @@ -78,9 +76,9 @@ public AcceleoWorkspace getAcceleoWorkspace() {
private final IWorkspace clientWorkspace;

/**
* The mapping form an {@link AcceleoWorkspace} to its {@link EclipseWorkspace2AcceleoWorkspace}.
* The {@link AcceleoSynchronizer}.
*/
private final Map<AcceleoWorkspace, AcceleSynchronizer> synchronizers = new HashMap<>();
private final AcceleoSynchronizer synchronizer;

/**
* The constructor.
Expand All @@ -90,22 +88,22 @@ public AcceleoWorkspace getAcceleoWorkspace() {
*/
public EclipseAcceleoLanguageServerContext(IWorkspace clientWorkspace) {
this.clientWorkspace = Objects.requireNonNull(clientWorkspace);
}

@Override
public AcceleoWorkspace createWorkspace() {
final AcceleoWorkspace acceleoWorkspace = new AcceleoWorkspace(getAcceleoWorkspaceName(
clientWorkspace), this);
final AcceleSynchronizer syncrhonizer = new AcceleSynchronizer(clientWorkspace, acceleoWorkspace);
synchronizers.put(syncrhonizer.getAcceleoWorkspace(), syncrhonizer);
syncrhonizer.synchronize();
return syncrhonizer.getAcceleoWorkspace();
synchronizer = new AcceleoSynchronizer(clientWorkspace, acceleoWorkspace);
synchronizer.synchronize();
}

/**
* Disposes this context.
*/
public void dispose() {
synchronizer.dispose();
}

@Override
public void deleteWorkspace(AcceleoWorkspace acceleoWorkspaceToDelete) {
final AcceleSynchronizer syncronizer = synchronizers.remove(acceleoWorkspaceToDelete);
syncronizer.dispose();
public AcceleoWorkspace getWorkspace() {
return synchronizer.getAcceleoWorkspace();
}

@Override
Expand Down Expand Up @@ -135,18 +133,17 @@ public String getResourceContents(URI resource) {
public AcceleoProject getProject(AcceleoWorkspace workspace, URI resource) {
final AcceleoProject res;

final AcceleSynchronizer synchoniser = synchronizers.get(workspace);
synchronized(synchoniser) {
synchronized(synchronizer) {

final IFile file = clientWorkspace.getRoot().getFileForLocation(new Path(resource.getPath()
.toString()));
final IProject eclipseProject = file.getProject();
final AcceleoProject existingProject = synchoniser.getProject(eclipseProject);
final AcceleoProject existingProject = synchronizer.getProject(eclipseProject);
if (existingProject != null) {
res = existingProject;
} else {
synchoniser.add(file.getProject());
res = synchoniser.getProject(eclipseProject);
synchronizer.add(file.getProject());
res = synchronizer.getProject(eclipseProject);
}
}

Expand All @@ -157,7 +154,6 @@ public AcceleoProject getProject(AcceleoWorkspace workspace, URI resource) {
public IQueryWorkspaceQualifiedNameResolver createResolver(AcceleoProject acceleoProject) {
Objects.nonNull(acceleoProject);

final AcceleSynchronizer synchronizer = synchronizers.get(acceleoProject.getWorkspace());
final IProject eclipseProject = synchronizer.getProject(acceleoProject);
final IQualifiedNameResolver resolver = QueryPlugin.getPlugin().createQualifiedNameResolver(
AcceleoPlugin.getPlugin().getClass().getClassLoader(), eclipseProject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public String getLabel() {
}

/**
* Creates the {@link AcceleoWorkspace}.
* Connects the {@link AcceleoWorkspace}.
*
* @return the newly-created {@link AcceleoWorkspace}.
* @return the {@link AcceleoWorkspace}.
*/
public AcceleoWorkspace createWorkspace() {
AcceleoWorkspace workspace = this.acceleoLanguageServerContext.createWorkspace();
public AcceleoWorkspace connectWorkspace() {
final AcceleoWorkspace workspace = this.acceleoLanguageServerContext.getWorkspace();
workspace.setOwner(this);
return workspace;
}
Expand Down Expand Up @@ -169,10 +169,14 @@ private TextDocumentSyncOptions createTextDocumentSyncOptions() {

@Override
public CompletableFuture<Object> shutdown() {
this.acceleoLanguageServerContext.deleteWorkspace(this.getWorkspace());
final AcceleoWorkspace workspace = this.acceleoLanguageServerContext.getWorkspace();
workspace.setOwner(null);
if (this.languageClient != null) {
this.languageClient.logMessage(new MessageParams(MessageType.Log,
"Acceleo Language Server is shutting down"));
this.languageClient = null;
this.textDocumentService.disconnect();
this.workspaceService.disconnect();
}
return CompletableFuture.completedFuture(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@
public interface IAcceleoLanguageServerContext {

/**
* Creates an {@link AcceleoWorkspace}.
* Gets the {@link AcceleoWorkspace}.
*
* @return the newly-created {@link AcceleoWorkspace}.
* @return the {@link AcceleoWorkspace}.
*/
AcceleoWorkspace createWorkspace();

/**
* Called when the owner {@link AcceleoLanguageServer} shutdowns.
*
* @param workspaceToDelete
* the (non-{@code null}) {@link AcceleoWorkspace} to delete.
*/
void deleteWorkspace(AcceleoWorkspace workspaceToDelete);
AcceleoWorkspace getWorkspace();

/**
* Gets the contents of the given resource {@link URI}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ public void connect(LanguageClient newLanguageClient) {
}
////

/**
* Disconnects from the {@link LanguageClient}.
*/
public void disconnect() {
this.languageClient = null;
}

// Mandatory TextDocumentService API.
@Override
public void didOpen(DidOpenTextDocumentParams params) {
Expand All @@ -125,6 +132,9 @@ public void didOpen(DidOpenTextDocumentParams params) {
final URI binaryURI = resolver.getBinaryURI(openedDocumentUri);
workspace.addResource(project, binaryURI);
openedAcceleoTextDocument = workspace.getDocument(openedDocumentUri);
if (openedAcceleoTextDocument != null) {
openedAcceleoTextDocument.validateAndPublishResults();
}
}
if (openedAcceleoTextDocument == null) {
throw new IllegalStateException("Could not find the Acceleo Text Document at URI "
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Obeo.
* Copyright (c) 2020, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -52,14 +52,21 @@ public class AcceleoWorkspaceService implements WorkspaceService, LanguageClient
*/
public AcceleoWorkspaceService(AcceleoLanguageServer acceleoLanguageServer) {
this.server = Objects.requireNonNull(acceleoLanguageServer);
this.acceleoWorkspace = this.server.createWorkspace();
this.acceleoWorkspace = this.server.connectWorkspace();
}

@Override
public void connect(LanguageClient newLanguageClient) {
this.languageClient = newLanguageClient;
}

/**
* Disconnects from the {@link LanguageClient}.
*/
public void disconnect() {
this.languageClient = null;
}

/**
* Provides the current {@link AcceleoWorkspace}.
*
Expand Down

0 comments on commit 75a8620

Please sign in to comment.