forked from dsldevkit/dsl-devkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for export contributions
Generalize existing IAdditionalModelInferrer pattern to support extending multiple services. For now we add services required for export grouped together (fingerprint computer, resource description strategy, qualified name provider) in IAdditionalExport. We keep IAdditionalModelInferrer as is, but do not register it directly anymore in an extension point. Instead in extension point we register ILanguageContribution where we can register extensions for multiple services. Check extension will then also come into this service. Later we might need a similar thing for UI services when we are to extend content assist. Both *InferrersService and *ExportService get are language specific and can be injected by languages that support extensions. These services have default implementations (DefaultAdditionalExportService, DefaultAdditionalInferrersService) which rely on global ILanguageContributionService. ILanguageContribution service is the one that may have different implementations for the standalone builder and for Eclipse OSGi use case. Issue dsldevkit#46
- Loading branch information
Roman Mitin
committed
Feb 5, 2018
1 parent
d95db3f
commit 114bb41
Showing
12 changed files
with
401 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...s.ddk.xtext/src/com/avaloq/tools/ddk/xtext/contribution/AbstractLanguageContribution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 Avaloq Evolution AG and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Avaloq Evolution AG - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package com.avaloq.tools.ddk.xtext.contribution; | ||
|
||
import com.avaloq.tools.ddk.xtext.modelinference.IAdditionalModelInferrer; | ||
import com.avaloq.tools.ddk.xtext.resource.IAdditionalExport; | ||
|
||
|
||
/** | ||
* Default implementation providing no contribution. | ||
*/ | ||
public abstract class AbstractLanguageContribution implements ILanguageContribution { | ||
|
||
@Override | ||
public IAdditionalModelInferrer getAdditionalModelInferrer() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public IAdditionalExport getAdditionalExport() { | ||
return null; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...oq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/contribution/ILanguageContribution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 Avaloq Evolution AG and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Avaloq Evolution AG - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package com.avaloq.tools.ddk.xtext.contribution; | ||
|
||
import com.avaloq.tools.ddk.xtext.modelinference.IAdditionalModelInferrer; | ||
import com.avaloq.tools.ddk.xtext.resource.IAdditionalExport; | ||
|
||
|
||
/** | ||
* Extends the specified language. | ||
*/ | ||
public interface ILanguageContribution { | ||
|
||
/** | ||
* Gets the full qualified name of the language supported. | ||
* | ||
* @return the language name | ||
*/ | ||
String getTargetLanguageName(); | ||
|
||
/** | ||
* Returns the additional model inferrer for this contribution, or {@code null} if no additional inference is necessary. | ||
* | ||
* @return a model inferrer, may be {@code null} | ||
*/ | ||
IAdditionalModelInferrer getAdditionalModelInferrer(); | ||
|
||
/** | ||
* Gets the contributions to export. | ||
* | ||
* @return the additional export | ||
*/ | ||
IAdditionalExport getAdditionalExport(); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...s.ddk.xtext/src/com/avaloq/tools/ddk/xtext/contribution/ILanguageContributionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 Avaloq Evolution AG and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Avaloq Evolution AG - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package com.avaloq.tools.ddk.xtext.contribution; | ||
|
||
import java.util.Collection; | ||
|
||
|
||
/** | ||
* Global service that manages contributions to all languages. | ||
* <p> | ||
* This service is implemented as a global singleton, one instance for all DSLs. | ||
* </p> | ||
*/ | ||
public interface ILanguageContributionService { | ||
|
||
/** | ||
* Gets all contributions for the given language. | ||
* | ||
* @param languageName | ||
* the language name | ||
* @return the collection of contributions, may be empty, never {@code null} | ||
*/ | ||
Collection<ILanguageContribution> getContributions(String languageName); | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
...text/src/com/avaloq/tools/ddk/xtext/modelinference/DefaultAdditionalInferrersService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 Avaloq Evolution AG and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Avaloq Evolution AG - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package com.avaloq.tools.ddk.xtext.modelinference; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.xtext.Constants; | ||
import org.eclipse.xtext.util.IAcceptor; | ||
|
||
import com.avaloq.tools.ddk.xtext.contribution.ILanguageContribution; | ||
import com.avaloq.tools.ddk.xtext.contribution.ILanguageContributionService; | ||
import com.google.common.collect.Lists; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Injector; | ||
import com.google.inject.name.Named; | ||
|
||
|
||
/** | ||
* Default implementation relying on {@link ILanguageContribution}. | ||
*/ | ||
public class DefaultAdditionalInferrersService implements IAdditionalInferrersService { | ||
|
||
@Inject | ||
@Named(Constants.LANGUAGE_NAME) | ||
private String languageName; | ||
|
||
@Inject | ||
private ILanguageContributionService contributionService; | ||
|
||
@Inject | ||
private Injector injector; | ||
|
||
private List<IAdditionalModelInferrer> inferrers; | ||
|
||
@Override | ||
public void inferTargetModel(final EObject sourceModelElement, final IAcceptor<EObject> acceptor, final boolean isPrelinkingPhase) { | ||
if (inferrers == null) { | ||
List<IAdditionalModelInferrer> unsortedInferrers = Lists.newArrayList(); | ||
for (ILanguageContribution contribution : contributionService.getContributions(languageName)) { | ||
IAdditionalModelInferrer inferrer = contribution.getAdditionalModelInferrer(); | ||
if (inferrer != null) { | ||
injector.injectMembers(inferrer); | ||
unsortedInferrers.add(inferrer); | ||
} | ||
} | ||
// We order the inferrers found by their class name to keep a strict ordering | ||
inferrers = unsortedInferrers.stream().sorted().collect(Collectors.toList()); | ||
} | ||
for (IAdditionalModelInferrer inferrer : inferrers) { | ||
inferrer.inferTargetModel(sourceModelElement, acceptor, isPrelinkingPhase); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...ols.ddk.xtext/src/com/avaloq/tools/ddk/xtext/resource/DefaultAdditionalExportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2016 Avaloq Evolution AG and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Avaloq Evolution AG - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package com.avaloq.tools.ddk.xtext.resource; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.emf.ecore.EClass; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.xtext.Constants; | ||
import org.eclipse.xtext.naming.QualifiedName; | ||
import org.eclipse.xtext.resource.IEObjectDescription; | ||
import org.eclipse.xtext.util.IAcceptor; | ||
|
||
import com.avaloq.tools.ddk.xtext.contribution.ILanguageContribution; | ||
import com.avaloq.tools.ddk.xtext.contribution.ILanguageContributionService; | ||
import com.google.common.collect.Lists; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Injector; | ||
import com.google.inject.Singleton; | ||
import com.google.inject.name.Named; | ||
|
||
|
||
/** | ||
* Default implementation relying on {@link ILanguageContribution}. | ||
*/ | ||
@Singleton | ||
public class DefaultAdditionalExportService implements IAdditionalExportService { | ||
|
||
@Inject | ||
@Named(Constants.LANGUAGE_NAME) | ||
private String languageName; | ||
|
||
@Inject | ||
private ILanguageContributionService contributionService; | ||
|
||
@Inject | ||
private Injector injector; | ||
|
||
private List<IAdditionalExport> exportContributions; | ||
|
||
@Override | ||
public QualifiedName qualifiedName(final EObject object) { | ||
return returnFirst(c -> c.qualifiedName(object)); | ||
} | ||
|
||
@Override | ||
public String computeFingerprint(final EObject object) { | ||
return returnFirst(c -> c.computeFingerprint(object)); | ||
} | ||
|
||
@Override | ||
public CharSequence getFragmentSegment(final EObject object) { | ||
return returnFirst(c -> c.getFragmentSegment(object)); | ||
} | ||
|
||
@Override | ||
public Set<EClass> getExportedEClasses(final Resource resource) { | ||
ensureInitilaized(); | ||
return exportContributions.stream().flatMap(c -> c.getExportedEClasses(resource).stream()).collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
public boolean doCreateEObjectDescriptions(final EObject object, final IAcceptor<IEObjectDescription> acceptor) { | ||
boolean traverse = false; | ||
for (IAdditionalExport contribution : exportContributions) { | ||
traverse |= contribution.doCreateEObjectDescriptions(object, acceptor); | ||
} | ||
return traverse; | ||
} | ||
|
||
private <T> T returnFirst(final Function<IAdditionalExport, T> mapper) { | ||
ensureInitilaized(); | ||
return exportContributions.stream().map(mapper).filter(Objects::nonNull).findFirst().orElse(null); | ||
} | ||
|
||
private void ensureInitilaized() { | ||
if (exportContributions == null) { | ||
exportContributions = Lists.newArrayList(); | ||
for (ILanguageContribution contribution : contributionService.getContributions(languageName)) { | ||
IAdditionalExport export = contribution.getAdditionalExport(); | ||
if (export != null) { | ||
injector.injectMembers(export); | ||
exportContributions.add(export); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.