}
try {
- return PtyProcess.exec(command, envs, directory != null ? directory : TerminalProjectOptionsProvider.getInstance(myProject).getStartingDirectory());
+ return PtyProcess.exec(command, envs, directory != null ? directory : TerminalProjectOptionsProvider.Companion.getInstance(myProject).getStartingDirectory());
}
catch (IOException e) {
throw new ExecutionException(e);
}
private String getShellPath() {
- return TerminalProjectOptionsProvider.getInstance(myProject).getShellPath();
+ return TerminalProjectOptionsProvider.Companion.getInstance(myProject).getShellPath();
}
@NotNull
public TerminalOptionsConfigurable(@NotNull Project project) {
myOptionsProvider = TerminalOptionsProvider.getInstance();
- myProjectOptionsProvider = TerminalProjectOptionsProvider.getInstance(project);
+ myProjectOptionsProvider = TerminalProjectOptionsProvider.Companion.getInstance(project);
}
@NotNull
+++ /dev/null
-/*
- * Copyright 2000-2016 JetBrains s.r.o.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jetbrains.plugins.terminal;
-
-import com.intellij.openapi.components.PersistentStateComponent;
-import com.intellij.openapi.components.ServiceManager;
-import com.intellij.openapi.components.State;
-import com.intellij.openapi.components.Storage;
-import com.intellij.openapi.diagnostic.Logger;
-import com.intellij.openapi.project.Project;
-import com.intellij.openapi.roots.ProjectRootManager;
-import com.intellij.openapi.util.SystemInfo;
-import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.vfs.VirtualFile;
-import org.jetbrains.annotations.NotNull;
-
-import java.io.File;
-
-/**
- * @author traff
- */
-@State(
- name = "TerminalProjectOptionsProvider",
- storages = @Storage("terminal.xml")
-)
-public class TerminalProjectOptionsProvider implements PersistentStateComponent<TerminalProjectOptionsProvider.State> {
- private static final Logger LOG = Logger.getInstance(TerminalProjectOptionsProvider.class);
-
- private State myState = new State();
- private final Project myProject;
-
- public TerminalProjectOptionsProvider(@NotNull Project project) {myProject = project;}
-
-
- public static TerminalProjectOptionsProvider getInstance(@NotNull Project project) {
- return ServiceManager.getService(project, TerminalProjectOptionsProvider.class);
- }
-
- @Override
- public State getState() {
- return myState;
- }
-
- @Override
- public void loadState(State state) {
- setShellPath(state.myShellPath);
- myState.myStartingDirectory = state.myStartingDirectory;
- }
-
-
- public static class State {
- public String myShellPath = null;
- public String myStartingDirectory = null;
- }
-
- public String getShellPath() {
- if (myState.myShellPath != null) {
- return myState.myShellPath;
- } else {
- return getDefaultShellPath();
- }
- }
-
-
- public void setShellPath(String shellPath) {
- if (isShellPathDefault(shellPath) || StringUtil.isEmpty(shellPath)) {
- myState.myShellPath = null;
- } else {
- myState.myShellPath = shellPath;
- }
- }
-
-
- public void setStartingDirectory(String startingDirectory) {
- if (isStartingDirectoryDefault(startingDirectory) || StringUtil.isEmpty(startingDirectory)) {
- myState.myStartingDirectory = null;
- } else {
- myState.myStartingDirectory = startingDirectory;
- }
- }
-
- public boolean isShellPathDefault(String shellPath) {
- return StringUtil.equals(shellPath, getDefaultShellPath());
- }
-
- public boolean isStartingDirectoryDefault(String startingDirectory) {
- return StringUtil.equals(startingDirectory, getDefaultStartingDirectory());
- }
-
- public String getStartingDirectory() {
- if (myState.myStartingDirectory != null) {
- return myState.myStartingDirectory;
- }
- else {
- return getDefaultStartingDirectory();
- }
- }
-
-
- private static String getDefaultShellPath() {
- String shell = System.getenv("SHELL");
-
- if (shell != null && new File(shell).canExecute()) {
- return shell;
- }
-
- if (SystemInfo.isUnix) {
- if (new File("/bin/bash").exists()) {
- return "/bin/bash";
- }
- else {
- return "/bin/sh";
- }
- }
- else {
- return "cmd.exe";
- }
- }
-
- public String getDefaultStartingDirectory() {
- String directory = null;
- for (LocalTerminalCustomizer customizer : LocalTerminalCustomizer.EP_NAME.getExtensions()) {
- try {
-
- if (directory == null) {
- directory = customizer.getDefaultFolder();
- }
- }
- catch (Exception e) {
- LOG.error("Exception during getting default folder", e);
- }
- }
-
- return currentProjectFolder();
- }
-
-
- private String currentProjectFolder() {
- final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(myProject);
-
- final VirtualFile[] roots = projectRootManager.getContentRoots();
- if (roots.length == 1) {
- roots[0].getCanonicalPath();
- }
- final VirtualFile baseDir = myProject.getBaseDir();
- return baseDir == null ? null : baseDir.getCanonicalPath();
- }
-}
-
--- /dev/null
+/*
+ * Copyright 2000-2016 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.plugins.terminal
+
+import com.intellij.openapi.components.PersistentStateComponent
+import com.intellij.openapi.components.ServiceManager
+import com.intellij.openapi.components.State
+import com.intellij.openapi.components.Storage
+import com.intellij.openapi.diagnostic.Logger
+import com.intellij.openapi.project.Project
+import com.intellij.openapi.roots.ProjectRootManager
+import com.intellij.openapi.util.SystemInfo
+import java.io.File
+import kotlin.reflect.KProperty
+
+/**
+ * @author traff
+ */
+@State(name = "TerminalProjectOptionsProvider", storages = arrayOf(Storage("terminal.xml")))
+class TerminalProjectOptionsProvider(private val myProject: Project) : PersistentStateComponent<TerminalProjectOptionsProvider.State> {
+
+ private val myState = State()
+
+ override fun getState(): State? {
+ return myState
+ }
+
+ override fun loadState(state: State) {
+ shellPath = state.myShellPath
+ myState.myStartingDirectory = state.myStartingDirectory
+ }
+
+ class State {
+ var myShellPath: String? = null
+ var myStartingDirectory: String? = null
+ }
+
+ var shellPath: String? by ValueWithDefault { defaultShellPath }
+
+ var startingDirectory: String? by ValueWithDefault { defaultStartingDirectory }
+
+ val defaultStartingDirectory: String?
+ get() {
+ var directory: String? = null
+ for (customizer in LocalTerminalCustomizer.EP_NAME.extensions) {
+ try {
+
+ if (directory == null) {
+ directory = customizer.defaultFolder
+ }
+ }
+ catch (e: Exception) {
+ LOG.error("Exception during getting default folder", e)
+ }
+ }
+
+ return currentProjectFolder()
+ }
+
+
+ private fun currentProjectFolder(): String? {
+ val projectRootManager = ProjectRootManager.getInstance(myProject)
+
+ val roots = projectRootManager.contentRoots
+ if (roots.size == 1) {
+ roots[0].canonicalPath
+ }
+ val baseDir = myProject.baseDir
+ return baseDir?.canonicalPath
+ }
+
+ val defaultShellPath: String
+ get() {
+ val shell = System.getenv("SHELL")
+
+ if (shell != null && File(shell).canExecute()) {
+ return shell
+ }
+
+ if (SystemInfo.isUnix) {
+ if (File("/bin/bash").exists()) {
+ return "/bin/bash"
+ }
+ else {
+ return "/bin/sh"
+ }
+ }
+ else {
+ return "cmd.exe"
+ }
+ }
+
+ companion object {
+ private val LOG = Logger.getInstance(TerminalProjectOptionsProvider::class.java)
+
+
+ fun getInstance(project: Project): TerminalProjectOptionsProvider {
+ return ServiceManager.getService(project, TerminalProjectOptionsProvider::class.java)
+ }
+ }
+
+}
+
+class ValueWithDefault(val default: () -> String?) {
+ private var _value: String? = null
+
+ operator fun getValue(thisRef: Any?, property: KProperty<*>): String? {
+ return if (_value !== null) _value else default()
+ }
+
+ operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) {
+ _value = if (value == default() || value.isNullOrEmpty()) null else value
+ }
+}
+
+
+
import com.intellij.openapi.ui.TextComponentAccessor;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.Comparing;
+import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.components.JBCheckBox;
@Override
protected void textChanged(DocumentEvent e) {
myShellPathField
- .getTextField().setForeground(myProjectOptionsProvider.isShellPathDefault(myShellPathField.getText()) ?
+ .getTextField().setForeground(StringUtil.equals(myShellPathField.getText(), myProjectOptionsProvider.getDefaultShellPath()) ?
getDefaultValueColor() : getChangedValueColor());
}
});
@Override
protected void textChanged(DocumentEvent e) {
myStartDirectoryField
- .getTextField().setForeground(myProjectOptionsProvider.isStartingDirectoryDefault(myStartDirectoryField.getText()) ?
- getDefaultValueColor() : getChangedValueColor());
+ .getTextField()
+ .setForeground(StringUtil.equals(myStartDirectoryField.getText(), myProjectOptionsProvider.getDefaultStartingDirectory()) ?
+ getDefaultValueColor() : getChangedValueColor());
}
});