From 4e3ffd895aa105f6fd93a3ece7b63f1a79c2a660 Mon Sep 17 00:00:00 2001 From: Jeremiah C Leary Date: Fri, 24 Nov 2017 06:53:12 -0600 Subject: [PATCH] Updates for issue #61 and issue #70 1) Comments after "when" statement are indented one level from "when" 2) Comments in entities are indented at the same level as ports --- vsg/rules/comment.py | 27 +++---------------- vsg/tests/comment/comment_case_test_input.vhd | 11 ++++---- vsg/tests/comment/test_rule_comment.py | 4 +-- .../test_vhdlFile_comment_assignments.py | 13 +++++++++ vsg/vhdlFile.py | 2 ++ 5 files changed, 26 insertions(+), 31 deletions(-) diff --git a/vsg/rules/comment.py b/vsg/rules/comment.py index 753b68b2..46144cbe 100644 --- a/vsg/rules/comment.py +++ b/vsg/rules/comment.py @@ -24,32 +24,11 @@ class rule_001(comment_rule): def analyze(self, oFile): for iLineNumber, oLine in enumerate(oFile.lines): if oLine.isComment: - if oFile.lines[iLineNumber - 1].hasComment: - if oFile.lines[iLineNumber - 1].isComment: - if not oLine.commentColumn == (oLine.indentLevel * self.indentSize): - self.add_violation(iLineNumber) - self.correctCommentColumn.append(oLine.indentLevel * self.indentSize) - else: - try: - if oFile.lines[iLineNumber + 1].isCaseWhenKeyword: - if not oLine.commentColumn == (oLine.indentLevel * self.indentSize) and not oLine.commentColumn == ((oLine.indentLevel - 1) * self.indentSize): - self.add_violation(iLineNumber) - self.correctCommentColumn.append(oLine.indentLevel * self.indentSize) - else: - if not oLine.commentColumn == (oLine.indentLevel * self.indentSize): - self.add_violation(iLineNumber) - self.correctCommentColumn.append(oLine.indentLevel * self.indentSize) - except IndexError: - pass + self._check_indent(oLine, iLineNumber) def _fix_violations(self, oFile): - for iViolationIndex, iLineNumber in enumerate(self.violations): - oLine = oFile.lines[iLineNumber] - oLine.line = ' ' * self.correctCommentColumn[iViolationIndex] + oLine.line.lstrip() - oLine.lineLower = oLine.line.lower() - oLine.commentColumn = self.correctCommentColumn[iViolationIndex] - - self.correctCommentColumn = [] + for iLineNumber in self.violations: + self._fix_indent(oFile.lines[iLineNumber]) # class rule_002(comment_rule): diff --git a/vsg/tests/comment/comment_case_test_input.vhd b/vsg/tests/comment/comment_case_test_input.vhd index 4589017e..cd1528be 100644 --- a/vsg/tests/comment/comment_case_test_input.vhd +++ b/vsg/tests/comment/comment_case_test_input.vhd @@ -8,19 +8,20 @@ begin case CASE1 is - when a => -- + when a => -- Comment + -- Comment x <= y; z <= a; - -- - when b => -- + -- Comment + when b => -- Comment x <= y; z <= a; -- - when b => -- + when b => -- Comment x <= y; z <= a; -- Comment - when b => -- + when b => -- Comment x <= y; z <= a; -- Comment diff --git a/vsg/tests/comment/test_rule_comment.py b/vsg/tests/comment/test_rule_comment.py index 30c328cf..6495b33d 100644 --- a/vsg/tests/comment/test_rule_comment.py +++ b/vsg/tests/comment/test_rule_comment.py @@ -17,7 +17,7 @@ class testRuleCommentMethods(unittest.TestCase): self.assertTrue(oRule) self.assertEqual(oRule.name, 'comment') self.assertEqual(oRule.identifier, '001') - dExpected = [3,7,12,13,21,23] + dExpected = [3,7,12,13,17,19,21,23,27,30,45,49,64,68] oRule.analyze(oFile) self.assertEqual(oRule.violations, dExpected) @@ -26,7 +26,7 @@ class testRuleCommentMethods(unittest.TestCase): self.assertTrue(oRule) self.assertEqual(oRule.name, 'comment') self.assertEqual(oRule.identifier, '001') - dExpected = [22,26] + dExpected = [19,23,27] oRule.analyze(oFileCase) self.assertEqual(oRule.violations, dExpected) diff --git a/vsg/tests/vhdlFile/test_vhdlFile_comment_assignments.py b/vsg/tests/vhdlFile/test_vhdlFile_comment_assignments.py index 416bc8b3..954733b3 100644 --- a/vsg/tests/vhdlFile/test_vhdlFile_comment_assignments.py +++ b/vsg/tests/vhdlFile/test_vhdlFile_comment_assignments.py @@ -4,6 +4,7 @@ import unittest from vsg import vhdlFile oFile = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','comment','comment_test_input.vhd')) +oFileCase = vhdlFile.vhdlFile(os.path.join(os.path.dirname(__file__),'..','comment','comment_case_test_input.vhd')) class testVhdlFileCommentAssignments(unittest.TestCase): @@ -37,3 +38,15 @@ class testVhdlFileCommentAssignments(unittest.TestCase): lActual.append(oLine.commentColumn) # Compare self.assertEqual(lActual, lExpected) + + def test_comment_indent_assignment(self): + # [12,15,17,23,27] + lExpected = [ 4, 3, 3, 3, 4] + # Generic actual list + lActual = [] + for iIndex, oLine in enumerate(oFileCase.lines): + if oLine.isComment: + lActual.append(oLine.indentLevel) + # Compare + self.assertEqual(lActual, lExpected) + diff --git a/vsg/vhdlFile.py b/vsg/vhdlFile.py index 9cf2e46b..674ea8f0 100644 --- a/vsg/vhdlFile.py +++ b/vsg/vhdlFile.py @@ -361,6 +361,8 @@ class vhdlFile(): oLine.isCaseWhenKeyword = True oLine.indentLevel = iCurrentIndentLevel - 1 fInsideCaseWhen = True + if self.lines[-1].isComment: + self.lines[-1].indentLevel -= 1 if fInsideCaseWhen: oLine.insideCaseWhen = True if re.match('^\s*.*=>', oLine.line):