Search in sources :

Example 1 with CompilationUnitContextType

use of org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType in project che by eclipse.

the class TemplateEngine method complete.

/**
	 * Inspects the context of the compilation unit around <code>completionPosition</code>
	 * and feeds the collector with proposals.
	 * @param viewer the text viewer
	 * @param completionPosition the context position in the document of the text viewer
	 * @param compilationUnit the compilation unit (may be <code>null</code>)
	 */
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
    IDocument document = viewer.getDocument();
    if (!(fContextType instanceof CompilationUnitContextType))
        return;
    Point selection = viewer.getSelectedRange();
    Position position = new Position(completionPosition, selection.y);
    // remember selected text
    String selectedText = null;
    if (selection.y != 0) {
        try {
            selectedText = document.get(selection.x, selection.y);
            document.addPosition(position);
            fPositions.put(document, position);
        } catch (BadLocationException e) {
        }
    }
    CompilationUnitContext context = ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
    //$NON-NLS-1$
    context.setVariable("selection", selectedText);
    int start = context.getStart();
    int end = context.getEnd();
    IRegion region = new Region(start, end - start);
    Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates();
    if (selection.y == 0) {
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template)) {
                fProposals.add(new TemplateProposal(template, context, region, getImage()));
            }
        }
    } else {
        if (context.getKey().length() == 0)
            context.setForceEvaluation(true);
        boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template) && (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) {
                fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
            }
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) Point(org.eclipse.swt.graphics.Point) CompilationUnitContextType(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) Template(org.eclipse.jface.text.templates.Template) CompilationUnitContext(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

CompilationUnitContext (org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext)1 CompilationUnitContextType (org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1 Region (org.eclipse.jface.text.Region)1 Template (org.eclipse.jface.text.templates.Template)1 Point (org.eclipse.swt.graphics.Point)1