Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Minor API refactoring of Go preference constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Nov 20, 2015
1 parent 5606493 commit 7f6e57d
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 180 deletions.
Expand Up @@ -39,10 +39,10 @@ public class GoProjectEnvironmentTest extends CommonGoCoreTest {
public void test() throws Exception { test$(); } public void test() throws Exception { test$(); }
public void test$() throws Exception { public void test$() throws Exception {


GoEnvironmentPrefs.GO_ROOT.setInstanceScopeValue(SAMPLE_GO_ROOT.asString()); GoEnvironmentPrefs.GO_ROOT.getGlobalPreference().setInstanceScopeValue(SAMPLE_GO_ROOT.asString());
GoEnvironmentPrefs.GO_ARCH.setInstanceScopeValue("386"); GoEnvironmentPrefs.GO_ARCH.getGlobalPreference().setInstanceScopeValue("386");
GoEnvironmentPrefs.GO_OS.setInstanceScopeValue("windows"); GoEnvironmentPrefs.GO_OS.getGlobalPreference().setInstanceScopeValue("windows");
GoEnvironmentPrefs.GO_PATH.setInstanceScopeValue(SAMPLE_GOPATH_Entry.toString()); GoEnvironmentPrefs.GO_PATH.getGlobalPreference().setInstanceScopeValue(SAMPLE_GOPATH_Entry.toString());
TestsWorkingDir.deleteDir(SAMPLE_GOPATH_Entry); TestsWorkingDir.deleteDir(SAMPLE_GOPATH_Entry);


{ {
Expand All @@ -64,7 +64,7 @@ public class GoProjectEnvironmentTest extends CommonGoCoreTest {


String goPathEntryOther = location.append("other").toOSString(); String goPathEntryOther = location.append("other").toOSString();
String gopath = location.toOSString() + File.pathSeparator + goPathEntryOther; String gopath = location.toOSString() + File.pathSeparator + goPathEntryOther;
GoEnvironmentPrefs.GO_PATH.setInstanceScopeValue(gopath); GoEnvironmentPrefs.GO_PATH.getGlobalPreference().setInstanceScopeValue(gopath);


// Test GOPATH which already has project location. Also nested GOPATH entry. // Test GOPATH which already has project location. Also nested GOPATH entry.
checkEnvGoPath(project, list(location.toOSString(), goPathEntryOther), false); checkEnvGoPath(project, list(location.toOSString(), goPathEntryOther), false);
Expand All @@ -73,7 +73,7 @@ public class GoProjectEnvironmentTest extends CommonGoCoreTest {
try (SampleGoProject sampleProject = new SampleGoProject(getClass().getSimpleName())){ try (SampleGoProject sampleProject = new SampleGoProject(getClass().getSimpleName())){
IProject project = sampleProject.getProject(); IProject project = sampleProject.getProject();


GoEnvironmentPrefs.GO_PATH.setInstanceScopeValue(SAMPLE_GO_PATH.asString()); GoEnvironmentPrefs.GO_PATH.getGlobalPreference().setInstanceScopeValue(SAMPLE_GO_PATH.asString());


sampleProject.moveToLocation(SAMPLE_GOPATH_Entry.resolve_valid("src/github.com/foo")); sampleProject.moveToLocation(SAMPLE_GOPATH_Entry.resolve_valid("src/github.com/foo"));
// Test that project location is not added, because project is in a Go source package // Test that project location is not added, because project is in a Go source package
Expand Down
Expand Up @@ -10,25 +10,32 @@
*******************************************************************************/ *******************************************************************************/
package com.googlecode.goclipse.core; package com.googlecode.goclipse.core;


import static melnorme.lang.ide.core.operations.ToolchainPreferences.USE_PROJECT_SETTINGS;

import melnorme.lang.ide.core.LangCore; import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.core.operations.ToolchainPreferences; import melnorme.lang.ide.core.operations.ToolchainPreferences;
import melnorme.lang.ide.core.utils.prefs.IProjectPreference;
import melnorme.lang.ide.core.utils.prefs.OptionalStringPreference; import melnorme.lang.ide.core.utils.prefs.OptionalStringPreference;
import melnorme.lang.ide.core.utils.prefs.StringPreference; import melnorme.lang.ide.core.utils.prefs.StringPreference;


public interface GoEnvironmentPrefs { public interface GoEnvironmentPrefs {


static StringPreference GO_ROOT = new StringPreference(LangCore.PLUGIN_ID, IProjectPreference<String> GO_ROOT = new StringPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.goroot", "", ToolchainPreferences.USE_PROJECT_SETTINGS); "com.googlecode.goclipse.goroot", "", USE_PROJECT_SETTINGS).getProjectPreference();
static OptionalStringPreference GO_PATH = new OptionalStringPreference(LangCore.PLUGIN_ID, IProjectPreference<String> GO_OS = new StringPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.gopath", ToolchainPreferences.USE_PROJECT_SETTINGS); "com.googlecode.goclipse.goos", "", USE_PROJECT_SETTINGS).getProjectPreference();
static StringPreference GO_OS = new StringPreference(LangCore.PLUGIN_ID, IProjectPreference<String> GO_ARCH = new StringPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.goos", "", ToolchainPreferences.USE_PROJECT_SETTINGS); "com.googlecode.goclipse.goarch", "", USE_PROJECT_SETTINGS).getProjectPreference();
static StringPreference GO_ARCH = new StringPreference(LangCore.PLUGIN_ID, IProjectPreference<String> FORMATTER_PATH = new StringPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.goarch", "", ToolchainPreferences.USE_PROJECT_SETTINGS); "com.googlecode.goclipse.formatter.path", "", USE_PROJECT_SETTINGS).getProjectPreference();
IProjectPreference<String> DOCUMENTOR_PATH = new StringPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.documentor.path", "", USE_PROJECT_SETTINGS).getProjectPreference();


IProjectPreference<String> GO_PATH = new OptionalStringPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.gopath", ToolchainPreferences.USE_PROJECT_SETTINGS).getProjectPreference();


static StringPreference FORMATTER_PATH = new StringPreference(LangCore.PLUGIN_ID, // IProjectPreference<Boolean> APPEND_PROJECT_LOC_TO_GOPATH = new BooleanPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.formatter.path", "", ToolchainPreferences.USE_PROJECT_SETTINGS); // "append_projloc_gopath", true, ToolchainPreferences.USE_PROJECT_SETTINGS).getProjectPreference();
static StringPreference DOCUMENTOR_PATH = new StringPreference(LangCore.PLUGIN_ID,
"com.googlecode.goclipse.documentor.path", "", ToolchainPreferences.USE_PROJECT_SETTINGS);


} }
Expand Up @@ -27,25 +27,25 @@
import com.googlecode.goclipse.tooling.env.GoWorkspaceLocation; import com.googlecode.goclipse.tooling.env.GoWorkspaceLocation;


import melnorme.lang.ide.core.utils.ResourceUtils; import melnorme.lang.ide.core.utils.ResourceUtils;
import melnorme.lang.ide.core.utils.prefs.StringPreference; import melnorme.lang.ide.core.utils.prefs.IProjectPreference;
import melnorme.utilbox.collections.ArrayList2; import melnorme.utilbox.collections.ArrayList2;
import melnorme.utilbox.core.CommonException; import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.misc.Location; import melnorme.utilbox.misc.Location;


public class GoProjectEnvironment implements GoEnvironmentConstants { public class GoProjectEnvironment implements GoEnvironmentConstants {


public static GoRoot getEffectiveGoRoot(IProject project) { public static GoRoot getEffectiveGoRoot(IProject project) {
String prefValue = GoEnvironmentPrefs.GO_ROOT.getProjectPreference().getEffectiveValue(project); String prefValue = GoEnvironmentPrefs.GO_ROOT.getEffectiveValue(project);
return new GoRoot(nullAsEmpty(prefValue)); return new GoRoot(nullAsEmpty(prefValue));
} }


public static GoArch getEffectiveGoArch(IProject project) { public static GoArch getEffectiveGoArch(IProject project) {
String strValue = emptyAsNull(GoEnvironmentPrefs.GO_ARCH.getProjectPreference().getEffectiveValue(project)); String strValue = emptyAsNull(GoEnvironmentPrefs.GO_ARCH.getEffectiveValue(project));
return strValue == null ? null : new GoArch(strValue); return strValue == null ? null : new GoArch(strValue);
} }


public static GoOs getEffectiveGoOs(IProject project) { public static GoOs getEffectiveGoOs(IProject project) {
String strValue = emptyAsNull(GoEnvironmentPrefs.GO_OS.getProjectPreference().getEffectiveValue(project)); String strValue = emptyAsNull(GoEnvironmentPrefs.GO_OS.getEffectiveValue(project));
return strValue == null ? null : new GoOs(strValue); return strValue == null ? null : new GoOs(strValue);
} }


Expand Down Expand Up @@ -82,8 +82,9 @@ public static boolean isProjectInsideGoPathSourceFolder(IProject project, GoPath
return goPath.findGoPathEntryForSourcePath(ResourceUtils.getProjectLocation2(project)) != null; return goPath.findGoPathEntryForSourcePath(ResourceUtils.getProjectLocation2(project)) != null;
} }


protected static String getEffectiveValueFromEnv(StringPreference pref, IProject project, String envAlternative) { protected static String getEffectiveValueFromEnv(IProjectPreference<String> pref, IProject project,
String prefValue = pref.getProjectPreference().getEffectiveValue(project); String envAlternative) {
String prefValue = pref.getEffectiveValue(project);
if(prefValue == null) { if(prefValue == null) {
return nullAsEmpty(System.getenv(envAlternative)); return nullAsEmpty(System.getenv(envAlternative));
} }
Expand Down
Expand Up @@ -49,7 +49,7 @@ protected IValidatedField<Path> getSDKToolPathField(IProject project) {
return new ValidatedSDKToolPath(project, getSDKToolPathValidator()) { return new ValidatedSDKToolPath(project, getSDKToolPathValidator()) {
@Override @Override
protected String getRawFieldValue2() { protected String getRawFieldValue2() {
return GoEnvironmentPrefs.GO_ROOT.getProjectPreference().getEffectiveValue(project); return GoEnvironmentPrefs.GO_ROOT.getEffectiveValue(project);
} }
}; };
} }
Expand Down
@@ -1,103 +1,106 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2014 Bruno Medeiros and other Contributors. * Copyright (c) 2014 Bruno Medeiros and other Contributors.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html * http://www.eclipse.org/legal/epl-v10.html
* *
* Contributors: * Contributors:
* Bruno Medeiros - initial API and implementation * Bruno Medeiros - initial API and implementation
*******************************************************************************/ *******************************************************************************/
package com.googlecode.goclipse.ui.editor.actions; package com.googlecode.goclipse.ui.editor.actions;




import static melnorme.lang.ide.ui.editor.EditorUtils.getEditorDocument; import static melnorme.lang.ide.ui.editor.EditorUtils.getEditorDocument;
import static melnorme.utilbox.core.CoreUtil.areEqual; import static melnorme.utilbox.core.CoreUtil.areEqual;


import java.nio.file.Path; import java.nio.file.Path;


import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.ITextEditor;


import com.googlecode.goclipse.core.GoProjectEnvironment; import com.googlecode.goclipse.core.GoProjectEnvironment;
import com.googlecode.goclipse.core.operations.GoToolManager; import com.googlecode.goclipse.core.operations.GoToolManager;
import com.googlecode.goclipse.tooling.env.GoEnvironment; import com.googlecode.goclipse.tooling.env.GoEnvironment;
import com.googlecode.goclipse.ui.editor.GoEditor; import com.googlecode.goclipse.ui.editor.GoEditor;


import melnorme.lang.ide.core.LangCore; import melnorme.lang.ide.core.LangCore;
import melnorme.lang.ide.ui.editor.EditorUtils; import melnorme.lang.ide.ui.editor.EditorUtils;
import melnorme.lang.ide.ui.utils.operations.AbstractEditorOperation2; import melnorme.lang.ide.ui.utils.operations.AbstractEditorOperation2;
import melnorme.utilbox.concurrency.OperationCancellation; import melnorme.utilbox.concurrency.OperationCancellation;
import melnorme.utilbox.core.CommonException; import melnorme.utilbox.core.CommonException;
import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult; import melnorme.utilbox.process.ExternalProcessHelper.ExternalProcessResult;


public abstract class AbstractEditorGoToolOperation extends AbstractEditorOperation2<String> { public abstract class AbstractEditorGoToolOperation extends AbstractEditorOperation2<String> {


protected GoEditor goEditor; protected GoEditor goEditor;
protected String editorText; protected IProject project; // Can be null
protected ProcessBuilder pb;

protected ProcessBuilder pb;
public AbstractEditorGoToolOperation(String operationName, ITextEditor editor) {
super(operationName, editor); protected String editorText;
}

public AbstractEditorGoToolOperation(String operationName, ITextEditor editor) {
@Override super(operationName, editor);
protected void prepareOperation() throws CoreException { }
if(!(editor instanceof GoEditor)) {
throw LangCore.createCoreException("Editor is not a GoEditor.", null); @Override
} protected void prepareOperation() throws CoreException {
goEditor = (GoEditor) editor; if(!(editor instanceof GoEditor)) {
editorText = getEditorDocument(editor).get(); throw LangCore.createCoreException("Editor is not a GoEditor.", null);

}
IProject project = EditorUtils.getAssociatedProject(editorInput); goEditor = (GoEditor) editor;

editorText = getEditorDocument(editor).get();
GoEnvironment goEnv = GoProjectEnvironment.getGoEnvironment(project);

project = EditorUtils.getAssociatedProject(editorInput);
try {
Path goSDKPath = GoToolManager.getDefault().getSDKToolPath(project); GoEnvironment goEnv = GoProjectEnvironment.getGoEnvironment(project);
pb = prepareProcessBuilder(goSDKPath, goEnv);
} catch (CommonException ce) { try {
throw LangCore.createCoreException(ce); Path goSDKPath = GoToolManager.getDefault().getSDKToolPath(project);
} pb = prepareProcessBuilder(goSDKPath, goEnv);
} } catch (CommonException ce) {

throw LangCore.createCoreException(ce);
protected abstract ProcessBuilder prepareProcessBuilder(Path goSDKPath, GoEnvironment goEnv) }
throws CoreException, CommonException; }


@Override protected abstract ProcessBuilder prepareProcessBuilder(Path goSDKPath, GoEnvironment goEnv)
protected String doBackgroundValueComputation(IProgressMonitor monitor) throws CoreException, CommonException;
throws CoreException, CommonException, OperationCancellation {
ExternalProcessResult processResult = @Override
GoToolManager.getDefault().newRunToolOperation2(pb, monitor).runProcess(editorText, true); protected String doBackgroundValueComputation(IProgressMonitor monitor)

throws CoreException, CommonException, OperationCancellation {
return processResult.getStdOutBytes().toString(); ExternalProcessResult processResult =
} GoToolManager.getDefault().newRunToolOperation2(pb, monitor).runProcess(editorText, true);


@Override return processResult.getStdOutBytes().toString();
protected void handleComputationResult() throws CoreException { }
if(!areEqual(result, editorText)) {
replaceText(goEditor.getSourceViewer_(), result); @Override
} protected void handleComputationResult() throws CoreException {
} if(!areEqual(result, editorText)) {

replaceText(goEditor.getSourceViewer_(), result);

}
public static void replaceText(ISourceViewer sourceViewer, String newText) { }
ISelection sel = sourceViewer.getSelectionProvider().getSelection();
int topIndex = sourceViewer.getTopIndex();

public static void replaceText(ISourceViewer sourceViewer, String newText) {
sourceViewer.getDocument().set(newText); ISelection sel = sourceViewer.getSelectionProvider().getSelection();

int topIndex = sourceViewer.getTopIndex();
if (sel != null) {
sourceViewer.getSelectionProvider().setSelection(sel); sourceViewer.getDocument().set(newText);
}

if (sel != null) {
if (topIndex != -1) { sourceViewer.getSelectionProvider().setSelection(sel);
sourceViewer.setTopIndex(topIndex); }
}
} if (topIndex != -1) {

sourceViewer.setTopIndex(topIndex);
}
}

} }

0 comments on commit 7f6e57d

Please sign in to comment.