mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Add an ability to enable mandatory encoding inspection on Python 3+ (PY-33087)
This commit is contained in:
@@ -19,6 +19,7 @@ import com.intellij.codeInspection.LocalInspectionToolSession;
|
||||
import com.intellij.codeInspection.ProblemsHolder;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.intellij.util.ui.CheckBox;
|
||||
import com.jetbrains.python.PyBundle;
|
||||
import com.jetbrains.python.PythonFileType;
|
||||
import com.jetbrains.python.inspections.quickfix.AddEncodingQuickFix;
|
||||
@@ -29,6 +30,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
@@ -63,7 +65,7 @@ public class PyMandatoryEncodingInspection extends PyInspection {
|
||||
|
||||
@Override
|
||||
public void visitPyFile(PyFile node) {
|
||||
if (!LanguageLevel.forElement(node).isPython2()) return;
|
||||
if (!(myAllPythons || LanguageLevel.forElement(node).isPython2())) return;
|
||||
|
||||
final String charsetString = PythonFileType.getCharsetFromEncodingDeclaration(node);
|
||||
if (charsetString == null) {
|
||||
@@ -78,6 +80,7 @@ public class PyMandatoryEncodingInspection extends PyInspection {
|
||||
|
||||
public String myDefaultEncoding = "utf-8";
|
||||
public int myEncodingFormatIndex = 0;
|
||||
public boolean myAllPythons = false;
|
||||
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
@@ -103,6 +106,10 @@ public class PyMandatoryEncodingInspection extends PyInspection {
|
||||
}
|
||||
});
|
||||
|
||||
return PyEncodingUtil.createEncodingOptionsPanel(defaultEncoding, encodingFormat);
|
||||
final JPanel panel = new JPanel(new BorderLayout());
|
||||
panel.add(new CheckBox("Enable in Python 3+", this, "myAllPythons"), BorderLayout.NORTH);
|
||||
panel.add(PyEncodingUtil.createEncodingOptionsPanel(defaultEncoding, encodingFormat), BorderLayout.CENTER);
|
||||
|
||||
return panel;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<warning descr="No encoding specified for file"></warning>from typing import Any
|
||||
@@ -271,7 +271,16 @@ public class PythonInspectionsTest extends PyTestCase {
|
||||
}
|
||||
|
||||
// PY-32364
|
||||
public void testAddEncodingInPy3() {
|
||||
doHighlightingTest(PyMandatoryEncodingInspection.class, LanguageLevel.PYTHON34);
|
||||
public void testAddEncodingInDisabledPy3() {
|
||||
final PyMandatoryEncodingInspection inspection = new PyMandatoryEncodingInspection();
|
||||
inspection.myAllPythons = false;
|
||||
doHighlightingTest(inspection, LanguageLevel.PYTHON34);
|
||||
}
|
||||
|
||||
// PY-32364
|
||||
public void testAddEncodingInEnabledPy3() {
|
||||
final PyMandatoryEncodingInspection inspection = new PyMandatoryEncodingInspection();
|
||||
inspection.myAllPythons = true;
|
||||
doHighlightingTest(inspection, LanguageLevel.PYTHON34);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user