<commit_before>package org.jboss.tools.vscode.java.handlers;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.jboss.tools.vscode.java.JavaLanguageServerPlugin;
import org.jboss.tools.vscode.java.model.Position;
import org.jboss.tools.vscode.java.model.Range;

import com.thetransactioncompany.jsonrpc2.JSONRPC2Notification;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;

import copied.org.eclipse.jdt.internal.corext.refactoring.util.TextEditUtil;

/**
 * @author IBM Corporation (<NAME>)
 */
public class FormatterHandler extends AbstractRequestHandler {
	
	private static final String REQ_FORMATTING = "textDocument/formatting";

	@Override
	public boolean canHandle(String request) {
		return REQ_FORMATTING.equals(request);
	}

	@Override
	public JSONRPC2Response process(JSONRPC2Request request) {
		List<org.jboss.tools.vscode.java.model.TextEdit> edits = format(resolveCompilationUnit(request));
		JSONRPC2Response response = new JSONRPC2Response(request.getID());
		response.setResult(edits);
		return response;
	}

	@Override
	public void process(JSONRPC2Notification request) {
		// not needed
	}

	public List<org.jboss.tools.vscode.java.model.TextEdit> format(ICompilationUnit cu) {
		CodeFormatter formatter = ToolFactory.createCodeFormatter(cu.getJavaProject().getOptions(true));
		try {
			IDocument document = JsonRpcHelpers.toDocument(cu.getBuffer());
			String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
			TextEdit format = formatter.format(CodeFormatter.K_COMPILATION_UNIT, document.get(), 0, document.getLength(), 0, lineDelimiter);
			MultiTextEdit flatEdit = TextEditUtil.flatten(format);
			return convertEdits(flatEdit.getChildren(), document);
		} catch (JavaModelException e) {
			JavaLanguageServerPlugin.logException(e.getMessage(), e);
			return null;
		}
	}
<commit_msg>fixed issue #18 Document formatting<commit_after>package org.jboss.tools.vscode.java.handlers;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.jboss.tools.vscode.java.JavaLanguageServerPlugin;
import org.jboss.tools.vscode.java.model.Position;
import org.jboss.tools.vscode.java.model.Range;

import com.thetransactioncompany.jsonrpc2.JSONRPC2Notification;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;

import copied.org.eclipse.jdt.internal.corext.refactoring.util.TextEditUtil;

/**
 * @author IBM Corporation (<NAME>)
 */
public class FormatterHandler extends AbstractRequestHandler {
	
	private static final String REQ_FORMATTING = "textDocument/formatting";

	@Override
	public boolean canHandle(String request) {
		return REQ_FORMATTING.equals(request);
	}

	@Override
	public JSONRPC2Response process(JSONRPC2Request request) {
		List<org.jboss.tools.vscode.java.model.TextEdit> edits = format(request);
		JSONRPC2Response response = new JSONRPC2Response(request.getID());
		response.setResult(edits);
		return response;
	}

	@Override
	public void process(JSONRPC2Notification request) {
		// not needed
	}

	private List<org.jboss.tools.vscode.java.model.TextEdit> format(JSONRPC2Request request) {
		ICompilationUnit cu = resolveCompilationUnit(request);
		Map<String, String> eclipseOptions = getOptions(cu, request);
		CodeFormatter formatter = ToolFactory.createCodeFormatter(eclipseOptions);
		try {
			IDocument document = JsonRpcHelpers.toDocument(cu.getBuffer());
			String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
			TextEdit format = formatter.format(CodeFormatter.K_COMPILATION_UNIT, document.get(), 0, document.getLength(), 0, lineDelimiter);
			MultiTextEdit flatEdit = TextEditUtil.flatten(format);
			return convertEdits(flatEdit.getChildren(), document);
		} catch (JavaModelException e) {
			JavaLanguageServerPlugin.logException(e.getMessage(), e);
			return null;
		}
	}

	private Map<String, String> getOptions(ICompilationUnit cu, JSONRPC2Request request) {
		Map<String, String> eclipseOptions = cu.getJavaProject().getOptions(true);
		@SuppressWarnings("unchecked")
		Map<String, Object> namedParams = (Map<String, Object>) request.getNamedParams().get("options");
		Long tabSize = (Long) namedParams.get("tabSize");
		if (tabSize != null && tabSize > 0) {
			eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, tabSize.toString());
		}
		Boolean insertSpaces = (Boolean) namedParams.get("insertSpaces");
		if (insertSpaces != null) {
			eclipseOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, insertSpaces ? JavaCore.SPACE : JavaCore.TAB);
		}
		return eclipseOptions;
	}

{'fraction_non_alphanumeric': 0.10827693909422176, 'fraction_numerical': 0.005379142807565504, 'mean_word_length': 6.205, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, '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': '7665577', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1776, 'n_words': 302}