Search in sources :

Example 1 with ImportOrganizingProposal

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

the class ImplementMemberFromSuperAssist method createOverrideMethodProposal.

protected ICompletionProposal createOverrideMethodProposal(XtendTypeDeclaration model, IResolvedExecutable overrideable, final ContentAssistContext context, IProposalConflictHelper conflictHelper) {
    IXtextDocument document = context.getDocument();
    XtextResource resource = (XtextResource) model.eResource();
    int offset = context.getReplaceRegion().getOffset();
    int currentIndentation = appendableFactory.getIndentationLevelAtOffset(offset, document, resource);
    final int indentationLevel = currentIndentation == 0 ? 1 : currentIndentation;
    ReplacingAppendable appendable = appendableFactory.create(document, resource, offset, context.getReplaceRegion().getLength(), new OptionalParameters() {

        {
            ensureEmptyLinesAround = true;
            baseIndentationLevel = indentationLevel;
        }
    });
    final String simpleName;
    JvmExecutable declaration = overrideable.getDeclaration();
    if (overrideable instanceof IResolvedOperation) {
        implementor.appendOverrideFunction(model, (IResolvedOperation) overrideable, appendable);
        simpleName = overrideable.getDeclaration().getSimpleName();
    } else if (model instanceof XtendClass) {
        implementor.appendConstructorFromSuper((XtendClass) model, (IResolvedConstructor) overrideable, appendable);
        simpleName = "new";
    } else {
        return null;
    }
    String code = appendable.getCode();
    if (!isValidProposal(code.trim(), context, conflictHelper) && !isValidProposal(simpleName, context, conflictHelper))
        return null;
    ImageDescriptor imageDescriptor = images.forOperation(declaration.getVisibility(), adornments.getOverrideAdornment(declaration));
    ImportOrganizingProposal completionProposal = createCompletionProposal(appendable, context.getReplaceRegion(), getLabel(overrideable), imageHelper.getImage(imageDescriptor));
    Matcher matcher = bodyExpressionPattern.matcher(code);
    if (matcher.find()) {
        int bodyExpressionLength = matcher.end(1) - matcher.start(1);
        int bodyExpressionStart = matcher.start(1) + appendable.getTotalOffset() - completionProposal.getReplacementOffset();
        if (bodyExpressionLength == 0) {
            completionProposal.setCursorPosition(bodyExpressionStart);
        } else {
            completionProposal.setSelectionStart(completionProposal.getReplacementOffset() + bodyExpressionStart);
            completionProposal.setSelectionLength(bodyExpressionLength);
            completionProposal.setAutoInsertable(false);
            completionProposal.setCursorPosition(bodyExpressionStart + bodyExpressionLength);
            completionProposal.setSimpleLinkedMode(context.getViewer(), '\t');
        }
    }
    completionProposal.setPriority(getPriority(model, declaration, context));
    completionProposal.setMatcher(new PrefixMatcher() {

        @Override
        public boolean isCandidateMatchingPrefix(String name, String prefix) {
            PrefixMatcher delegate = context.getMatcher();
            boolean result = delegate.isCandidateMatchingPrefix(simpleName, prefix);
            return result;
        }
    });
    return completionProposal;
}
Also used : PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) Matcher(java.util.regex.Matcher) XtextResource(org.eclipse.xtext.resource.XtextResource) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) StyledString(org.eclipse.jface.viewers.StyledString) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation) IResolvedConstructor(org.eclipse.xtext.xbase.typesystem.override.IResolvedConstructor) OptionalParameters(org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) ImportOrganizingProposal(org.eclipse.xtext.xbase.ui.contentassist.ImportOrganizingProposal) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 2 with ImportOrganizingProposal

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

the class ImplementSuperMemberAssistTest method testExistingStaticImport.

@Test
public void testExistingStaticImport() throws Exception {
    ICompletionProposal[] proposals = newBuilder().append("import static java.util.Collection.*" + "class SpecialList extends java.util.ArrayList { removeAll").computeCompletionProposals();
    assertEquals(1, proposals.length);
    ImportOrganizingProposal proposal = (ImportOrganizingProposal) proposals[0];
    ReplacingAppendable appendable = proposal.getAppendable();
    RewritableImportSection importSection = appendable.getImportSection();
    List<ReplaceRegion> imports = importSection.rewrite();
    assertEquals(1, imports.size());
    assertEquals("import java.util.Collection", imports.get(0).getText().trim());
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ReplacingAppendable(org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable) ImportOrganizingProposal(org.eclipse.xtext.xbase.ui.contentassist.ImportOrganizingProposal) RewritableImportSection(org.eclipse.xtext.xbase.imports.RewritableImportSection) Test(org.junit.Test)

Aggregations

ImportOrganizingProposal (org.eclipse.xtext.xbase.ui.contentassist.ImportOrganizingProposal)2 ReplacingAppendable (org.eclipse.xtext.xbase.ui.contentassist.ReplacingAppendable)2 Matcher (java.util.regex.Matcher)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 StyledString (org.eclipse.jface.viewers.StyledString)1 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)1 JvmExecutable (org.eclipse.xtext.common.types.JvmExecutable)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 PrefixMatcher (org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1 ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)1 RewritableImportSection (org.eclipse.xtext.xbase.imports.RewritableImportSection)1 IResolvedConstructor (org.eclipse.xtext.xbase.typesystem.override.IResolvedConstructor)1 IResolvedOperation (org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)1 OptionalParameters (org.eclipse.xtext.xbase.ui.document.DocumentSourceAppender.Factory.OptionalParameters)1 Test (org.junit.Test)1