2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.jetbrains.python.codeInsight.intentions;
18 import com.intellij.codeInsight.controlflow.ControlFlow;
19 import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
20 import com.intellij.openapi.editor.Editor;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.psi.PsiElement;
23 import com.intellij.psi.PsiFile;
24 import com.intellij.psi.PsiReference;
25 import com.intellij.psi.util.PsiTreeUtil;
26 import com.intellij.util.IncorrectOperationException;
27 import com.jetbrains.python.PyBundle;
28 import com.jetbrains.python.codeInsight.controlflow.ControlFlowCache;
29 import com.jetbrains.python.codeInsight.controlflow.ScopeOwner;
30 import com.jetbrains.python.psi.PyFile;
31 import com.jetbrains.python.psi.PyFunction;
32 import com.jetbrains.python.psi.PyUtil;
33 import org.jetbrains.annotations.Nls;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
38 * @author Mikhail Golubev
40 public class PyConvertLocalFunctionToTopLevelFunction extends BaseIntentionAction {
44 public String getFamilyName() {
45 return PyBundle.message("INTN.convert.local.function.to.top.level.function");
49 public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
50 final PyFunction nestedFunction = findNestedFunctionUnderCaret(editor, file);
51 return nestedFunction != null;
55 private static PyFunction findNestedFunctionUnderCaret(Editor editor, PsiFile file) {
56 if (!(file instanceof PyFile)) return null;
57 final PsiElement element = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());
58 if (element == null) {
61 if (isLocalFunction(element.getParent()) && ((PyFunction)element.getParent()).getNameIdentifier() == element) {
62 return (PyFunction)element.getParent();
64 final PsiReference reference = element.getReference();
65 if (reference == null) {
68 final PsiElement resolved = reference.resolve();
69 if (isLocalFunction(resolved)) {
70 return (PyFunction)resolved;
75 private static boolean isLocalFunction(@Nullable PsiElement resolved) {
76 if (resolved instanceof PyFunction && PsiTreeUtil.getParentOfType(resolved, ScopeOwner.class, true) instanceof PyFunction) {
83 public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
84 final PyFunction function = findNestedFunctionUnderCaret(editor, file);
85 assert function != null;
86 final ControlFlow flow = ControlFlowCache.getControlFlow(function);