PY-19705 Add an option for blanks between class header and its first method

On the whole, we don't touch these blank lines (as opposed to deleting
them previously) unless they exceed the limit specified in settings
(Keep maximum blank lines in declarations).
If someone wants to always put a blank line before the first method,
as in PY-1765, they can use this new option.
This commit is contained in:
Mikhail Golubev
2017-09-04 16:41:35 +03:00
parent 66dfdf1442
commit 30c586b5a1
22 changed files with 9 additions and 22 deletions
@@ -1031,6 +1031,7 @@ formatter.before.hash = Before '#'
formatter.after.hash = After '#'
formatter.around.top.level.imports = After top-level imports:
formatter.after.local.imports=After local imports:
formatter.before.first.method=Before the first method:
formatter.around.top.level.classes.and.function=Around top-level classes and functions:
formatter.around.multiplicative.operators=Multiplicative operators (*, @, /, %)
formatter.around.power.operator=Power operator (**)
@@ -737,7 +737,7 @@ public class PyBlock implements ASTBlock {
if (myNode.getElementType() == PyElementTypes.CLASS_DECLARATION) {
final PyStatement[] statements = ((PyStatementList)psi2).getStatements();
if (statements.length > 0 && statements[0] instanceof PyFunction) {
return getBlankLinesForOption(settings.BLANK_LINES_AROUND_METHOD);
return getBlankLinesForOption(pySettings.BLANK_LINES_BEFORE_FIRST_METHOD);
}
}
if (childType1 == PyTokenTypes.COLON && needLineBreakInStatement()) {
@@ -816,9 +816,9 @@ public class PyBlock implements ASTBlock {
}
@NotNull
private Spacing getBlankLinesForOption(int option) {
final int blankLines = option + 1;
return Spacing.createSpacing(0, 0, blankLines,
private Spacing getBlankLinesForOption(int minBlankLines) {
final int lineFeeds = minBlankLines + 1;
return Spacing.createSpacing(0, 0, lineFeeds,
myContext.getSettings().KEEP_LINE_BREAKS,
myContext.getSettings().KEEP_BLANK_LINES_IN_DECLARATIONS);
}
@@ -63,6 +63,7 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings {
public boolean SPACE_BEFORE_BACKSLASH = true;
public int BLANK_LINES_AROUND_TOP_LEVEL_CLASSES_FUNCTIONS = 2;
public int BLANK_LINES_BEFORE_FIRST_METHOD = 0;
// TODO make boolean (it needs special editor in BlankLinesPanel)
public boolean BLANK_LINE_AT_FILE_END = true;
@@ -102,6 +102,8 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin
PyBundle.message("formatter.around.top.level.classes.and.function"), BLANK_LINES);
consumer.showCustomOption(PyCodeStyleSettings.class, "BLANK_LINES_AFTER_LOCAL_IMPORTS",
PyBundle.message("formatter.after.local.imports"), BLANK_LINES);
consumer.showCustomOption(PyCodeStyleSettings.class, "BLANK_LINES_BEFORE_FIRST_METHOD",
PyBundle.message("formatter.before.first.method"), BLANK_LINES);
}
else if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) {
consumer.showStandardOptions("RIGHT_MARGIN",
@@ -2,7 +2,6 @@ from unittest import TestCase
class Spam(TestCase):
def eggs(self):
self.fail()
@@ -1,5 +1,4 @@
class Checkpoints(webapp2.RequestHandler):
def get(self):
self.response.write(json.dumps({"meta": {"code": 400,
"errorType": "paramError",
@@ -2,6 +2,5 @@ from unittest import TestCase
class MyTest(TestCase):
def test_pass(self):
self.assertEqual(1 + 1, 2)
@@ -1,5 +1,4 @@
class Adjunct:
def apply(self, right, arg):
pass
@@ -1,5 +1,4 @@
class C:
def foo(self):
pass
@@ -1,5 +1,4 @@
class T1(object):
def m1(self):
pass
@@ -7,6 +6,5 @@ class T1(object):
# comment about T2
class T2(object):
def m2(self):
pass
@@ -1,5 +1,4 @@
class A(object):
def foo(self):
pass
@@ -1,5 +1,4 @@
class A:
def y(self):
pass
@@ -1,5 +1,4 @@
class Ancestor(object):
def __init__(self, a, b):
self.a = a
self.b = b
@@ -3,7 +3,6 @@ from shared_module import module_function as my_function, ModuleClass
class NewParent(object):
def do_useful_stuff(self):
i = shared_module.MODULE_CONTANT
my_function()
@@ -1,5 +1,4 @@
class Parent(object):
def __init__(self):
self.eggs = 12
@@ -7,7 +7,6 @@ abstractmethod()
class NewParent(metaclass=ABCMeta):
@classmethod
@abstractmethod
def foo_method(cls):
@@ -3,6 +3,5 @@ A = 1
class Suppa:
def foo(self):
print "bar"
@@ -1,4 +1,3 @@
class Suppa:
def foo(self):
print "bar"
@@ -1,5 +1,4 @@
class Parent(object):
def spam(self):
pass
@@ -1,5 +1,4 @@
class Suppa:
def foo(self):
print "bar"
@@ -1,5 +1,4 @@
class Suppa(object):
def foo(self):
print "bar"
@@ -837,6 +837,7 @@ public class PyFormatterTest extends PyTestCase {
// PY-19705
public void testBlankLinesAroundFirstMethod() {
getPythonCodeStyleSettings().BLANK_LINES_BEFORE_FIRST_METHOD = 1;
doTest();
}