Search in sources :

Example 1 with LinkedModeModelImpl

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

the class LinkedCorrectionProposal method performChange.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ChangeCorrectionProposal#performChange(org.eclipse.jface.text.IDocument, org.eclipse.ui.IEditorPart)
	 */
@Override
protected void performChange(/*IEditorPart part,*/
IDocument document) throws CoreException {
    //		try {
    super.performChange(/*part,*/
    document);
    //
    if (fLinkedProposalModel != null) {
        if (fLinkedProposalModel.hasLinkedPositions()) {
            // enter linked mode
            //					ITextViewer viewer= ((JavaEditor) part).getViewer();
            //					new LinkedProposalModelPresenter().enterLinkedMode(viewer, part, didOpenEditor(), fLinkedProposalModel);
            boolean added = false;
            LinkedModeModelImpl model = new LinkedModeModelImpl();
            Iterator<LinkedProposalPositionGroup> iterator = fLinkedProposalModel.getPositionGroupIterator();
            while (iterator.hasNext()) {
                LinkedProposalPositionGroup curr = iterator.next();
                LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
                LinkedProposalPositionGroup.PositionInformation[] positions = curr.getPositions();
                if (positions.length > 0) {
                    LinkedProposalPositionGroup.Proposal[] linkedModeProposals = curr.getProposals();
                    if (linkedModeProposals.length <= 1) {
                        for (int i = 0; i < positions.length; i++) {
                            LinkedProposalPositionGroup.PositionInformation pos = positions[i];
                            if (pos.getOffset() != -1) {
                                RegionImpl position = new RegionImpl();
                                position.setOffset(pos.getOffset());
                                position.setLength(pos.getLength());
                                //                                        group.addPositions(
                                //                                                new LinkedPosition(document, pos.getOffset(), pos.getLength(), pos.getSequenceRank()));
                                group.addPositions(position);
                            }
                        }
                    } else {
                        //                                LinkedPositionProposalImpl[] proposalImpls= new LinkedPositionProposalImpl[linkedModeProposals.length];
                        LinkedDataImpl data = new LinkedDataImpl();
                        for (int i = 0; i < linkedModeProposals.length; i++) {
                            //                                    proposalImpls[i] = new LinkedPositionProposalImpl(linkedModeProposals[i], model);
                            data.addValues(linkedModeProposals[i].getDisplayString());
                        }
                        group.setData(data);
                        for (int i = 0; i < positions.length; i++) {
                            LinkedProposalPositionGroup.PositionInformation pos = positions[i];
                            if (pos.getOffset() != -1) {
                                //                                        group.addPosition(
                                //                                                new ProposalPosition(document, pos.getOffset(), pos.getLength(), pos.getSequenceRank(),
                                //                                                                     proposalImpls));
                                RegionImpl position = new RegionImpl();
                                position.setOffset(pos.getOffset());
                                position.setLength(pos.getLength());
                                group.addPositions(position);
                            }
                        }
                    }
                    model.addGroups(group);
                    added = true;
                }
            }
            if (added) {
                LinkedProposalPositionGroup.PositionInformation endPosition = fLinkedProposalModel.getEndPosition();
                if (endPosition != null && endPosition.getOffset() != -1) {
                    model.setEscapePosition(endPosition.getOffset() + endPosition.getLength());
                }
                this.linkedModel = model;
            }
        }
    //				else if (part instanceof ITextEditor) {
    //					LinkedProposalPositionGroup.PositionInformation endPosition= fLinkedProposalModel.getEndPosition();
    //					if (endPosition != null) {
    //						// select a result
    //						int pos= endPosition.getOffset() + endPosition.getLength();
    //						((ITextEditor) part).selectAndReveal(pos, 0);
    //					}
    //				}
    }
//		} catch (BadLocationException e) {
//			throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
//		}
}
Also used : LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) LinkedDataImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)

Example 2 with LinkedModeModelImpl

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

the class LinkedNamesAssistProposal method apply.

/* (non-Javadoc)
     * @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) {
    try {
        Point seletion = viewer.getSelectedRange();
        // get full ast
        CompilationUnit root = SharedASTProvider.getAST(fContext.getCompilationUnit(), SharedASTProvider.WAIT_YES, null);
        ASTNode nameNode = NodeFinder.perform(root, fNode.getStartPosition(), fNode.getLength());
        final int pos = fNode.getStartPosition();
        ASTNode[] sameNodes;
        if (nameNode instanceof SimpleName) {
            sameNodes = LinkedNodeFinder.findByNode(root, (SimpleName) nameNode);
        } else {
            sameNodes = new ASTNode[] { nameNode };
        }
        // sort for iteration order, starting with the node @ offset
        Arrays.sort(sameNodes, new Comparator<ASTNode>() {

            public int compare(ASTNode o1, ASTNode o2) {
                return rank(o1) - rank(o2);
            }

            /**
                 * Returns the absolute rank of an <code>ASTNode</code>. Nodes
                 * preceding <code>offset</code> are ranked last.
                 *
                 * @param node the node to compute the rank for
                 * @return the rank of the node with respect to the invocation offset
                 */
            private int rank(ASTNode node) {
                int relativeRank = node.getStartPosition() + node.getLength() - pos;
                if (relativeRank < 0)
                    return Integer.MAX_VALUE + relativeRank;
                else
                    return relativeRank;
            }
        });
        IDocument document = viewer.getDocument();
        LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
        for (int i = 0; i < sameNodes.length; i++) {
            ASTNode elem = sameNodes[i];
            RegionImpl region = new RegionImpl();
            region.setOffset(elem.getStartPosition());
            region.setLength(elem.getLength());
            group.addPositions(region);
        //				group.addPosition(new LinkedPosition(document, elem.getStartPosition(), elem.getLength(), i));
        }
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        model.addGroups(group);
        //			model.forceInstall();
        model.setEscapePosition(offset);
        this.linkedModel = model;
        if (fContext instanceof AssistContext) {
        //				IEditorPart editor = ((AssistContext)fContext).getEditor();
        //				if (editor instanceof JavaEditor) {
        //					model.addLinkingListener(new EditorHighlightingSynchronizer((JavaEditor)editor));
        //				}
        }
        if (fValueSuggestion != null) {
            document.replace(nameNode.getStartPosition(), nameNode.getLength(), fValueSuggestion);
        //				IRegion selectedRegion = ui.getSelectedRegion();
        //				seletion = new Point(selectedRegion.getOffset(), fValueSuggestion.length());
        }
    //			viewer.setSelectedRange(seletion.x, seletion.y); // by default full word is selected, restore original selection
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) SimpleName(org.eclipse.jdt.core.dom.SimpleName) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with LinkedModeModelImpl

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

the class RenameLinkedModeRefactoringSession method getModel.

public LinkedModeModel getModel() {
    CompilationUnit root = SharedASTProvider.getAST(compilationUnit, SharedASTProvider.WAIT_YES, null);
    LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
    ASTNode selectedNode = NodeFinder.perform(root, offset, 0);
    if (!(selectedNode instanceof SimpleName)) {
        return null;
    }
    SimpleName nameNode = (SimpleName) selectedNode;
    fOriginalName = nameNode.getIdentifier();
    final int pos = nameNode.getStartPosition();
    ASTNode[] sameNodes = LinkedNodeFinder.findByNode(root, nameNode);
    //TODO: copied from LinkedNamesAssistProposal#apply(..):
    // sort for iteration order, starting with the node @ offset
    Arrays.sort(sameNodes, new Comparator<ASTNode>() {

        public int compare(ASTNode o1, ASTNode o2) {
            return rank(o1) - rank(o2);
        }

        /**
             * Returns the absolute rank of an <code>ASTNode</code>. Nodes
             * preceding <code>pos</code> are ranked last.
             *
             * @param node the node to compute the rank for
             * @return the rank of the node with respect to the invocation offset
             */
        private int rank(ASTNode node) {
            int relativeRank = node.getStartPosition() + node.getLength() - pos;
            if (relativeRank < 0)
                return Integer.MAX_VALUE + relativeRank;
            else
                return relativeRank;
        }
    });
    for (int i = 0; i < sameNodes.length; i++) {
        ASTNode elem = sameNodes[i];
        RegionImpl position = new RegionImpl();
        position.setOffset(elem.getStartPosition());
        position.setLength(elem.getLength());
        group.addPositions(position);
    }
    LinkedModeModelImpl model = new LinkedModeModelImpl();
    model.addGroups(group);
    return model;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)

Example 4 with LinkedModeModelImpl

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

the class FilledArgumentNamesMethodProposal method apply.

/*
     * @see ICompletionProposalExtension#apply(IDocument, char)
     */
@Override
public void apply(IDocument document, char trigger, int offset) {
    super.apply(document, trigger, offset);
    int baseOffset = getReplacementOffset();
    String replacement = getReplacementString();
    if (fArgumentOffsets != null && getTextViewer() != null) {
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        for (int i = 0; i != fArgumentOffsets.length; i++) {
            LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
            RegionImpl region = new RegionImpl();
            region.setLength(fArgumentLengths[i]);
            region.setOffset(baseOffset + fArgumentOffsets[i]);
            group.addPositions(region);
            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);
        //                ui.setExitPolicy(new ExitPolicy(')', document));
        //                ui.setDoContextInfo(true);
        //                ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT);
        //                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) 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) Point(org.eclipse.swt.graphics.Point)

Example 5 with LinkedModeModelImpl

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

the class LazyGenericTypeProposal method installLinkedMode.

private void installLinkedMode(final IDocument document, int[] offsets, int[] lengths, TypeArgumentProposal[] typeArgumentProposals, boolean withParentheses, final boolean onlyAppendArguments) {
    int replacementOffset = getReplacementOffset();
    String replacementString = getReplacementString();
    try {
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        for (int i = 0; i != offsets.length; i++) {
            if (typeArgumentProposals[i].isAmbiguous()) {
                LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
                RegionImpl region = new RegionImpl();
                region.setOffset(replacementOffset + offsets[i]);
                region.setLength(lengths[i]);
                group.addPositions(region);
                model.addGroups(group);
            }
        }
        if (withParentheses) {
            LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
            RegionImpl region = new RegionImpl();
            region.setOffset(replacementOffset + getCursorPosition());
            region.setLength(0);
            group.addPositions(region);
            model.addGroups(group);
        }
        model.setEscapePosition(replacementOffset + replacementString.length());
        this.linkedModel = model;
        if (!onlyAppendArguments && (document instanceof IDocumentExtension)) {
            // see bug 301990
            FormatterPrefs prefs = getFormatterPrefs();
            final Position firstBracketPosition;
            final Position secondBracketPosition;
            int firstBracketOffset = replacementOffset + offsets[0] - 1;
            if (prefs.afterOpeningBracket) {
                firstBracketOffset--;
            }
            firstBracketPosition = new Position(firstBracketOffset, 1);
            document.addPosition(firstBracketPosition);
            int secondBracketOffset = replacementOffset + offsets[offsets.length - 1] + lengths[offsets.length - 1] + 1;
            if (prefs.beforeClosingBracket) {
                secondBracketOffset++;
            }
            secondBracketPosition = new Position(secondBracketOffset, 0);
            document.addPosition(secondBracketPosition);
        //				model.addLinkingListener(new ILinkedModeListener() {
        //					public void left(LinkedModeModel environment, int flags) {
        //						try {
        //							if (getTextViewer().getSelectedRange().y > 1 || flags != ILinkedModeListener.EXTERNAL_MODIFICATION)
        //								return;
        //							((IDocumentExtension) document).registerPostNotificationReplace(null, new IDocumentExtension.IReplace() {
        //								public void perform(IDocument d, IDocumentListener owner) {
        //									try {
        //										if ((firstBracketPosition.length == 0 || firstBracketPosition.isDeleted) && !secondBracketPosition.isDeleted) {
        //											d.replace(firstBracketPosition.offset, secondBracketPosition.offset - firstBracketPosition.offset, ""); //$NON-NLS-1$
        //										}
        //									} catch (BadLocationException e) {
        //										JavaPlugin.log(e);
        //									}
        //								}
        //							});
        //						} finally {
        //							document.removePosition(firstBracketPosition);
        //							document.removePosition(secondBracketPosition);
        //						}
        //					}
        //
        //					public void suspend(LinkedModeModel environment) {
        //					}
        //
        //					public void resume(LinkedModeModel environment, int flags) {
        //					}
        //				});
        }
        //			LinkedModeUI ui= new EditorLinkedModeUI(model, getTextViewer());
        //			ui.setExitPolicy(new ExitPolicy(withParentheses ? ')' : '>', document));
        //			ui.setExitPosition(getTextViewer(), replacementOffset + replacementString.length(), 0, Integer.MAX_VALUE);
        //			ui.setDoContextInfo(true);
        //			ui.enter();
        //ui.getSelectedRegion();
        fSelectedRegion = new Region(replacementOffset + replacementString.length(), 0);
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
        openErrorDialog(e);
    }
}
Also used : IDocumentExtension(org.eclipse.jface.text.IDocumentExtension) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) Position(org.eclipse.jface.text.Position) 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) Point(org.eclipse.swt.graphics.Point) BadLocationException(org.eclipse.jface.text.BadLocationException)

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