<commit_before>package ccw.editors.antlrbased;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.IDocument;

public class FormatAction extends Action {
	private AntlrBasedClojureEditor editor;

	public FormatAction(AntlrBasedClojureEditor editor) {
		super(ClojureEditorMessages.FormatAction_label);
		Assert.isNotNull(editor);
		this.editor = editor;
		setEnabled(true);
	}

	@Override
	public void run() {
		IDocument document = editor.getDocument();
		String original = document.get();
		String formatted = new ClojureFormat().formatCode(original);
		if (!formatted.equals(original)) {
			document.set(formatted);
		}
	}
}<commit_msg>per line replacement, so the cursor doesn't jump to the top of the file when formatting<commit_after>package ccw.editors.antlrbased;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;

public class FormatAction extends Action {
    private AntlrBasedClojureEditor editor;

    public FormatAction(AntlrBasedClojureEditor editor) {
        super(ClojureEditorMessages.FormatAction_label);
        Assert.isNotNull(editor);
        this.editor = editor;
        setEnabled(true);
    }

    @Override
    public void run() {
        IDocument original = editor.getDocument();
        String originalContents = original.get();
        String formatted = new ClojureFormat().formatCode(originalContents);
        if (!formatted.equals(originalContents)) {
            replaceWithIndented(original, new Document(formatted));
        }
    }

    private void replaceWithIndented(IDocument original, Document formatted) {
        MultiTextEdit combinedEdit = new MultiTextEdit();
        try {
            for (int i = 0; i < original.getNumberOfLines(); i++) {
                String originalLine = getLine(original, i);
                String formattedLine = getLine(formatted, i);
                if (!originalLine.equals(formattedLine)) {
                    combinedEdit.addChild(replacementLine(original, i, formattedLine));
                }
            }
            combinedEdit.apply(original);
        } catch (MalformedTreeException e) {
            throw new RuntimeException(e);
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }
    }

    private ReplaceEdit replacementLine(IDocument original, int i, String formattedLine) throws BadLocationException {
        return new ReplaceEdit(original.getLineOffset(i), original.getLineLength(i), formattedLine);
    }

    private String getLine(IDocument document, int lineNumber) throws BadLocationException {
        return document.get(document.getLineOffset(lineNumber), document.getLineLength(lineNumber));
    }
}
{'fraction_non_alphanumeric': 0.09334657398212512, 'fraction_numerical': 0.00033101621979476995, 'mean_word_length': 3.5443609022556393, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3430374', 'n_tokens_mistral': 849, 'n_tokens_neox': 808, 'n_words': 179}