com.jetbrains.python.codeInsight.imports.ImportFromExistingAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of python-community Show documentation
Show all versions of python-community Show documentation
A packaging of the IntelliJ Community Edition python-community library.
This is release number 1 of trunk branch 142.
The newest version!
/*
* Copyright 2000-2014 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 com.jetbrains.python.codeInsight.imports;
import com.intellij.codeInsight.hint.QuestionAction;
import com.intellij.ide.DataManager;
import com.intellij.lang.injection.InjectedLanguageManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.util.QualifiedName;
import com.intellij.ui.SimpleColoredComponent;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.components.JBList;
import com.intellij.util.Consumer;
import com.intellij.util.Function;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.*;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.*;
import java.util.List;
/**
* Turns an unqualified unresolved identifier into qualified and resolvable.
*
* @author dcheryasov
*/
public class ImportFromExistingAction implements QuestionAction {
PsiElement myTarget;
List mySources; // list of
String myName;
boolean myUseQualifiedImport;
private Runnable myOnDoneCallback;
private final boolean myImportLocally;
/**
* @param target element to become qualified as imported.
* @param sources clauses of import to be used.
* @param name relevant name ot the target element (e.g. of identifier in an expression).
* @param useQualified if True, use qualified "import modulename" instead of "from modulename import ...".
*/
public ImportFromExistingAction(@NotNull PsiElement target, @NotNull List sources, @NotNull String name,
boolean useQualified, boolean importLocally) {
myTarget = target;
mySources = sources;
myName = name;
myUseQualifiedImport = useQualified;
myImportLocally = importLocally;
}
public void onDone(Runnable callback) {
assert myOnDoneCallback == null;
myOnDoneCallback = callback;
}
/**
* Alters either target (by qualifying a name) or source (by explicitly importing the name).
* @return true if action succeeded
*/
public boolean execute() {
// check if the tree is sane
PsiDocumentManager.getInstance(myTarget.getProject()).commitAllDocuments();
if (!myTarget.isValid()) return false;
if ((myTarget instanceof PyQualifiedExpression) && ((((PyQualifiedExpression)myTarget).isQualified()))) return false; // we cannot be qualified
for (ImportCandidateHolder item : mySources) {
if (!item.getImportable().isValid()) return false;
if (!item.getFile().isValid()) return false;
if (item.getImportElement() != null && !item.getImportElement().isValid()) return false;
}
if (mySources.isEmpty()) {
return false;
}
// act
if (mySources.size() > 1) {
selectSourceAndDo();
}
else doWriteAction(mySources.get(0));
return true;
}
private void selectSourceAndDo() {
// GUI part
ImportCandidateHolder[] items = mySources.toArray(new ImportCandidateHolder[mySources.size()]); // silly JList can't handle modern collections
final JList list = new JBList(items);
list.setCellRenderer(new CellRenderer(myName));
final Runnable runnable = new Runnable() {
public void run() {
final Object selected = list.getSelectedValue();
if (selected instanceof ImportCandidateHolder) {
final ImportCandidateHolder item = (ImportCandidateHolder)selected;
PsiDocumentManager.getInstance(myTarget.getProject()).commitAllDocuments();
doWriteAction(item);
}
}
};
DataManager.getInstance().getDataContextFromFocus().doWhenDone(new Consumer() {
@Override
public void consume(DataContext dataContext) {
new PopupChooserBuilder(list)
.setTitle(myUseQualifiedImport? PyBundle.message("ACT.qualify.with.module") : PyBundle.message("ACT.from.some.module.import"))
.setItemChoosenCallback(runnable)
.setFilteringEnabled(new Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy