package org.commonquail.linefeed;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.BadLocationException;
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;
import org.openide.util.Exceptions;

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() {
        if (document == null) {
            return;
        }

        try {
            int length = document.getLength();

            if (length == 0) {
                document.insertString(0, LF, null);
            } else if (!document.getText(length - 1, 1).equals(LF)) {
                document.insertString(length, LF, null);
            }
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
            logger.log(Level.SEVERE, ex.getMessage());
        }
    }

    @Override
    public void runLocked(@NonNull Runnable run) {
        run.run();
    }

    @Override
    public boolean 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());
        }
    }
}

@@ -698,57 +698,328 @@
     
-public EofLineFeed() %7B%0A        this(null);%0A    %7D%0A
+/**%0A     * Creates a no-op EOF line-feed guarantee.%0A     */%0A    public EofLineFeed() %7B%0A        this(null);%0A    %7D%0A%0A    /**%0A     * Creates a new EOF line-feed guarantee for the given document.%0A     *%0A     * If document is null the class' task becomes a no-op.%0A     *%0A     * @param document The document to process.%0A     */
 %0A   

add comments