Search in sources :

Example 1 with ReformatFix

use of com.jetbrains.python.inspections.quickfix.ReformatFix in project intellij-community by JetBrains.

the class Pep8ExternalAnnotator method apply.

@Override
public void apply(@NotNull PsiFile file, Results annotationResult, @NotNull AnnotationHolder holder) {
    if (annotationResult == null || !file.isValid())
        return;
    final String text = file.getText();
    Project project = file.getProject();
    final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    for (Problem problem : annotationResult.problems) {
        final int line = problem.myLine - 1;
        final int column = problem.myColumn - 1;
        int offset;
        if (document != null) {
            offset = line >= document.getLineCount() ? document.getTextLength() - 1 : document.getLineStartOffset(line) + column;
        } else {
            offset = StringUtil.lineColToOffset(text, line, column);
        }
        PsiElement problemElement = file.findElementAt(offset);
        // E3xx - blank lines warnings
        if (!(problemElement instanceof PsiWhiteSpace) && problem.myCode.startsWith("E3")) {
            final PsiElement elementBefore = file.findElementAt(Math.max(0, offset - 1));
            if (elementBefore instanceof PsiWhiteSpace) {
                problemElement = elementBefore;
            }
        }
        // W292 no newline at end of file
        if (problemElement == null && document != null && offset == document.getTextLength() && problem.myCode.equals("W292")) {
            problemElement = file.findElementAt(Math.max(0, offset - 1));
        }
        if (ignoreDueToSettings(project, problem, problemElement) || ignoredDueToProblemSuppressors(problem, file, problemElement)) {
            continue;
        }
        if (problemElement != null) {
            // TODO Remove: a workaround until the bundled pycodestyle.py supports Python 3.6 variable annotations by itself
            if (problem.myCode.equals("E701") && problemElement.getNode().getElementType() == PyTokenTypes.COLON && problemElement.getParent() instanceof PyAnnotation) {
                continue;
            }
            TextRange problemRange = problemElement.getTextRange();
            // So we register it only on that line where pycodestyle.py found the problem originally.
            if (crossesLineBoundary(document, text, problemRange)) {
                final int lineEndOffset;
                if (document != null) {
                    lineEndOffset = line >= document.getLineCount() ? document.getTextLength() - 1 : document.getLineEndOffset(line);
                } else {
                    lineEndOffset = StringUtil.lineColToOffset(text, line + 1, 0) - 1;
                }
                if (offset > lineEndOffset) {
                    // PSI/document don't match, don't try to highlight random places
                    continue;
                }
                problemRange = new TextRange(offset, lineEndOffset);
            }
            final Annotation annotation;
            final boolean inInternalMode = ApplicationManager.getApplication().isInternal();
            final String message = "PEP 8: " + (inInternalMode ? problem.myCode + " " : "") + problem.myDescription;
            if (annotationResult.level == HighlightDisplayLevel.ERROR) {
                annotation = holder.createErrorAnnotation(problemRange, message);
            } else if (annotationResult.level == HighlightDisplayLevel.WARNING) {
                annotation = holder.createWarningAnnotation(problemRange, message);
            } else {
                annotation = holder.createWeakWarningAnnotation(problemRange, message);
            }
            if (problem.myCode.equals("E401")) {
                annotation.registerUniversalFix(new OptimizeImportsQuickFix(), null, null);
            } else if (problem.myCode.equals("W391")) {
                annotation.registerUniversalFix(new RemoveTrailingBlankLinesFix(), null, null);
            } else if (problem.myCode.equals("E501")) {
                annotation.registerFix(new PyFillParagraphFix());
            } else {
                annotation.registerUniversalFix(new ReformatFix(), null, null);
            }
            annotation.registerFix(new IgnoreErrorFix(problem.myCode));
            annotation.registerFix(new CustomEditInspectionToolsSettingsAction(HighlightDisplayKey.find(PyPep8Inspection.INSPECTION_SHORT_NAME), () -> "Edit inspection profile setting"));
        }
    }
}
Also used : TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) Annotation(com.intellij.lang.annotation.Annotation) RemoveTrailingBlankLinesFix(com.jetbrains.python.inspections.quickfix.RemoveTrailingBlankLinesFix) Project(com.intellij.openapi.project.Project) PyFillParagraphFix(com.jetbrains.python.inspections.quickfix.PyFillParagraphFix) OptimizeImportsQuickFix(com.jetbrains.python.codeInsight.imports.OptimizeImportsQuickFix) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) ReformatFix(com.jetbrains.python.inspections.quickfix.ReformatFix) CustomEditInspectionToolsSettingsAction(com.intellij.codeInspection.ex.CustomEditInspectionToolsSettingsAction)

Aggregations

CustomEditInspectionToolsSettingsAction (com.intellij.codeInspection.ex.CustomEditInspectionToolsSettingsAction)1 Annotation (com.intellij.lang.annotation.Annotation)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 OptimizeImportsQuickFix (com.jetbrains.python.codeInsight.imports.OptimizeImportsQuickFix)1 PyFillParagraphFix (com.jetbrains.python.inspections.quickfix.PyFillParagraphFix)1 ReformatFix (com.jetbrains.python.inspections.quickfix.ReformatFix)1 RemoveTrailingBlankLinesFix (com.jetbrains.python.inspections.quickfix.RemoveTrailingBlankLinesFix)1