2

I want to develop an eclipse plugin and need to do some operation based on cursor location in a texteditor. It seems easy to get cursor's line location, see: How to get cursor position in an eclipse TextEditor

But how to get the column location?

Community
  • 1
  • 1
Patrick
  • 1,283
  • 2
  • 13
  • 20

1 Answers1

1

The following code seems to work:

package plugin_test.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;


/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class SampleHandler extends AbstractHandler {
    /**
     * The constructor.
     */
    public SampleHandler() {
    }

    /**
     * the command has been executed, so extract extract the needed information
     * from the application context.
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbench wb = PlatformUI.getWorkbench();
        IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
        IWorkbenchPage page = win.getActivePage();
        IEditorPart editor = page.getActiveEditor();
        if(editor instanceof ITextEditor){
            ISelectionProvider selectionProvider = ((ITextEditor)editor).getSelectionProvider();
            ISelection selection = selectionProvider.getSelection();
            if (selection instanceof ITextSelection) {
                ITextSelection textSelection = (ITextSelection)selection;
                IDocumentProvider provider = ((ITextEditor)editor).getDocumentProvider();
                IDocument document = provider.getDocument(editor.getEditorInput());
                int line = textSelection.getStartLine();
                int column =0;
                try {
                    column = textSelection.getOffset() - document.getLineOffset(line);
                } catch (BadLocationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                MessageDialog.openInformation(
                        win.getShell(),
                        "Plugin_test",
                        "line:"+(line+1) + 
                        " column:"+ (column+1) );
            }
        }



        return null;
    }
}
Patrick
  • 1,283
  • 2
  • 13
  • 20
  • I haven't done that for a while, but I remember it was awkward to get the X,Y-position of the cursor, so you are probably right with that solution. – Bananeweizen Apr 21 '13 at 19:32