Java Code Examples for org.eclipse.jface.text.TextSelection#getOffset()

The following examples show how to use org.eclipse.jface.text.TextSelection#getOffset() . 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: AbstractLangStructureEditor.java    From goclipse with Eclipse Public License 1.0 6 votes vote down vote up
protected void setSelectedElementField() {
	ISelectionProvider selectionProvider = getSelectionProvider();
	if(selectionProvider == null) {
		return; // Can happen during dispose 
	}
	
	ISelection selection = selectionProvider.getSelection();
	if(selection instanceof TextSelection) {
		TextSelection textSelection = (TextSelection) selection;
		int caretOffset = textSelection.getOffset();
		
		SourceFileStructure structure;
		try {
			structure = getSourceStructure();
		} catch(CommonException e) {
			return;
		}
		if(structure != null) {
			StructureElement selectedElement = structure.getStructureElementAt(caretOffset);
			selectedElementField.setFieldValue(selectedElement);
		}
	}
}
 
Example 2
Source File: BoxedCommentHandler.java    From tlaplus with MIT License 5 votes vote down vote up
private void formatAndBoxComment()
		throws org.eclipse.jface.text.BadLocationException {
	if (formatComment()) {
		// Need to recompute things that were changed by executing
		// formatComment.
		text = doc.get();
		selection = (TextSelection) selectionProvider.getSelection();
		offset = selection.getOffset();

		boxComment();
	}
	return;
}
 
Example 3
Source File: BoxedCommentHandler.java    From tlaplus with MIT License 4 votes vote down vote up
public Object execute(ExecutionEvent event) throws ExecutionException {
	TLAEditor editor;
	editor = EditorUtil.getTLAEditorWithFocus();
	// gets the editor to which command applies
	if (editor == null) {
		return null;
	}

	doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
	text = doc.get();
	selectionProvider = editor.getSelectionProvider();
	selection = (TextSelection) selectionProvider.getSelection();
	offset = selection.getOffset();

	RightMargin = Activator.getDefault().getPreferenceStore().getInt(
			EditorPreferencePage.EDITOR_RIGHT_MARGIN);
	if (offset < 0) {
		return null;
	}
	try {
		lineInfo = doc.getLineInformationOfOffset(offset);

		String cmd = event.getCommand().getId();

		if (cmd.equals(startBoxedCommentId)) {
			startBoxedComment();
		} else if (cmd.equals(boxCommentId)) {
			boxComment();
		} else if (cmd.equals(formatAndBoxCommentId)) {
			formatAndBoxComment();
		} else if (cmd.equals(unboxCommentId)) {
			unboxComment();
		} else if (cmd.equals(formatCommentId)) {
			formatComment();
		} else {
			Activator.getDefault().logInfo("Unrecognized boxing command.");
		}

	} catch (org.eclipse.jface.text.BadLocationException e) {
		Activator.getDefault().logError("Error executing comment-boxing command", e);
		// just do nothing
	}

	// free the space. (Probably not worth doing.)
	doc = null;
	text = null;
	selectionProvider = null;
	selection = null;
	lineInfo = null;

	return null;
}
 
Example 4
Source File: CursorMovementHandler.java    From tlaplus with MIT License 4 votes vote down vote up
public Object execute(ExecutionEvent event) throws ExecutionException
{ TLAEditor editor ;
  editor = EditorUtil.getTLAEditorWithFocus() ;  // gets the editor to which command applies
  if (editor == null) {
      return null;
  }
  
  IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());  // gets document being edited.
  selectionProvider = editor.getSelectionProvider() ;
  TextSelection selection = (TextSelection) selectionProvider.getSelection();
  offset = selection.getOffset();
  if (offset < 0){
      return null;
  }
  try {
      lineInfo = doc.getLineInformationOfOffset(offset);

      String cmd = event.getCommand().getId(); 
      int repeatVal = 
            CommandPrefixDigitHandler.getAndResetRepeatValue(editor);
        while (repeatVal > 0)
        {
            repeatVal-- ;
            if (cmd.equals(charRightId))
            {
                charRight();
            } else if (cmd.equals(charLeftId))
            {
                charLeft();
            } else
            {
                System.out.println("Unrecognized command.");
                System.out.println(cmd);
            }
        }
                  
  
      
  } catch (org.eclipse.jface.text.BadLocationException e) {
    return null;
  } 
 return null;
}
 
Example 5
Source File: EditorUtils.java    From goclipse with Eclipse Public License 1.0 4 votes vote down vote up
public static SourceRange getSelectionSR(ITextEditor editor) {
	TextSelection sel = getSelection(editor);
	return new SourceRange(sel.getOffset(), sel.getLength());
}