diff --git a/com.avaloq.tools.ddk.xtext.ui/.classpath b/com.avaloq.tools.ddk.xtext.ui/.classpath index 87e4027a1c..974d542993 100644 --- a/com.avaloq.tools.ddk.xtext.ui/.classpath +++ b/com.avaloq.tools.ddk.xtext.ui/.classpath @@ -1,7 +1,6 @@ - diff --git a/com.avaloq.tools.ddk.xtext.ui/.project b/com.avaloq.tools.ddk.xtext.ui/.project index 4c73890035..854b0a4359 100644 --- a/com.avaloq.tools.ddk.xtext.ui/.project +++ b/com.avaloq.tools.ddk.xtext.ui/.project @@ -20,11 +20,6 @@ - - org.eclipse.xtext.ui.shared.xtextBuilder - - - net.sf.eclipsecs.core.CheckstyleBuilder @@ -44,7 +39,6 @@ org.eclipse.jdt.core.javanature org.eclipse.pde.PluginNature - org.eclipse.xtext.ui.shared.xtextNature net.sourceforge.pmd.eclipse.plugin.pmdNature net.sf.eclipsecs.core.CheckstyleNature edu.umd.cs.findbugs.plugin.eclipse.findbugsNature diff --git a/com.avaloq.tools.ddk.xtext.ui/META-INF/MANIFEST.MF b/com.avaloq.tools.ddk.xtext.ui/META-INF/MANIFEST.MF index 783ec2f6aa..5cde4e95ce 100644 --- a/com.avaloq.tools.ddk.xtext.ui/META-INF/MANIFEST.MF +++ b/com.avaloq.tools.ddk.xtext.ui/META-INF/MANIFEST.MF @@ -10,7 +10,6 @@ Require-Bundle: com.avaloq.tools.ddk.xtext, org.eclipse.jdt.core, org.eclipse.xtext.ui, org.eclipse.xtext.ui.shared, - org.eclipse.xtext.xbase.lib, org.eclipse.search, org.eclipse.ui.ide, org.apache.commons.lang3, diff --git a/com.avaloq.tools.ddk.xtext.ui/build.properties b/com.avaloq.tools.ddk.xtext.ui/build.properties index 9ff95aa6e5..f55bd01264 100644 --- a/com.avaloq.tools.ddk.xtext.ui/build.properties +++ b/com.avaloq.tools.ddk.xtext.ui/build.properties @@ -1,5 +1,4 @@ -source.. = src/,\ - xtend-gen/ +source.. = src/ bin.includes = META-INF/,\ icons/,\ schema/,\ diff --git a/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/templates/TemplateProposalProviderHelper.java b/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/templates/TemplateProposalProviderHelper.java new file mode 100644 index 0000000000..996784c9e1 --- /dev/null +++ b/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/templates/TemplateProposalProviderHelper.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2016 Avaloq Group 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 Group AG - initial API and implementation + *******************************************************************************/ +package com.avaloq.tools.ddk.xtext.ui.templates; + +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.Validate; +import org.apache.commons.text.StringEscapeUtils; + +import com.google.inject.Singleton; + +/** + * Helper methods for {@link org.eclipse.xtext.ui.editor.contentassist.ITemplateProposalProvider ITemplateProposalProvider} implementations. + */ +@Singleton +@SuppressWarnings("nls") +public class TemplateProposalProviderHelper { + + private static final String SIMPLE_ENUM_TYPE = new SimpleEnumTemplateVariableResolver().getType(); + + /** + * Create a literal value pattern, including quotes if necessary, for a {@link org.eclipse.jface.text.templates.Template Template}. + * + * @param name the name of the variable, may not be {@code null} nor contain whitespace + * @param defaultValue default value, may be {@code null} + * @return pattern, never {@code null} + * @throws NullPointerException if name is null + * @throws IllegalArgumentException if name contains whitespace + */ + public String createLiteralValuePattern(final String name, final Object defaultValue) throws NullPointerException, IllegalArgumentException { + final String pattern = createTemplateVariablePattern(SIMPLE_ENUM_TYPE, name, defaultValue); + + if (defaultValue instanceof String) { + // Surround pattern with quotes + return "\"" + pattern + "\""; + } else { + return pattern; + } + } + + /** + * Create a variable pattern for a {@link org.eclipse.jface.text.templates.Template Template}. + * + * @param type the type of the variable, may not be {@code null} nor contain whitespace + * @param name the name of the variable, may not be {@code null} nor contain whitespace + * @param values the values available at this variable, may not be {@code null} nor empty + * @return pattern, never {@code null} + * @throws NullPointerException if type, name or values is null + * @throws IllegalArgumentException if type or name contains whitespace or values is empty + */ + public String createTemplateVariablePattern(final String type, final String name, final Object... values) throws NullPointerException, IllegalArgumentException { + Objects.requireNonNull(type); + Objects.requireNonNull(name); + Objects.requireNonNull(values); + Validate.isTrue(!type.chars().anyMatch(Character::isWhitespace)); + Validate.isTrue(!name.chars().anyMatch(Character::isWhitespace)); + Validate.notEmpty(values); + + final Object[] useValues; + if (values.length == 1 && values[0] instanceof Boolean) { + // Offer both false and true as dropdown options + useValues = new Object[] {values[0], !((Boolean) values[0])}; + } else { + useValues = values; + } + + final String sanitisedValues = Arrays.stream(useValues) + .map(it -> + // Escape double-quotes to comply with string grammar. + // Double-up single-quotes to comply with template pattern grammar. + // Wrap each value in single quotes. + "'" + StringEscapeUtils.escapeJava(String.valueOf(it)).replace("'", "''") + "'") + .collect(Collectors.joining(", ")); + + return "${" + name + ":" + type + "(" + sanitisedValues + ")}"; + } + +} diff --git a/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/templates/TemplateProposalProviderHelper.xtend b/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/templates/TemplateProposalProviderHelper.xtend deleted file mode 100644 index 2d3246bdf5..0000000000 --- a/com.avaloq.tools.ddk.xtext.ui/src/com/avaloq/tools/ddk/xtext/ui/templates/TemplateProposalProviderHelper.xtend +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Copyright (c) 2016 Avaloq Group 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 Group AG - initial API and implementation - */ -package com.avaloq.tools.ddk.xtext.ui.templates - -import com.google.inject.Singleton -import java.util.Objects -import org.apache.commons.text.StringEscapeUtils -import org.apache.commons.lang3.Validate -import org.eclipse.jface.text.templates.Template -import org.eclipse.xtext.ui.editor.contentassist.ITemplateProposalProvider - -/** - * Helper methods for {@link ITemplateProposalProvider} implementations. - */ -@Singleton -class TemplateProposalProviderHelper { - - static val SIMPLE_ENUM_TYPE = new SimpleEnumTemplateVariableResolver().type - - /** - * Create a literal value pattern, including quotes if necessary, for a {@link Template}. - * - * @param name the name of the variable, may not be {@code null} nor contain whitespace - * @param defaultValue default value, may be {@code null} - * @return pattern, never {@code null} - * @throws {@link NullPointerException} if name is null - * @throws {@link IllegalArgumentException} if name contains whitespace - */ - def String createLiteralValuePattern(String name, Object defaultValue) throws NullPointerException, IllegalArgumentException { - val pattern = createTemplateVariablePattern(SIMPLE_ENUM_TYPE, name, defaultValue) - - return if (defaultValue instanceof String) { - // Surround pattern with quotes - '''"«pattern»"''' - } else { - pattern - } - } - - /** - * Create a variable pattern for a {@link Template}. - * - * @param type the type of the variable, may not be {@code null} nor contain whitespace - * @param name the name of the variable, may not be {@code null} nor contain whitespace - * @param values the values available at this variable, may not be {@code null} nor empty - * @return pattern, never {@code null} - * @throws {@link NullPointerException} if type, name or values is null - * @throws {@link IllegalArgumentException} if type or name contains whitespace or values is empty - */ - def String createTemplateVariablePattern(String type, String name, Object... values) throws NullPointerException, IllegalArgumentException { - Objects.requireNonNull(type); - Objects.requireNonNull(name); - Objects.requireNonNull(values); - Validate.isTrue(!type.chars().anyMatch[Character.isWhitespace(it)]); - Validate.isTrue(!name.chars().anyMatch[Character.isWhitespace(it)]); - Validate.notEmpty(values); - - val Object[] useValues = if (values.length == 1 && values.get(0) instanceof Boolean) { - // Offer both false and true as dropdown options - #[values.get(0), !(values.get(0) as Boolean)] - } else { - values - } - - val sanitisedValues = useValues.map [ - // Escape double-quotes to comply with string grammar. - // Double-up single-quotes to comply with template pattern grammar. - // Wrap each value in single quotes. - "'" + StringEscapeUtils::escapeJava(String.valueOf(it)).replace("'", "''") + "'" - ] - - return '''${«name»:«type»(«String.join(", ", sanitisedValues)»)}''' - } - -} diff --git a/com.avaloq.tools.ddk.xtext.ui/xtend-gen/.gitignore b/com.avaloq.tools.ddk.xtext.ui/xtend-gen/.gitignore deleted file mode 100644 index e69de29bb2..0000000000