Search in sources :

Example 6 with LinkedPositionGroupImpl

use of org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl 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 7 with LinkedPositionGroupImpl

use of org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl in project che by eclipse.

the class ParameterGuessingProposal method apply.

/*
     * @see ICompletionProposalExtension#apply(IDocument, char)
     */
@Override
public void apply(final IDocument document, char trigger, int offset) {
    super.apply(document, trigger, offset);
    int baseOffset = getReplacementOffset();
    String replacement = getReplacementString();
    if (fPositions != null && getTextViewer() != null) {
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        for (int i = 0; i < fPositions.length; i++) {
            LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
            int positionOffset = fPositions[i].getOffset();
            int positionLength = fPositions[i].getLength();
            if (fChoices[i].length < 2) {
                RegionImpl region = new RegionImpl();
                region.setOffset(positionOffset);
                region.setLength(positionLength);
                //                        group.addPositions(new LinkedPosition(document, positionOffset, positionLength, LinkedPositionGroup.NO_STOP));
                group.addPositions(region);
            } else {
                //                        ensurePositionCategoryInstalled(document, model);
                //                        document.addPosition(getCategory(), fPositions[i]);
                RegionImpl region = new RegionImpl();
                region.setOffset(positionOffset);
                region.setLength(positionLength);
                //                        group.addPositions(
                //                                new ProposalPosition(document, positionOffset, positionLength, LinkedPositionGroup.NO_STOP, fChoices[i]));
                group.addPositions(region);
                LinkedDataImpl data = new LinkedDataImpl();
                for (ICompletionProposal proposal : fChoices[i]) {
                    data.addValues(proposal.getDisplayString());
                }
                group.setData(data);
            }
            model.addGroups(group);
        }
        model.setEscapePosition(baseOffset + replacement.length());
        this.linkedModel = model;
        //                model.forceInstall();
        //                JavaEditor editor = getJavaEditor();
        //                if (editor != null) {
        //                    model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
        //                }
        //                LinkedModeUI ui = new EditorLinkedModeUI(model, getTextViewer());
        //                ui.setExitPosition(getTextViewer(), baseOffset + replacement.length(), 0, Integer.MAX_VALUE);
        //                // exit character can be either ')' or ';'
        //                final char exitChar = replacement.charAt(replacement.length() - 1);
        //                ui.setExitPolicy(new ExitPolicy(exitChar, document) {
        //                    @Override
        //                    public ExitFlags doExit(LinkedModeModel model2, VerifyEvent event, int offset2, int length) {
        //                        if (event.character == ',') {
        //                            for (int i = 0; i < fPositions.length - 1; i++) { // not for the last one
        //                                Position position = fPositions[i];
        //                                if (position.offset <= offset2 && offset2 + length <= position.offset + position.length) {
        //                                    try {
        //                                        ITypedRegion partition = TextUtilities
        //                                                .getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset2 + length, false);
        //                                        if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
        //                                            || offset2 + length == partition.getOffset() + partition.getLength()) {
        //                                            event.character = '\t';
        //                                            event.keyCode = SWT.TAB;
        //                                            return null;
        //                                        }
        //                                    } catch (BadLocationException e) {
        //                                        // continue; not serious enough to log
        //                                    }
        //                                }
        //                            }
        //                        } else if (event.character == ')' && exitChar != ')') {
        //                            // exit from link mode when user is in the last ')' position.
        //                            Position position = fPositions[fPositions.length - 1];
        //                            if (position.offset <= offset2 && offset2 + length <= position.offset + position.length) {
        //								return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
        //							}
        //						}
        //						return super.doExit(model2, event, offset2, length);
        //					}
        //				});
        //				ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT);
        //				ui.setDoContextInfo(true);
        //				ui.enter();
        fSelectedRegion = new Region(baseOffset + replacement.length(), 0);
    } else {
        fSelectedRegion = new Region(baseOffset + replacement.length(), 0);
    }
}
Also used : LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) LinkedDataImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl) Point(org.eclipse.swt.graphics.Point)

Aggregations

LinkedModeModelImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl)7 LinkedPositionGroupImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl)7 RegionImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)7 Point (org.eclipse.swt.graphics.Point)5 IRegion (org.eclipse.jface.text.IRegion)4 Region (org.eclipse.jface.text.Region)4 LinkedDataImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 IDocument (org.eclipse.jface.text.IDocument)2 Position (org.eclipse.jface.text.Position)2 ProposalPosition (org.eclipse.che.jface.text.link.ProposalPosition)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 LinkedProposalPositionGroup (org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)1 CompilationUnitContext (org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext)1 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)1 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)1