Search in sources :

Example 1 with LinkedPosition

use of org.eclipse.jface.text.link.LinkedPosition in project che by eclipse.

the class TemplateProposal method apply.

/*
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
     */
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
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        TemplateVariable[] variables = templateBuffer.getVariables();
        MultiVariableGuess guess = fContext instanceof CompilationUnitContext ? ((CompilationUnitContext) fContext).getMultiVariableGuess() : null;
        boolean hasPositions = false;
        for (int i = 0; i != variables.length; i++) {
            TemplateVariable variable = variables[i];
            if (variable.isUnambiguous())
                continue;
            LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
            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) {
                if (first instanceof ProposalPosition) {
                    RegionImpl region = new RegionImpl();
                    region.setLength(first.getLength());
                    region.setOffset(first.getOffset());
                    LinkedDataImpl data = new LinkedDataImpl();
                    ICompletionProposal[] choices = ((ProposalPosition) first).getChoices();
                    if (choices != null) {
                        for (ICompletionProposal choice : choices) {
                            data.addValues(choice.getDisplayString());
                        }
                        group.setData(data);
                    }
                    group.addPositions(region);
                } else {
                    RegionImpl region = new RegionImpl();
                    region.setLength(first.getLength());
                    region.setOffset(first.getOffset());
                    group.addPositions(region);
                }
            } else {
                RegionImpl region = new RegionImpl();
                region.setLength(length);
                region.setOffset(offsets[j] + start);
                group.addPositions(region);
            }
            model.addGroups(group);
            hasPositions = true;
        }
        if (hasPositions) {
            model.setEscapePosition(getCaretOffset(templateBuffer) + start);
            this.linkedModeModel = model;
            //				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();
            //ui.getSelectedRegion();
            fSelectedRegion = fRegion;
        } else {
            fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
        }
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
        //			openErrorDialog(viewer.getTextWidget().getShell(), e);
        fSelectedRegion = fRegion;
    }
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) StyledString(org.eclipse.jface.viewers.StyledString) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) TemplateException(org.eclipse.jface.text.templates.TemplateException) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) ProposalPosition(org.eclipse.che.jface.text.link.ProposalPosition) Position(org.eclipse.jface.text.Position) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) LinkedDataImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl) Point(org.eclipse.swt.graphics.Point) ProposalPosition(org.eclipse.che.jface.text.link.ProposalPosition) CompilationUnitContext(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with LinkedPosition

use of org.eclipse.jface.text.link.LinkedPosition in project ow by vtst.

the class AbstractCompletionProposal method setUpLinkedMode.

/**
   * Set up the linked mode for the replacement text.
   * @param document  The document in which the replacement is made.
   * @param replacementOffset  The offset at which the replacement is made.
   * @param fragments   The fragments to insert.
   * @param closingChar  The closing character (used for the exit mode).
   * @throws BadLocationException
   */
private void setUpLinkedMode(IDocument document, int replacementOffset, Iterable<Fragment> fragments) throws BadLocationException {
    int currentOffset = replacementOffset;
    boolean hasLinkedFragment = false;
    LinkedModeModel model = new LinkedModeModel();
    for (Fragment fragment : fragments) {
        int length = fragment.getText().length();
        if (fragment instanceof LinkedFragment) {
            LinkedPositionGroup group = new LinkedPositionGroup();
            group.addPosition(new LinkedPosition(document, currentOffset, length, LinkedPositionGroup.NO_STOP));
            model.addGroup(group);
            hasLinkedFragment = true;
        }
        currentOffset += length;
    }
    if (!hasLinkedFragment)
        return;
    model.forceInstall();
    LinkedModeUI ui = new EditorLinkedModeUI(model, context.getViewer());
    ui.setExitPosition(context.getViewer(), currentOffset, 0, Integer.MAX_VALUE);
    ui.setExitPolicy(new ExitPolicy(makeExitCharactersForLinkedMode(), document));
    ui.setDoContextInfo(true);
    ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT);
    ui.enter();
    IRegion region = ui.getSelectedRegion();
    regionToSelect = new Point(region.getOffset(), region.getLength());
}
Also used : IExitPolicy(org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) EditorLinkedModeUI(org.eclipse.ui.texteditor.link.EditorLinkedModeUI) LinkedModeModel(org.eclipse.jface.text.link.LinkedModeModel) LinkedModeUI(org.eclipse.jface.text.link.LinkedModeUI) EditorLinkedModeUI(org.eclipse.ui.texteditor.link.EditorLinkedModeUI) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)2 LinkedPosition (org.eclipse.jface.text.link.LinkedPosition)2 Point (org.eclipse.swt.graphics.Point)2 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)1 ProposalPosition (org.eclipse.che.jface.text.link.ProposalPosition)1 LinkedDataImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl)1 LinkedModeModelImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl)1 LinkedPositionGroupImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl)1 RegionImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)1 CompilationUnitContext (org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)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 IExitPolicy (org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy)1 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)1 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)1 TemplateException (org.eclipse.jface.text.templates.TemplateException)1