use of org.eclipse.jdt.core.CompletionProposal in project che by eclipse.
the class SimilarElementsRequestor method getStaticImportFavorites.
public static String[] getStaticImportFavorites(ICompilationUnit cu, final String elementName, boolean isMethod, String[] favorites) throws JavaModelException {
StringBuffer dummyCU = new StringBuffer();
String packName = cu.getParent().getElementName();
IType type = cu.findPrimaryType();
if (type == null)
return new String[0];
if (packName.length() > 0) {
//$NON-NLS-1$
dummyCU.append("package ").append(packName).append(';');
}
// static initializer //$NON-NLS-1$//$NON-NLS-2$
dummyCU.append("public class ").append(type.getElementName()).append("{\n static {\n").append(elementName);
int offset = dummyCU.length();
//$NON-NLS-1$
dummyCU.append("\n}\n }");
ICompilationUnit newCU = null;
try {
newCU = cu.getWorkingCopy(null);
newCU.getBuffer().setContents(dummyCU.toString());
final HashSet<String> result = new HashSet<String>();
CompletionRequestor requestor = new CompletionRequestor(true) {
@Override
public void accept(CompletionProposal proposal) {
if (elementName.equals(new String(proposal.getName()))) {
CompletionProposal[] requiredProposals = proposal.getRequiredProposals();
for (int i = 0; i < requiredProposals.length; i++) {
CompletionProposal curr = requiredProposals[i];
if (curr.getKind() == CompletionProposal.METHOD_IMPORT || curr.getKind() == CompletionProposal.FIELD_IMPORT) {
result.add(JavaModelUtil.concatenateName(Signature.toCharArray(curr.getDeclarationSignature()), curr.getName()));
}
}
}
}
};
if (isMethod) {
requestor.setIgnored(CompletionProposal.METHOD_REF, false);
requestor.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
} else {
requestor.setIgnored(CompletionProposal.FIELD_REF, false);
requestor.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
}
requestor.setFavoriteReferences(favorites);
newCU.codeComplete(offset, requestor);
return result.toArray(new String[result.size()]);
} finally {
if (newCU != null) {
newCU.discardWorkingCopy();
}
}
}
use of org.eclipse.jdt.core.CompletionProposal in project che by eclipse.
the class AbstractJavaCompletionProposal method isSupportingRequiredProposals.
/**
* Tells whether required proposals are supported by this proposal.
*
* @return <code>true</code> if required proposals are supported by this proposal
* @see org.eclipse.jdt.core.CompletionProposal#getRequiredProposals()
* @since 3.3
*/
protected boolean isSupportingRequiredProposals() {
if (fInvocationContext == null)
return false;
ProposalInfo proposalInfo = getProposalInfo();
if (!(proposalInfo instanceof MemberProposalInfo || proposalInfo instanceof AnonymousTypeProposalInfo))
return false;
CompletionProposal proposal = ((MemberProposalInfo) proposalInfo).fProposal;
return proposal != null && (proposal.getKind() == CompletionProposal.METHOD_REF || proposal.getKind() == CompletionProposal.FIELD_REF || proposal.getKind() == CompletionProposal.TYPE_REF || proposal.getKind() == CompletionProposal.CONSTRUCTOR_INVOCATION || proposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION);
}
use of org.eclipse.jdt.core.CompletionProposal in project che by eclipse.
the class AnonymousTypeCompletionProposal method updateReplacementString.
/*
* @see org.eclipse.jdt.internal.ui.text.java.JavaTypeCompletionProposal#updateReplacementString(org.eclipse.jface.text.IDocument, char, int, org.eclipse.jdt.core.dom.rewrite.ImportRewrite)
*/
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
fImportRewrite = impRewrite;
String newBody = createNewBody(impRewrite);
if (newBody == null)
return false;
CompletionProposal coreProposal = ((MemberProposalInfo) getProposalInfo()).fProposal;
boolean isAnonymousConstructorInvoc = coreProposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION;
//$NON-NLS-1$
boolean replacementStringEndsWithParentheses = isAnonymousConstructorInvoc || getReplacementString().endsWith(")");
// construct replacement text: an expression to be formatted
//$NON-NLS-1$
StringBuffer buf = new StringBuffer("new A(");
if (!replacementStringEndsWithParentheses || isAnonymousConstructorInvoc)
buf.append(')');
buf.append(newBody);
// use the code formatter
String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
final IJavaProject project = fCompilationUnit.getJavaProject();
IRegion lineInfo = document.getLineInformationOfOffset(getReplacementOffset());
int indent = Strings.computeIndentUnits(document.get(lineInfo.getOffset(), lineInfo.getLength()), project);
Map<String, String> options = project != null ? project.getOptions(true) : JavaCore.getOptions();
options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.TRUE);
String replacementString = CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, buf.toString(), 0, lineDelim, options);
int lineEndOffset = lineInfo.getOffset() + lineInfo.getLength();
int p = offset;
char ch = document.getChar(p);
while (p < lineEndOffset) {
if (ch == '(' || ch == ')' || ch == ';' || ch == ',')
break;
ch = document.getChar(++p);
}
if (ch != ';' && ch != ',' && ch != ')')
replacementString = replacementString + ';';
replacementString = Strings.changeIndent(replacementString, 0, project, CodeFormatterUtil.createIndentString(indent, project), lineDelim);
int beginIndex = replacementString.indexOf('(');
if (!isAnonymousConstructorInvoc)
beginIndex++;
replacementString = replacementString.substring(beginIndex);
int pos = offset;
if (isAnonymousConstructorInvoc && (insertCompletion() ^ isInsertModeToggled())) {
// Keep existing code
int endPos = pos;
ch = document.getChar(endPos);
while (endPos < lineEndOffset && ch != '(' && ch != ')' && ch != ';' && ch != ',' && !Character.isWhitespace(ch)) ch = document.getChar(++endPos);
int keepLength = endPos - pos;
if (keepLength > 0) {
String keepStr = document.get(pos, keepLength);
replacementString = replacementString + keepStr;
setCursorPosition(replacementString.length() - keepLength);
}
} else
setCursorPosition(replacementString.length());
setReplacementString(replacementString);
if (pos < document.getLength() && document.getChar(pos) == ')') {
int currentLength = getReplacementLength();
if (replacementStringEndsWithParentheses)
setReplacementLength(currentLength + pos - offset);
else
setReplacementLength(currentLength + pos - offset + 1);
}
return false;
}
use of org.eclipse.jdt.core.CompletionProposal in project che by eclipse.
the class AnonymousTypeCompletionProposal method computeContextInformation.
protected IContextInformation computeContextInformation() {
try {
fContextInformationPosition = getReplacementOffset() - 1;
CompletionProposal proposal = ((MemberProposalInfo) getProposalInfo()).fProposal;
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=94654
if (hasParameters() && (getReplacementString().endsWith(")") || getReplacementString().length() == 0)) {
//$NON-NLS-1$
ProposalContextInformation contextInformation = new ProposalContextInformation(proposal);
fContextInformationPosition = getReplacementOffset() + getCursorPosition();
if (fContextInformationPosition != 0 && proposal.getCompletion().length == 0)
contextInformation.setContextInformationPosition(fContextInformationPosition);
return contextInformation;
}
return null;
} finally {
fIsContextInformationComputed = true;
}
}
use of org.eclipse.jdt.core.CompletionProposal in project che by eclipse.
the class AbstractJavaCompletionProposal method apply.
/*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension#apply(org.eclipse.jface.text.IDocument, char, int)
*/
public void apply(IDocument document, char trigger, int offset) {
if (isSupportingRequiredProposals()) {
CompletionProposal coreProposal = ((MemberProposalInfo) getProposalInfo()).fProposal;
CompletionProposal[] requiredProposals = coreProposal.getRequiredProposals();
for (int i = 0; requiredProposals != null && i < requiredProposals.length; i++) {
int oldLen = document.getLength();
if (requiredProposals[i].getKind() == CompletionProposal.TYPE_REF) {
LazyJavaCompletionProposal proposal = createRequiredTypeCompletionProposal(requiredProposals[i], fInvocationContext);
proposal.apply(document);
setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen);
} else if (requiredProposals[i].getKind() == CompletionProposal.TYPE_IMPORT) {
ImportCompletionProposal proposal = new ImportCompletionProposal(requiredProposals[i], fInvocationContext, coreProposal.getKind());
proposal.setReplacementOffset(getReplacementOffset());
proposal.apply(document);
setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen);
} else if (requiredProposals[i].getKind() == CompletionProposal.METHOD_IMPORT) {
ImportCompletionProposal proposal = new ImportCompletionProposal(requiredProposals[i], fInvocationContext, coreProposal.getKind());
proposal.setReplacementOffset(getReplacementOffset());
proposal.apply(document);
setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen);
} else if (requiredProposals[i].getKind() == CompletionProposal.FIELD_IMPORT) {
ImportCompletionProposal proposal = new ImportCompletionProposal(requiredProposals[i], fInvocationContext, coreProposal.getKind());
proposal.setReplacementOffset(getReplacementOffset());
proposal.apply(document);
setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen);
} else {
/*
* In 3.3 we only support the above required proposals, see
* CompletionProposal#getRequiredProposals()
*/
Assert.isTrue(false);
}
}
}
try {
boolean isSmartTrigger = isSmartTrigger(trigger);
String replacement;
if (isSmartTrigger || trigger == (char) 0) {
int referenceOffset = getReplacementOffset() + getReplacementLength();
replacement = getReplacementString();
// a ; at the reference offset.
if (trigger == ';' && replacement.charAt(replacement.length() - 1) != ';' && (referenceOffset >= document.getLength() || document.getChar(referenceOffset) != ';')) {
//$NON-NLS-1$
replacement = replacement + ";";
setReplacementString(replacement);
}
} else {
StringBuffer buffer = new StringBuffer(getReplacementString());
// fix for PR #5533. Assumes that no eating takes place.
if ((getCursorPosition() > 0 && getCursorPosition() <= buffer.length() && buffer.charAt(getCursorPosition() - 1) != trigger)) {
// insert trigger ';' for methods with parameter at the end of the replacement string and not at the cursor position.
int length = getReplacementString().length();
if (trigger == ';' && getCursorPosition() != length) {
if (buffer.charAt(length - 1) != trigger) {
buffer.insert(length, trigger);
}
} else {
buffer.insert(getCursorPosition(), trigger);
setCursorPosition(getCursorPosition() + 1);
}
}
replacement = buffer.toString();
setReplacementString(replacement);
}
// reference position just at the end of the document change.
int referenceOffset = getReplacementOffset() + getReplacementLength();
final ReferenceTracker referenceTracker = new ReferenceTracker();
referenceTracker.preReplace(document, referenceOffset);
replace(document, getReplacementOffset(), getReplacementLength(), replacement);
referenceOffset = referenceTracker.postReplace(document);
setReplacementOffset(referenceOffset - (replacement == null ? 0 : replacement.length()));
// PR 47097
if (isSmartTrigger) {
// avoid inserting redundant semicolon when smart insert is enabled.
if (!(trigger == ';' && (replacement.endsWith(";") || document.getChar(referenceOffset) == ';'))) {
//$NON-NLS-1$
handleSmartTrigger(document, trigger, referenceOffset);
}
}
} catch (BadLocationException x) {
// ignore
}
}
Aggregations