public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) { if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) { LinkedPosition position= model.findPosition(new LinkedPosition(fDocument, offset, 0, LinkedPositionGroup.NO_STOP)); if (position != null) { if (event.character == SWT.BS) { if (offset - 1 < position.getOffset()) { //skip backspace at beginning of linked position event.doit= false; } } else /* event.character == SWT.DEL */ { if (offset + 1 > position.getOffset() + position.getLength()) { //skip delete at end of linked position event.doit= false; } } } } return null; // don't change behavior } }
/** * Returns whether this position includes <code>position</code>. * * @param position the position to check. * @return <code>true</code> if this position includes * <code>position</code>,<code>false</code> otherwise */ public boolean includes(LinkedPosition position) { return includes(position.getDocument(), position.getOffset(), position.getLength()); }
private void restoreFullSelection() { if (fOriginalSelection.y != 0) { int originalOffset= fOriginalSelection.x; LinkedPosition[] positions= fLinkedPositionGroup.getPositions(); for (int i= 0; i < positions.length; i++) { LinkedPosition position= positions[i]; if (! position.isDeleted() && position.includes(originalOffset)) { fEditor.getViewer().setSelectedRange(position.offset, position.length); return; } } } }
/** * Returns whether this position overlaps with <code>position</code>. * * @param position the position to check. * @return <code>true</code> if this position overlaps with * <code>position</code>,<code>false</code> otherwise */ public boolean overlapsWith(LinkedPosition position) { return position.getDocument() == fDocument && overlapsWith(position.getOffset(), position.getLength()); }
@Override public int compare(LinkedPosition p1, LinkedPosition p2) { int i= p1.getSequenceNumber() - p2.getSequenceNumber(); if (i != 0) return i; return p1.getOffset() - p2.getOffset(); }
if (p == fLastPosition || p.isDeleted()) continue; // don't re-update the origin of the change List<ReplaceEdit> edits= map.get(p.getDocument()); if (edits == null) { edits= new ArrayList<>(); map.put(p.getDocument(), edits); edits.add(new ReplaceEdit(p.getOffset(), p.getLength(), fLastPosition.getContent())); } catch (BadLocationException e) { throw new RuntimeException(e); // should not happen edits.add(new ReplaceEdit(p.getOffset() + relativeOffset, length, text));
LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, offset, 0)); if (linkedPosition != null) { int linkedPositionOffset= linkedPosition.getOffset(); int lineOffset; try {
@Override public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { IDocument doc= viewer.getDocument(); LinkedPosition position= fLinkedPositionModel.findPosition(new LinkedPosition(doc, offset, 0)); if (position != null) { try { try { TextEdit edit= fProposal.computeEdits(offset, position, trigger, stateMask, fLinkedPositionModel); if (edit != null) { edit.apply(position.getDocument(), 0); } } catch (MalformedTreeException e) { throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, "Unexpected exception applying edit", e)); //$NON-NLS-1$ } catch (BadLocationException e) { throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, "Unexpected exception applying edit", e)); //$NON-NLS-1$ } } catch (CoreException e) { JavaPlugin.log(e); } } }
/** * Checks whether the given event touches the given position. To touch means * to overlap or come up to the borders of the position. * * @param position the position * @param event the event * @return <code>true</code> if <code>position</code> and * <code>event</code> are not absolutely disjoint * @since 3.1 */ private boolean overlapsOrTouches(LinkedPosition position, DocumentEvent event) { return position.getDocument().equals(event.getDocument()) && position.getOffset() <= event.getOffset() + event.getLength() && position.getOffset() + position.getLength() >= event.getOffset(); }
public LinkedPosition apply(ReplaceEdit edit) { try { String textToReplace = document.get(edit.getOffset(), edit.getLength()); int indexOf = textToReplace.indexOf(originalName); if (indexOf != -1) { int calculatedOffset = edit.getOffset() + indexOf; return new LinkedPosition(document, calculatedOffset, originalName.length()); } } catch (BadLocationException exc) { LOG.error("Skipping invalid text edit " + notNull(edit), exc); } return null; } }), Predicates.notNull());
@Override public void selectionChanged(SelectionChangedEvent event) { ISelection selection= event.getSelection(); if (selection instanceof ITextSelection) { ITextSelection textsel= (ITextSelection) selection; if (event.getSelectionProvider() instanceof ITextViewer) { IDocument doc= ((ITextViewer) event.getSelectionProvider()).getDocument(); if (doc != null) { int offset= textsel.getOffset(); int length= textsel.getLength(); if (offset >= 0 && length >= 0) { LinkedPosition find= new LinkedPosition(doc, offset, length, LinkedPositionGroup.NO_STOP); LinkedPosition pos= fModel.findPosition(find); if (pos == null && fExitPosition != null && fExitPosition.includes(find)) pos= fExitPosition; if (pos != null) switchPosition(pos, false, false); } } } } }
/** * Finds the closest position to <code>toFind</code>. * * @param toFind the linked position for which to find the closest position * @return the closest position to <code>toFind</code>. */ LinkedPosition getPosition(LinkedPosition toFind) { for (LinkedPosition p : fPositions) { if (p.includes(toFind)) return p; } return null; }
private boolean controlUndoBehavior(int offset, int length) { LinkedPosition position= fModel.findPosition(new LinkedPosition(fCurrentTarget.getViewer().getDocument(), offset, length, LinkedPositionGroup.NO_STOP)); if (position != null) { // if the last position is not the same and there is an open change: close it. if (!position.equals(fPreviousPosition)) endCompoundChangeIfNeeded(); beginCompoundChangeIfNeeded(); } fPreviousPosition= position; return fPreviousPosition != null; }
IDocument[] getDocuments() { IDocument[] docs= new IDocument[fPositions.size()]; int i= 0; for (Iterator<LinkedPosition> it= fPositions.iterator(); it.hasNext(); i++) { LinkedPosition pos= it.next(); docs[i]= pos.getDocument(); } return docs; }
public boolean isOriginalName() { try { String newName= fNamePosition.getContent(); return fOriginalName.equals(newName); } catch (BadLocationException e) { return false; } }
private int rank(LinkedPosition o1) { int relativeRank = o1.getOffset() + o1.length - invocationOffset; if (relativeRank < 0) return Integer.MAX_VALUE + relativeRank; else return relativeRank; } };
/** * Registers a <code>LinkedPosition</code> with this model. Called * by <code>PositionGroup</code>. * * @param position the position to register * @throws BadLocationException if the position cannot be added to its * document */ void register(LinkedPosition position) throws BadLocationException { Assert.isNotNull(position); IDocument document= position.getDocument(); manageDocument(document); try { document.addPosition(getCategory(), position); } catch (BadPositionCategoryException e) { // won't happen as the category has been added by manageDocument() Assert.isTrue(false); } int seqNr= position.getSequenceNumber(); if (seqNr != LinkedPositionGroup.NO_STOP) { fPositionSequence.add(position); } }
if ((flags & ILinkedModeListener.UPDATE_CARET) != 0 && fExitPosition != null && fFramePosition != fExitPosition && !fExitPosition.isDeleted()) switchPosition(fExitPosition, true, false); fExitPosition.getDocument().removePosition(fExitPosition);
private void switchPosition(LinkedPosition pos, boolean select, boolean showProposals) { Assert.isNotNull(pos); if (pos.equals(fFramePosition)) return; if (fFramePosition != null && fCurrentTarget != null) fPositionListener.linkingFocusLost(fFramePosition, fCurrentTarget); // undo endCompoundChangeIfNeeded(); redraw(); // redraw current position being left - usually not needed IDocument oldDoc= fFramePosition == null ? null : fFramePosition.getDocument(); IDocument newDoc= pos.getDocument(); switchViewer(oldDoc, newDoc, pos); fFramePosition= pos; if (select) select(); if (fFramePosition == fExitPosition && !fIterator.isCycling()) leave(ILinkedModeListener.NONE); else { redraw(); // redraw new position ensureAnnotationModelInstalled(); } if (showProposals) triggerContentAssist(); if (fFramePosition != fExitPosition && fDoContextInfo) triggerContextInfo(); if (fFramePosition != null && fCurrentTarget != null) fPositionListener.linkingFocusGained(fFramePosition, fCurrentTarget); }