Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Schindl2015-06-19 08:53:53 +0000
committerTom Schindl2015-06-19 08:53:53 +0000
commit2a7667a40d62014b943f2edb6e6b301863aff528 (patch)
treeb6da92f20f484b8398274c20464108ebfd1952ab /experimental
parent8aba68f13f6cc5a7e08dee41f7735256b07b0651 (diff)
downloadorg.eclipse.efxclipse-2a7667a40d62014b943f2edb6e6b301863aff528.tar.gz
org.eclipse.efxclipse-2a7667a40d62014b943f2edb6e6b301863aff528.tar.xz
org.eclipse.efxclipse-2a7667a40d62014b943f2edb6e6b301863aff528.zip
factored out code to be reuseable more easily
Diffstat (limited to 'experimental')
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/.classpath7
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/.gitignore1
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/.project33
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/.settings/org.eclipse.jdt.core.prefs7
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/META-INF/MANIFEST.MF17
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/OSGI-INF/services/org.eclipse.fx.code.editor.hsl.internal.HSLModelCF.xml9
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/build.properties5
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLConfiguration.java123
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLEditorComponent.java62
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLModelProvider.java9
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLModelCF.java49
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleBasedPartitionScanner.java43
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleScanner.java177
-rw-r--r--experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/JavaScriptHelper.java56
14 files changed, 598 insertions, 0 deletions
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/.classpath b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.classpath
new file mode 100644
index 000000000..eca7bdba8
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/.gitignore b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.gitignore
new file mode 100644
index 000000000..ae3c17260
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/.project b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.project
new file mode 100644
index 000000000..b3fa83ef4
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.project
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.fx.code.editor.hsl</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ds.core.builder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/.settings/org.eclipse.jdt.core.prefs b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 000000000..0c68a61dc
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
+org.eclipse.jdt.core.compiler.compliance=1.8
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.8
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/META-INF/MANIFEST.MF b/experimental/compensator/org.eclipse.fx.code.editor.hsl/META-INF/MANIFEST.MF
new file mode 100644
index 000000000..656f23148
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/META-INF/MANIFEST.MF
@@ -0,0 +1,17 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Hsl
+Bundle-SymbolicName: org.eclipse.fx.code.editor.hsl
+Bundle-Version: 1.0.0.qualifier
+Bundle-RequiredExecutionEnvironment: JavaSE-1.8
+Import-Package: javax.inject,
+ org.eclipse.e4.core.di.annotations,
+ org.osgi.service.component.annotations;version="1.2.0"
+Require-Bundle: org.eclipse.fx.code.compensator.hsl,
+ org.eclipse.fx.text.ui;bundle-version="1.0.0",
+ org.eclipse.fx.code.compensator.editor,
+ org.eclipse.fx.core.di,
+ org.eclipse.e4.core.contexts
+Export-Package: org.eclipse.fx.code.editor.hsl
+Service-Component: OSGI-INF/services/org.eclipse.fx.code.editor.hsl.internal.HSLModelCF.xml
+Bundle-ActivationPolicy: lazy
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/OSGI-INF/services/org.eclipse.fx.code.editor.hsl.internal.HSLModelCF.xml b/experimental/compensator/org.eclipse.fx.code.editor.hsl/OSGI-INF/services/org.eclipse.fx.code.editor.hsl.internal.HSLModelCF.xml
new file mode 100644
index 000000000..df2750b37
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/OSGI-INF/services/org.eclipse.fx.code.editor.hsl.internal.HSLModelCF.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.2.0" name="org.eclipse.fx.code.editor.hsl.internal.HSLModelCF">
+ <implementation class="org.eclipse.fx.code.editor.hsl.internal.HSLModelCF"/>
+ <service>
+ <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
+ </service>
+ <property name="service.context.key" value="org.eclipse.fx.code.compensator.hsl.hSL.Model"/>
+ <reference bind="registerProvider" cardinality="0..n" interface="org.eclipse.fx.code.editor.hsl.HSLModelProvider" name="registerProvider" policy="dynamic" policy-option="greedy" unbind="unregisterProvider"/>
+</scr:component> \ No newline at end of file
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/build.properties b/experimental/compensator/org.eclipse.fx.code.editor.hsl/build.properties
new file mode 100644
index 000000000..447d6cf88
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/build.properties
@@ -0,0 +1,5 @@
+output.. = bin/
+bin.includes = META-INF/,\
+ .,\
+ OSGI-INF/services/org.eclipse.fx.code.editor.hsl.internal.HSLModelCF.xml
+source.. = src/
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLConfiguration.java b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLConfiguration.java
new file mode 100644
index 000000000..ddc3454ff
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLConfiguration.java
@@ -0,0 +1,123 @@
+/*******************************************************************************
+ * Copyright (c) 2014 BestSolution.at 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:
+ * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.code.editor.hsl;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+
+import javax.inject.Inject;
+
+import org.eclipse.e4.core.di.annotations.Optional;
+import org.eclipse.fx.code.compensator.editor.Input;
+import org.eclipse.fx.code.compensator.editor.ProposalComputer;
+import org.eclipse.fx.code.compensator.editor.ProposalComputer.ProposalContext;
+import org.eclipse.fx.code.compensator.hsl.hSL.Damager;
+import org.eclipse.fx.code.compensator.hsl.hSL.JSDamager;
+import org.eclipse.fx.code.compensator.hsl.hSL.Model;
+import org.eclipse.fx.code.compensator.hsl.hSL.RuleDamager;
+import org.eclipse.fx.code.editor.hsl.internal.HSLRuleScanner;
+import org.eclipse.fx.code.editor.hsl.internal.JavaScriptHelper;
+import org.eclipse.fx.core.di.Service;
+import org.eclipse.jface.text.contentassist.ContentAssistant;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+import org.eclipse.jface.text.contentassist.IContentAssistant;
+import org.eclipse.jface.text.presentation.IPresentationDamager;
+import org.eclipse.jface.text.presentation.IPresentationReconciler;
+import org.eclipse.jface.text.presentation.IPresentationRepairer;
+import org.eclipse.jface.text.presentation.PresentationReconciler;
+import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
+import org.eclipse.jface.text.source.AnnotationPresenter;
+import org.eclipse.jface.text.source.IAnnotationModel;
+import org.eclipse.jface.text.source.ISourceViewer;
+import org.eclipse.jface.text.source.SourceViewerConfiguration;
+
+public class HSLConfiguration extends SourceViewerConfiguration {
+ private final Model model;
+ private final ClassLoader cl;
+
+ private final IAnnotationModel annotationModel;
+ private final List<AnnotationPresenter> annotationPresenters;
+ private final ProposalComputer proposalComputer;
+ private final Input<?> input;
+
+ @Inject
+ public HSLConfiguration(
+ Model model,
+ Input<?> input,
+ @Optional ProposalComputer proposalComputer,
+ @Optional IAnnotationModel annotationModel,
+ @Service List<AnnotationPresenter> annotationPresenters) {
+ this.cl = getClass().getClassLoader();
+ this.model = model;
+ this.input = input;
+ this.annotationModel = annotationModel;
+ this.annotationPresenters = annotationPresenters;
+ this.proposalComputer = proposalComputer;
+ }
+
+ public final IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
+ PresentationReconciler reconciler = new PresentationReconciler();
+
+ for( Damager sc : model.getDamagers() ) {
+ if( sc instanceof RuleDamager ) {
+ RuleDamager rs = (RuleDamager) sc;
+ HSLRuleScanner s = new HSLRuleScanner(rs);
+ DefaultDamagerRepairer dr = new DefaultDamagerRepairer(s);
+ reconciler.setDamager(dr, rs.getPartition().getName());
+ reconciler.setRepairer(dr, rs.getPartition().getName());
+ } else {
+ JSDamager js = (JSDamager) sc;
+ Object o = JavaScriptHelper.loadScript(cl, js, js.getFileURI());
+ reconciler.setDamager((IPresentationDamager) o, js.getPartition().getName());
+ reconciler.setRepairer((IPresentationRepairer) o, js.getPartition().getName());
+ }
+ }
+
+ return reconciler;
+ }
+
+ @Override
+ public String getStyleclassName() {
+ return model.getName();
+ }
+
+ @Override
+ public IContentAssistant getContentAssist() {
+ if( proposalComputer != null ) {
+ return new ContentAssistant(this::computeProposals);
+ }
+ return super.getContentAssist();
+ }
+
+ List<ICompletionProposal> computeProposals(Integer offset) {
+ try {
+ return proposalComputer.compute(new ProposalContext(input, offset)).get();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ExecutionException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return Collections.emptyList();
+ }
+
+ @Override
+ public IAnnotationModel getAnnotationModel() {
+ return annotationModel;
+ }
+
+ @Override
+ public List<AnnotationPresenter> getAnnotationPresenters() {
+ return annotationPresenters;
+ }
+}
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLEditorComponent.java b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLEditorComponent.java
new file mode 100644
index 000000000..879bcc828
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLEditorComponent.java
@@ -0,0 +1,62 @@
+package org.eclipse.fx.code.editor.hsl;
+
+import java.util.List;
+
+import org.eclipse.fx.code.compensator.editor.Input;
+import org.eclipse.fx.code.compensator.editor.services.PartitionerFactory;
+import org.eclipse.fx.code.compensator.editor.services.SourceViewerConfigurationFactory;
+import org.eclipse.fx.code.compensator.hsl.hSL.JavaLikeParitioner;
+import org.eclipse.fx.code.compensator.hsl.hSL.Model;
+import org.eclipse.fx.code.compensator.hsl.hSL.Partitioner;
+import org.eclipse.fx.code.compensator.hsl.hSL.RulePartitioner;
+import org.eclipse.fx.code.editor.hsl.internal.HSLRuleBasedPartitionScanner;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IDocumentPartitioner;
+import org.eclipse.jface.text.rules.FastPartitioner;
+import org.eclipse.jface.text.rules.IPartitionTokenScanner;
+import org.eclipse.jface.text.source.FastJavaLikePartitionScanner;
+
+public abstract class HSLEditorComponent implements PartitionerFactory, SourceViewerConfigurationFactory {
+
+ protected abstract Model getModel(Input<?> input);
+
+ @Override
+ public final IDocumentPartitioner createPartitioner(Input<?> input) {
+ Model m = getModel(input);
+ if( m == null ) {
+ throw new IllegalArgumentException("Unsupported input '"+input+"'");
+ }
+
+ Partitioner partitioner = m.getPartitioner();
+ IPartitionTokenScanner scanner = null;
+ if( partitioner instanceof RulePartitioner ) {
+ scanner = new HSLRuleBasedPartitionScanner(getClass().getClassLoader(), (RulePartitioner) m.getPartitioner());
+ } else if( partitioner instanceof JavaLikeParitioner ) {
+ JavaLikeParitioner jp = (JavaLikeParitioner) partitioner;
+ scanner = new FastJavaLikePartitionScanner(
+ jp.getSingleLineDocParition().getName(),
+ jp.getMultiLineDocParition().getName(),
+ jp.getJavaDocParition().getName(),
+ jp.getCharacterParition().getName(),
+ jp.getStringParition().getName());
+ }
+
+ if( scanner != null ) {
+ return new FastPartitioner(scanner, getParitions(m));
+ }
+
+ throw new IllegalStateException("Unsupported partitioner '"+partitioner+"'");
+ }
+
+ private static String[] getParitions(Model m) {
+ return m.getPartitions().stream().filter((p) -> { return ! p.getName().equals(IDocument.DEFAULT_CONTENT_TYPE); })
+ .map((p) -> p.getName())
+ .toArray((size) -> new String[size]);
+ }
+
+ @Override
+ public List<ConfigurationType> getConfigurationData() {
+ return null;
+ }
+
+}
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLModelProvider.java b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLModelProvider.java
new file mode 100644
index 000000000..6fdb96a23
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/HSLModelProvider.java
@@ -0,0 +1,9 @@
+package org.eclipse.fx.code.editor.hsl;
+
+import org.eclipse.fx.code.compensator.editor.Input;
+import org.eclipse.fx.code.compensator.hsl.hSL.Model;
+
+public interface HSLModelProvider {
+ public boolean applies(Input<?> input);
+ public Model getModel(Input<?> input);
+}
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLModelCF.java b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLModelCF.java
new file mode 100644
index 000000000..6629e07cb
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLModelCF.java
@@ -0,0 +1,49 @@
+package org.eclipse.fx.code.editor.hsl.internal;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.e4.core.contexts.ContextFunction;
+import org.eclipse.e4.core.contexts.IContextFunction;
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.fx.code.compensator.editor.Input;
+import org.eclipse.fx.code.editor.hsl.HSLModelProvider;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
+import org.osgi.service.component.annotations.ReferencePolicyOption;
+
+@Component(service=IContextFunction.class,property={"service.context.key=org.eclipse.fx.code.compensator.hsl.hSL.Model"})
+public class HSLModelCF extends ContextFunction {
+ private List<HSLModelProvider> providerList = new ArrayList<>();
+
+ @Reference(cardinality=ReferenceCardinality.MULTIPLE,policy=ReferencePolicy.DYNAMIC,policyOption=ReferencePolicyOption.GREEDY)
+ public void registerProvider(HSLModelProvider provider) {
+ synchronized (providerList) {
+ providerList.add(provider);
+ }
+ }
+
+ public void unregisterProvider(HSLModelProvider provider) {
+ synchronized (providerList) {
+ providerList.remove(provider);
+ }
+ }
+
+ @Override
+ public Object compute(IEclipseContext context) {
+ List<HSLModelProvider> list;
+
+ synchronized (providerList) {
+ list = new ArrayList<HSLModelProvider>(providerList);
+ }
+
+ Input<?> input = context.get(Input.class);
+
+ return list.stream()
+ .filter( p -> p.applies(input))
+ .findFirst()
+ .map( p -> p.getModel(input)).orElse(null);
+ }
+}
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleBasedPartitionScanner.java b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleBasedPartitionScanner.java
new file mode 100644
index 000000000..80874cb51
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleBasedPartitionScanner.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2014 BestSolution.at 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:
+ * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.code.editor.hsl.internal;
+
+import org.eclipse.fx.code.compensator.hsl.hSL.ParitionRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.PartitionJSRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.PartitionMultiLineRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.PartitionSingleLineRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.RulePartitioner;
+import org.eclipse.jface.text.rules.IPredicateRule;
+import org.eclipse.jface.text.rules.MultiLineRule;
+import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
+import org.eclipse.jface.text.rules.SingleLineRule;
+import org.eclipse.jface.text.rules.Token;
+
+public class HSLRuleBasedPartitionScanner extends RuleBasedPartitionScanner {
+ public HSLRuleBasedPartitionScanner(ClassLoader cl, RulePartitioner paritioner) {
+ IPredicateRule[] pr = new IPredicateRule[paritioner.getRules().size()];
+ int i = 0;
+
+ for( ParitionRule r : paritioner.getRules() ) {
+ if( r instanceof PartitionSingleLineRule ) {
+ PartitionSingleLineRule sr = (PartitionSingleLineRule) r;
+ pr[i] = new SingleLineRule(sr.getStartSeq(), sr.getEndSeq(), new Token(sr.getParition().getName()), sr.getEscapeSeq() != null ? sr.getEndSeq().charAt(0) : 0, false);
+ } else if( r instanceof PartitionMultiLineRule ) {
+ PartitionMultiLineRule mr = (PartitionMultiLineRule) r;
+ pr[i] = new MultiLineRule(mr.getStartSeq(), mr.getEndSeq(), new Token(mr.getParition().getName()), mr.getEscapeSeq() != null ? mr.getEscapeSeq().charAt(0) : 0, false);
+ } else if( r instanceof PartitionJSRule ) {
+ pr[i] = JavaScriptHelper.loadScript(cl, r, ((PartitionJSRule) r).getFileURI());
+ }
+ i++;
+ }
+ setPredicateRules(pr);
+ }
+}
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleScanner.java b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleScanner.java
new file mode 100644
index 000000000..3525141fe
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/HSLRuleScanner.java
@@ -0,0 +1,177 @@
+/*******************************************************************************
+ * Copyright (c) 2014 BestSolution.at 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:
+ * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.code.editor.hsl.internal;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import javafx.scene.paint.Color;
+import javafx.scene.text.Font;
+import javafx.scene.text.FontPosture;
+import javafx.scene.text.FontWeight;
+
+import org.eclipse.fx.code.compensator.hsl.hSL.FontType;
+import org.eclipse.fx.code.compensator.hsl.hSL.Keyword;
+import org.eclipse.fx.code.compensator.hsl.hSL.KeywordGroup;
+import org.eclipse.fx.code.compensator.hsl.hSL.RGBColor;
+import org.eclipse.fx.code.compensator.hsl.hSL.RuleDamager;
+import org.eclipse.fx.code.compensator.hsl.hSL.ScannerCharacterRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.ScannerJSRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.ScannerMultiLineRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.ScannerRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.ScannerSingleLineRule;
+import org.eclipse.fx.code.compensator.hsl.hSL.ScannerToken;
+import org.eclipse.fx.code.compensator.hsl.hSL.ScannerWhitespaceRule;
+import org.eclipse.jface.text.TextAttribute;
+import org.eclipse.jface.text.rules.CombinedWordRule;
+import org.eclipse.jface.text.rules.IRule;
+import org.eclipse.jface.text.rules.IToken;
+import org.eclipse.jface.text.rules.IWhitespaceDetector;
+import org.eclipse.jface.text.rules.MultiLineRule;
+import org.eclipse.jface.text.rules.RuleBasedScanner;
+import org.eclipse.jface.text.rules.SingleLineRule;
+import org.eclipse.jface.text.rules.Token;
+import org.eclipse.jface.text.rules.WhitespaceRule;
+import org.eclipse.jface.text.source.CharacterRule;
+import org.eclipse.jface.text.source.JavaLikeWordDetector;
+
+public class HSLRuleScanner extends RuleBasedScanner {
+ public HSLRuleScanner(RuleDamager scanner) {
+ Token defaultToken = null;
+ Map<String, IToken> tokenMap = new HashMap<String, IToken>();
+ for( ScannerToken st : scanner.getTokens() ) {
+ Token token = new Token(new TextAttribute(st.getName()));
+// if( "default_tagtoken".equals(st.getName()) ) {
+// token = new Token(new TextAttribute(st.getName(),Color.rgb(255, 0, 0)));
+// } else if( "xml_string".equals(st.getName()) ) {
+// token = new Token(new TextAttribute(st.getName(),Color.rgb(0, 255, 0)));
+// }
+ if( st.isDefault() ) {
+ defaultToken = token;
+ setDefaultReturnToken(token);
+ }
+ tokenMap.put(st.getName(), token);
+ }
+
+ IRule[] rules = new IRule[scanner.getRules().size()+(!scanner.getKeywordGroups().isEmpty() ? 1 : 0)];
+
+ int i = 0;
+ for( ScannerRule ru : scanner.getRules() ) {
+ if( ru instanceof ScannerSingleLineRule ) {
+ ScannerSingleLineRule sru = (ScannerSingleLineRule) ru;
+ rules[i] = new SingleLineRule(
+ sru.getStartSeq(),
+ sru.getEndSeq(),
+ tokenMap.get(sru.getToken().getName()),
+ sru.getEscapeSeq() != null ? sru.getEscapeSeq().charAt(0) : 0,
+ false);
+ } else if( ru instanceof ScannerMultiLineRule ) {
+ ScannerMultiLineRule sml = (ScannerMultiLineRule) ru;
+ rules[i] = new MultiLineRule(
+ sml.getStartSeq(),
+ sml.getEndSeq(),
+ tokenMap.get(sml.getToken().getName()),
+ sml.getEscapeSeq() != null ? sml.getEscapeSeq().charAt(0) : 0,
+ false);
+ } else if( ru instanceof ScannerWhitespaceRule ) {
+ ScannerWhitespaceRule wru = (ScannerWhitespaceRule) ru;
+ if( wru.isJavawhitespace() ) {
+ rules[i] = new WhitespaceRule(new IWhitespaceDetector() {
+
+ @Override
+ public boolean isWhitespace(char c) {
+ return Character.isWhitespace(c);
+ }
+ });
+ } else {
+ rules[i] = new WhitespaceRule(wru.getFileURI() != null ? new JSWSDectector() : new FixedCharacterWSDetector(wru.getCharacters()));
+ }
+ } else if( ru instanceof ScannerCharacterRule ) {
+ ScannerCharacterRule scr = (ScannerCharacterRule) ru;
+ char[] c = new char[scr.getCharacters().size()];
+ for( int j = 0; j < c.length; j++ ) {
+ c[j] = scr.getCharacters().get(0).charAt(0);
+ }
+ rules[i] = new CharacterRule(tokenMap.get(scr.getToken().getName()), c);
+ } else if( ru instanceof ScannerJSRule ) {
+ ScannerJSRule sjr = (ScannerJSRule) ru;
+ Function<IToken, IRule> f = JavaScriptHelper.loadScript(getClass().getClassLoader(), ru, sjr.getFileURI());
+ rules[i] = f.apply(tokenMap.get(sjr.getToken().getName()));
+ }
+ i++;
+ }
+
+ if( ! scanner.getKeywordGroups().isEmpty() ) {
+ JavaLikeWordDetector wordDetector= new JavaLikeWordDetector();
+ CombinedWordRule combinedWordRule= new CombinedWordRule(wordDetector, defaultToken);
+ for( KeywordGroup kg : scanner.getKeywordGroups() ) {
+ CombinedWordRule.WordMatcher wordRule= new CombinedWordRule.WordMatcher();
+ for( Keyword k : kg.getKeywords() ) {
+ wordRule.addWord(k.getName(), tokenMap.get(kg.getToken().getName()));
+ }
+ combinedWordRule.addWordMatcher(wordRule);
+ }
+ rules[i] = combinedWordRule;
+ }
+
+ setRules(rules);
+ }
+
+ private static Font createFont(org.eclipse.fx.code.compensator.hsl.hSL.Font font) {
+ if( font == null ) {
+ return null;
+ }
+
+ return Font.font(font.getName(), font.getAttributes().contains(FontType.BOLD) ? FontWeight.BOLD : FontWeight.NORMAL, font.getAttributes().contains(FontType.ITALIC) ? FontPosture.ITALIC : FontPosture.ITALIC, font.getSize());
+ }
+
+ private static Color createColor(org.eclipse.fx.code.compensator.hsl.hSL.Color color) {
+ if( color instanceof RGBColor ) {
+ RGBColor rcolor = (RGBColor) color;
+ return Color.rgb(rcolor.getR(), rcolor.getG(), rcolor.getB());
+ }
+ return null;
+ }
+
+ static class JSWSDectector implements IWhitespaceDetector {
+
+ @Override
+ public boolean isWhitespace(char c) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ }
+
+ static class FixedCharacterWSDetector implements IWhitespaceDetector {
+ private final char[] chars;
+
+ public FixedCharacterWSDetector(List<String> list) {
+ chars = new char[list.size()];
+ int i = 0;
+ for( String s : list ) {
+ chars[i++] = s.charAt(0);
+ }
+ }
+
+ @Override
+ public boolean isWhitespace(char c) {
+ for( char ch : chars ) {
+ if( ch == c ) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+}
diff --git a/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/JavaScriptHelper.java b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/JavaScriptHelper.java
new file mode 100644
index 000000000..d6448b36e
--- /dev/null
+++ b/experimental/compensator/org.eclipse.fx.code.editor.hsl/src/org/eclipse/fx/code/editor/hsl/internal/JavaScriptHelper.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2014 BestSolution.at 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:
+ * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.fx.code.editor.hsl.internal;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+
+public class JavaScriptHelper {
+ private static String getPluginPath(URI uri) {
+ String[] parts = uri.segments();
+ StringBuffer b = new StringBuffer(parts[2]);
+
+ for( int i = 3; i < parts.length; i++ ) {
+ b.append("/" + parts[i]);
+ }
+ return b.toString();
+ }
+
+ public static <O> O loadScript(ClassLoader cl, EObject m, String jsURL) {
+ URI uri = m.eResource().getURI();
+ uri = uri.trimSegments(1);
+ uri = uri.appendSegment(jsURL);
+
+ ScriptEngineManager mgr = new ScriptEngineManager();
+ ScriptEngine engine = mgr.getEngineByName("nashorn");
+// System.err.println(" ======================> " + uri);
+ try( InputStreamReader reader = new InputStreamReader(uri.isPlatform() ? cl.getResourceAsStream(getPluginPath(uri)) : new FileInputStream(new File(uri.toFileString()))) ) {
+ return (O) engine.eval(reader);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (ScriptException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+}

Back to the top