QuBiT / cucumber-netbeans-plugin

Plugin / Module which allow Syntax Highlighting and many more in NetBeans with .feature Files
MIT License
50 stars 19 forks source link

Formatting source moves caret to end of file #24

Open jorahood opened 14 years ago

jorahood commented 14 years ago

This is my only current niggle with this awesome plugin: I like to let Netbeans format the code by hitting ^F, but in a .feature file, this moves the caret from whatever line I'm on down to the bottom of the file, unlike in any other file type. Could this behavior be stopped?

Thanks, Andy

QuBiT commented 14 years ago

Hi,

yes this is currently true, as the PrettyFormatter of Gherkin just takes the whole text as input argument and then returns the formatted source (after lexing and parsing inside of the gerkin-classes).

As this formatted source is just written back into the file, the position of your cursor is lost (overwriting the complete text, hence yes you are at the bottom)

I will look into that issue (try to store your cursor position from the editor if this is possible and restore it).

QuBiT commented 14 years ago

Hi again,

I've looked into that issue and currently I do not know how to get access to the caret position to store it and restore it after the formatting. ( I do not develop more than this module for NetBeans, hence I am no expert for this IDE and API )

If someone wants to help here are some infos:


    private void writeSource(String reformattedSource) throws BadLocationException {
        // here you should store the location of the caret
        // ...
        context.document().remove(0, context.document().getLength());
        context.document().insertString(0, reformattedSource, null);
        // and here you could then restore it
        // ...
    }

from

http://github.com/QuBiT/cucumber-netbeans-plugin/blob/master/src/qubit/cucumber/editor/format/GherkinFormatTask.java

Hopefully someone has the knowledge to contribute to get this feature implemented.

Cheers, Roland

aslakhellesoy commented 14 years ago

Maybe something like this:

javax.swing.text.JTextComponent = ... // Get it from the context or document somehow...
t.getCaret().setDot(n);
QuBiT commented 14 years ago

yes that was what I tried, when I looked for a solution, but there is no such access.

aslakhellesoy commented 14 years ago

Fix here: http://github.com/aslakhellesoy/cucumber-netbeans-plugin/commit/bec642fbaf1f1f2fbc7d2f006848039b26a4b60e

QuBiT commented 14 years ago

hi,

first I thought: "doooh" ... but then I remembered why I did not used that solution

adding your fix produces the following:


java.lang.NullPointerException
        at org.netbeans.modules.editor.indent.TaskHandler.caretOffset(TaskHandler.java:123)
        at org.netbeans.modules.editor.indent.spi.Context.caretOffset(Context.java:227)
        at qubit.cucumber.editor.format.GherkinFormatTask.reformat(GherkinFormatTask.java:40)
        at org.netbeans.modules.editor.indent.TaskHandler$MimeItem.runTask(TaskHandler.java:547)
        at org.netbeans.modules.editor.indent.TaskHandler.runTasks(TaskHandler.java:314)
        at org.netbeans.modules.editor.indent.IndentImpl.reformat(IndentImpl.java:293)
        at org.netbeans.modules.editor.indent.FormatterImpl.reformat(FormatterImpl.java:187)
        at org.netbeans.editor.ActionFactory$FormatAction$1$1.run(ActionFactory.java:1675)
        at org.netbeans.editor.GuardedDocument.runAtomicAsUser(GuardedDocument.java:357)
        at org.netbeans.editor.ActionFactory$FormatAction$1.run(ActionFactory.java:1643)
        at org.netbeans.modules.progress.ui.RunOffEDTImpl$1.run(RunOffEDTImpl.java:119)
        at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:602)
        at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:1084)

So this is not fixed yet.

Even if this would work, this would not be 100% correct as the position would move, when the position of your text where your caret was has moved through reformatting.