Search in sources :

Example 1 with ProposalPosition

use of org.eclipse.jface.text.link.ProposalPosition in project eclipse.platform.text by eclipse.

the class TemplateProposal method apply.

/**
 * Inserts the template offered by this proposal into the viewer's document
 * and sets up a <code>LinkedModeUI</code> on the viewer to edit any of
 * the template's unresolved variables.
 *
 * @param viewer {@inheritDoc}
 * @param trigger {@inheritDoc}
 * @param stateMask {@inheritDoc}
 * @param offset {@inheritDoc}
 */
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    IDocument document = viewer.getDocument();
    try {
        fContext.setReadOnly(false);
        int start;
        TemplateBuffer templateBuffer;
        {
            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
            String templateString = templateBuffer.getString();
            document.replace(start, end - start, templateString);
        }
        // translate positions
        LinkedModeModel model = new LinkedModeModel();
        TemplateVariable[] variables = templateBuffer.getVariables();
        boolean hasPositions = false;
        for (int i = 0; i != variables.length; i++) {
            TemplateVariable variable = variables[i];
            if (variable.isUnambiguous())
                continue;
            LinkedPositionGroup group = new LinkedPositionGroup();
            int[] offsets = variable.getOffsets();
            int length = variable.getLength();
            LinkedPosition first;
            {
                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();
            LinkedModeUI ui = new LinkedModeUI(model, viewer);
            ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
            ui.enter();
            fSelectedRegion = ui.getSelectedRegion();
        } else {
            ensurePositionCategoryRemoved(document);
            fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
        }
    } catch (BadLocationException e) {
        openErrorDialog(viewer.getTextWidget().getShell(), e);
        ensurePositionCategoryRemoved(document);
        fSelectedRegion = fRegion;
    } catch (BadPositionCategoryException e) {
        openErrorDialog(viewer.getTextWidget().getShell(), e);
        fSelectedRegion = fRegion;
    }
}
Also used : LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) Position(org.eclipse.jface.text.Position) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) LinkedModeModel(org.eclipse.jface.text.link.LinkedModeModel) Point(org.eclipse.swt.graphics.Point) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) ProposalPosition(org.eclipse.jface.text.link.ProposalPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) LinkedModeUI(org.eclipse.jface.text.link.LinkedModeUI) 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

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 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 Point (org.eclipse.swt.graphics.Point)1