mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-16 00:38:56 +00:00
Merge pull request #36 from eclipse/msp/generatorEncoding
[generator] Added initialization of encoding for the Xtext grammar language
This commit is contained in:
commit
bd6c3d0c47
2 changed files with 56 additions and 1 deletions
|
@ -27,15 +27,17 @@ import org.eclipse.xtend.lib.annotations.Accessors
|
|||
import org.eclipse.xtext.GeneratedMetamodel
|
||||
import org.eclipse.xtext.Grammar
|
||||
import org.eclipse.xtext.XtextStandaloneSetup
|
||||
import org.eclipse.xtext.parser.IEncodingProvider
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider
|
||||
import org.eclipse.xtext.util.MergeableManifest
|
||||
import org.eclipse.xtext.util.Tuples
|
||||
import org.eclipse.xtext.util.internal.Log
|
||||
import org.eclipse.xtext.xtext.generator.model.IXtextGeneratorFileSystemAccess
|
||||
import org.eclipse.xtext.xtext.generator.model.ManifestAccess
|
||||
import org.eclipse.xtext.xtext.generator.model.PluginXmlAccess
|
||||
import org.eclipse.xtext.xtext.generator.model.TextFileAccess
|
||||
import org.eclipse.xtext.xtext.generator.model.project.BundleProjectConfig
|
||||
import org.eclipse.xtext.xtext.generator.model.project.IXtextProjectConfig
|
||||
import org.eclipse.xtext.xtext.generator.model.TextFileAccess
|
||||
|
||||
/**
|
||||
* The Xtext language infrastructure generator. Can be configured with {@link IXtextGeneratorFragment}
|
||||
|
@ -58,6 +60,9 @@ class XtextGenerator extends AbstractWorkflowComponent2 {
|
|||
@Accessors
|
||||
XtextGeneratorStandaloneSetup standaloneSetup = new XtextGeneratorStandaloneSetup
|
||||
|
||||
@Accessors
|
||||
String grammarEncoding
|
||||
|
||||
Injector injector
|
||||
|
||||
@Inject IXtextProjectConfig projectConfig
|
||||
|
@ -100,6 +105,7 @@ class XtextGenerator extends AbstractWorkflowComponent2 {
|
|||
if (injector === null) {
|
||||
LOG.info('Initializing Xtext generator')
|
||||
new StandaloneSetup().addRegisterGeneratedEPackage('org.eclipse.xtext.common.types.TypesPackage')
|
||||
initializeEncoding
|
||||
injector = createInjector
|
||||
injector.injectMembers(this)
|
||||
injector.getInstance(CodeConfig) => [initialize(injector)]
|
||||
|
@ -113,6 +119,17 @@ class XtextGenerator extends AbstractWorkflowComponent2 {
|
|||
}
|
||||
}
|
||||
|
||||
protected def initializeEncoding() {
|
||||
val serviceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE
|
||||
val serviceProvider = serviceProviderRegistry.extensionToFactoryMap.get('xtext') as IResourceServiceProvider
|
||||
val encoding = grammarEncoding ?: configuration.code.encoding
|
||||
if (serviceProvider !== null && encoding !== null) {
|
||||
val encodingProvider = serviceProvider.get(IEncodingProvider)
|
||||
if (encodingProvider instanceof IEncodingProvider.Runtime)
|
||||
encodingProvider.defaultEncoding = encoding
|
||||
}
|
||||
}
|
||||
|
||||
protected def Injector createInjector() {
|
||||
Guice.createInjector(configuration)
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.io.InputStream;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import org.apache.log4j.Logger;
|
||||
|
@ -37,6 +38,8 @@ import org.eclipse.xtext.AbstractMetamodelDeclaration;
|
|||
import org.eclipse.xtext.GeneratedMetamodel;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.XtextStandaloneSetup;
|
||||
import org.eclipse.xtext.parser.IEncodingProvider;
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider;
|
||||
import org.eclipse.xtext.util.MergeableManifest;
|
||||
import org.eclipse.xtext.util.Triple;
|
||||
import org.eclipse.xtext.util.Tuples;
|
||||
|
@ -95,6 +98,9 @@ public class XtextGenerator extends AbstractWorkflowComponent2 {
|
|||
@Accessors
|
||||
private XtextGeneratorStandaloneSetup standaloneSetup = new XtextGeneratorStandaloneSetup();
|
||||
|
||||
@Accessors
|
||||
private String grammarEncoding;
|
||||
|
||||
private Injector injector;
|
||||
|
||||
@Inject
|
||||
|
@ -159,6 +165,7 @@ public class XtextGenerator extends AbstractWorkflowComponent2 {
|
|||
XtextGenerator.LOG.info("Initializing Xtext generator");
|
||||
StandaloneSetup _standaloneSetup = new StandaloneSetup();
|
||||
_standaloneSetup.addRegisterGeneratedEPackage("org.eclipse.xtext.common.types.TypesPackage");
|
||||
this.initializeEncoding();
|
||||
Injector _createInjector = this.createInjector();
|
||||
this.injector = _createInjector;
|
||||
this.injector.injectMembers(this);
|
||||
|
@ -179,6 +186,28 @@ public class XtextGenerator extends AbstractWorkflowComponent2 {
|
|||
}
|
||||
}
|
||||
|
||||
protected void initializeEncoding() {
|
||||
final IResourceServiceProvider.Registry serviceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE;
|
||||
Map<String, Object> _extensionToFactoryMap = serviceProviderRegistry.getExtensionToFactoryMap();
|
||||
Object _get = _extensionToFactoryMap.get("xtext");
|
||||
final IResourceServiceProvider serviceProvider = ((IResourceServiceProvider) _get);
|
||||
String _elvis = null;
|
||||
if (this.grammarEncoding != null) {
|
||||
_elvis = this.grammarEncoding;
|
||||
} else {
|
||||
CodeConfig _code = this.configuration.getCode();
|
||||
String _encoding = _code.getEncoding();
|
||||
_elvis = _encoding;
|
||||
}
|
||||
final String encoding = _elvis;
|
||||
if (((serviceProvider != null) && (encoding != null))) {
|
||||
final IEncodingProvider encodingProvider = serviceProvider.<IEncodingProvider>get(IEncodingProvider.class);
|
||||
if ((encodingProvider instanceof IEncodingProvider.Runtime)) {
|
||||
((IEncodingProvider.Runtime)encodingProvider).setDefaultEncoding(encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected Injector createInjector() {
|
||||
return Guice.createInjector(this.configuration);
|
||||
}
|
||||
|
@ -546,4 +575,13 @@ public class XtextGenerator extends AbstractWorkflowComponent2 {
|
|||
public void setStandaloneSetup(final XtextGeneratorStandaloneSetup standaloneSetup) {
|
||||
this.standaloneSetup = standaloneSetup;
|
||||
}
|
||||
|
||||
@Pure
|
||||
public String getGrammarEncoding() {
|
||||
return this.grammarEncoding;
|
||||
}
|
||||
|
||||
public void setGrammarEncoding(final String grammarEncoding) {
|
||||
this.grammarEncoding = grammarEncoding;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue