views:

280

answers:

1

What is needed?

We are writing an extension to eclipse's JavaEditor. We need a way to add a line before and after the line the cursor is in.

The cursor on the new line should be on the correct position (correctly indeted).

Sample (# is the cursor):

before (I):

public class Test {
    public static void main#(String[] args) {
        System.out.println("Test!");
    }
}

after wanted (II):

public class Test {
    #
    public static void main(String[] args) {
        System.out.println("Test!");
    }

after not wanted (a.k.a. the present state) (III):

public class Test {
#
    public static void main(String[] args) {
        System.out.println("Test!");
    }

Present state:

The transformation from I to III can be done via IDocument.replace(), an InsertEdit or via IDocumentExtension4's rewriteSessions.

The problem is how to call JavaEditor's indent function after inserting the new line from the extension. Or is it even possible to indent the line directly correct (I to II)? (The length of the indent should not always be the one of the current line, but the correct one. internal packages should not be used if possible, otherwise IndentUtil would be the solution.)

A: 

One solution seems to be not to use IDocument.replace(), e.g.

myTextViewer.getDocument().replace(...)

but insert(), e.g.

textViewer.getTextWidget().insert(...)

That works but is not yet a complete solution to call the indent function without generating dependencies to org.eclipse.jdt :-(. That is still needed.

Johannes Weiß