Search in sources :

Example 1 with IDocumentExtension

use of org.eclipse.jface.text.IDocumentExtension 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)

Example 2 with IDocumentExtension

use of org.eclipse.jface.text.IDocumentExtension in project erlide_eclipse by erlang.

the class ErlangBracketInserter method left.

/*
     * @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.eclipse.
     * jface.text.link.LinkedModeModel, int)
     */
@Override
@SuppressWarnings("synthetic-access")
public void left(final LinkedModeModel environment, final int flags) {
    final BracketLevel level = fBracketLevelStack.pop();
    if (flags != ILinkedModeListener.EXTERNAL_MODIFICATION) {
        return;
    }
    // remove brackets
    final IDocument document = sourceViewer.getDocument();
    if (document instanceof IDocumentExtension) {
        final IDocumentExtension extension = (IDocumentExtension) document;
        extension.registerPostNotificationReplace(null, new IDocumentExtension.IReplace() {

            @Override
            public void perform(final IDocument d, final IDocumentListener owner) {
                if ((level.fFirstPosition.isDeleted || level.fFirstPosition.length == 0) && !level.fSecondPosition.isDeleted && level.fSecondPosition.offset == level.fFirstPosition.offset) {
                    try {
                        document.replace(level.fSecondPosition.offset, level.fSecondPosition.length, // $NON-NLS-1$
                        "");
                    } catch (final BadLocationException e) {
                        ErlLogger.error(e);
                    }
                }
                if (fBracketLevelStack.isEmpty()) {
                    document.removePositionUpdater(fUpdater);
                    try {
                        document.removePositionCategory(CATEGORY);
                    } catch (final BadPositionCategoryException e) {
                        ErlLogger.error(e);
                    }
                }
            }
        });
    }
}
Also used : IDocumentExtension(org.eclipse.jface.text.IDocumentExtension) IDocumentListener(org.eclipse.jface.text.IDocumentListener) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocumentExtension (org.eclipse.jface.text.IDocumentExtension)2 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 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)1 IDocument (org.eclipse.jface.text.IDocument)1 IDocumentListener (org.eclipse.jface.text.IDocumentListener)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1 Region (org.eclipse.jface.text.Region)1 Point (org.eclipse.swt.graphics.Point)1