Search in sources :

Example 1 with PyCodeStyleSettings

use of com.jetbrains.python.formatter.PyCodeStyleSettings in project intellij-community by JetBrains.

the class Pep8ExternalAnnotator method collectInformation.

@Nullable
@Override
public State collectInformation(@NotNull PsiFile file) {
    VirtualFile vFile = file.getVirtualFile();
    if (vFile == null || vFile.getFileType() != PythonFileType.INSTANCE) {
        return null;
    }
    Sdk sdk = PythonSdkType.findLocalCPython(ModuleUtilCore.findModuleForPsiElement(file));
    if (sdk == null) {
        if (!myReportedMissingInterpreter) {
            myReportedMissingInterpreter = true;
            reportMissingInterpreter();
        }
        return null;
    }
    final String homePath = sdk.getHomePath();
    if (homePath == null) {
        if (!myReportedMissingInterpreter) {
            myReportedMissingInterpreter = true;
            LOG.info("Could not find home path for interpreter " + homePath);
        }
        return null;
    }
    final InspectionProfile profile = InspectionProjectProfileManager.getInstance(file.getProject()).getCurrentProfile();
    final HighlightDisplayKey key = HighlightDisplayKey.find(PyPep8Inspection.INSPECTION_SHORT_NAME);
    if (!profile.isToolEnabled(key, file)) {
        return null;
    }
    if (file instanceof PyFileImpl && !((PyFileImpl) file).isAcceptedFor(PyPep8Inspection.class)) {
        return null;
    }
    final PyPep8Inspection inspection = (PyPep8Inspection) profile.getUnwrappedTool(PyPep8Inspection.KEY.toString(), file);
    final CodeStyleSettings commonSettings = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings();
    final PyCodeStyleSettings customSettings = commonSettings.getCustomSettings(PyCodeStyleSettings.class);
    final List<String> ignoredErrors = Lists.newArrayList(inspection.ignoredErrors);
    if (!customSettings.SPACE_AFTER_NUMBER_SIGN) {
        // Block comment should start with a space
        ignoredErrors.add("E262");
        // Inline comment should start with a space
        ignoredErrors.add("E265");
    }
    if (!customSettings.SPACE_BEFORE_NUMBER_SIGN) {
        // At least two spaces before inline comment
        ignoredErrors.add("E261");
    }
    final int margin = commonSettings.getRightMargin(file.getLanguage());
    return new State(homePath, file.getText(), profile.getErrorLevel(key, file), ignoredErrors, margin, customSettings.HANG_CLOSING_BRACKETS);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) PyCodeStyleSettings(com.jetbrains.python.formatter.PyCodeStyleSettings) InspectionProfile(com.intellij.codeInspection.InspectionProfile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) PyCodeStyleSettings(com.jetbrains.python.formatter.PyCodeStyleSettings) Sdk(com.intellij.openapi.projectRoots.Sdk) PyFileImpl(com.jetbrains.python.psi.impl.PyFileImpl) PyPep8Inspection(com.jetbrains.python.inspections.PyPep8Inspection) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PyCodeStyleSettings

use of com.jetbrains.python.formatter.PyCodeStyleSettings in project intellij-community by JetBrains.

the class Pep8ExternalAnnotator method ignoreDueToSettings.

private static boolean ignoreDueToSettings(Project project, Problem problem, @Nullable PsiElement element) {
    final EditorSettingsExternalizable editorSettings = EditorSettingsExternalizable.getInstance();
    if (!editorSettings.getStripTrailingSpaces().equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE)) {
        // ignore trailing spaces errors if they're going to disappear after save
        if (problem.myCode.equals("W291") || problem.myCode.equals("W293")) {
            return true;
        }
    }
    final CodeStyleSettings codeStyleSettings = CodeStyleSettingsManager.getSettings(project);
    final CommonCodeStyleSettings commonSettings = codeStyleSettings.getCommonSettings(PythonLanguage.getInstance());
    final PyCodeStyleSettings pySettings = codeStyleSettings.getCustomSettings(PyCodeStyleSettings.class);
    if (element instanceof PsiWhiteSpace) {
        // E303 too many blank lines (num)
        if (problem.myCode.equals("E303")) {
            final Matcher matcher = E303_LINE_COUNT_PATTERN.matcher(problem.myDescription);
            if (matcher.matches()) {
                final int reportedBlanks = Integer.parseInt(matcher.group(1));
                final PsiElement nonWhitespaceAfter = PyPsiUtils.getNextNonWhitespaceSibling(element);
                final PsiElement nonWhitespaceBefore = PyPsiUtils.getPrevNonWhitespaceSibling(element);
                final boolean classNearby = nonWhitespaceBefore instanceof PyClass || nonWhitespaceAfter instanceof PyClass;
                final boolean functionNearby = nonWhitespaceBefore instanceof PyFunction || nonWhitespaceAfter instanceof PyFunction;
                if (functionNearby || classNearby) {
                    if (PyUtil.isTopLevel(element)) {
                        if (reportedBlanks <= pySettings.BLANK_LINES_AROUND_TOP_LEVEL_CLASSES_FUNCTIONS) {
                            return true;
                        }
                    } else {
                        // Blanks around classes have priority over blanks around functions as defined in Python spacing builder
                        if (classNearby && reportedBlanks <= commonSettings.BLANK_LINES_AROUND_CLASS || functionNearby && reportedBlanks <= commonSettings.BLANK_LINES_AROUND_METHOD) {
                            return true;
                        }
                    }
                }
            }
        }
        // Note that E222 (multiple spaces after operator) is not suppressed, though. 
        if (problem.myCode.equals("E251") && (element.getParent() instanceof PyParameter && pySettings.SPACE_AROUND_EQ_IN_NAMED_PARAMETER || element.getParent() instanceof PyKeywordArgument && pySettings.SPACE_AROUND_EQ_IN_KEYWORD_ARGUMENT)) {
            return true;
        }
    }
    // thus underlying PSI element is not necessarily a whitespace
    if (problem.myCode.equals("W191") && codeStyleSettings.useTabCharacter(PythonFileType.INSTANCE)) {
        return true;
    }
    return false;
}
Also used : Matcher(java.util.regex.Matcher) EditorSettingsExternalizable(com.intellij.openapi.editor.ex.EditorSettingsExternalizable) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) PyCodeStyleSettings(com.jetbrains.python.formatter.PyCodeStyleSettings) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) PyCodeStyleSettings(com.jetbrains.python.formatter.PyCodeStyleSettings) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Aggregations

CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)2 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)2 PyCodeStyleSettings (com.jetbrains.python.formatter.PyCodeStyleSettings)2 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)1 InspectionProfile (com.intellij.codeInspection.InspectionProfile)1 EditorSettingsExternalizable (com.intellij.openapi.editor.ex.EditorSettingsExternalizable)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 PyPep8Inspection (com.jetbrains.python.inspections.PyPep8Inspection)1 PyFileImpl (com.jetbrains.python.psi.impl.PyFileImpl)1 Matcher (java.util.regex.Matcher)1 Nullable (org.jetbrains.annotations.Nullable)1