Java Code Examples for org.eclipse.jface.text.ITextSelection#isEmpty()

The following examples show how to use org.eclipse.jface.text.ITextSelection#isEmpty() . 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: IndentAction.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Returns if the current selection is valid, i.e. whether it is empty and
 * the caret in the whitespace at the start of a line, or covers multiple
 * lines.
 * 
 * @return <code>true</code> if the selection is valid for an indent
 *         operation
 */
private boolean isValidSelection() {
	ITextSelection selection = getSelection();
	if (selection.isEmpty())
		return false;

	int offset = selection.getOffset();
	int length = selection.getLength();

	IDocument document = getDocument();
	if (document == null)
		return false;

	try {
		IRegion firstLine = document.getLineInformationOfOffset(offset);
		int lineOffset = firstLine.getOffset();

		// either the selection has to be empty and the caret in the WS at
		// the line start
		// or the selection has to extend over multiple lines
		if (length == 0)
			return document.get(lineOffset, offset - lineOffset).trim().length() == 0;
		else
			// return lineOffset + firstLine.getLength() < offset + length;
			return false; // only enable for empty selections for now

	} catch (BadLocationException e) {
	}

	return false;
}
 
Example 2
Source File: IndentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
	 * Returns if the current selection is valid, i.e. whether it is empty and the caret in the
	 * whitespace at the start of a line, or covers multiple lines.
	 *
	 * @return <code>true</code> if the selection is valid for an indent operation
	 */
	private boolean isValidSelection() {
		ITextSelection selection= getSelection();
		if (selection.isEmpty())
			return false;

		int offset= selection.getOffset();
		int length= selection.getLength();

		IDocument document= getDocument();
		if (document == null)
			return false;

		try {
			IRegion firstLine= document.getLineInformationOfOffset(offset);
			int lineOffset= firstLine.getOffset();

			// either the selection has to be empty and the caret in the WS at the line start
			// or the selection has to extend over multiple lines
			if (length == 0)
				return document.get(lineOffset, offset - lineOffset).trim().length() == 0;
			else
//				return lineOffset + firstLine.getLength() < offset + length;
				return false; // only enable for empty selections for now

		} catch (BadLocationException e) {
		}

		return false;
	}
 
Example 3
Source File: TextViewerMoveLinesAction.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void runWithEvent(Event event) {
	ITextViewer viewer = getTextViewer();
	if (viewer == null)
		return;

	if (!canModifyViewer())
		return;

	// get involved objects

	IDocument document= viewer.getDocument();
	if (document == null)
		return;

	StyledText widget= viewer.getTextWidget();
	if (widget == null)
		return;

	// get selection
	ITextSelection sel= (ITextSelection) viewer.getSelectionProvider().getSelection();
	if (sel.isEmpty())
		return;

	ITextSelection skippedLine= getSkippedLine(document, sel);
	if (skippedLine == null)
		return;

	try {

		ITextSelection movingArea= getMovingSelection(document, sel, viewer);

		// if either the skipped line or the moving lines are outside the widget's
		// visible area, bail out
		if (!containedByVisibleRegion(movingArea, viewer) || !containedByVisibleRegion(skippedLine, viewer))
			return;

		// get the content to be moved around: the moving (selected) area and the skipped line
		String moving= movingArea.getText();
		String skipped= skippedLine.getText();
		if (moving == null || skipped == null || document.getLength() == 0)
			return;

		String delim;
		String insertion;
		int offset, deviation;
		if (fUpwards) {
			delim= document.getLineDelimiter(skippedLine.getEndLine());
			if (fCopy) {
				delim= TextUtilities.getDefaultLineDelimiter(document);
				insertion= moving + delim;
				offset= movingArea.getOffset();
				deviation= 0;
			} else {
				Assert.isNotNull(delim);
				insertion= moving + delim + skipped;
				offset= skippedLine.getOffset();
				deviation= -skippedLine.getLength() - delim.length();
			}
		} else {
			delim= document.getLineDelimiter(movingArea.getEndLine());
			if (fCopy) {
				if (delim == null) {
					delim= TextUtilities.getDefaultLineDelimiter(document);
					insertion= delim + moving;
				} else {
					insertion= moving + delim;
				}
				offset= skippedLine.getOffset();
				deviation= movingArea.getLength() + delim.length();
			} else {
				Assert.isNotNull(delim);
				insertion= skipped + delim + moving;
				offset= movingArea.getOffset();
				deviation= skipped.length() + delim.length();
			}
		}

		// modify the document
		beginCompoundEdit();
		if (fCopy) {
			document.replace(offset, 0, insertion);
		} else {
			document.replace(offset, insertion.length(), insertion);
		}

		// move the selection along
		int selOffset= movingArea.getOffset() + deviation;
		int selLength= movingArea.getLength() + (fAddDelimiter ? delim.length() : 0);
		if (! (viewer instanceof ITextViewerExtension5))
			selLength= Math.min(selLength, viewer.getVisibleRegion().getOffset() + viewer.getVisibleRegion().getLength() - selOffset);
		else {
			// TODO need to check what is necessary in the projection case
		}
		selectAndReveal(viewer, selOffset, selLength);
	} catch (BadLocationException x) {
		// won't happen without concurrent modification - bail out
		return;
	}
}
 
Example 4
Source File: OpenAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean checkEnabled(ITextSelection selection) {
	if (selection == null || selection.isEmpty())
		return false;

	return PropertyKeyHyperlinkDetector.checkEnabled(fEditor, selection.getOffset());
}
 
Example 5
Source File: AddBlockCommentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean isValidSelection(ITextSelection selection) {
	return selection != null && !selection.isEmpty() && selection.getLength() > 0;
}
 
Example 6
Source File: RemoveBlockCommentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected boolean isValidSelection(ITextSelection selection) {
	return selection != null && !selection.isEmpty();
}