Skip to content

Commit

Permalink
[ui] Unimplemented method quick fix uses the SARL syntax.
Browse files Browse the repository at this point in the history
close #647

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Apr 26, 2017
1 parent 2bfe1c3 commit e3573f2
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 38 deletions.
Expand Up @@ -798,6 +798,11 @@ public XExpression getFormalParameterDefaultValue(int position) {
return null;
}

@Override
public String getFormalParameterDefaultValueString(int position) {
return null;
}

@Override
public EObject getFormalParameter(int position) {
throw new UnsupportedOperationException();
Expand Down
4 changes: 4 additions & 0 deletions main/coreplugins/io.sarl.lang.mwe2/GenerateSARL2.mwe2
Expand Up @@ -692,6 +692,10 @@ Workflow {
to = "io.sarl.lang.ui.hyperlinking.SARLHyperLinkingLabelProvider"
functionName = "configureHyperlinkLabelProvider"
}
ui = {
bind = "org.eclipse.xtend.ide.codebuilder.CodeBuilderFactory"
to = "io.sarl.lang.ui.codebuilder.SarlCodeBuilderFactory"
}
ui = {
bind = "org.eclipse.xtend.ide.codebuilder.XtendMethodBuilder"
to = "io.sarl.lang.ui.codebuilder.SarlMethodBuilder"
Expand Down
Expand Up @@ -33,6 +33,7 @@
import io.sarl.lang.ide.contentassist.antlr.internal.InternalSARLLexer;
import io.sarl.lang.ui.builder.EclipseGeneratorConfigProvider2;
import io.sarl.lang.ui.builder.ProjectRelativeFileSystemAccess;
import io.sarl.lang.ui.codebuilder.SarlCodeBuilderFactory;
import io.sarl.lang.ui.codebuilder.SarlConstructorBuilder;
import io.sarl.lang.ui.codebuilder.SarlFieldBuilder;
import io.sarl.lang.ui.codebuilder.SarlMethodBuilder;
Expand Down Expand Up @@ -84,6 +85,7 @@
import org.eclipse.xtend.ide.XtendResourceUiServiceProvider;
import org.eclipse.xtend.ide.autoedit.AutoEditStrategyProvider;
import org.eclipse.xtend.ide.builder.UIResourceChangeRegistry;
import org.eclipse.xtend.ide.codebuilder.CodeBuilderFactory;
import org.eclipse.xtend.ide.codebuilder.XtendConstructorBuilder;
import org.eclipse.xtend.ide.codebuilder.XtendFieldBuilder;
import org.eclipse.xtend.ide.codebuilder.XtendMethodBuilder;
Expand Down Expand Up @@ -562,6 +564,11 @@ public Class<? extends IGeneratorConfigProvider2> bindIGeneratorConfigProvider2(
return EclipseGeneratorConfigProvider2.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings provided by SARL API]
public Class<? extends CodeBuilderFactory> bindCodeBuilderFactory() {
return SarlCodeBuilderFactory.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings provided by SARL API]
public Class<? extends IContentProposalPriorities> bindIContentProposalPriorities() {
return SARLContentProposalPriorities.class;
Expand Down
@@ -0,0 +1,39 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.sarl.lang.ui.codebuilder;

import org.eclipse.xtend.ide.codebuilder.CodeBuilderFactory;


/** Factory for the ICodeBuilder builders.
*
* <p>This implementation generates the code according to the SARL syntax (not the Xtend syntax).
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.6
*/
public class SarlCodeBuilderFactory extends CodeBuilderFactory {
//
}
Expand Up @@ -21,9 +21,12 @@

package io.sarl.lang.ui.codebuilder;

import java.util.List;

import javax.inject.Inject;

import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtend.ide.codebuilder.AbstractParameterBuilder;
import org.eclipse.xtend.ide.codebuilder.XtendMethodBuilder;
import org.eclipse.xtext.common.types.JvmVisibility;
import org.eclipse.xtext.xbase.compiler.ISourceAppender;
Expand Down Expand Up @@ -85,9 +88,13 @@ public ISourceAppender build(ISourceAppender appendable) {

@Override
protected ISourceAppender appendParameters(ISourceAppender appendable) {
if (getParameterBuilders().isEmpty()) {
final List<AbstractParameterBuilder> builders = getParameterBuilders();
if (builders.isEmpty()) {
return appendable;
}
if (isAbstractFlag()) {
((SarlParameterBuilder) builders.get(builders.size())).setDefaultValue(null);
}
return super.appendParameters(appendable);
}

Expand Down
Expand Up @@ -24,6 +24,7 @@
import javax.inject.Inject;

import org.eclipse.xtend.ide.codebuilder.XtendParameterBuilder;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.compiler.ISourceAppender;
import org.eclipse.xtext.xbase.typesystem.references.ArrayTypeReference;

Expand All @@ -45,6 +46,24 @@ public class SarlParameterBuilder extends XtendParameterBuilder {
@Inject
private SARLGrammarKeywordAccess keywords;

private String defaultValue;

/** Replies the default value for the parameter.
*
* @return the default value, or {@code null}.
*/
public String getDefaultValue() {
return this.defaultValue;
}

/** Change the default value for the parameter.
*
* @param defaultValue the default value, or {@code null}.
*/
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

@Override
public ISourceAppender build(ISourceAppender appendable) {
appendModifiers(appendable);
Expand All @@ -57,6 +76,11 @@ public ISourceAppender build(ISourceAppender appendable) {
appendable.append(this.keywords.getWildcardAsteriskKeyword());
} else {
appendType(appendable, getType(), Object.class.getName());
final String defaultVal = getDefaultValue();
if (!Strings.isEmpty(defaultVal)) {
appendable.append(" ").append(this.keywords.getEqualsSignKeyword()); //$NON-NLS-1$
appendable.append(" ").append(defaultVal); //$NON-NLS-1$
}
}
return appendable;
}
Expand Down
Expand Up @@ -60,8 +60,14 @@
import org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable;
import org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters;

import io.sarl.lang.actionprototype.FormalParameterProvider;
import io.sarl.lang.actionprototype.IActionPrototypeProvider;
import io.sarl.lang.actionprototype.InferredPrototype;
import io.sarl.lang.actionprototype.QualifiedActionName;
import io.sarl.lang.annotation.SyntheticMember;
import io.sarl.lang.jvmmodel.SarlJvmModelAssociations;
import io.sarl.lang.typesystem.SARLAnnotationUtil;
import io.sarl.lang.ui.codebuilder.SarlParameterBuilder;
import io.sarl.lang.ui.quickfix.SARLQuickfixProvider;
import io.sarl.lang.util.Utils;

Expand All @@ -87,6 +93,12 @@ public final class MissedMethodAddModification extends SARLSemanticModification
@Inject
private CommonTypeComputationServices services;

@Inject
private IActionPrototypeProvider actionPrototypeProvider;

@Inject
private SARLAnnotationUtil annotationUtils;

private MissedMethodAddModification(String[] operationUris) {
this.operationUris = operationUris;
}
Expand Down Expand Up @@ -138,7 +150,8 @@ public void apply(EObject element, IModificationContext context) throws Exceptio
final Map<String, JvmTypeReference> typeParameterMap = buildTypeParameterMapping(container);

for (final JvmOperation operation : tools.getJvmOperationsFromURIs(container, this.operationUris)) {
if (!isGeneratedOperation(operation)) {
if (this.annotationUtils.findAnnotation(operation, SyntheticMember.class.getName()) == null
&& !isGeneratedOperation(operation)) {

appendable.newLine().newLine();

Expand All @@ -156,8 +169,27 @@ public void apply(EObject element, IModificationContext context) throws Exceptio

builder.setVisibility(operation.getVisibility());
builder.setTypeParameters(cloneTypeParameters(operation.getTypeParameters()));

final QualifiedActionName qualifiedActionName = this.actionPrototypeProvider.createQualifiedActionName(
declaringType,
operation.getSimpleName());
final InferredPrototype prototype = this.actionPrototypeProvider.createPrototypeFromJvmModel(
qualifiedActionName,
operation.isVarArgs(),
operation.getParameters());
final FormalParameterProvider formalParameters = prototype.getFormalParameters();

int i = 0;
for (final JvmFormalParameter parameter : operation.getParameters()) {
builder.newParameterBuilder().setType(cloneTypeReference(parameter.getParameterType(), typeParameterMap));
final SarlParameterBuilder paramBuilder = (SarlParameterBuilder) builder.newParameterBuilder();
paramBuilder.setType(cloneTypeReference(parameter.getParameterType(), typeParameterMap));
if (formalParameters.hasFormalParameterDefaultValue(i)) {
final String defaultValue = formalParameters.getFormalParameterDefaultValueString(i);
if (defaultValue != null) {
paramBuilder.setDefaultValue(defaultValue);
}
}
++i;
}
builder.setVarArgsFlag(operation.isVarArgs());

Expand Down
Expand Up @@ -30,18 +30,26 @@

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.xtend.core.xtend.XtendParameter;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.JvmField;
import org.eclipse.xtext.common.types.JvmFormalParameter;
import org.eclipse.xtext.common.types.JvmIdentifiableElement;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.util.AnnotationLookup;
import org.eclipse.xtext.common.types.util.TypeReferences;
import org.eclipse.xtext.xbase.lib.Pair;

import io.sarl.lang.annotation.DefaultValue;
import io.sarl.lang.annotation.SarlSourceCode;
import io.sarl.lang.sarl.SarlFormalParameter;
import io.sarl.lang.services.SARLGrammarKeywordAccess;
import io.sarl.lang.typesystem.SARLAnnotationUtil;
import io.sarl.lang.util.Utils;

/**
Expand Down Expand Up @@ -69,6 +77,9 @@ public class DefaultActionPrototypeProvider implements IActionPrototypeProvider
@Inject
private AnnotationLookup annotationFinder;

@Inject
private SARLAnnotationUtil annotationUtils;

/** Construct a provider of action prototypes.
*/
public DefaultActionPrototypeProvider() {
Expand Down Expand Up @@ -266,7 +277,7 @@ public final InferredPrototype createPrototypeFromSarlModel(QualifiedActionName
public final InferredPrototype createPrototypeFromJvmModel(QualifiedActionName id,
boolean isVarargs, List<JvmFormalParameter> parameters) {
return createPrototype(id, isVarargs,
new JvmFormalParameterProvider(parameters, this.annotationFinder));
new JvmFormalParameterProvider(parameters, this.annotationFinder, this));
}

@Override
Expand Down Expand Up @@ -407,4 +418,40 @@ public String toJavaArgument(String callerQualifiedName, String id) {
return b.toString();
}

@Override
public String extractDefaultValueString(JvmFormalParameter parameter) {
final String fieldId = this.annotationUtils.findStringValue(parameter, DefaultValue.class);
if (!Strings.isNullOrEmpty(fieldId)) {
final JvmDeclaredType container = EcoreUtil2.getContainerOfType(parameter, JvmDeclaredType.class);
if (container != null) {
final int index = fieldId.indexOf('#');
final JvmDeclaredType target;
final String fieldName;
if (index > 0) {
final JvmType type = this.references.findDeclaredType(fieldId.substring(0, index), container);
if (type instanceof JvmDeclaredType) {
target = (JvmDeclaredType) type;
} else {
target = container;
}
fieldName = Utils.createNameForHiddenDefaultValueAttribute(fieldId.substring(index + 1));
} else {
target = container;
fieldName = Utils.createNameForHiddenDefaultValueAttribute(fieldId);
}

final JvmField field = Iterables.find(target.getDeclaredFields(),
(it) -> Objects.equal(it.getSimpleName(), fieldName),
null);
if (field != null) {
final String value = this.annotationUtils.findStringValue(field, SarlSourceCode.class);
if (!Strings.isNullOrEmpty(fieldId)) {
return value;
}
}
}
}
return null;
}

}
Expand Up @@ -73,12 +73,31 @@ public interface FormalParameterProvider {
boolean hasFormalParameterDefaultValue(int position);

/** Replies the default value of the formal parameter at the given position.
*
* <p>This function replies the Xbase expression for the default value.
*
* <p>If this function replies {@code null}, the string representation of the
* default value may be still available. See {@link #getFormalParameterDefaultValueString(int)}.
*
* @param position - the position of the formal parameter.
* @return the default value, or <code>null</code> if none.
* @see #getFormalParameterDefaultValueString(int)
*/
XExpression getFormalParameterDefaultValue(int position);

/** Replies the default value of the formal parameter at the given position.
*
* <p>This function replies the string representation of the default value.
*
* <p>If this function replies {@code null} or an empty string of characters, the Xbase representation of the
* default value may be still available. See {@link #getFormalParameterDefaultValue(int)}.
*
* @param position - the position of the formal parameter.
* @return the default value, or <code>null</code> if none.
* @see #getFormalParameterDefaultValue(int)
*/
String getFormalParameterDefaultValueString(int position);

/** Replies the formal parameter at the given position.
*
* @param position - the position of the formal parameter.
Expand Down
Expand Up @@ -175,4 +175,13 @@ InferredPrototype createPrototypeFromSarlModel(QualifiedActionName id, boolean i
*/
String toJavaArgument(String callerQualifiedName, String argumentSpecification);

/** Replies the default value of the formal parameter at the given position.
*
* <p>This function replies the string representation of the default value.
*
* @param parameter - the parameter for which the default valud should be extracted.
* @return the default value, or <code>null</code> if none.
*/
String extractDefaultValueString(JvmFormalParameter parameter);

}

0 comments on commit e3573f2

Please sign in to comment.