Search in sources :

Example 31 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class SmartSemicolonAutoEditStrategy method isDefaultPartition.

/**
	 * Checks whether <code>position</code> resides in a default (Java) partition of <code>document</code>.
	 *
	 * @param document the document being modified
	 * @param position the position to be checked
	 * @param partitioning the document partitioning
	 * @return <code>true</code> if <code>position</code> is in the default partition of <code>document</code>, <code>false</code> otherwise
	 */
private static boolean isDefaultPartition(IDocument document, int position, String partitioning) {
    Assert.isTrue(position >= 0);
    Assert.isTrue(position <= document.getLength());
    try {
        // don't use getPartition2 since we're interested in the scanned character's partition
        ITypedRegion region = TextUtilities.getPartition(document, partitioning, position, false);
        return region.getType().equals(IDocument.DEFAULT_CONTENT_TYPE);
    } catch (BadLocationException e) {
    }
    return false;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 32 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class SmartSemicolonAutoEditStrategy method nextPartitionOrLineEnd.

/**
	 * Returns a position in the first java partition after the last non-empty and non-comment partition.
	 * There is no non-whitespace from the returned position to the end of the partition it is contained in.
	 *
	 * @param document the document being modified
	 * @param line the line under investigation
	 * @param offset the caret offset into <code>line</code>
	 * @param partitioning the document partitioning
	 * @return the position of the next Java partition, or the end of <code>line</code>
	 */
private static int nextPartitionOrLineEnd(IDocument document, ITextSelection line, int offset, String partitioning) {
    // run relative to document
    final int docOffset = offset + line.getOffset();
    final int eol = line.getOffset() + line.getLength();
    // init with line end
    int nextPartitionPos = eol;
    int validPosition = docOffset;
    try {
        ITypedRegion partition = TextUtilities.getPartition(document, partitioning, nextPartitionPos, true);
        validPosition = getValidPositionForPartition(document, partition, eol);
        while (validPosition == -1) {
            nextPartitionPos = partition.getOffset() - 1;
            if (nextPartitionPos < docOffset) {
                validPosition = docOffset;
                break;
            }
            partition = TextUtilities.getPartition(document, partitioning, nextPartitionPos, false);
            validPosition = getValidPositionForPartition(document, partition, eol);
        }
    } catch (BadLocationException e) {
    }
    validPosition = Math.max(validPosition, docOffset);
    // make relative to line
    validPosition -= line.getOffset();
    return validPosition;
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 33 with BadLocationException

use of org.eclipse.jface.text.BadLocationException in project che by eclipse.

the class TemplateEngine method complete.

/**
	 * Inspects the context of the compilation unit around <code>completionPosition</code>
	 * and feeds the collector with proposals.
	 * @param viewer the text viewer
	 * @param completionPosition the context position in the document of the text viewer
	 * @param compilationUnit the compilation unit (may be <code>null</code>)
	 */
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
    IDocument document = viewer.getDocument();
    if (!(fContextType instanceof CompilationUnitContextType))
        return;
    Point selection = viewer.getSelectedRange();
    Position position = new Position(completionPosition, selection.y);
    // remember selected text
    String selectedText = null;
    if (selection.y != 0) {
        try {
            selectedText = document.get(selection.x, selection.y);
            document.addPosition(position);
            fPositions.put(document, position);
        } catch (BadLocationException e) {
        }
    }
    CompilationUnitContext context = ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
    //$NON-NLS-1$
    context.setVariable("selection", selectedText);
    int start = context.getStart();
    int end = context.getEnd();
    IRegion region = new Region(start, end - start);
    Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates();
    if (selection.y == 0) {
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template)) {
                fProposals.add(new TemplateProposal(template, context, region, getImage()));
            }
        }
    } else {
        if (context.getKey().length() == 0)
            context.setForceEvaluation(true);
        boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
        for (int i = 0; i != templates.length; i++) {
            Template template = templates[i];
            if (context.canEvaluate(template) && (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) {
                fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
            }
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) Point(org.eclipse.swt.graphics.Point) CompilationUnitContextType(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType) Point(org.eclipse.swt.graphics.Point) IRegion(org.eclipse.jface.text.IRegion) Template(org.eclipse.jface.text.templates.Template) CompilationUnitContext(org.eclipse.jdt.internal.corext.template.java.CompilationUnitContext) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 34 with BadLocationException

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

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

Aggregations

BadLocationException (org.eclipse.jface.text.BadLocationException)455 IDocument (org.eclipse.jface.text.IDocument)196 IRegion (org.eclipse.jface.text.IRegion)161 Test (org.junit.Test)102 Position (org.eclipse.jface.text.Position)101 Region (org.eclipse.jface.text.Region)68 Point (org.eclipse.swt.graphics.Point)61 Document (org.eclipse.jface.text.Document)47 CoreException (org.eclipse.core.runtime.CoreException)34 ArrayList (java.util.ArrayList)27 ITypedRegion (org.eclipse.jface.text.ITypedRegion)27 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)22 DocumentEvent (org.eclipse.jface.text.DocumentEvent)21 ITextSelection (org.eclipse.jface.text.ITextSelection)21 StyledText (org.eclipse.swt.custom.StyledText)18 StyledString (org.eclipse.jface.viewers.StyledString)17 IStatus (org.eclipse.core.runtime.IStatus)16 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)15 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)15 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)15