Converted file to Kotlin (step 1)
This commit is contained in:
@@ -14,157 +14,135 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.actions;
|
package org.jetbrains.kotlin.idea.actions
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists
|
||||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
import com.intellij.codeInsight.daemon.QuickFixBundle
|
||||||
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction;
|
import com.intellij.codeInsight.daemon.impl.actions.AddImportAction
|
||||||
import com.intellij.codeInsight.hint.QuestionAction;
|
import com.intellij.codeInsight.hint.QuestionAction
|
||||||
import com.intellij.openapi.application.ApplicationManager;
|
import com.intellij.openapi.application.ApplicationManager
|
||||||
import com.intellij.openapi.command.CommandProcessor;
|
import com.intellij.openapi.command.CommandProcessor
|
||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.ui.popup.JBPopupFactory;
|
import com.intellij.openapi.ui.popup.JBPopupFactory
|
||||||
import com.intellij.openapi.ui.popup.PopupStep;
|
import com.intellij.openapi.ui.popup.PopupStep
|
||||||
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
|
import com.intellij.openapi.ui.popup.util.BaseListPopupStep
|
||||||
import com.intellij.psi.PsiDocumentManager;
|
import com.intellij.psi.PsiDocumentManager
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiFile;
|
import com.intellij.psi.PsiFile
|
||||||
import com.intellij.util.PlatformIcons;
|
import com.intellij.util.PlatformIcons
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.idea.JetBundle
|
||||||
import org.jetbrains.kotlin.idea.JetBundle;
|
import org.jetbrains.kotlin.idea.quickfix.ImportInsertHelper
|
||||||
import org.jetbrains.kotlin.idea.quickfix.ImportInsertHelper;
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.name.FqName;
|
import org.jetbrains.kotlin.psi.JetFile
|
||||||
import org.jetbrains.kotlin.psi.JetFile;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Automatically adds import directive to the file for resolving reference.
|
* Automatically adds import directive to the file for resolving reference.
|
||||||
* Based on {@link AddImportAction}
|
* Based on {@link AddImportAction}
|
||||||
*/
|
*/
|
||||||
public class JetAddImportAction implements QuestionAction {
|
public class JetAddImportAction
|
||||||
|
/**
|
||||||
|
* @param project Project where action takes place.
|
||||||
|
* @param editor Editor where modification should be done.
|
||||||
|
* @param element Element with unresolved reference.
|
||||||
|
* @param imports Variants for resolution.
|
||||||
|
*/
|
||||||
|
(private val myProject: Project, private val myEditor: Editor, private val myElement: PsiElement, imports: Iterable<FqName>) : QuestionAction {
|
||||||
|
private val possibleImports: List<FqName>
|
||||||
|
|
||||||
private final Project myProject;
|
{
|
||||||
private final Editor myEditor;
|
possibleImports = Lists.newArrayList<FqName>(imports)
|
||||||
private final PsiElement myElement;
|
|
||||||
private final List<FqName> possibleImports;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param project Project where action takes place.
|
|
||||||
* @param editor Editor where modification should be done.
|
|
||||||
* @param element Element with unresolved reference.
|
|
||||||
* @param imports Variants for resolution.
|
|
||||||
*/
|
|
||||||
public JetAddImportAction(
|
|
||||||
@NotNull Project project,
|
|
||||||
@NotNull Editor editor,
|
|
||||||
@NotNull PsiElement element,
|
|
||||||
@NotNull Iterable<FqName> imports
|
|
||||||
) {
|
|
||||||
myProject = project;
|
|
||||||
myEditor = editor;
|
|
||||||
myElement = element;
|
|
||||||
possibleImports = Lists.newArrayList(imports);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun execute(): Boolean {
|
||||||
public boolean execute() {
|
PsiDocumentManager.getInstance(myProject).commitAllDocuments()
|
||||||
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
|
|
||||||
|
|
||||||
if (!myElement.isValid()){
|
if (!myElement.isValid()) {
|
||||||
return false;
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Validate resolution variants. See AddImportAction.execute()
|
// TODO: Validate resolution variants. See AddImportAction.execute()
|
||||||
|
|
||||||
if (possibleImports.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
|
if (possibleImports.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
|
||||||
addImport(myElement, myProject, possibleImports.get(0));
|
addImport(myElement, myProject, possibleImports.get(0))
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
chooseClassAndImport();
|
chooseClassAndImport()
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
protected BaseListPopupStep getImportSelectionPopup() {
|
protected fun getImportSelectionPopup(): BaseListPopupStep<Any> {
|
||||||
return new BaseListPopupStep<FqName>(JetBundle.message("imports.chooser.title"), possibleImports) {
|
return object : BaseListPopupStep<FqName>(JetBundle.message("imports.chooser.title"), possibleImports) {
|
||||||
@Override
|
override fun isAutoSelectionEnabled(): Boolean {
|
||||||
public boolean isAutoSelectionEnabled() {
|
return false
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun onChosen(selectedValue: FqName?, finalChoice: Boolean): PopupStep<Any>? {
|
||||||
public PopupStep onChosen(FqName selectedValue, boolean finalChoice) {
|
|
||||||
if (selectedValue == null) {
|
if (selectedValue == null) {
|
||||||
return FINAL_CHOICE;
|
return PopupStep.FINAL_CHOICE
|
||||||
}
|
}
|
||||||
|
|
||||||
if (finalChoice) {
|
if (finalChoice) {
|
||||||
addImport(myElement, myProject, selectedValue);
|
addImport(myElement, myProject, selectedValue)
|
||||||
return FINAL_CHOICE;
|
return PopupStep.FINAL_CHOICE
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> toExclude = AddImportAction.getAllExcludableStrings(selectedValue.asString());
|
val toExclude = AddImportAction.getAllExcludableStrings(selectedValue.asString())
|
||||||
|
|
||||||
return new BaseListPopupStep<String>(null, toExclude) {
|
return object : BaseListPopupStep<String>(null, toExclude) {
|
||||||
@NotNull
|
override fun getTextFor(value: String): String {
|
||||||
@Override
|
return "Exclude '" + value + "' from auto-import"
|
||||||
public String getTextFor(String value) {
|
|
||||||
return "Exclude '" + value + "' from auto-import";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun onChosen(selectedValue: String?, finalChoice: Boolean): PopupStep<Any>? {
|
||||||
public PopupStep onChosen(String selectedValue, boolean finalChoice) {
|
|
||||||
if (finalChoice) {
|
if (finalChoice) {
|
||||||
AddImportAction.excludeFromImport(myProject, selectedValue);
|
AddImportAction.excludeFromImport(myProject, selectedValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onChosen(selectedValue, finalChoice);
|
return super.onChosen(selectedValue, finalChoice)
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun hasSubstep(selectedValue: FqName?): Boolean {
|
||||||
public boolean hasSubstep(FqName selectedValue) {
|
return true
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
override fun getTextFor(value: FqName): String {
|
||||||
@Override
|
return value.asString()
|
||||||
public String getTextFor(FqName value) {
|
|
||||||
return value.asString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
override fun getIconFor(aValue: FqName): Icon? {
|
||||||
public Icon getIconFor(FqName aValue) {
|
|
||||||
// TODO: change icon
|
// TODO: change icon
|
||||||
return PlatformIcons.CLASS_ICON;
|
return PlatformIcons.CLASS_ICON
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void addImport(final PsiElement element, Project project, final FqName selectedImport) {
|
private fun chooseClassAndImport() {
|
||||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
JBPopupFactory.getInstance().createListPopup(getImportSelectionPopup()).showInBestPositionFor(myEditor)
|
||||||
|
|
||||||
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
PsiFile file = element.getContainingFile();
|
|
||||||
if (!(file instanceof JetFile)) return;
|
|
||||||
ImportInsertHelper.getInstance().addImportDirectiveIfNeeded(selectedImport, (JetFile) file);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, QuickFixBundle.message("add.import"), null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void chooseClassAndImport() {
|
class object {
|
||||||
JBPopupFactory.getInstance().createListPopup(getImportSelectionPopup()).showInBestPositionFor(myEditor);
|
|
||||||
|
protected fun addImport(element: PsiElement, project: Project, selectedImport: FqName) {
|
||||||
|
PsiDocumentManager.getInstance(project).commitAllDocuments()
|
||||||
|
|
||||||
|
CommandProcessor.getInstance().executeCommand(project, object : Runnable {
|
||||||
|
override fun run() {
|
||||||
|
ApplicationManager.getApplication().runWriteAction(object : Runnable {
|
||||||
|
override fun run() {
|
||||||
|
val file = element.getContainingFile()
|
||||||
|
if (!(file is JetFile)) return
|
||||||
|
ImportInsertHelper.getInstance().addImportDirectiveIfNeeded(selectedImport, file as JetFile)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, QuickFixBundle.message("add.import"), null)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user