Search in sources :

Example 1 with TemplateCompletionProposalComputer

use of org.eclipse.jdt.internal.ui.text.java.TemplateCompletionProposalComputer in project che by eclipse.

the class CodeAssist method computeProposals.

public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content) throws JavaModelException {
    WorkingCopyOwner copyOwner = new WorkingCopyOwner() {

        @Override
        public IBuffer createBuffer(ICompilationUnit workingCopy) {
            return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, workingCopy.getPath(), content);
        }
    };
    ICompilationUnit compilationUnit;
    IType type = project.findType(fqn);
    if (type == null) {
        return null;
    }
    if (type.isBinary()) {
        compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null);
    } else {
        compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null);
    }
    IBuffer buffer = compilationUnit.getBuffer();
    IDocument document;
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
    List<ICompletionProposal> proposals = new ArrayList<>();
    proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
    proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
    Collections.sort(proposals, new RelevanceSorter());
    return convertProposals(offset, compilationUnit, viewer, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) TemplateCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.TemplateCompletionProposalComputer) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) Point(org.eclipse.swt.graphics.Point) RelevanceSorter(org.eclipse.jdt.internal.ui.text.java.RelevanceSorter) IBuffer(org.eclipse.jdt.core.IBuffer) IType(org.eclipse.jdt.core.IType) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) JavaAllCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

ArrayList (java.util.ArrayList)1 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)1 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)1 IBuffer (org.eclipse.jdt.core.IBuffer)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IType (org.eclipse.jdt.core.IType)1 WorkingCopyOwner (org.eclipse.jdt.core.WorkingCopyOwner)1 DocumentAdapter (org.eclipse.jdt.internal.core.DocumentAdapter)1 JavaAllCompletionProposalComputer (org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer)1 RelevanceSorter (org.eclipse.jdt.internal.ui.text.java.RelevanceSorter)1 TemplateCompletionProposalComputer (org.eclipse.jdt.internal.ui.text.java.TemplateCompletionProposalComputer)1 JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)1 IDocument (org.eclipse.jface.text.IDocument)1 Point (org.eclipse.swt.graphics.Point)1