Search in sources :

Example 1 with StringBuilderBasedAppendable

use of org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable in project xtext-xtend by eclipse.

the class CreateMemberQuickfixes method newLocalVariableQuickfix.

protected void newLocalVariableQuickfix(final String variableName, XAbstractFeatureCall call, Issue issue, IssueResolutionAcceptor issueResolutionAcceptor) {
    LightweightTypeReference variableType = getNewMemberType(call);
    final StringBuilderBasedAppendable localVarDescriptionBuilder = new StringBuilderBasedAppendable();
    localVarDescriptionBuilder.append("...").newLine();
    final String defaultValueLiteral = getDefaultValueLiteral(variableType);
    localVarDescriptionBuilder.append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
    localVarDescriptionBuilder.newLine().append("...");
    issueResolutionAcceptor.accept(issue, "Create local variable '" + variableName + "'", localVarDescriptionBuilder.toString(), "fix_local_var.png", new SemanticModificationWrapper(issue.getUriToProblem(), new ISemanticModification() {

        @Override
        public void apply(/* @Nullable */
        final EObject element, /* @Nullable */
        final IModificationContext context) throws Exception {
            if (element != null) {
                XtendMember xtendMember = EcoreUtil2.getContainerOfType(element, XtendMember.class);
                if (xtendMember != null) {
                    int offset = getFirstOffsetOfKeyword(xtendMember, "{");
                    IXtextDocument xtextDocument = context.getXtextDocument();
                    if (offset != -1 && xtextDocument != null) {
                        final ReplacingAppendable appendable = appendableFactory.create(xtextDocument, (XtextResource) element.eResource(), offset, 0, new OptionalParameters() {

                            {
                                baseIndentationLevel = 1;
                            }
                        });
                        appendable.increaseIndentation().newLine().append("val ").append(variableName).append(" = ").append(defaultValueLiteral);
                        appendable.commitChanges();
                    }
                }
            }
        }
    }));
}
Also used : OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) SemanticModificationWrapper(org.eclipse.xtext.ui.editor.model.edit.SemanticModificationWrapper) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 2 with StringBuilderBasedAppendable

use of org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable in project xtext-xtend by eclipse.

the class ExtractMethodRefactoring method getMethodSignature.

public String getMethodSignature() {
    StringBuilderBasedAppendable appendable = new StringBuilderBasedAppendable();
    appendMethodSignature(appendable);
    return appendable.toString();
}
Also used : StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable)

Example 3 with StringBuilderBasedAppendable

use of org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable in project xtext-xtend by eclipse.

the class CreateMemberQuickfixes method newLocalVariableQuickfix.

protected void newLocalVariableQuickfix(final String variableName, boolean isFinal, XAbstractFeatureCall call, Issue issue, IssueResolutionAcceptor issueResolutionAcceptor) {
    LightweightTypeReference variableType = getNewMemberType(call);
    final StringBuilderBasedAppendable localVarDescriptionBuilder = new StringBuilderBasedAppendable();
    localVarDescriptionBuilder.append("...").newLine();
    final String defaultValueLiteral = getDefaultValueLiteral(variableType);
    localVarDescriptionBuilder.append(isFinal ? "val " : "var ").append(variableName).append(" = ").append(defaultValueLiteral);
    localVarDescriptionBuilder.newLine().append("...");
    issueResolutionAcceptor.accept(issue, "Create local " + (isFinal ? "value" : "variable") + " '" + variableName + "'", localVarDescriptionBuilder.toString(), "fix_local_var.png", new SemanticModificationWrapper(issue.getUriToProblem(), new ISemanticModification() {

        @Override
        public void apply(/* @Nullable */
        final EObject element, /* @Nullable */
        final IModificationContext context) throws Exception {
            if (element != null) {
                XtendMember xtendMember = EcoreUtil2.getContainerOfType(element, XtendMember.class);
                if (xtendMember != null) {
                    int offset = getFirstOffsetOfKeyword(xtendMember, "{");
                    IXtextDocument xtextDocument = context.getXtextDocument();
                    if (offset != -1 && xtextDocument != null) {
                        final ReplacingAppendable appendable = appendableFactory.create(xtextDocument, (XtextResource) element.eResource(), offset, 0, new OptionalParameters() {

                            {
                                baseIndentationLevel = 1;
                            }
                        });
                        appendable.increaseIndentation().newLine().append(isFinal ? "val " : "var ").append(variableName).append(" = ").append(defaultValueLiteral);
                        appendable.commitChanges();
                    }
                }
            }
        }
    }));
}
Also used : OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) SemanticModificationWrapper(org.eclipse.xtext.ui.editor.model.edit.SemanticModificationWrapper) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 4 with StringBuilderBasedAppendable

use of org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable in project xtext-xtend by eclipse.

the class CompilationContextImpl method toJavaCode.

@Override
public String toJavaCode(final TypeReference typeref) {
    StringBuilderBasedAppendable _xifexpression = null;
    if ((this.importManager != null)) {
        _xifexpression = new StringBuilderBasedAppendable(this.importManager);
    } else {
        _xifexpression = new StringBuilderBasedAppendable();
    }
    final StringBuilderBasedAppendable appendable = _xifexpression;
    final LightweightTypeReference typeRef = this.compilationUnit.toLightweightTypeReference(typeref);
    appendable.append(typeRef);
    return appendable.toString();
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable)

Example 5 with StringBuilderBasedAppendable

use of org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable in project xtext-xtend by eclipse.

the class SuperMemberImplementorTest method checkImplementConstructor.

protected void checkImplementConstructor(final String firstParamType, String implementCode) {
    StringBuilderBasedAppendable appendable = new StringBuilderBasedAppendable();
    JvmConstructor constructor = Iterables.find(superClass.getDeclaredConstructors(), new Predicate<JvmConstructor>() {

        @Override
        public boolean apply(JvmConstructor c) {
            if (firstParamType == null)
                return c.getParameters().isEmpty();
            if (c.getParameters().size() >= 1) {
                return firstParamType.equals(c.getParameters().get(0).getParameterType().getSimpleName());
            }
            return false;
        }
    });
    LightweightTypeReference contextType = getContextType();
    ResolvedConstructor resolvedConstructor = new ResolvedConstructor(constructor, contextType);
    implementor.appendConstructorFromSuper(xtendClass, resolvedConstructor, appendable);
    String code = appendable.toString();
    if (!equalsIgnoreWhitespace(implementCode, code))
        assertEquals(implementCode, code);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable) ResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.ResolvedConstructor)

Aggregations

StringBuilderBasedAppendable (org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable)8 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)5 EObject (org.eclipse.emf.ecore.EObject)3 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)3 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)2 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)2 SemanticModificationWrapper (org.eclipse.xtext.ui.editor.model.edit.SemanticModificationWrapper)2 ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)2 OptionalParameters (org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters)2 Consumer (java.util.function.Consumer)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IMember (org.eclipse.jdt.core.IMember)1 IType (org.eclipse.jdt.core.IType)1 JavaConstructorBuilder (org.eclipse.xtend.ide.codebuilder.JavaConstructorBuilder)1 JavaFieldBuilder (org.eclipse.xtend.ide.codebuilder.JavaFieldBuilder)1 JavaMethodBuilder (org.eclipse.xtend.ide.codebuilder.JavaMethodBuilder)1 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)1 JdtHyperlink (org.eclipse.xtext.common.types.xtext.ui.JdtHyperlink)1