Search in sources :

Example 11 with Position

use of org.eclipse.jface.text.Position 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 12 with Position

use of org.eclipse.jface.text.Position 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 13 with Position

use of org.eclipse.jface.text.Position in project tdi-studio-se by Talend.

the class ReconcilerViewer method getAllSnippetsAnnotations.

private Map<ProjectionAnnotation, Position> getAllSnippetsAnnotations() {
    Map<ProjectionAnnotation, Position> annotations = new HashMap<ProjectionAnnotation, Position>();
    IDocument document = getDocument();
    int curOffset = 0;
    FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
    try {
        //$NON-NLS-1$
        IRegion startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false);
        while (startRegion != null && startRegion.getOffset() >= curOffset) {
            int startLine = document.getLineOfOffset(startRegion.getOffset());
            int startOffset = document.getLineOffset(startLine);
            curOffset = startOffset + document.getLineLength(startLine);
            //$NON-NLS-1$
            IRegion endRegion = frda.find(startRegion.getOffset(), "SNIPPET_END", true, false, false, false);
            if (endRegion != null) {
                int endLine = document.getLineOfOffset(endRegion.getOffset());
                int endOffset = document.getLineOffset(endLine);
                endOffset += document.getLineLength(endLine);
                curOffset = endOffset;
                String text = document.get(startOffset, endOffset - startOffset);
                ProjectionAnnotation annotation = new ProjectionAnnotation(true);
                annotation.setText(text);
                annotation.setRangeIndication(true);
                annotations.put(annotation, new Position(startOffset, endOffset - startOffset));
            }
            if (curOffset < document.getLength()) {
                //$NON-NLS-1$
                startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false);
            }
        }
    } catch (BadLocationException e) {
        ExceptionHandler.process(e);
    }
    return annotations;
}
Also used : ProjectionAnnotation(org.eclipse.jface.text.source.projection.ProjectionAnnotation) HashMap(java.util.HashMap) Position(org.eclipse.jface.text.Position) IDocument(org.eclipse.jface.text.IDocument) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 14 with Position

use of org.eclipse.jface.text.Position in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonEditor method show.

@Override
public boolean show(ShowInContext context) {
    ISelection selection = context.getSelection();
    if (selection instanceof IStructuredSelection) {
        Object selected = ((IStructuredSelection) selection).getFirstElement();
        if (selected instanceof AbstractNode) {
            Position position = ((AbstractNode) selected).getPosition(getSourceViewer().getDocument());
            selectAndReveal(position.getOffset(), position.getLength());
            return true;
        }
    }
    return false;
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) Position(org.eclipse.jface.text.Position) ISelection(org.eclipse.jface.viewers.ISelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 15 with Position

use of org.eclipse.jface.text.Position in project dbeaver by serge-rider.

the class SQLReconcilingStrategy method calculatePositions.

protected void calculatePositions() {
    ProjectionAnnotationModel annotationModel = editor.getAnnotationModel();
    if (annotationModel == null) {
        return;
    }
    Set<SQLScriptPosition> removedPositions = editor.getRuleManager().getRemovedPositions(true);
    Set<SQLScriptPosition> addedPositions = editor.getRuleManager().getAddedPositions(true);
    Annotation[] removedAnnotations = null;
    if (!removedPositions.isEmpty()) {
        removedAnnotations = new Annotation[removedPositions.size()];
        int index = 0;
        for (SQLScriptPosition pos : removedPositions) {
            removedAnnotations[index++] = pos.getFoldingAnnotation();
        }
    }
    Map<Annotation, Position> addedAnnotations = null;
    if (!addedPositions.isEmpty()) {
        addedAnnotations = new HashMap<>();
        for (SQLScriptPosition pos : addedPositions) {
            addedAnnotations.put(pos.getFoldingAnnotation(), pos);
        }
    }
    if (removedAnnotations != null || addedAnnotations != null) {
        annotationModel.modifyAnnotations(removedAnnotations, addedAnnotations, null);
    }
/*
        final List<Position> positions;

        try {
            positions = parseRegion();
        } catch (BadLocationException e) {
            e.printStackTrace();
            return;
        }
*/
/*
        Display.getDefault().asyncExec(new Runnable()
        {
            public void run()
            {
                editor.updateFoldingStructure(regionOffset, regionLength, positions);
            }

        });
*/
}
Also used : ProjectionAnnotationModel(org.eclipse.jface.text.source.projection.ProjectionAnnotationModel) Position(org.eclipse.jface.text.Position) Annotation(org.eclipse.jface.text.source.Annotation)

Aggregations

Position (org.eclipse.jface.text.Position)421 BadLocationException (org.eclipse.jface.text.BadLocationException)145 Annotation (org.eclipse.jface.text.source.Annotation)110 IDocument (org.eclipse.jface.text.IDocument)80 ArrayList (java.util.ArrayList)75 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)57 IRegion (org.eclipse.jface.text.IRegion)55 Test (org.junit.Test)54 HashMap (java.util.HashMap)49 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)46 Point (org.eclipse.swt.graphics.Point)40 List (java.util.List)39 Iterator (java.util.Iterator)31 TypedPosition (org.eclipse.jface.text.TypedPosition)31 Region (org.eclipse.jface.text.Region)27 IFile (org.eclipse.core.resources.IFile)22 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)21 ProjectionAnnotation (org.eclipse.jface.text.source.projection.ProjectionAnnotation)21 Map (java.util.Map)15 CoreException (org.eclipse.core.runtime.CoreException)15