Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Add code and text fragments at cursor(How can I insert arbitrary code text at the current cursor position when having a JDT tab open)
Add code and text fragments at cursor [message #658347] Tue, 08 March 2011 03:07 Go to next message
Thomas Spall is currently offline Thomas SpallFriend
Messages: 29
Registered: July 2009
Junior Member
Hi,

I am currently working on a plugin which, amongst other functions, tries to do the following thing:

1. It defines a key binding Ctrl+Alt+I in the plugin.xml which links to a command, which is serviced by a handler. The key binding's context is set to 'org.eclipse.jdt.ui.javaEditorScope' so it is only active when the user has a JDT tab open to edit java code.

2. The handler opens a modal dialog and presents the user with a series of parameter choices, relating to an external repository of some sort. What comes back from that dialog, is basically some java code which the external repository came up with. However, that java code won't be completely syntactically correct, as there might be some template parameters remaining, but this is intentional.

3. The handler inserts the obtained java code/text as it is (i.e. with the remaining non-Java conform parameter markup) at the current cursor position in the source file which is currently opened in the JDT tab.

Now, 1. and 2. are working fine, but I have problems with 3.. How do I obtain the current cursor position in the source file from the hander's 'execute(...)' method? And how do I afterwards insert the code fragment there?

Thanks for any help and ideas,
Thomas

[Updated on: Tue, 08 March 2011 03:08]

Report message to a moderator

Re: Add code and text fragments at cursor [message #658576 is a reply to message #658347] Wed, 09 March 2011 04:02 Go to previous message
Thomas Spall is currently offline Thomas SpallFriend
Messages: 29
Registered: July 2009
Junior Member
I think I found a viable solution for 3.:
In my command handler, I do the following:

    public Object execute(ExecutionEvent event) throws ExecutionException {
        //get some context information:
        //1. what file are we displaying
        //2. what position is the cursor at
        ITextEditor editor = (ITextEditor) HandlerUtil.getActiveEditorChecked(event);
        IEditorInput input = editor.getEditorInput();
        IFile file = ((FileEditorInput) input).getFile();
        ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
        TextSelection selection = (TextSelection) editor.getSelectionProvider().getSelection();
        int cursorOffset = selection.getOffset();

        //display the dialog
        MyDialog dialog = new MyDialog(getShell(event));
        int res = dialog.open();
        if (res==0) {
            String theText = dialog.getTheTextToInsert();
            try {
                //add the code section as text at the cursor
                TextEdit edit = new InsertEdit(cursorOffset, theText);   
                cu.applyTextEdit(edit, null);                    

            } catch (JavaModelException e) {                    
                throw new ExecutionException(e.getMessage());
            }
        }
    }

    protected Shell getShell(ExecutionEvent event) {
        return HandlerUtil.getActiveShell(event);
    }
Previous Topic:"create project from existing source" option
Next Topic:New server dialog does not accept localhost with dot
Goto Forum:
  


Current Time: Fri Apr 26 22:42:11 GMT 2024

Powered by FUDForum. Page generated in 0.05277 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top