Java Code Examples for org.eclipse.jface.text.link.LinkedModeModel#getModel()

The following examples show how to use org.eclipse.jface.text.link.LinkedModeModel#getModel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: XtextEditor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: SubWordActions.java    From Pydev with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: SelectNextOccurrenceHandler.java    From eclipse-multicursor with Eclipse Public License 1.0 4 votes vote down vote up
private void startEditing(ISourceViewer viewer) throws ExecutionException {
	final Point selOffsetAndLen = viewer.getSelectedRange();
	final IDocument document = viewer.getDocument();

	try {
		final String searchText;
		final int candidateSearchOffset;
		final int selStart = CoordinatesUtil.fromOffsetAndLengthToStartAndEnd(selOffsetAndLen).x;
		if (selOffsetAndLen.y == 0) { // no characters selected
			final String documentText = document.get();
			final Point wordOffsetAndLen = TextUtil.findWordSurrounding(documentText, selStart);
			if (wordOffsetAndLen != null) {
				searchText = document.get(wordOffsetAndLen.x, wordOffsetAndLen.y);
				candidateSearchOffset = wordOffsetAndLen.x;
			} else {
				final IRegion selectedLine = document.getLineInformationOfOffset(selStart);
				searchText = document.get(selectedLine.getOffset(), selectedLine.getLength());
				candidateSearchOffset = selectedLine.getOffset();
			}
		} else {
			searchText = document.get(selOffsetAndLen.x, selOffsetAndLen.y);
			candidateSearchOffset = selOffsetAndLen.x;
		}

		final int searchOffset;
		final List<IRegion> selections;
		final Point startingSelection;

		final SelectInProgress currentState = getCurrentState();
		if (LinkedModeModel.getModel(document, 0) != null &&
				currentState != null
				&& selOffsetAndLen.equals(currentState.startingSelection)
				&& searchText.equals(currentState.searchText)) {
			startingSelection = currentState.startingSelection;
			selections = new ArrayList<IRegion>(currentState.existingSelections);
			searchOffset = currentState.nextOffset;
		} else {
			startingSelection = selOffsetAndLen;
			selections = new ArrayList<IRegion>();
			searchOffset = candidateSearchOffset;
		}

		final IRegion matchingRegion = new FindReplaceDocumentAdapter(document).find(searchOffset,
				searchText, true, true, false, false);
		if (matchingRegion != null) {
			selections.add(matchingRegion);

			if (selections.size() == 1) {
				// select the next occurrence too; only selecting the current cursor pos isn't useful
				final IRegion secondMatchingRegion = new FindReplaceDocumentAdapter(document).find(
						matchingRegion.getOffset() + matchingRegion.getLength(), searchText, true, true, false, false);
				if (secondMatchingRegion != null) {
					selections.add(secondMatchingRegion);
				}
			}

			if (selections.size() > 1) {
				final IRegion lastSelection = selections.get(selections.size() - 1);
				saveCurrentState(new SelectInProgress(startingSelection, searchText, selections,
						lastSelection.getOffset() + lastSelection.getLength()));

				startLinkedEdit(selections, viewer, selOffsetAndLen);
			}
		}
	} catch (BadLocationException e) {
		throw new ExecutionException("Editing failed", e);
	}
}