package org.commonquail.linefeed;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.Document;
import org.netbeans.api.annotations.common.NonNull;
import org.netbeans.api.editor.mimelookup.MimeRegistration;
import org.netbeans.spi.editor.document.OnSaveTask;

public final class EofLineFeed implements OnSaveTask {

    private static final int AFTER_TRAILING_WHITESPACE_REMOVAL = 1100;

    public static final String LF = "\n";

    private static final Logger logger = Logger.getLogger(
            EofLineFeed.class.getName()
    );

    private final Document document;

    public EofLineFeed() {
        this(null);
    }

    public EofLineFeed(Document document) {
        this.document = document;
    }

    @Override
    public void performTask() {
        logger.log(Level.INFO, "EofLineFeed.performTask()");
        logger.log(Level.INFO, "Document: {0}", document);
    }

    @Override
    public void runLocked(@NonNull Runnable run) {
        logger.log(Level.INFO, "EofLineFeed.runLocked()");
        run.run();
    }

    @Override
    public boolean cancel() {
        logger.log(Level.INFO, "EofLineFeed.cancel()");
        return false;
    }

    @MimeRegistration(mimeType = "", service = OnSaveTask.Factory.class,
            position = AFTER_TRAILING_WHITESPACE_REMOVAL)
    public static final class FactoryImpl implements Factory {
        @Override
        public OnSaveTask createTask(@NonNull Context context) {
            return new EofLineFeed(context.getDocument());
        }
    }

}

@@ -93,16 +93,62 @@
 Logger;%0A
+import javax.swing.text.BadLocationException;%0A
 import j
@@ -173,16 +173,16 @@
 cument;%0A
-
 import o
@@ -336,16 +336,52 @@
 aveTask;
+%0Aimport org.openide.util.Exceptions;
 %0A%0Apublic
@@ -887,119 +887,501 @@
     
-logger.log(Level.INFO, %22EofLineFeed.performTask()%22);%0A        logger.log(Level.INFO, %22Document: %7B0%7D%22, document);
+if (document == null) %7B%0A            return;%0A        %7D%0A%0A        try %7B%0A            int length = document.getLength();%0A%0A            if (length == 0) %7B%0A                document.insertString(0, LF, null);%0A            %7D else if (!document.getText(length - 1, 1).equals(LF)) %7B%0A                document.insertString(length, LF, null);%0A            %7D%0A        %7D catch (BadLocationException ex) %7B%0A            Exceptions.printStackTrace(ex);%0A            logger.log(Level.SEVERE, ex.getMessage());%0A        %7D
 %0A   
@@ -1453,67 +1453,8 @@
 ) %7B%0A
-        logger.log(Level.INFO, %22EofLineFeed.runLocked()%22);%0A
     
@@ -1523,64 +1523,8 @@
 ) %7B%0A
-        logger.log(Level.INFO, %22EofLineFeed.cancel()%22);%0A
     
@@ -1894,17 +1894,16 @@
       %7D%0A
+
     %7D%0A
-%0A
 %7D%0A

Implement functionality