use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project che by eclipse.
the class CompletionProposalCollector method createFieldWithCastedReceiverProposal.
/**
* Creates the Java completion proposal for the JDT Core
* {@link org.eclipse.jdt.core.CompletionProposal#FIELD_REF_WITH_CASTED_RECEIVER} proposal.
*
* @param proposal the JDT Core proposal
* @return the Java completion proposal
* @since 3.4
*/
private IJavaCompletionProposal createFieldWithCastedReceiverProposal(CompletionProposal proposal) {
String completion = String.valueOf(proposal.getCompletion());
//$NON-NLS-1$
completion = CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, completion, 0, "\n", fJavaProject);
int start = proposal.getReplaceStart();
int length = getLength(proposal);
StyledString label = fLabelProvider.createStyledLabel(proposal);
Image image = getImage(fLabelProvider.createFieldImageDescriptor(proposal));
int relevance = computeRelevance(proposal);
JavaCompletionProposal javaProposal = new JavaFieldWithCastedReceiverCompletionProposal(completion, start, length, image, label, relevance, getContext().isInJavadoc(), getInvocationContext(), proposal);
if (fJavaProject != null)
javaProposal.setProposalInfo(new FieldProposalInfo(fJavaProject, proposal));
javaProposal.setTriggerCharacters(VAR_TRIGGER);
return javaProposal;
}
use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project che by eclipse.
the class CompletionProposalCollector method createLocalVariableProposal.
private IJavaCompletionProposal createLocalVariableProposal(CompletionProposal proposal) {
String completion = String.valueOf(proposal.getCompletion());
int start = proposal.getReplaceStart();
int length = getLength(proposal);
Image image = getImage(fLabelProvider.createLocalImageDescriptor(proposal));
StyledString label = fLabelProvider.createSimpleLabelWithType(proposal);
int relevance = computeRelevance(proposal);
final JavaCompletionProposal javaProposal = new JavaCompletionProposal(completion, start, length, image, label, relevance);
javaProposal.setTriggerCharacters(VAR_TRIGGER);
return javaProposal;
}
use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project che by eclipse.
the class CompletionProposalCollector method createAnonymousTypeProposal.
private IJavaCompletionProposal createAnonymousTypeProposal(CompletionProposal proposal, JavaContentAssistInvocationContext invocationContext) {
if (fCompilationUnit == null || fJavaProject == null)
return null;
char[] declarationKey = proposal.getDeclarationKey();
if (declarationKey == null)
return null;
try {
IJavaElement element = fJavaProject.findElement(new String(declarationKey), null);
if (!(element instanceof IType))
return null;
IType type = (IType) element;
String completion = String.valueOf(proposal.getCompletion());
int start = proposal.getReplaceStart();
int length = getLength(proposal);
int relevance = computeRelevance(proposal);
StyledString label = fLabelProvider.createAnonymousTypeLabel(proposal);
JavaCompletionProposal javaProposal = new AnonymousTypeCompletionProposal(fJavaProject, fCompilationUnit, invocationContext, start, length, completion, label, String.valueOf(proposal.getDeclarationSignature()), type, relevance);
javaProposal.setProposalInfo(new AnonymousTypeProposalInfo(fJavaProject, proposal));
return javaProposal;
} catch (JavaModelException e) {
return null;
}
}
use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project tdi-studio-se by Talend.
the class TalendJavaCompletionProcessor method sortProposals.
/*
* (non-Javadoc)
*
* @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor#filterAndSortProposals(java.util.List,
* org.eclipse.core.runtime.IProgressMonitor, org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext)
*/
@Override
protected List<ICompletionProposal> sortProposals(List<ICompletionProposal> proposals, IProgressMonitor monitor, ContentAssistInvocationContext context) {
proposals = super.sortProposals(proposals, monitor, context);
Map<String, ICompletionProposal> completionProposalMap = new HashMap<String, ICompletionProposal>();
boolean globalFieldsDone = false;
for (Object o : proposals) {
ICompletionProposal proposal = (ICompletionProposal) o;
String longna = proposal.getDisplayString();
//$NON-NLS-1$
int indexna = longna.indexOf("-");
boolean shouldRemove = false;
if (indexna > 0) {
if (longna.substring(indexna + 2, longna.length()).equals(TalendJavaSourceViewer.getClassName())) {
shouldRemove = true;
}
}
if (proposal instanceof JavaCompletionProposal) {
JavaCompletionProposal javaProposal = ((JavaCompletionProposal) proposal);
if (javaProposal.getJavaElement() instanceof SourceField) {
globalFieldsDone = true;
}
if (javaProposal.getJavaElement() == null && globalFieldsDone) {
shouldRemove = true;
}
}
if (proposal.getDisplayString().startsWith(TalendJavaSourceViewer.VIEWER_CLASS_NAME)) {
shouldRemove = true;
}
if (!shouldRemove) {
completionProposalMap.put(longna, proposal);
}
}
List<ICompletionProposal> newProposals = new ArrayList<ICompletionProposal>(completionProposalMap.values());
Collections.sort(newProposals, new Comparator<ICompletionProposal>() {
@Override
public int compare(ICompletionProposal arg0, ICompletionProposal arg1) {
if (arg1 instanceof TalendCompletionProposal && (!(arg0 instanceof TalendCompletionProposal))) {
return 1;
} else if (arg0 instanceof TalendCompletionProposal && (!(arg1 instanceof TalendCompletionProposal))) {
return -1;
} else if (arg1 instanceof TalendCompletionProposal && (arg0 instanceof TalendCompletionProposal)) {
TalendCompletionProposal a = (TalendCompletionProposal) arg0;
TalendCompletionProposal b = (TalendCompletionProposal) arg1;
return a.getType().compareTo(b.getType());
}
return 0;
}
});
return newProposals;
}
use of org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal in project tdi-studio-se by Talend.
the class TalendJavaSourceViewer method createViewerForComponent.
public static ISourceViewer createViewerForComponent(Composite composite, int styles, CompilationUnitEditor editor, List<Variable> variableList, String uniqueName, String codePart) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String selectedPart = "[" + uniqueName + " " + codePart + " ] start";
IDocument originalDocument = editor.getDocumentProvider().getDocument(editor.getEditorInput());
int documentOffset = -1;
//$NON-NLS-1$
String globalFields = "";
//$NON-NLS-1$
String localFields = "";
//$NON-NLS-1$
globalFields = "\tprivate static java.util.Properties context = new java.util.Properties();\n";
//$NON-NLS-1$
globalFields += "\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n";
IDocument newDoc = new Document();
boolean checkCode = false;
FindReplaceDocumentAdapter frda = null;
// hywang modified for bug 9180
String documentText = originalDocument.get();
if (documentText != null && !documentText.equals("") && documentText.length() > 0) {
//$NON-NLS-1$
frda = new FindReplaceDocumentAdapter(originalDocument);
}
try {
Region region = null;
if (frda != null) {
region = (Region) frda.find(0, selectedPart, true, false, false, false);
}
if (region != null) {
checkCode = true;
documentOffset = region.getOffset();
documentOffset = originalDocument.getLineOffset(originalDocument.getLineOfOffset(documentOffset) + 2);
JavaCompletionProcessor processor = new JavaCompletionProcessor(editor, new ContentAssistant(), IDocument.DEFAULT_CONTENT_TYPE);
boolean globalFieldsDone = false;
//$NON-NLS-1$
globalFields = "";
String className = editor.getPartName().substring(0, editor.getPartName().indexOf('.') + 1);
ICompletionProposal[] proposals = processor.computeCompletionProposals(editor.getViewer(), documentOffset);
for (ICompletionProposal curProposal : proposals) {
if (curProposal instanceof JavaCompletionProposal) {
JavaCompletionProposal javaProposal = ((JavaCompletionProposal) curProposal);
if (javaProposal.getJavaElement() instanceof SourceField) {
globalFieldsDone = true;
SourceField sourceField = (SourceField) javaProposal.getJavaElement();
//$NON-NLS-1$ //$NON-NLS-2$
globalFields += "\t" + sourceField.getSource() + "\n";
// System.out.println();
}
if (javaProposal.getJavaElement() == null && !globalFieldsDone) {
//$NON-NLS-1$
String[] str = javaProposal.getSortString().split(" ");
//$NON-NLS-1$
String variable = "";
for (int i = str.length - 1; i >= 0; i--) {
//$NON-NLS-1$
variable += str[i].length() == 0 ? " " : str[i];
}
if (variable.contains(className)) {
continue;
}
//$NON-NLS-1$
variable += ";";
//$NON-NLS-1$ //$NON-NLS-2$
localFields += "\t\t" + variable + "\n";
// System.out.println(variable);
}
}
}
}
} catch (BadLocationException e) {
ExceptionHandler.process(e);
} catch (JavaModelException e) {
// e.printStackTrace();
ExceptionHandler.process(e);
}
StringBuffer buff = new StringBuffer();
//$NON-NLS-1$
buff.append("package internal;\n\n");
buff.append(getImports());
//$NON-NLS-1$ //$NON-NLS-2$
buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
buff.append(globalFields);
//$NON-NLS-1$
buff.append("\tpublic void myFunction(){\n");
buff.append(localFields);
documentOffset = buff.toString().length();
//$NON-NLS-1$
buff.append("\n\t\n}\n}");
newDoc.set(buff.toString());
return initializeViewer(composite, styles, checkCode, newDoc, documentOffset);
}
Aggregations