2 * Copyright 2000-2014 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.inspections;
18 import com.intellij.codeInspection.LocalInspectionToolSession;
19 import com.intellij.codeInspection.LocalQuickFix;
20 import com.intellij.codeInspection.ProblemDescriptor;
21 import com.intellij.codeInspection.ProblemsHolder;
22 import com.intellij.openapi.module.Module;
23 import com.intellij.openapi.module.ModuleUtilCore;
24 import com.intellij.openapi.options.ShowSettingsUtil;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.projectRoots.Sdk;
27 import com.intellij.psi.PsiElementVisitor;
28 import com.intellij.util.PlatformUtils;
29 import com.jetbrains.python.PyBundle;
30 import com.jetbrains.python.psi.PyFile;
31 import com.jetbrains.python.sdk.PythonSdkType;
32 import org.jetbrains.annotations.Nls;
33 import org.jetbrains.annotations.NotNull;
34 import org.jetbrains.annotations.Nullable;
40 public class PyInterpreterInspection extends PyInspection {
44 public String getDisplayName() {
45 return PyBundle.message("INSP.NAME.invalid.interpreter");
50 public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder,
51 final boolean isOnTheFly,
52 @NotNull final LocalInspectionToolSession session) {
53 return new Visitor(holder, session);
56 public static class Visitor extends PyInspectionVisitor {
58 public Visitor(@Nullable ProblemsHolder holder,
59 @NotNull LocalInspectionToolSession session) {
60 super(holder, session);
64 public void visitPyFile(PyFile node) {
65 super.visitPyFile(node);
66 if (PlatformUtils.isPyCharm()) {
67 final Module module = ModuleUtilCore.findModuleForPsiElement(node);
69 final Sdk sdk = PythonSdkType.findPythonSdk(module);
71 registerProblem(node, "No Python interpreter configured for the project", new ConfigureInterpreterFix());
73 else if (PythonSdkType.isInvalid(sdk)) {
74 registerProblem(node, "Invalid Python interpreter selected for the project", new ConfigureInterpreterFix());
81 private static class ConfigureInterpreterFix implements LocalQuickFix {
85 public String getFamilyName() {
86 return "Configure Python Interpreter";
90 public boolean startInWriteAction() {
95 public void applyFix(@NotNull final Project project, @NotNull ProblemDescriptor descriptor) {
96 ShowSettingsUtil.getInstance().showSettingsDialog(project, "Project Interpreter");