use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyExtractMethodHandler method invokeOnEditor.
private static void invokeOnEditor(final Project project, final Editor editor, final PsiFile file) {
CommonRefactoringUtil.checkReadOnlyStatus(project, file);
PsiElement element1 = null;
PsiElement element2 = null;
final SelectionModel selectionModel = editor.getSelectionModel();
if (selectionModel.hasSelection()) {
element1 = file.findElementAt(selectionModel.getSelectionStart());
element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
} else {
final CaretModel caretModel = editor.getCaretModel();
final Document document = editor.getDocument();
int lineNumber = document.getLineNumber(caretModel.getOffset());
if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
}
}
// Pass comments and whitespaces
element1 = PyPsiUtils.getNextSignificantLeaf(element1, false);
element2 = PyPsiUtils.getPrevSignificantLeaf(element2, false);
if (element1 == null || element2 == null) {
CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.extract.method.error.bad.selection"), RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
return;
}
if (rangeBelongsToSameClassBody(element1, element2)) {
CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.extract.method.error.class.level"), RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
return;
}
final Couple<PsiElement> statements = getStatementsRange(element1, element2);
if (statements != null) {
final ScopeOwner owner = PsiTreeUtil.getParentOfType(statements.getFirst(), ScopeOwner.class);
if (owner == null) {
return;
}
final PyCodeFragment fragment;
try {
fragment = PyCodeFragmentUtil.createCodeFragment(owner, element1, element2);
} catch (CannotCreateCodeFragmentException e) {
CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
return;
}
PyExtractMethodUtil.extractFromStatements(project, editor, fragment, statements.getFirst(), statements.getSecond());
return;
}
final PsiElement expression = PyRefactoringUtil.getSelectedExpression(project, file, element1, element2);
if (expression != null) {
final ScopeOwner owner = PsiTreeUtil.getParentOfType(element1, ScopeOwner.class);
if (owner == null) {
return;
}
final PyCodeFragment fragment;
try {
fragment = PyCodeFragmentUtil.createCodeFragment(owner, element1, element2);
} catch (CannotCreateCodeFragmentException e) {
CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
return;
}
PyExtractMethodUtil.extractFromExpression(project, editor, fragment, expression);
return;
}
CommonRefactoringUtil.showErrorHint(project, editor, PyBundle.message("refactoring.extract.method.error.bad.selection"), RefactoringBundle.message("extract.method.title"), "refactoring.extractMethod");
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyUtil method isInstanceAttribute.
public static boolean isInstanceAttribute(PyExpression target) {
if (!(target instanceof PyTargetExpression)) {
return false;
}
final ScopeOwner owner = ScopeUtil.getScopeOwner(target);
if (owner instanceof PyFunction) {
final PyFunction method = (PyFunction) owner;
if (method.getContainingClass() != null) {
if (method.getStub() != null) {
return true;
}
final PyParameter[] params = method.getParameterList().getParameters();
if (params.length > 0) {
final PyTargetExpression targetExpr = (PyTargetExpression) target;
final PyExpression qualifier = targetExpr.getQualifier();
return qualifier != null && qualifier.getText().equals(params[0].getName());
}
}
}
return false;
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PythonImportUtils method proposeImportFix.
@Nullable
public static AutoImportQuickFix proposeImportFix(final PyElement node, PsiReference reference) {
final String text = reference.getElement().getText();
// text of the part we're working with
final String refText = reference.getRangeInElement().substring(text);
// don't propose meaningless auto imports if no interpreter is configured
final Module module = ModuleUtilCore.findModuleForPsiElement(node);
if (module != null && PythonSdkType.findPythonSdk(module) == null) {
return null;
}
// don't show auto-import fix if we're trying to reference a variable which is defined below in the same scope
ScopeOwner scopeOwner = PsiTreeUtil.getParentOfType(node, ScopeOwner.class);
if (scopeOwner != null && ControlFlowCache.getScope(scopeOwner).containsDeclaration(refText)) {
return null;
}
AutoImportQuickFix fix = addCandidates(node, reference, refText, null);
if (fix != null)
return fix;
final String packageName = PyPackageAliasesProvider.commonImportAliases.get(refText);
if (packageName != null) {
fix = addCandidates(node, reference, packageName, refText);
if (fix != null)
return fix;
}
return null;
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class DeclarationConflictChecker method findDefinitions.
/**
* For each reference in the collection, finds a definition of name visible from the point of the reference. Returns a list of
* such definitions.
* @param name what to look for.
* @param references references to check.
* @param ignored if an element defining the name is also listed here, ignore it.
* @return a list of pairs (referring element, element that defines name).
*/
@NotNull
public static List<Pair<PsiElement, PsiElement>> findDefinitions(@NotNull String name, @NotNull Collection<PsiReference> references, @NotNull Set<PsiElement> ignored) {
final List<Pair<PsiElement, PsiElement>> conflicts = new ArrayList<>();
for (PsiReference ref : references) {
final PsiElement refElement = ref.getElement();
final ScopeOwner owner = ScopeUtil.getScopeOwner(refElement);
if (owner != null) {
for (PsiElement element : PyResolveUtil.resolveLocally(owner, name)) {
if (!ignored.contains(element)) {
conflicts.add(Pair.create(refElement, element));
}
}
}
}
return conflicts;
}
use of com.jetbrains.python.codeInsight.controlflow.ScopeOwner in project intellij-community by JetBrains.
the class PyStdlibTypeProvider method getEnumType.
@Nullable
private static PyType getEnumType(@NotNull PsiElement referenceTarget, @NotNull TypeEvalContext context, @Nullable PsiElement anchor) {
if (referenceTarget instanceof PyTargetExpression) {
final PyTargetExpression target = (PyTargetExpression) referenceTarget;
final ScopeOwner owner = ScopeUtil.getScopeOwner(target);
if (owner instanceof PyClass) {
final PyClass cls = (PyClass) owner;
final List<PyClassLikeType> types = cls.getAncestorTypes(context);
for (PyClassLikeType type : types) {
if (type != null && "enum.Enum".equals(type.getClassQName())) {
final PyType classType = context.getType(cls);
if (classType instanceof PyClassType) {
return ((PyClassType) classType).toInstance();
}
}
}
}
}
if (referenceTarget instanceof PyQualifiedNameOwner) {
final PyQualifiedNameOwner qualifiedNameOwner = (PyQualifiedNameOwner) referenceTarget;
final String name = qualifiedNameOwner.getQualifiedName();
if ("enum.Enum.name".equals(name)) {
return PyBuiltinCache.getInstance(referenceTarget).getStrType();
} else if ("enum.Enum.value".equals(name) && anchor instanceof PyReferenceExpression && context.maySwitchToAST(anchor)) {
final PyReferenceExpression anchorExpr = (PyReferenceExpression) anchor;
final PyExpression qualifier = anchorExpr.getQualifier();
if (qualifier instanceof PyReferenceExpression) {
final PyReferenceExpression qualifierExpr = (PyReferenceExpression) qualifier;
final PsiElement resolvedQualifier = qualifierExpr.getReference().resolve();
if (resolvedQualifier instanceof PyTargetExpression) {
final PyTargetExpression qualifierTarget = (PyTargetExpression) resolvedQualifier;
// Requires switching to AST, we cannot use getType(qualifierTarget) here, because its type is overridden by this type provider
if (context.maySwitchToAST(qualifierTarget)) {
final PyExpression value = qualifierTarget.findAssignedValue();
if (value != null) {
return context.getType(value);
}
}
}
}
} else if ("enum.EnumMeta.__members__".equals(name)) {
return PyTypeParser.getTypeByName(referenceTarget, "dict[str, unknown]");
}
}
return null;
}
Aggregations