line_length: Extract inline logic to new config option
This commit extracts the inline mappings logic defined in the previous commit to a separate config option, as suggested by @adrienverge. I'll squash this into the previous commit if the change is accepted. (I named the option slightly differently to what was suggested as I think my proposal reads better without consulting the docs: I'd be happy to reconsider this.)
This commit is contained in:
@@ -85,7 +85,8 @@ class LineLengthTestCase(RuleTestCase):
|
||||
' - https://localhost/very/very/long/url\n'
|
||||
'...\n', conf)
|
||||
self.check('---\n'
|
||||
'long_line: http://localhost/very/very/long/url\n', conf)
|
||||
'long_line: http://localhost/very/very/long/url\n', conf,
|
||||
problem=(2, 21))
|
||||
|
||||
conf = 'line-length: {max: 20, allow-non-breakable-words: no}'
|
||||
self.check('---\n' + 30 * 'A' + '\n', conf, problem=(2, 21))
|
||||
@@ -111,3 +112,8 @@ class LineLengthTestCase(RuleTestCase):
|
||||
self.check('---\n'
|
||||
'long_line: http://localhost/very/very/long/url\n'
|
||||
'...\n', conf, problem=(2, 21))
|
||||
|
||||
conf = 'line-length: {max: 20, ' \
|
||||
'allow-non-breakable-inline-mappings: yes}'
|
||||
self.check('---\n'
|
||||
'long_line: http://localhost/very/very/long/url\n', conf)
|
||||
|
||||
@@ -38,6 +38,7 @@ rules:
|
||||
line-length:
|
||||
max: 80
|
||||
allow-non-breakable-words: yes
|
||||
allow-non-breakable-inline-mappings: no
|
||||
new-line-at-end-of-file: enable
|
||||
new-lines:
|
||||
type: unix
|
||||
|
||||
@@ -25,3 +25,4 @@ rules:
|
||||
indent-sequences: consistent
|
||||
line-length:
|
||||
level: warning
|
||||
allow-non-breakable-inline-mappings: yes
|
||||
|
||||
@@ -23,6 +23,8 @@ Use this rule to set a limit to lines length.
|
||||
* ``allow-non-breakable-words`` is used to allow non breakable words (without
|
||||
spaces inside) to overflow the limit. This is useful for long URLs, for
|
||||
instance. Use ``yes`` to allow, ``no`` to forbid.
|
||||
* ``allow-non-breakable-inline-mappings`` implies ``allow-non-breakable-words``
|
||||
and extends it to also allow non-breakable words in inline mappings.
|
||||
|
||||
.. rubric:: Examples
|
||||
|
||||
@@ -61,6 +63,19 @@ Use this rule to set a limit to lines length.
|
||||
|
||||
- this line is waaaaaaaaaaaaaay too long but could be easily split...
|
||||
|
||||
and the following code snippet would also **FAIL**:
|
||||
::
|
||||
|
||||
- foobar: http://localhost/very/very/very/very/very/very/very/very/long/url
|
||||
|
||||
#. With ``line-length: {max: 60, allow-non-breakable-words: yes,
|
||||
allow-non-breakable-inline-mappings: yes}``
|
||||
|
||||
the following code snippet would **PASS**:
|
||||
::
|
||||
|
||||
- foobar: http://localhost/very/very/very/very/very/very/very/very/long/url
|
||||
|
||||
#. With ``line-length: {max: 60, allow-non-breakable-words: no}``
|
||||
|
||||
the following code snippet would **FAIL**:
|
||||
@@ -80,11 +95,14 @@ from yamllint.linter import LintProblem
|
||||
ID = 'line-length'
|
||||
TYPE = 'line'
|
||||
CONF = {'max': int,
|
||||
'allow-non-breakable-words': bool}
|
||||
'allow-non-breakable-words': bool,
|
||||
'allow-non-breakable-inline-mappings': bool}
|
||||
|
||||
|
||||
def check(conf, line):
|
||||
if line.end - line.start > conf['max']:
|
||||
conf['allow-non-breakable-words'] |= \
|
||||
conf['allow-non-breakable-inline-mappings']
|
||||
if conf['allow-non-breakable-words']:
|
||||
start = line.start
|
||||
while start < line.end and line.buffer[start] == ' ':
|
||||
@@ -97,10 +115,11 @@ def check(conf, line):
|
||||
if line.buffer.find(' ', start, line.end) == -1:
|
||||
return
|
||||
|
||||
line_yaml = yaml.safe_load(line.content)
|
||||
if (isinstance(line_yaml, dict) and
|
||||
' ' not in line_yaml.popitem()[1]):
|
||||
return
|
||||
if conf['allow-non-breakable-inline-mappings']:
|
||||
line_yaml = yaml.safe_load(line.content)
|
||||
if (isinstance(line_yaml, dict) and
|
||||
' ' not in line_yaml.popitem()[1]):
|
||||
return
|
||||
|
||||
yield LintProblem(line.line_no, conf['max'] + 1,
|
||||
'line too long (%d > %d characters)' %
|
||||
|
||||
Reference in New Issue
Block a user