/*******************************************************************************
 * Copyright (c) 2016 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.jboss.tools.vscode.java.internal.handlers;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;

final public class JsonRpcHelpers {
	
	/**
	 * Convert line, column to a document offset.  
	 * @param buffer
	 * @param line
	 * @param column
	 * @return
	 */
	public static int toOffset(IBuffer buffer, int line, int column){
		try {
			return toDocument(buffer).getLineOffset(line) + column;
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	/**
	 * Convert offset to line number and column.
	 * @param buffer
	 * @param line
	 * @param column
	 * @return
	 */
	public static int[] toLine(IBuffer buffer, int offset){
		IDocument document = toDocument(buffer);
		try {
			int line = document.getLineOfOffset(offset);
			int column = offset - document.getLineOffset(line); 
			return new int[] {line, column};
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static Map<String, Object> convertRange(int startLine, int startCol, int endLine, int endCol) {
		Map<String, Object> range = new HashMap<String, Object>();
		Map<String, Object> start = new HashMap<String, Object>();
		Map<String, Object> end = new HashMap<String, Object>();
		if(startLine >-1)
			start.put("line",startLine);
		if(startCol > -1)
			start.put("character", startCol);
		if(endCol >-1)
			end.put("character",endCol);
		if(endLine > -1)
			end.put("line",endLine);
		range.put("start",start);
		range.put("end",end);
		return range;
	}

	/**
	 * Returns an {@link IDocument} for the given buffer.
	 * The implementation tries to avoid copying the buffer unless required.
	 * The returned document may or may not be connected to the buffer.
	 * 
	 * @param buffer a buffer
	 * @return a document with the same contents as the buffer
	 */
	public static IDocument toDocument(IBuffer buffer) {
		if (buffer instanceof IDocument) {
			return (IDocument) buffer;
		} else if (buffer instanceof org.jboss.tools.vscode.java.internal.DocumentAdapter) {
			IDocument document = ((org.jboss.tools.vscode.java.internal.DocumentAdapter) buffer).getDocument();
			if (document != null) {
				return document;
			}
		}
		return new org.eclipse.jdt.internal.core.DocumentAdapter(buffer);
	}
}

@@ -582,57 +582,8 @@
 s;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 impo
@@ -615,16 +615,16 @@
 Buffer;%0A
+
 import o
@@ -745,18 +745,17 @@
 lpers %7B%0A
-%09
 %0A
+
 %09/**%0A%09 *
@@ -797,18 +797,16 @@
  offset.
-  
 %0A%09 * @pa
@@ -1451,17 +1451,16 @@
 t(line);
- 
 %0A%09%09%09retu
@@ -1612,571 +1612,8 @@
 ;%0A%09%7D
-%0A%09%0A%09public static Map%3CString, Object%3E convertRange(int startLine, int startCol, int endLine, int endCol) %7B%0A%09%09Map%3CString, Object%3E range = new HashMap%3CString, Object%3E();%0A%09%09Map%3CString, Object%3E start = new HashMap%3CString, Object%3E();%0A%09%09Map%3CString, Object%3E end = new HashMap%3CString, Object%3E();%0A%09%09if(startLine %3E-1)%0A%09%09%09start.put(%22line%22,startLine);%0A%09%09if(startCol %3E -1)%0A%09%09%09start.put(%22character%22, startCol);%0A%09%09if(endCol %3E-1)%0A%09%09%09end.put(%22character%22,endCol);%0A%09%09if(endLine %3E -1)%0A%09%09%09end.put(%22line%22,endLine);%0A%09%09range.put(%22start%22,start);%0A%09%09range.put(%22end%22,end);%0A%09%09return range;%0A%09%7D
 %0A%0A%09/
@@ -1813,20 +1813,19 @@
 buffer.%0A
+
 %09 *
- 
 %0A%09 * @pa

Remove unused method