diff --git a/vsg/rules/__init__.py b/vsg/rules/__init__.py index b9ad941f..289cd5d6 100644 --- a/vsg/rules/__init__.py +++ b/vsg/rules/__init__.py @@ -23,6 +23,7 @@ from .space_between_items_rule import space_between_items_rule from .insert_item_after_item_rule import insert_item_after_item_rule from .copy_item_value_and_insert_new_item_after_item_rule import copy_item_value_and_insert_new_item_after_item_rule from .move_items_after_item_to_next_line_rule import move_items_after_item_to_next_line_rule +from .move_item_and_items_to_the_right_to_next_line_rule import move_item_and_items_to_the_right_to_next_line_rule from vsg.rules import after from vsg.rules import architecture diff --git a/vsg/rules/context/__init__.py b/vsg/rules/context/__init__.py index c80bad7d..ae30d6f3 100644 --- a/vsg/rules/context/__init__.py +++ b/vsg/rules/context/__init__.py @@ -5,7 +5,7 @@ from .rule_004 import rule_004 from .rule_005 import rule_005 from .rule_006 import rule_006 from .rule_007 import rule_007 - +from .rule_008 import rule_008 from .rule_009 import rule_009 from .rule_010 import rule_010 from .rule_011 import rule_011 diff --git a/vsg/rules/context/rule_008.py b/vsg/rules/context/rule_008.py new file mode 100644 index 00000000..91345c1b --- /dev/null +++ b/vsg/rules/context/rule_008.py @@ -0,0 +1,13 @@ + +from vsg import parser +from vsg.rules import move_item_and_items_to_the_right_to_next_line_rule + + +class rule_008(move_item_and_items_to_the_right_to_next_line_rule): + ''' + Moves code after the is keyword to the next line. + + ''' + + def __init__(self): + move_item_and_items_to_the_right_to_next_line_rule.__init__(self, 'context', '008', parser.context_end_keyword) diff --git a/vsg/rules/move_item_and_items_to_the_right_to_next_line_rule.py b/vsg/rules/move_item_and_items_to_the_right_to_next_line_rule.py new file mode 100644 index 00000000..7613bbfd --- /dev/null +++ b/vsg/rules/move_item_and_items_to_the_right_to_next_line_rule.py @@ -0,0 +1,71 @@ + +import copy + +from vsg import rule +from vsg import parser +from vsg import utils + + +class move_item_and_items_to_the_right_to_next_line_rule(rule.rule): + ''' + Splits the line at items and moves items after it to the next line. + + Parameters + ---------- + + name : string + The group the rule belongs to. + + identifier : string + unique identifier. Usually in the form of 00N. + + sObjectType : string + The object to check the space after. + + Attributes + ---------- + + self.phase : integer = 2 + Sets the phase the rule will run in. + + self.solution : string = None + Instructions on how to fix the violation. + ''' + + def __init__(self, name, identifier, trigger): + rule.rule.__init__(self, name=name, identifier=identifier) + self.phase = 1 + self.subphase = 1 + self.solution = None + self.trigger = trigger + + def analyze(self, oFile): + lContexts = oFile.get_context_declarations() + for dContext in lContexts: + bFound = False + bBreak = False + for iLine, oLine in enumerate(dContext['lines']): + lObjects = oLine.get_objects() + for iObject, oObject in enumerate(lObjects): + if iObject > 1: + if isinstance(oObject, self.trigger): + dViolation = utils.create_violation_dict(dContext['metadata']['iStartLineNumber'] + iLine) + dViolation['iObject'] = iObject + self.add_violation(dViolation) + + + def _fix_violations(self, oFile): + for dViolation in self.violations[::-1]: + oLine = utils.get_violating_line(oFile, dViolation) + oNewLine = copy.deepcopy(oLine) + lObjects = oLine.get_objects() + iLineNumber = utils.get_violation_line_number(dViolation) + + lOldObjects = lObjects[:dViolation['iObject']] + lNewObjects = lObjects[dViolation['iObject']:] + oLine.update_objects(lOldObjects) + oNewLine.update_objects(lNewObjects) + oFile.insert_line(iLineNumber + 1, oNewLine) + + def _get_solution(self): + return 'Move word ' + self.tigger.get_value() + ' and code to the right to the next line.' diff --git a/vsg/tests/context/rule_008_test_input.fixed.vhd b/vsg/tests/context/rule_008_test_input.fixed.vhd new file mode 100644 index 00000000..491782d1 --- /dev/null +++ b/vsg/tests/context/rule_008_test_input.fixed.vhd @@ -0,0 +1,53 @@ + +context c1 is + library ieee; +end context c1; + +-- try multiline +context c1 +is + library ieee; +end context c1; + +context +c1 +is library ieee; +end context +c1 +; + +context +c1 +is + library ieee; +end +; + +context +c1 +is + library ieee; +end --comment +context +; + +-- Try single line +context c1 is library ieee; use ieee.std_logic_1164; +end; + +context c1 is library ieee; use ieee.std_logic_1164; +end context; + +context c1 is library ieee; use ieee.std_logic_1164; +end context c1; + +-- Check comments + +context -- comment 1 +c1 -- comment 2 +is -- comment 3 + library ieee; -- comment 4 + end -- comment 5 +context -- comment 6 +c1 -- comment 7 +; -- comment 8 diff --git a/vsg/tests/context/rule_008_test_input.vhd b/vsg/tests/context/rule_008_test_input.vhd new file mode 100644 index 00000000..bd2383a8 --- /dev/null +++ b/vsg/tests/context/rule_008_test_input.vhd @@ -0,0 +1,46 @@ + +context c1 is + library ieee; +end context c1; + +-- try multiline +context c1 +is + library ieee; end context c1; + +context +c1 +is library ieee; end context +c1 +; + +context +c1 +is + library ieee; end +; + +context +c1 +is + library ieee; end --comment +context +; + +-- Try single line +context c1 is library ieee; use ieee.std_logic_1164; end; + +context c1 is library ieee; use ieee.std_logic_1164; end context; + +context c1 is library ieee; use ieee.std_logic_1164; end context c1; + +-- Check comments + +context -- comment 1 +c1 -- comment 2 +is -- comment 3 + library ieee; -- comment 4 + end -- comment 5 +context -- comment 6 +c1 -- comment 7 +; -- comment 8 diff --git a/vsg/tests/context/test_rule_008.py b/vsg/tests/context/test_rule_008.py new file mode 100644 index 00000000..70bf9022 --- /dev/null +++ b/vsg/tests/context/test_rule_008.py @@ -0,0 +1,74 @@ + +import os +import unittest + +from vsg.rules import context +from vsg import vhdlFile +from vsg.tests import utils + +sTestDir = os.path.dirname(__file__) + +lFile = utils.read_vhdlfile(os.path.join(sTestDir,'rule_008_test_input.vhd')) +lExpected = [] +lExpected.append('') +utils.read_file(os.path.join(sTestDir, 'rule_008_test_input.fixed.vhd'), lExpected, False) + + +class test_context_rule(unittest.TestCase): + + def setUp(self): + self.oFile = vhdlFile.vhdlFile(lFile) + + def test_rule_008(self): + oRule = context.rule_008() + self.assertTrue(oRule) + self.assertEqual(oRule.name, 'context') + self.assertEqual(oRule.identifier, '008') + + lExpected = [] + dViolation = utils.add_violation(9) + dViolation['iObject'] = 6 + lExpected.append(dViolation) + + dViolation = utils.add_violation(13) + dViolation['iObject'] = 7 + lExpected.append(dViolation) + + dViolation = utils.add_violation(20) + dViolation['iObject'] = 6 + lExpected.append(dViolation) + + dViolation = utils.add_violation(26) + dViolation['iObject'] = 6 + lExpected.append(dViolation) + + dViolation = utils.add_violation(31) + dViolation['iObject'] = 16 + lExpected.append(dViolation) + + dViolation = utils.add_violation(33) + dViolation['iObject'] = 16 + lExpected.append(dViolation) + + dViolation = utils.add_violation(35) + dViolation['iObject'] = 16 + lExpected.append(dViolation) + + oRule.analyze(self.oFile) + self.assertEqual(oRule.violations, lExpected) + + def test_fix_rule_008(self): + self.maxDiff = None + oRule = context.rule_008() + + oRule.fix(self.oFile) + + lActual = [] + for oLine in self.oFile.lines: + lActual.append(oLine.line) + + self.assertEqual(lExpected, lActual) + + oRule.analyze(self.oFile) + self.assertEqual(oRule.violations, []) + diff --git a/vsg/tests/utils.py b/vsg/tests/utils.py index 75478838..97736456 100644 --- a/vsg/tests/utils.py +++ b/vsg/tests/utils.py @@ -10,10 +10,13 @@ def debug_lines(oFile, iLineNumber, iNumberOfLines): print('{0:5d} | {1:s}'.format(iLineNumber + iIndex, oFile.lines[iLineNumber + iIndex].line)) -def read_file(sFilename, lLines): +def read_file(sFilename, lLines, bStrip=True): with open(sFilename) as oFile: for sLine in oFile: - lLines.append(sLine.rstrip()) + if bStrip: + lLines.append(sLine.rstrip()) + else: + lLines.append(sLine.strip('\n')) def print_attributes(oLine):