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; }
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); } } } } }
/** * Starts this UI at position p. * * @throws BadLocationException * if p does not exist in the underlying model */ public void enter(Position p) throws BadLocationException { if (p == null) enter(); else { init(); LinkedPosition initFramePosition = null; while (fFramePosition != p) { if (initFramePosition == null) initFramePosition = fFramePosition; next(); if (foundNotwhere(p, initFramePosition)) throw new BadLocationException(); } } }
private void switchViewer(IDocument oldDoc, IDocument newDoc, LinkedPosition pos) { if (oldDoc != newDoc) { // redraw current document with new position before switching viewer if (fCurrentTarget.fAnnotationModel != null) fCurrentTarget.fAnnotationModel.switchToPosition(fModel, pos); LinkedModeUITarget target = null; for (int i = 0; i < fTargets.length; i++) { if (fTargets[i].getViewer().getDocument() == newDoc) { target = fTargets[i]; break; } } if (target != fCurrentTarget) { disconnect(); fCurrentTarget = target; target.linkingFocusLost(fFramePosition, target); connect(); ensureAnnotationModelInstalled(); if (fCurrentTarget != null) fCurrentTarget.linkingFocusGained(pos, fCurrentTarget); } } }
@Override 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 }
/** * Sets up a simple linked mode at {@link #getCursorPosition()} and an exit policy that will * exit the mode when <code>closingCharacter</code> is typed and an exit position at * <code>getCursorPosition() + 1</code>. * * @param document the document */ protected void setUpLinkedMode(IDocument document) { try { LinkedPositionGroup group= new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, getSelectionStart(), getSelectionLength(), LinkedPositionGroup.NO_STOP)); LinkedModeModel model= new LinkedModeModel(); model.addGroup(group); model.forceInstall(); LinkedModeUI ui= new LinkedModeUI(model, viewer); // ui.setSimpleMode(true); ui.setExitPolicy(new ExitPolicy(exitChars)); ui.setExitPosition(viewer, getCursorPosition() + getReplacementOffset(), 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER); ui.enter(); } catch (BadLocationException e) { log.info(e.getMessage(), e); } }
public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) { showPreview = (event.stateMask & SWT.CTRL) != 0 && (event.character == SWT.CR || event.character == SWT.LF); if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) { LinkedPosition position = model.findPosition(new LinkedPosition(document, 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 }
protected Point computePopupLocation() { if (popup == null || popup.isDisposed()) return null; LinkedPosition position = renameLinkedMode.getCurrentLinkedPosition(); if (position == null) return null; ISourceViewer viewer = editor.getInternalSourceViewer(); ITextViewerExtension5 viewer5 = (ITextViewerExtension5) viewer; int widgetOffset = viewer5.modelOffset2WidgetOffset(position.offset); StyledText textWidget = viewer.getTextWidget(); Point pos = textWidget.getLocationAtOffset(widgetOffset); Point pSize = getExtent(); pSize.y += HAH + 1; pos.x -= HAO; pos.y += textWidget.getLineHeight(widgetOffset); Point dPos = textWidget.toDisplay(pos); Rectangle displayBounds = textWidget.getDisplay().getClientArea(); Rectangle dPopupRect = Geometry.createRectangle(dPos, pSize); Geometry.moveInside(dPopupRect, displayBounds); return new Point(dPopupRect.x, dPopupRect.y); }
protected Iterable<LinkedPosition> sortPositions(Iterable<LinkedPosition> linkedPositions, final int invocationOffset) { Comparator<LinkedPosition> comparator = new Comparator<LinkedPosition>() { public int compare(LinkedPosition left, LinkedPosition right) { return rank(left) - rank(right); } private int rank(LinkedPosition o1) { int relativeRank = o1.getOffset() + o1.length - invocationOffset; if (relativeRank < 0) return Integer.MAX_VALUE + relativeRank; else return relativeRank; } }; return ImmutableSortedSet.copyOf(comparator, linkedPositions); }
/** * Sets up a simple linked mode at {@link #getCursorPosition()} and an exit policy that will * exit the mode when <code>closingCharacter</code> is typed and an exit position at * <code>getCursorPosition() + 1</code>. * * @param document the document * @param closingCharacter the exit character */ protected void setUpLinkedMode(IDocument document, char closingCharacter) { if (getTextViewer() != null && autocloseBrackets()) { int offset= getReplacementOffset() + getCursorPosition(); int exit= getReplacementOffset() + getReplacementString().length(); try { LinkedPositionGroup group= new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, offset, 0, LinkedPositionGroup.NO_STOP)); LinkedModeModel model= new LinkedModeModel(); model.addGroup(group); model.forceInstall(); LinkedModeUI ui= new EditorLinkedModeUI(model, getTextViewer()); ui.setSimpleMode(true); ui.setExitPolicy(new ExitPolicy(closingCharacter, document)); ui.setExitPosition(getTextViewer(), exit, 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER); ui.enter(); } catch (BadLocationException x) { JavaPlugin.log(x); } } }
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 }
private void startLinkedEdit(List<IRegion> selections, ITextViewer viewer, Point originalSelection) throws BadLocationException { final LinkedPositionGroup linkedPositionGroup = new LinkedPositionGroup(); for (IRegion selection : selections) { linkedPositionGroup.addPosition(new LinkedPosition(viewer.getDocument(), selection.getOffset(), selection .getLength())); } LinkedModeModel model = new LinkedModeModel(); model.addGroup(linkedPositionGroup); model.forceInstall(); //FIXME can add a listener here to listen for the end of linked mode //model.addLinkingListener(null); LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPolicy(new DeleteBlockingExitPolicy(viewer.getDocument())); ui.enter(); // by default the text being edited is selected so restore original selection viewer.setSelectedRange(originalSelection.x, originalSelection.y); }
protected LinkedModeModel getLinkedModeModel(ITextViewer viewer) throws BadLocationException { Indexable<SourceRange> sourceSubElements = proposal.getSourceSubElements(); if(sourceSubElements == null || sourceSubElements.isEmpty()) { return null; } LinkedModeModel model = new LinkedModeModel(); IDocument document = viewer.getDocument(); int replaceOffset = getReplaceOffset(); firstLinkedModeGroupPosition = -1; for (SourceRange sr : sourceSubElements) { LinkedPositionGroup group = new LinkedPositionGroup(); int posOffset = replaceOffset + sr.getOffset(); group.addPosition(new LinkedPosition(document, posOffset, sr.getLength())); if(firstLinkedModeGroupPosition == -1) { firstLinkedModeGroupPosition = posOffset; } model.addGroup(group); } return model; }
/** * Prune <code>list</code> of all <code>LinkedPosition</code>s that * do not belong to this model's <code>IDocument</code>. * * @param list the list of positions to prune */ private void prune(List list) { for (Iterator iter= list.iterator(); iter.hasNext();) { LinkedPosition pos= (LinkedPosition) iter.next(); if (!pos.getDocument().equals(fDocument)) iter.remove(); } }
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); }
/** * {@inheritDoc} * * <p><code>o1</code> and <code>o2</code> are required to be instances * of <code>LinkedPosition</code>.</p> */ public int compare(Object o1, Object o2) { LinkedPosition p1= (LinkedPosition)o1; LinkedPosition p2= (LinkedPosition)o2; int i= p1.getSequenceNumber() - p2.getSequenceNumber(); if (i != 0) return i; return p1.getOffset() - p2.getOffset(); }
private int getNextIndex(LinkedPosition current) { if (current != null && fList.get(fIndex) != current) return findNext(current); else if (fIsCycling && fIndex == fSize - 1) return 0; else // default: increase return fIndex + 1; }
/** * Finds the closest position in the iteration set that follows after * <code>current</code> and sets <code>fIndex</code> accordingly. If <code>current</code> * is in the iteration set, the next in turn is chosen. * * @param current the current position * @return <code>true</code> if there is a next position, <code>false</code> otherwise */ private int findNext(LinkedPosition current) { Assert.isNotNull(current); // if the position is in the iteration set, jump to the next one int index= fList.indexOf(current); if (index != -1) { if (fIsCycling && index == fSize - 1) return 0; return index + 1; } // index == -1 // find the position that follows closest to the current position LinkedPosition found= null; for (Iterator it= fList.iterator(); it.hasNext(); ) { LinkedPosition p= (LinkedPosition) it.next(); if (p.offset > current.offset) if (found == null || found.offset > p.offset) found= p; } if (found != null) { return fList.indexOf(found); } else if (fIsCycling) { return 0; } else return fSize; }
private int getPreviousIndex(LinkedPosition current) { if (current != null && fList.get(fIndex) != current) return findPrevious(current); else if (fIsCycling && fIndex == 0) return fSize - 1; else return fIndex - 1; }
/** * Finds the closest position in the iteration set that precedes * <code>current</code>. If <code>current</code> * is in the iteration set, the previous in turn is chosen. * * @param current the current position * @return the index of the previous position */ private int findPrevious(LinkedPosition current) { Assert.isNotNull(current); // if the position is in the iteration set, jump to the next one int index= fList.indexOf(current); if (index != -1) { if (fIsCycling && index == 0) return fSize - 1; return index - 1; } // index == -1 // find the position that follows closest to the current position LinkedPosition found= null; for (Iterator it= fList.iterator(); it.hasNext(); ) { LinkedPosition p= (LinkedPosition) it.next(); if (p.offset < current.offset) if (found == null || found.offset < p.offset) found= p; } if (found != null) { return fList.indexOf(found); } else if (fIsCycling) { return fSize - 1; } else return -1; }
public void apply(IDocument document) { try { if (fentry.arguments > 0) { StringBuffer displayKey = new StringBuffer(fentry.key); for (int j=0; j < fentry.arguments; j++) displayKey.append("{}"); document.replace(fReplacementOffset, fReplacementLength, displayKey.toString()); if (TexlipsePlugin.getDefault().getPreferenceStore() .getBoolean(TexlipseProperties.SMART_PARENS)){ LinkedModeModel model= new LinkedModeModel(); for (int j=0; j < fentry.arguments; j++){ int newOffset = fReplacementOffset + fentry.key.length() + j*2 + 1; LinkedPositionGroup group = new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, newOffset, 0, LinkedPositionGroup.NO_STOP)); model.addGroup(group); } model.forceInstall(); LinkedModeUI ui = new EditorLinkedModeUI(model, fviewer); ui.setSimpleMode(false); ui.setExitPolicy(new ExitPolicy('}', fviewer)); ui.setExitPosition(fviewer, fReplacementOffset + displayKey.length(), 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER); ui.enter(); } } else { document.replace(fReplacementOffset, fReplacementLength, fentry.key); } } catch (BadLocationException x) { } }
@Override public TextEdit computeEdits(int offset, LinkedPosition position, char trigger, int stateMask, LinkedModeModel model) throws CoreException { ImportRewrite impRewrite= StubUtility.createImportRewrite(fCompilationUnit, true); String replaceString= impRewrite.addImport(fTypeProposal); MultiTextEdit composedEdit= new MultiTextEdit(); composedEdit.addChild(new ReplaceEdit(position.getOffset(), position.getLength(), replaceString)); composedEdit.addChild(impRewrite.rewriteImports(null)); return composedEdit; }
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; } } } }
public LinkedPosition getCurrentLinkedPosition() { Point selection = fEditor.getViewer().getSelectedRange(); int start = selection.x; int end = start + selection.y; LinkedPosition[] positions = fLinkedPositionGroup.getPositions(); for (int i = 0; i < positions.length; i++) { LinkedPosition position = positions[i]; if (position.includes(start) && position.includes(end)) return position; } return null; }
/** * Finds the next position after the given position. * * @param position * the current position * @return the next position */ protected int findNextPosition(int position) { ISourceViewer viewer = getSourceViewer(); int widget = -1; int next = position; while (next != BreakIterator.DONE && widget == -1) { next = fIterator.following(next); if (next != BreakIterator.DONE) widget = modelOffset2WidgetOffset(viewer, next); } IDocument document = viewer.getDocument(); LinkedModeModel model = LinkedModeModel.getModel(document, position); if (model != null) { LinkedPosition linkedPosition = model.findPosition(new LinkedPosition(document, position, 0)); if (linkedPosition != null) { int linkedPositionEnd = linkedPosition.getOffset() + linkedPosition.getLength(); if (position != linkedPositionEnd && linkedPositionEnd < next) next = linkedPositionEnd; } else { LinkedPosition nextLinkedPosition = model.findPosition(new LinkedPosition(document, next, 0)); if (nextLinkedPosition != null) { int nextLinkedPositionOffset = nextLinkedPosition.getOffset(); if (position != nextLinkedPositionOffset && nextLinkedPositionOffset < next) next = nextLinkedPositionOffset; } } } return next; }
/** * Finds the previous position before the given position. * * @param position * the current position * @return the previous position */ protected int findPreviousPosition(int position) { ISourceViewer viewer = getSourceViewer(); int widget = -1; int previous = position; while (previous != BreakIterator.DONE && widget == -1) { // XXX: optimize previous = fIterator.preceding(previous); if (previous != BreakIterator.DONE) widget = modelOffset2WidgetOffset(viewer, previous); } IDocument document = viewer.getDocument(); LinkedModeModel model = LinkedModeModel.getModel(document, position); if (model != null) { LinkedPosition linkedPosition = model.findPosition(new LinkedPosition(document, position, 0)); if (linkedPosition != null) { int linkedPositionOffset = linkedPosition.getOffset(); if (position != linkedPositionOffset && previous < linkedPositionOffset) previous = linkedPositionOffset; } else { LinkedPosition previousLinkedPosition = model .findPosition(new LinkedPosition(document, previous, 0)); if (previousLinkedPosition != null) { int previousLinkedPositionEnd = previousLinkedPosition.getOffset() + previousLinkedPosition.getLength(); if (position != previousLinkedPositionEnd && previous < previousLinkedPositionEnd) previous = previousLinkedPositionEnd; } } } return previous; }
public LinkedPosition getCurrentLinkedPosition() { Point selection = editor.getInternalSourceViewer().getSelectedRange(); int start = selection.x; int end = start + selection.y; LinkedPosition[] positions = linkedPositionGroup.getPositions(); for (int i = 0; i < positions.length; i++) { LinkedPosition position = positions[i]; if (position.includes(start) && position.includes(end)) return position; } return null; }
protected LinkedPositionGroup createLinkedGroupFromReplaceEdits(List<ReplaceEdit> edits, XtextEditor xtextEditor, final String originalName, SubMonitor progress) { if (edits == null) return null; final IXtextDocument document = xtextEditor.getDocument(); LinkedPositionGroup group = new LinkedPositionGroup(); Iterable<LinkedPosition> linkedPositions = filter( Iterables.transform(edits, new Function<ReplaceEdit, LinkedPosition>() { 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()); progress.worked(10); final int invocationOffset = xtextEditor.getInternalSourceViewer().getSelectedRange().x; int i = 0; for (LinkedPosition position : sortPositions(linkedPositions, invocationOffset)) { try { position.setSequenceNumber(i); i++; group.addPosition(position); } catch (BadLocationException e) { LOG.error(e.getMessage(), e); return null; } } return group; }
@Override public TextEdit computeEdits( int offset, LinkedPosition position, char trigger, int stateMask, LinkedModeModel model) throws CoreException { ImportRewrite impRewrite = StubUtility.createImportRewrite(fCompilationUnit, true); String replaceString = impRewrite.addImport(fTypeProposal); MultiTextEdit composedEdit = new MultiTextEdit(); composedEdit.addChild( new ReplaceEdit(position.getOffset(), position.getLength(), replaceString)); composedEdit.addChild(impRewrite.rewriteImports(null)); return composedEdit; }
@Override public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { apply(viewer.getDocument()); try { LinkedModeModel model = new LinkedModeModel(); StringBuffer insert = new StringBuffer(); insert.append(macro.name); insert.append("("); int parameterOffset = position.offset + insert.length(); for (int k = 0; k < macro.parameters.length; k++) { LinkedPositionGroup group = new LinkedPositionGroup(); if (k > 0) // space between parameters parameterOffset++; group.addPosition(new LinkedPosition(viewer.getDocument(), parameterOffset, macro.parameters[k].length(), LinkedPositionGroup.NO_STOP)); model.addGroup(group); parameterOffset += macro.parameters[k].length(); } model.forceInstall(); LinkedModeUI ui = new EditorLinkedModeUI(model, viewer); ui.setExitPosition(viewer, parameterOffset + 1, 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_ALWAYS); ui.enter(); fSelectedRegion = ui.getSelectedRegion(); } catch (Exception e) { e.printStackTrace(); } }
/** * Special code added to allow tabstop positions so we can easily tab past the quotes for Events/Attributes. */ @Override public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { super.apply(viewer, trigger, stateMask, offset); // See if there are any positions that should be linked. Last is always exit, first is cursor position if (_positions != null && _positions.length > 0) { IDocument document = viewer.getDocument(); boolean validPrefix = isValidPrefix(getPrefix(document, offset), getDisplayString()); int shift = (validPrefix) ? offset - this._replacementOffset : 0; try { LinkedModeModel.closeAllModels(document); // Exit out of any existing linked mode LinkedModeModel model = new LinkedModeModel(); int i = 0; for (int pos : _positions) { LinkedPositionGroup group = new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, (offset - shift) + pos, 0, i++)); model.addGroup(group); } model.forceInstall(); LinkedModeUI ui = new LinkedModeUI(model, viewer); ui.setCyclingMode(LinkedModeUI.CYCLE_ALWAYS); ui.setExitPosition(viewer, (offset - shift) + _positions[_positions.length - 1], 0, Integer.MAX_VALUE); ui.enter(); } catch (BadLocationException e) { IdeLog.logError(XMLPlugin.getDefault(), e); } } }
/** * Special code added to allow tabstop positions so we can easily tab past the quotes for Events/Attributes. */ @Override public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { super.apply(viewer, trigger, stateMask, offset); // See if there are any positions that should be linked. Last is always exit, first is cursor position if (_positions != null && _positions.length > 0) { IDocument document = viewer.getDocument(); boolean validPrefix = isValidPrefix(getPrefix(document, offset), getDisplayString()); int shift = (validPrefix) ? offset - this._replacementOffset : 0; try { LinkedModeModel.closeAllModels(document); // Exit out of any existing linked mode LinkedModeModel model = new LinkedModeModel(); int i = 0; for (int pos : _positions) { LinkedPositionGroup group = new LinkedPositionGroup(); group.addPosition(new LinkedPosition(document, (offset - shift) + pos, 0, i++)); model.addGroup(group); } model.forceInstall(); LinkedModeUI ui = new LinkedModeUI(model, viewer); ui.setCyclingMode(LinkedModeUI.CYCLE_ALWAYS); ui.setExitPosition(viewer, (offset - shift) + _positions[_positions.length - 1], 0, Integer.MAX_VALUE); ui.enter(); } catch (BadLocationException e) { IdeLog.logError(HTMLPlugin.getDefault(), e); } } }
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; } } } }
public LinkedPosition getCurrentLinkedPosition() { Point selection= fEditor.getViewer().getSelectedRange(); int start= selection.x; int end= start + selection.y; LinkedPosition[] positions= fLinkedPositionGroup.getPositions(); for (int i= 0; i < positions.length; i++) { LinkedPosition position= positions[i]; if (position.includes(start) && position.includes(end)) return position; } return null; }
/** * Finds the next position after the given position. * * @param position the current position * @return the next position */ protected int findNextPosition(int position) { ISourceViewer viewer= getSourceViewer(); int widget= -1; int next= position; while (next != BreakIterator.DONE && widget == -1) { // XXX: optimize next= fIterator.following(next); if (next != BreakIterator.DONE) widget= modelOffset2WidgetOffset(viewer, next); } IDocument document= viewer.getDocument(); LinkedModeModel model= LinkedModeModel.getModel(document, position); if (model != null && next != BreakIterator.DONE) { LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, position, 0)); if (linkedPosition != null) { int linkedPositionEnd= linkedPosition.getOffset() + linkedPosition.getLength(); if (position != linkedPositionEnd && linkedPositionEnd < next) next= linkedPositionEnd; } else { LinkedPosition nextLinkedPosition= model.findPosition(new LinkedPosition(document, next, 0)); if (nextLinkedPosition != null) { int nextLinkedPositionOffset= nextLinkedPosition.getOffset(); if (position != nextLinkedPositionOffset && nextLinkedPositionOffset < next) next= nextLinkedPositionOffset; } } } return next; }
/** * Finds the previous position before the given position. * * @param position the current position * @return the previous position */ protected int findPreviousPosition(int position) { ISourceViewer viewer= getSourceViewer(); int widget= -1; int previous= position; while (previous != BreakIterator.DONE && widget == -1) { // XXX: optimize previous= fIterator.preceding(previous); if (previous != BreakIterator.DONE) widget= modelOffset2WidgetOffset(viewer, previous); } IDocument document= viewer.getDocument(); LinkedModeModel model= LinkedModeModel.getModel(document, position); if (model != null && previous != BreakIterator.DONE) { LinkedPosition linkedPosition= model.findPosition(new LinkedPosition(document, position, 0)); if (linkedPosition != null) { int linkedPositionOffset= linkedPosition.getOffset(); if (position != linkedPositionOffset && previous < linkedPositionOffset) previous= linkedPositionOffset; } else { LinkedPosition previousLinkedPosition= model.findPosition(new LinkedPosition(document, previous, 0)); if (previousLinkedPosition != null) { int previousLinkedPositionEnd= previousLinkedPosition.getOffset() + previousLinkedPosition.getLength(); if (position != previousLinkedPositionEnd && previous < previousLinkedPositionEnd) previous= previousLinkedPositionEnd; } } } return previous; }