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;
}
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());
}
Aggregations