Java Code Examples for org.eclipse.ui.texteditor.ITextEditor#getSelectionProvider()

The following examples show how to use org.eclipse.ui.texteditor.ITextEditor#getSelectionProvider() . 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: EditorUtils.java    From Pydev with Eclipse Public License 1.0 6 votes vote down vote up
public static TextSelectionUtils createTextSelectionUtils(ITextEditor editor) {
    IDocument document = getDocument(editor);
    ISelectionProvider selectionProvider = editor.getSelectionProvider();
    if (selectionProvider != null) {
        ISelection selection = selectionProvider.getSelection();
        if (selection instanceof ITextSelection) {
            ITextSelection textSelection = (ITextSelection) selection;
            CoreTextSelection coreTextSelection = new CoreTextSelection(document, textSelection.getOffset(),
                    textSelection.getLength());
            return new TextSelectionUtils(document, coreTextSelection);
        }
    }

    Log.log("Unable to get selection provider from editor: " + editor);
    return new TextSelectionUtils(document, 0);
}
 
Example 2
Source File: IndentAction.java    From typescript.java with MIT License 5 votes vote down vote up
/**
 * Returns the editor's selection provider.
 * 
 * @return the editor's selection provider or <code>null</code>
 */
private ISelectionProvider getSelectionProvider() {
	ITextEditor editor = getTextEditor();
	if (editor != null) {
		return editor.getSelectionProvider();
	}
	return null;
}
 
Example 3
Source File: BlockCommentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the editor's selection, or <code>null</code> if no selection can be obtained or the
 * editor is <code>null</code>.
 *
 * @return the selection of the action's editor, or <code>null</code>
 */
protected ITextSelection getCurrentSelection() {
	ITextEditor editor= getTextEditor();
	if (editor != null) {
		ISelectionProvider provider= editor.getSelectionProvider();
		if (provider != null) {
			ISelection selection= provider.getSelection();
			if (selection instanceof ITextSelection)
				return (ITextSelection) selection;
		}
	}
	return null;
}
 
Example 4
Source File: IndentAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the editor's selection provider.
 *
 * @return the editor's selection provider or <code>null</code>
 */
private ISelectionProvider getSelectionProvider() {
	ITextEditor editor= getTextEditor();
	if (editor != null) {
		return editor.getSelectionProvider();
	}
	return null;
}
 
Example 5
Source File: MarkUtils.java    From e4macs with Eclipse Public License 1.0 5 votes vote down vote up
private static void clearMarkRegion(ITextEditor editor) {
	int markOffset = getMark(editor);
	if (markOffset != -1) {
		// clear shift flag as well
		EmacsMovementHandler.clearShifted();
		ISelectionProvider isp = editor.getSelectionProvider();
		ITextSelection sel = (ITextSelection) isp.getSelection();

		if (sel.getOffset() <= markOffset && markOffset <= sel.getOffset() + sel.getLength()) {
			int offset = getCursorOffset(editor);
			// clear the selection
			setCursorOffset(editor, offset);
		}
	}
}
 
Example 6
Source File: EditorAPI.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the line base selection values for the given editor part.
 *
 * <p>The given editor part must not be <code>null</code>.
 *
 * @param editorPart the editorPart for which to get the text selection
 * @return the line base selection values for the given editor part or {@link
 *     TextSelection#EMPTY_SELECTION} if the given editor part is is <code>null</code> or not a
 *     text editor, the editor does not have a valid selection provider or document provider, a
 *     valid IDocument could not be obtained form the document provider, or the correct line
 *     numbers or in-lin offsets could not be calculated
 */
public static TextSelection getSelection(IEditorPart editorPart) {
  if (!(editorPart instanceof ITextEditor)) {
    return TextSelection.EMPTY_SELECTION;
  }

  ITextEditor textEditor = (ITextEditor) editorPart;
  ISelectionProvider selectionProvider = textEditor.getSelectionProvider();

  if (selectionProvider == null) {
    return TextSelection.EMPTY_SELECTION;
  }

  IDocumentProvider documentProvider = textEditor.getDocumentProvider();

  if (documentProvider == null) {
    return TextSelection.EMPTY_SELECTION;
  }

  IDocument document = documentProvider.getDocument(editorPart.getEditorInput());

  if (document == null) {
    return TextSelection.EMPTY_SELECTION;
  }

  ITextSelection textSelection = (ITextSelection) selectionProvider.getSelection();
  int offset = textSelection.getOffset();
  int length = textSelection.getLength();

  return calculateSelection(document, offset, length);
}
 
Example 7
Source File: EmacsPlusCmdHandler.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
 */
@SuppressWarnings("unchecked")
public Object execute(ExecutionEvent event) throws ExecutionException {
	ITextEditor editor = getTextEditor(event);
	if (editor == null) { 
		if (isWindowCommand()) {
			Object result = checkExecute(event); 
			if (result == Check.Fail) {
				beep();
				result = null;
			}
			return result; 
		} else if (isConsoleCommand()) {
			// intercept and dispatch execution if console supported and used in a console view
			IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
			if (activePart != null && (activePart instanceof IConsoleView) && (activePart instanceof PageBookView)) {
				IPage textPage = ((PageBookView)activePart).getCurrentPage();
				if (textPage instanceof TextConsolePage) {
					return ((IConsoleDispatch)this).consoleDispatch(((TextConsolePage)textPage).getViewer(),(IConsoleView)activePart,event);
				}				
			}
		}
	}
	try {
		setThisEditor(editor);
		isEditable = getEditable();
		if (editor == null || isBlocked()) {
			beep();
			asyncShowMessage(editor, INEDITABLE_BUFFER, true);
			return null;
		}
		
		// Retrieve the universal-argument parameter value if passed 
		if (extractUniversalCount(event) != 1) {
			// check if we should dispatch a related command based on the universal argument
			String dispatchId = checkDispatchId(event.getCommand().getId());
			if (dispatchId != null) {
				// recurse on new id (inverse or arg value driven)
				return dispatchId(editor, dispatchId, getParams(event.getCommand(), event.getParameters()));
			}
		}
		
		setThisDocument(editor.getDocumentProvider().getDocument(editor.getEditorInput()));

		// Get the current selection
		ISelectionProvider selectionProvider = editor.getSelectionProvider();
		ITextSelection selection = (ITextSelection) selectionProvider.getSelection();
		preTransform(editor, selection);
		return transformWithCount(editor, getThisDocument(), selection, event);
		
	} finally {
		// normal commands clean up here
		if (isTransform()) {
			postExecute();
		}
	}
}
 
Example 8
Source File: RectangleSupport.java    From e4macs with Eclipse Public License 1.0 4 votes vote down vote up
ITextSelection getCurrentSelection(ITextEditor editor){
	ISelectionProvider selectionProvider = editor.getSelectionProvider();
	return (ITextSelection) selectionProvider.getSelection();
}
 
Example 9
Source File: EmacsPlusCmdHandler.java    From e4macs with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Return the editor's current selection
 * Never returns null
 * 
 * @param editor
 * @return the current selection or (0,0)
 */
protected ITextSelection getCurrentSelection(ITextEditor editor){
	ISelectionProvider sp = editor.getSelectionProvider();
	// never return null
	return (sp != null ? (ITextSelection) sp.getSelection() : new TextSelection(0,0));
}