Rules: Keep a persistent context for token rules
This will be needed to build a clean indentation checking algorithm.
This commit is contained in:
@@ -38,12 +38,17 @@ def get_costemic_problems(buffer, conf):
|
|||||||
token_rules = [r for r in rules if r.TYPE == 'token']
|
token_rules = [r for r in rules if r.TYPE == 'token']
|
||||||
line_rules = [r for r in rules if r.TYPE == 'line']
|
line_rules = [r for r in rules if r.TYPE == 'line']
|
||||||
|
|
||||||
|
context = {}
|
||||||
|
for rule in token_rules:
|
||||||
|
context[rule.ID] = {}
|
||||||
|
|
||||||
for elem in parser.token_or_line_generator(buffer):
|
for elem in parser.token_or_line_generator(buffer):
|
||||||
if isinstance(elem, parser.Token):
|
if isinstance(elem, parser.Token):
|
||||||
for rule in token_rules:
|
for rule in token_rules:
|
||||||
rule_conf = conf[rule.ID]
|
rule_conf = conf[rule.ID]
|
||||||
for problem in rule.check(rule_conf,
|
for problem in rule.check(rule_conf,
|
||||||
elem.curr, elem.prev, elem.next):
|
elem.curr, elem.prev, elem.next,
|
||||||
|
context[rule.ID]):
|
||||||
problem.rule = rule.ID
|
problem.rule = rule.ID
|
||||||
problem.level = rule_conf['level']
|
problem.level = rule_conf['level']
|
||||||
yield problem
|
yield problem
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ CONF = {'min-spaces-inside': int,
|
|||||||
'max-spaces-inside': int}
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if isinstance(token, yaml.FlowMappingStartToken):
|
if isinstance(token, yaml.FlowMappingStartToken):
|
||||||
problem = spaces_after(token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
min=conf['min-spaces-inside'],
|
min=conf['min-spaces-inside'],
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ CONF = {'min-spaces-inside': int,
|
|||||||
'max-spaces-inside': int}
|
'max-spaces-inside': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if isinstance(token, yaml.FlowSequenceStartToken):
|
if isinstance(token, yaml.FlowSequenceStartToken):
|
||||||
problem = spaces_after(token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
min=conf['min-spaces-inside'],
|
min=conf['min-spaces-inside'],
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ CONF = {'max-spaces-before': int,
|
|||||||
'max-spaces-after': int}
|
'max-spaces-after': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if isinstance(token, yaml.ValueToken):
|
if isinstance(token, yaml.ValueToken):
|
||||||
problem = spaces_before(token, prev, next,
|
problem = spaces_before(token, prev, next,
|
||||||
max=conf['max-spaces-before'],
|
max=conf['max-spaces-before'],
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ CONF = {'max-spaces-before': int,
|
|||||||
'max-spaces-after': int}
|
'max-spaces-after': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if isinstance(token, yaml.FlowEntryToken):
|
if isinstance(token, yaml.FlowEntryToken):
|
||||||
problem = spaces_before(token, prev, next,
|
problem = spaces_before(token, prev, next,
|
||||||
max=conf['max-spaces-before'],
|
max=conf['max-spaces-before'],
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ CONF = {'require-starting-space': bool,
|
|||||||
'min-spaces-from-content': int}
|
'min-spaces-from-content': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
for comment in get_comments_between_tokens(token, next):
|
for comment in get_comments_between_tokens(token, next):
|
||||||
if (conf['min-spaces-from-content'] != -1 and
|
if (conf['min-spaces-from-content'] != -1 and
|
||||||
not isinstance(token, yaml.StreamStartToken) and
|
not isinstance(token, yaml.StreamStartToken) and
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ TYPE = 'token'
|
|||||||
# # commented line 2
|
# # commented line 2
|
||||||
# current: line
|
# current: line
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if prev is None:
|
if prev is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ TYPE = 'token'
|
|||||||
CONF = {'present': bool}
|
CONF = {'present': bool}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if conf['present']:
|
if conf['present']:
|
||||||
if (isinstance(token, yaml.StreamEndToken) and
|
if (isinstance(token, yaml.StreamEndToken) and
|
||||||
not (isinstance(prev, yaml.DocumentEndToken) or
|
not (isinstance(prev, yaml.DocumentEndToken) or
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ TYPE = 'token'
|
|||||||
CONF = {'present': bool}
|
CONF = {'present': bool}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if conf['present']:
|
if conf['present']:
|
||||||
if (isinstance(prev, (yaml.StreamStartToken,
|
if (isinstance(prev, (yaml.StreamStartToken,
|
||||||
yaml.DocumentEndToken,
|
yaml.DocumentEndToken,
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ TYPE = 'token'
|
|||||||
CONF = {'max-spaces-after': int}
|
CONF = {'max-spaces-after': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if isinstance(token, yaml.BlockEntryToken):
|
if isinstance(token, yaml.BlockEntryToken):
|
||||||
problem = spaces_after(token, prev, next,
|
problem = spaces_after(token, prev, next,
|
||||||
max=conf['max-spaces-after'],
|
max=conf['max-spaces-after'],
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ TYPE = 'token'
|
|||||||
CONF = {'spaces': int}
|
CONF = {'spaces': int}
|
||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next):
|
def check(conf, token, prev, next, context):
|
||||||
if isinstance(token, (yaml.StreamStartToken, yaml.StreamEndToken)):
|
if isinstance(token, (yaml.StreamStartToken, yaml.StreamEndToken)):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user