Skip to content

Commit

Permalink
dsldevkit#51: Autocorrect to add check to configuration does not put …
Browse files Browse the repository at this point in the history
…string

params in quotes

Autocorrect to add check to configuration now does put string params in
quotes.
It also now handles default string parameters containing whitespace;
previously it did not.

Issue-Id: dsldevkit#51
  • Loading branch information
GrahamPearsonAvaloq committed Feb 6, 2018
1 parent d95db3f commit aeb04c0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
import org.eclipse.xtext.ui.editor.IXtextEditorCallback;
import org.eclipse.xtext.ui.editor.contentassist.ITemplateProposalProvider;
import org.eclipse.xtext.ui.editor.templates.CrossReferenceTemplateVariableResolver;
import org.eclipse.xtext.ui.editor.templates.XtextTemplateContextType;
import org.eclipse.xtext.xbase.compiler.GeneratorConfigProvider;
import org.eclipse.xtext.xbase.compiler.IGeneratorConfigProvider;

import com.avaloq.tools.ddk.checkcfg.ui.templates.CheckCfgTemplateContextType;
import com.avaloq.tools.ddk.checkcfg.ui.templates.CheckCfgTemplateProposalProvider;
import com.avaloq.tools.ddk.xtext.ui.editor.FixedDirtyStateEditorSupport;
import com.avaloq.tools.ddk.xtext.ui.templates.KeywordAwareCrossReferenceTemplateVariableResolver;
import com.avaloq.tools.ddk.xtext.ui.templates.SimpleEnumTemplateVariableResolver;


/**
Expand All @@ -31,6 +34,15 @@ public CheckCfgUiModule(final AbstractUIPlugin plugin) {
super(plugin);
}

/**
* Binds a {@link XtextTemplateContextType} which adds {@link SimpleEnumTemplateVariableResolver}.
*
* @return {@link CheckCfgTemplateContextType}
*/
public Class<? extends XtextTemplateContextType> bindXtextTemplateContextType() {
return CheckCfgTemplateContextType.class;
}

/** Binds a proposal provider for check configuration templates. {@inheritDoc} */
@Override
public Class<? extends ITemplateProposalProvider> bindITemplateProposalProvider() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* 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.checkcfg.ui.templates;

import org.eclipse.xtext.ui.editor.templates.XtextTemplateContextType;

import com.avaloq.tools.ddk.xtext.ui.templates.SimpleEnumTemplateVariableResolver;


/**
* Used for adding custom template variable resolvers.
*/
public class CheckCfgTemplateContextType extends XtextTemplateContextType {

@Override
protected void addDefaultTemplateVariables() {
super.addDefaultTemplateVariables();
addResolver(new SimpleEnumTemplateVariableResolver());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.StringJoiner;

import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.text.templates.ContextTypeRegistry;
Expand All @@ -22,6 +24,7 @@
import org.eclipse.jface.text.templates.persistence.TemplateStore;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.conversion.impl.QualifiedNameValueConverter;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.scoping.IScopeProvider;
Expand All @@ -31,6 +34,7 @@
import org.eclipse.xtext.ui.editor.templates.DefaultTemplateProposalProvider;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter;
import org.eclipse.xtext.xbase.jvmmodel.JvmTypeReferenceBuilder;

import com.avaloq.tools.ddk.check.check.Check;
import com.avaloq.tools.ddk.check.check.CheckCatalog;
Expand Down Expand Up @@ -70,6 +74,9 @@ public class CheckCfgTemplateProposalProvider extends DefaultTemplateProposalPro

private final TemplateStore templateStore;

@Inject
private JvmTypeReferenceBuilder.Factory typeRefBuilderFactory;

@Inject
public CheckCfgTemplateProposalProvider(final TemplateStore templateStore, final ContextTypeRegistry registry, final ContextTypeIdHelper helper) {
super(templateStore, registry, helper);
Expand Down Expand Up @@ -125,8 +132,8 @@ protected void createTemplates(final TemplateContext templateContext, final Cont
*/
@SuppressWarnings("all")
private void addCatalogConfigurations(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) {
final String templateName = "Add all registered catalogs";
final String templateDescription = "configures all missing catalogs";
final String templateName = "Add all registered catalogs"; //$NON-NLS-1$
final String templateDescription = "configures all missing catalogs"; //$NON-NLS-1$

final String contextTypeId = templateContext.getContextType().getId();
if (context.getRootModel() instanceof CheckConfiguration) {
Expand All @@ -145,20 +152,20 @@ private void addCatalogConfigurations(final TemplateContext templateContext, fin
} else if (allElements.indexOf(description) > 0) {
builder.append(Strings.newLine());
}
builder.append("catalog ").append(qualifiedNameValueConverter.toString(description.getQualifiedName().toString())).append(" {").append(Strings.newLine());
builder.append("catalog ").append(qualifiedNameValueConverter.toString(description.getQualifiedName().toString())).append(" {").append(Strings.newLine()); //$NON-NLS-1$ //$NON-NLS-2$
for (Check check : catalog.getAllChecks()) {
builder.append(" default ").append(qualifiedNameValueConverter.toString(check.getName())).append(Strings.newLine());
builder.append(" default ").append(qualifiedNameValueConverter.toString(check.getName())).append(Strings.newLine()); //$NON-NLS-1$
}
// CHECKSTYLE:OFF
builder.append("}");
builder.append("}"); //$NON-NLS-1$
builder.append(Strings.newLine());
// CHECKSTYLE:ON
}

}

if (builder.length() > 0) {
builder.append("${cursor}");
builder.append("${cursor}"); //$NON-NLS-1$
Template t = new Template(templateName, templateDescription, contextTypeId, builder.toString(), true);
TemplateProposal tp = createProposal(t, templateContext, context, images.forConfiguredCatalog(), getRelevance(t));
acceptor.accept(tp);
Expand Down Expand Up @@ -216,31 +223,29 @@ public String apply(final ConfiguredCheck from) {
}
}), Predicates.notNull());
final CheckCatalog catalog = configuredCatalog.getCatalog();
final JvmType stringType = typeRefBuilderFactory.create(context.getResource().getResourceSet()).typeRef(String.class).getType();
for (final Check check : catalog.getAllChecks()) {
// create a template on the fly
final String checkName = check.getName();
if (!Iterables.contains(alreadyConfiguredCheckNames, checkName)) {

// check if referenced check has configurable parameters
String paramString = ""; //$NON-NLS-1$
if (!check.getFormalParameters().isEmpty()) {
StringBuilder params = new StringBuilder("("); //$NON-NLS-1$
for (final FormalParameter p : check.getFormalParameters()) {
final String paramName = p.getName();
final String defaultValue = String.valueOf(interpreter.evaluate(p.getRight()).getResult());
params.append(paramName).append(" = ").append("${").append(defaultValue).append('}'); //$NON-NLS-1$ //$NON-NLS-2$
params.append(", "); //$NON-NLS-1$
}
if (params.length() > 2) {
paramString = params.substring(0, params.length() - 2) + ')';
}
final StringJoiner paramsJoiner = new StringJoiner(", ", " ( ", ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
paramsJoiner.setEmptyValue(""); //$NON-NLS-1$
for (final FormalParameter param : check.getFormalParameters()) {
final JvmType paramType = param.getType().getType();
final String paramName = param.getName();
final String defaultValue = String.valueOf(interpreter.evaluate(param.getRight()).getResult());

// Use SimpleEnumTemplateVariableResolver with strings because otherwise TemplateTranslator can't cope with whitespace
paramsJoiner.add(paramName + " = " + (Objects.equals(stringType, paramType) ? "\"${defaultValue:SimpleEnum('" + defaultValue + "')}\"" //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
: "${" + defaultValue + '}')); //$NON-NLS-1$
}

final String severity = (catalog.isFinal() || check.isFinal()) ? "default " : "${default:Enum('SeverityKind')} "; //$NON-NLS-1$ //$NON-NLS-2$
final String description = "Configures the check \"" + check.getLabel() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
final String contextTypeId = "com.avaloq.tools.ddk.checkcfg.CheckCfg.ConfiguredCheck." + checkName; //$NON-NLS-1$
final String pattern = severity + qualifiedNameValueConverter.toString(checkName)
+ (paramString.length() == 0 ? "${cursor}" : " " + paramString + "${cursor}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
final String pattern = severity + qualifiedNameValueConverter.toString(checkName) + paramsJoiner + "${cursor}"; //$NON-NLS-1$

Template t = new Template(checkName, description, contextTypeId, pattern, true);
TemplateProposal tp = createProposal(t, templateContext, context, images.forConfiguredCheck(check.getDefaultSeverity()), getRelevance(t));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.avaloq.tools.ddk.xtext.ui.templates;

import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jface.text.templates.TemplateVariable;
import org.eclipse.xtext.ui.editor.templates.AbstractTemplateVariableResolver;
import org.eclipse.xtext.ui.editor.templates.XtextTemplateContext;


/**
* Simple enumeration template where enumeration is passed as string arguments to the template.
*/
public class SimpleEnumTemplateVariableResolver extends AbstractTemplateVariableResolver {

public SimpleEnumTemplateVariableResolver() {
super("SimpleEnum", ""); //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
public List<String> resolveValues(final TemplateVariable variable, final XtextTemplateContext castedContext) {
return variable.getVariableType().getParams().stream().filter(param -> param instanceof String).collect(Collectors.toList());
}

}

0 comments on commit aeb04c0

Please sign in to comment.