use of org.eclipse.jdt.core.CompletionProposal in project flux 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 flux by eclipse.
the class ContentAssistService method computeContentAssist.
protected JSONArray computeContentAssist(String username, String resourcePath, int offset, String prefix) throws JSONException {
final List<CompletionProposal> proposals = new ArrayList<CompletionProposal>();
final CompletionContext[] completionContextParam = new CompletionContext[] { null };
ICompilationUnit liveEditUnit = liveEditUnits.getLiveEditUnit(username, resourcePath);
try {
if (liveEditUnit != null) {
CompletionRequestor collector = new CompletionRequestor() {
@Override
public void accept(CompletionProposal proposal) {
proposals.add(proposal);
}
@Override
public void acceptContext(CompletionContext context) {
super.acceptContext(context);
completionContextParam[0] = context;
}
};
// Allow completions for unresolved types - since 3.3
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);
collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);
collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);
liveEditUnit.codeComplete(offset, collector, new NullProgressMonitor());
}
} catch (JavaModelException e) {
e.printStackTrace();
}
List<JSONObject> jsonProposals = new ArrayList<JSONObject>(proposals.size());
CompletionContext completionContext = completionContextParam[0];
for (CompletionProposal proposal : proposals) {
JSONObject jsonDescription = getDescription(proposal, completionContext);
ProposalReplcamentInfo replacementInfo = new CompletionProposalReplacementProvider(liveEditUnit, proposal, completionContext, offset, prefix).createReplacement();
JSONObject jsonProposal = new JSONObject();
jsonProposal.put("description", jsonDescription);
jsonProposal.put("proposal", replacementInfo.replacement);
int initOffset = offset - prefix.length();
if (replacementInfo.extraChanges != null) {
jsonProposal.put("additionalEdits", Utils.editsToJsonArray(replacementInfo.extraChanges));
initOffset += Utils.getOffsetAdjustment(replacementInfo.extraChanges, initOffset);
}
if (replacementInfo.positions != null && !replacementInfo.positions.isEmpty()) {
jsonProposal.put("positions", getPositions(replacementInfo.positions, initOffset));
}
jsonProposal.put("escapePosition", initOffset + replacementInfo.replacement.length());
jsonProposal.put("style", "attributedString");
jsonProposal.put("replace", true);
jsonProposal.put("relevance", proposal.getRelevance());
jsonProposals.add(jsonProposal);
}
Collections.sort(jsonProposals, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
try {
int diff = o2.getInt("relevance") - o1.getInt("relevance");
if (diff == 0) {
JSONArray nameDescription1 = o1.getJSONObject("description").getJSONArray("segments");
JSONArray nameDescription2 = o2.getJSONObject("description").getJSONArray("segments");
StringBuilder nameBuffer1 = new StringBuilder();
for (int i = 0; i < nameDescription1.length(); i++) {
nameBuffer1.append(nameDescription1.getJSONObject(i).getString("value"));
}
StringBuilder nameBuffer2 = new StringBuilder();
for (int i = 0; i < nameDescription2.length(); i++) {
nameBuffer2.append(nameDescription2.getJSONObject(i).getString("value"));
}
return nameBuffer1.toString().compareTo(nameBuffer2.toString());
} else {
return diff;
}
} catch (JSONException e) {
return -1;
}
}
});
return new JSONArray(jsonProposals);
}
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
}
}
use of org.eclipse.jdt.core.CompletionProposal in project che by eclipse.
the class JavaTypeCompletionProposalComputer method createTypeProposal.
private IJavaCompletionProposal createTypeProposal(int relevance, String fullyQualifiedType, JavaContentAssistInvocationContext context) throws JavaModelException {
IType type = context.getCompilationUnit().getJavaProject().findType(fullyQualifiedType);
if (type == null)
return null;
CompletionProposal proposal = CompletionProposal.create(CompletionProposal.TYPE_REF, context.getInvocationOffset());
proposal.setCompletion(fullyQualifiedType.toCharArray());
proposal.setDeclarationSignature(type.getPackageFragment().getElementName().toCharArray());
proposal.setFlags(type.getFlags());
proposal.setRelevance(relevance);
proposal.setReplaceRange(context.getInvocationOffset(), context.getInvocationOffset());
proposal.setSignature(Signature.createTypeSignature(fullyQualifiedType, true).toCharArray());
if (shouldProposeGenerics(context.getProject()))
return new LazyGenericTypeProposal(proposal, context);
else
return new LazyJavaTypeCompletionProposal(proposal, context);
}
use of org.eclipse.jdt.core.CompletionProposal in project che by eclipse.
the class AnonymousTypeCompletionProposal method getPrefix.
/*
* @see org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal#getPrefix(org.eclipse.jface.text.IDocument, int)
* @since 3.5
*/
@Override
protected String getPrefix(IDocument document, int offset) {
CompletionProposal coreProposal = ((MemberProposalInfo) getProposalInfo()).fProposal;
if (coreProposal.getKind() != CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION)
return super.getPrefix(document, offset);
int replacementOffset = coreProposal.getRequiredProposals()[0].getReplaceStart();
try {
int length = offset - replacementOffset;
if (length > 0)
return document.get(replacementOffset, length);
} catch (BadLocationException x) {
}
//$NON-NLS-1$
return "";
}
Aggregations