Search in sources :

Example 1 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.

the class JavaTypeQuickfixes method createImportProposals.

@SuppressWarnings("restriction")
protected void createImportProposals(final JvmDeclaredType contextType, final Issue issue, String typeName, IJavaSearchScope searchScope, final IssueResolutionAcceptor acceptor) throws JavaModelException {
    if (contextType != null) {
        final IVisibilityHelper visibilityHelper = getVisibilityHelper(contextType);
        final Pair<String, String> packageAndType = typeNameGuesser.guessPackageAndTypeName(contextType, typeName);
        final String wantedPackageName = packageAndType.getFirst();
        org.eclipse.jdt.internal.core.search.BasicSearchEngine searchEngine = new org.eclipse.jdt.internal.core.search.BasicSearchEngine();
        final char[] wantedPackageChars = (isEmpty(wantedPackageName)) ? null : wantedPackageName.toCharArray();
        final String wantedTypeName = packageAndType.getSecond();
        searchEngine.searchAllTypeNames(wantedPackageChars, SearchPattern.R_EXACT_MATCH, wantedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, IJavaSearchConstants.TYPE, searchScope, new org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor() {

            @Override
            public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, org.eclipse.jdt.internal.compiler.env.AccessRestriction access) {
                final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName);
                if (access == null || (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter())) {
                    JvmType importType = services.getTypeReferences().findDeclaredType(qualifiedTypeName, contextType);
                    if (importType instanceof JvmDeclaredType && visibilityHelper.isVisible((JvmDeclaredType) importType)) {
                        StringBuilder label = new StringBuilder("Import '");
                        label.append(simpleTypeName);
                        label.append("' (");
                        label.append(packageName);
                        if (enclosingTypeNames.length > 0) {
                            for (char[] enclosingTypeName : enclosingTypeNames) {
                                label.append(".");
                                label.append(enclosingTypeName);
                            }
                        }
                        label.append(")");
                        acceptor.accept(issue, label.toString(), label.toString(), "impc_obj.gif", new ISemanticModification() {

                            @Override
                            public void apply(EObject element, IModificationContext context) throws Exception {
                                ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) element.eResource(), 0, 0);
                                appendable.append(services.getTypeReferences().findDeclaredType(qualifiedTypeName, element));
                                appendable.insertNewImports();
                            }
                        }, jdtTypeRelevance.getRelevance(qualifiedTypeName, wantedTypeName) + 100);
                    }
                }
            }
        }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.IVisibilityHelper) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext)

Example 2 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method fixIncompleteCasesOnEnum.

@Fix(IssueCodes.INCOMPLETE_CASES_ON_ENUM)
public void fixIncompleteCasesOnEnum(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Add 'default' case", "Add 'default' case", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
            if (switchExpression == null) {
                return;
            }
            int insertOffset = getInsertOffset(switchExpression);
            IXtextDocument document = context.getXtextDocument();
            ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) element.eResource(), insertOffset, 0);
            if (switchExpression.getCases().isEmpty()) {
                appendable.increaseIndentation();
            }
            appendable.newLine();
            appendable.append("default: {");
            appendable.newLine().append("}");
            appendable.commitChanges();
        }
    });
    acceptor.accept(issue, "Add missing cases", "Add missing cases", null, new ISemanticModification() {

        @Override
        public void apply(EObject element, IModificationContext context) throws Exception {
            XSwitchExpression switchExpression = EcoreUtil2.getContainerOfType(element, XSwitchExpression.class);
            if (switchExpression == null) {
                return;
            }
            int insertOffset = getInsertOffset(switchExpression);
            IXtextDocument document = context.getXtextDocument();
            ReplacingAppendable appendable = appendableFactory.create(document, (XtextResource) element.eResource(), insertOffset, 0);
            if (switchExpression.getCases().isEmpty()) {
                appendable.increaseIndentation();
            }
            for (String expectedEnumerationLiteral : issue.getData()) {
                appendable.newLine().append("case ").append(expectedEnumerationLiteral).append(": {");
                appendable.newLine().append("}");
            }
            appendable.commitChanges();
        }
    });
}
Also used : EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) XSwitchExpression(org.eclipse.xtext.xbase.XSwitchExpression) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) WrappedException(org.eclipse.emf.common.util.WrappedException) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 3 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method addTypeCastToExplicitReceiver.

private void addTypeCastToExplicitReceiver(XAbstractFeatureCall featureCall, IModificationContext context, JvmType declaringType, EReference structuralFeature) throws BadLocationException {
    List<INode> nodes = NodeModelUtils.findNodesForFeature(featureCall, structuralFeature);
    if (nodes.isEmpty())
        return;
    INode firstNode = IterableExtensions.head(nodes);
    INode lastNode = IterableExtensions.last(nodes);
    int offset = firstNode.getOffset();
    int length = lastNode.getEndOffset() - offset;
    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) featureCall.eResource(), offset, length);
    appendable.append("(");
    ListIterator<INode> nodeIter = nodes.listIterator();
    while (nodeIter.hasNext()) {
        String text = nodeIter.next().getText();
        if (nodeIter.previousIndex() == 0)
            appendable.append(Strings.removeLeadingWhitespace(text));
        else if (nodeIter.nextIndex() == nodes.size())
            appendable.append(Strings.trimTrailingLineBreak(text));
        else
            appendable.append(text);
    }
    appendable.append(" as ");
    appendable.append(declaringType);
    appendable.append(")");
    appendable.commitChanges();
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)

Example 4 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable in project xtext-eclipse by eclipse.

the class XbaseQuickfixProvider method addTypeCastToImplicitReceiver.

private void addTypeCastToImplicitReceiver(XFeatureCall featureCall, IModificationContext context, JvmType declaringType) throws BadLocationException {
    String receiver;
    if (featureCall.getImplicitReceiver() instanceof XAbstractFeatureCall)
        receiver = ((XAbstractFeatureCall) featureCall.getImplicitReceiver()).getFeature().getSimpleName();
    else
        return;
    List<INode> nodes = NodeModelUtils.findNodesForFeature(featureCall, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
    if (nodes.isEmpty())
        return;
    INode firstNode = IterableExtensions.head(nodes);
    int offset = firstNode.getOffset();
    ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(), (XtextResource) featureCall.eResource(), offset, 0);
    appendable.append("(");
    appendable.append(receiver);
    appendable.append(" as ");
    appendable.append(declaringType);
    appendable.append(").");
    appendable.commitChanges();
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) XAbstractFeatureCall(org.eclipse.xtext.xbase.XAbstractFeatureCall)

Example 5 with ReplacingAppendable

use of org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable 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)

Aggregations

ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)17 EObject (org.eclipse.emf.ecore.EObject)11 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)11 XtextResource (org.eclipse.xtext.resource.XtextResource)10 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)10 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)8 BadLocationException (org.eclipse.jface.text.BadLocationException)6 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)6 OptionalParameters (org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters)5 CoreException (org.eclipse.core.runtime.CoreException)4 INode (org.eclipse.xtext.nodemodel.INode)4 URI (org.eclipse.emf.common.util.URI)3 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)3 JvmType (org.eclipse.xtext.common.types.JvmType)3 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)3 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 EList (org.eclipse.emf.common.util.EList)2 WrappedException (org.eclipse.emf.common.util.WrappedException)2