Search in sources :

Example 1 with JavaContext

use of org.eclipse.jdt.internal.corext.template.java.JavaContext in project eclipse.jdt.ui by eclipse-jdt.

the class ChainCompletionTemplateBuilder method createJavaContext.

private static JavaContext createJavaContext(final JavaContentAssistInvocationContext contentAssistContext) {
    final ContextTypeRegistry templateContextRegistry = JavaPlugin.getDefault().getTemplateContextRegistry();
    final TemplateContextType templateContextType = templateContextRegistry.getContextType(JavaContextType.ID_ALL);
    final CompletionContext ctx = contentAssistContext.getCoreContext();
    final JavaContext javaTemplateContext = new JavaContext(templateContextType, contentAssistContext.getDocument(), ctx.getTokenStart(), ctx.getToken().length, contentAssistContext.getCompilationUnit());
    javaTemplateContext.setForceEvaluation(true);
    return javaTemplateContext;
}
Also used : CompletionContext(org.eclipse.jdt.core.CompletionContext) JavaContext(org.eclipse.jdt.internal.corext.template.java.JavaContext) ContextTypeRegistry(org.eclipse.text.templates.ContextTypeRegistry) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType)

Example 2 with JavaContext

use of org.eclipse.jdt.internal.corext.template.java.JavaContext in project eclipse.jdt.ui by eclipse-jdt.

the class TemplateProposal method apply.

/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
	 */
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    IDocument document = viewer.getDocument();
    try {
        fContext.setReadOnly(false);
        int start;
        TemplateBuffer templateBuffer;
        try {
            beginCompoundChange(viewer);
            int oldReplaceOffset = getReplaceOffset();
            try {
                // this may already modify the document (e.g. add imports)
                templateBuffer = fContext.evaluate(fTemplate);
            } catch (TemplateException e1) {
                fSelectedRegion = fRegion;
                return;
            }
            start = getReplaceOffset();
            int shift = start - oldReplaceOffset;
            int end = Math.max(getReplaceEndOffset(), offset + shift);
            // insert template string
            if (end > document.getLength())
                end = offset;
            String templateString = templateBuffer.getString();
            document.replace(start, end - start, templateString);
        } finally {
            endCompoundChange(viewer);
        }
        // translate positions
        LinkedModeModel model = new LinkedModeModel();
        TemplateVariable[] variables = templateBuffer.getVariables();
        MultiVariableGuess guess = fContext instanceof JavaContext ? ((JavaContext) fContext).getMultiVariableGuess() : null;
        boolean hasPositions = false;
        for (TemplateVariable variable : variables) {
            if (variable.isUnambiguous())
                continue;
            LinkedPositionGroup group = new LinkedPositionGroup();
            int[] offsets = variable.getOffsets();
            int length = variable.getLength();
            LinkedPosition first;
            if (guess != null && variable instanceof MultiVariable) {
                first = new VariablePosition(document, offsets[0] + start, length, guess, (MultiVariable) variable);
                guess.addSlave((VariablePosition) first);
            } else {
                String[] values = variable.getValues();
                ICompletionProposal[] proposals = new ICompletionProposal[values.length];
                for (int j = 0; j < values.length; j++) {
                    ensurePositionCategoryInstalled(document, model);
                    Position pos = new Position(offsets[0] + start, length);
                    document.addPosition(getCategory(), pos);
                    proposals[j] = new PositionBasedCompletionProposal(values[j], pos, length);
                }
                if (proposals.length > 1)
                    first = new ProposalPosition(document, offsets[0] + start, length, proposals);
                else
                    first = new LinkedPosition(document, offsets[0] + start, length);
            }
            for (int j = 0; j != offsets.length; j++) if (j == 0)
                group.addPosition(first);
            else
                group.addPosition(new LinkedPosition(document, offsets[j] + start, length));
            model.addGroup(group);
            hasPositions = true;
        }
        if (hasPositions) {
            model.forceInstall();
            JavaEditor editor = getJavaEditor();
            if (editor != null) {
                model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
            }
            LinkedModeUI ui = new EditorLinkedModeUI(model, viewer);
            ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
            ui.enter();
            fSelectedRegion = ui.getSelectedRegion();
        } else {
            fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
        }
    } catch (BadLocationException | BadPositionCategoryException e) {
        JavaPlugin.log(e);
        openErrorDialog(viewer.getTextWidget().getShell(), e);
        fSelectedRegion = fRegion;
    }
}
Also used : EditorLinkedModeUI(org.eclipse.ui.texteditor.link.EditorLinkedModeUI) LinkedModeModel(org.eclipse.jface.text.link.LinkedModeModel) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) StyledString(org.eclipse.jface.viewers.StyledString) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) EditorHighlightingSynchronizer(org.eclipse.jdt.internal.ui.javaeditor.EditorHighlightingSynchronizer) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) TemplateException(org.eclipse.jface.text.templates.TemplateException) Position(org.eclipse.jface.text.Position) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) Point(org.eclipse.swt.graphics.Point) JavaEditor(org.eclipse.jdt.internal.ui.javaeditor.JavaEditor) JavaContext(org.eclipse.jdt.internal.corext.template.java.JavaContext) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) EditorLinkedModeUI(org.eclipse.ui.texteditor.link.EditorLinkedModeUI) LinkedModeUI(org.eclipse.jface.text.link.LinkedModeUI) IRegion(org.eclipse.jface.text.IRegion) Region(org.eclipse.jface.text.Region) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

JavaContext (org.eclipse.jdt.internal.corext.template.java.JavaContext)2 CompletionContext (org.eclipse.jdt.core.CompletionContext)1 EditorHighlightingSynchronizer (org.eclipse.jdt.internal.ui.javaeditor.EditorHighlightingSynchronizer)1 JavaEditor (org.eclipse.jdt.internal.ui.javaeditor.JavaEditor)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)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 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 LinkedModeModel (org.eclipse.jface.text.link.LinkedModeModel)1 LinkedModeUI (org.eclipse.jface.text.link.LinkedModeUI)1 LinkedPosition (org.eclipse.jface.text.link.LinkedPosition)1 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)1 ProposalPosition (org.eclipse.jface.text.link.ProposalPosition)1 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)1 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)1 TemplateException (org.eclipse.jface.text.templates.TemplateException)1 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)1