use of com.jetbrains.python.inspections.quickfix.RemoveTrailingBlankLinesFix 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"));
}
}
}
Aggregations