package com.googlecode.goclipse.gocode;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.Status;

import com.googlecode.goclipse.Activator;
import com.googlecode.goclipse.SysUtils;
import com.googlecode.goclipse.builder.ExternalCommand;
import com.googlecode.goclipse.builder.GoConstants;
import com.googlecode.goclipse.builder.ProcessOStreamFilter;
import com.googlecode.goclipse.builder.StreamAsLines;

public class GoCodeClient {

	private String error;

	public GoCodeClient() {
	}
	
	public List<String> getCompletions(String fileName, final String bufferText, int offset) {
		ExternalCommand command = new ExternalCommand("/home/gmiller/go/bin/gocode");
		StreamAsLines output = new StreamAsLines();
		command.setResultsFilter(output);
		command.setInputFilter(new ProcessOStreamFilter() {
			@Override
			public void setStream(OutputStream outputStream) {
				OutputStreamWriter osw = new OutputStreamWriter(outputStream);
				try {
					osw.append(bufferText);
					osw.flush();
					outputStream.close();
				} catch (IOException e) {
					// do nothing
				}
			}
		});

		List<String> parameters = new LinkedList<String>();
		parameters.add("-f=csv");
		parameters.add("autocomplete");
		parameters.add(fileName);
		parameters.add(""+offset);
		error = command.execute(parameters, true);
		if (error != null) {
			Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, error));
		}
		return output.getLines();
	}

	public String getError() {
		return error;
	}

}

@@ -179,16 +179,54 @@
 .List;%0A%0A
+import org.eclipse.core.runtime.Path;%0A
 import o
@@ -257,16 +257,16 @@
 Status;%0A
-
 %0Aimport 
@@ -563,16 +563,80 @@
 AsLines;
+%0Aimport com.googlecode.goclipse.preferences.PreferenceConstants;
 %0A%0Apublic
@@ -804,16 +804,126 @@
 fset) %7B%0A
+%09%09String goroot = %0AActivator.getDefault().getPreferenceStore().getString(%0A%09%09%09%09PreferenceConstants.GOROOT);%0A%09%09%0A
 %09%09Extern
@@ -946,16 +946,20 @@
 d = new 
+%0A%09%09%09
 External
@@ -970,37 +970,77 @@
 and(
-%22/home/gmiller/go/bin/gocode%22
+Path.fromOSString(goroot).append(%22bin%22).append(%22gocode%22).toOSString()
 );%0A%09

Remove hard-coded path to gocode