From 5e6dcd544d22d2fa297ccd358b0dbb0f844191b2 Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Tue, 1 Dec 2020 10:52:01 -0600 Subject: [PATCH] Refactoring extract.get_line_preceeding_line to use token map. 1) Had to change remove_excessive_blank_lines_above_line_starting_with_token base rule. --- ...nk_lines_above_line_starting_with_token.py | 26 ++++++------------- .../extract/get_line_preceeding_line.py | 26 ++++++------------- vsg/vhdlFile/vhdlFile.py | 4 +-- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/vsg/rules/remove_excessive_blank_lines_above_line_starting_with_token.py b/vsg/rules/remove_excessive_blank_lines_above_line_starting_with_token.py index 21a661b8..bb224081 100644 --- a/vsg/rules/remove_excessive_blank_lines_above_line_starting_with_token.py +++ b/vsg/rules/remove_excessive_blank_lines_above_line_starting_with_token.py @@ -54,29 +54,19 @@ class remove_excessive_blank_lines_above_line_starting_with_token(rule.Rule): oViolation.set_action(dAction) self.add_violation(oViolation) - def fix(self, oFile): - ''' - Applies fixes for any rule violations. - ''' - if self.fixable: - self.analyze(oFile) - self._print_debug_message('Fixing rule: ' + self.name + '_' + self.identifier) - self._fix_violation(oFile) - self.violations = [] - - def _fix_violation(self, oFile): - for oViolation in self.violations: - lTokens = oViolation.get_tokens() - dAction = oViolation.get_action() - lNewTokens = lTokens[0:dAction['index']] - oViolation.set_tokens(lNewTokens) - oFile.update(self.violations) + def _fix_violation(self, oViolation): + lTokens = oViolation.get_tokens() + dAction = oViolation.get_action() + lNewTokens = lTokens[0:dAction['index']] + oViolation.set_tokens(lNewTokens) def check_if_override_exists(oFile, iLine, lOverrides): - oMyToi = oFile.get_line_preceeding_line(iLine) + + oMyToi = oFile.get_line_preceeding_line(iLine - 1) try: lTokens = oMyToi.get_tokens() + for oOverride in lOverrides: if utils.does_token_type_exist_in_list_of_tokens(oOverride, lTokens): return True diff --git a/vsg/vhdlFile/extract/get_line_preceeding_line.py b/vsg/vhdlFile/extract/get_line_preceeding_line.py index 45367c1d..6ddde1dd 100644 --- a/vsg/vhdlFile/extract/get_line_preceeding_line.py +++ b/vsg/vhdlFile/extract/get_line_preceeding_line.py @@ -1,25 +1,15 @@ from vsg import parser -from vsg.vhdlFile import vhdlFile as utils +from vsg.vhdlFile.extract import tokens -def get_line_preceeding_line(iLine, lAllTokens): +def get_line_preceeding_line(iLine, lAllTokens, iNumLines, oTokenMap): + lCarriageReturns = oTokenMap.get_token_indexes(parser.carriage_return) - iMyLine = 1 - iTargetLine = iLine - 1 - bStoreTokens = False - lTemp = [] - for iToken, oToken in enumerate(lAllTokens): - if bStoreTokens: - lTemp.append(oToken) - if isinstance(oToken, parser.carriage_return): - iMyLine += 1 - if bStoreTokens: - lTemp.pop() - return utils.Tokens(iStartIndex, iTargetLine, lTemp) - if iMyLine == iTargetLine: - bStoreTokens = True - iStartIndex = iToken + 1 + iAdjust = -1 + iStart = lCarriageReturns[iLine - iNumLines + iAdjust] + 1 + iEnd = lCarriageReturns[iLine + iAdjust] + lTemp = lAllTokens[iStart:iEnd] - return None + return tokens.New(iStart, iLine - iNumLines, lTemp) diff --git a/vsg/vhdlFile/vhdlFile.py b/vsg/vhdlFile/vhdlFile.py index 17eb86ac..63d88f1d 100644 --- a/vsg/vhdlFile/vhdlFile.py +++ b/vsg/vhdlFile/vhdlFile.py @@ -119,8 +119,8 @@ class vhdlFile(): def set_token_indent(self): set_token_indent(self.lAllObjects) - def get_line_preceeding_line(self, iLine): - return extract.get_line_preceeding_line(iLine, self.lAllObjects) + def get_line_preceeding_line(self, iLine, iNumLines=1): + return extract.get_line_preceeding_line(iLine, self.lAllObjects, iNumLines, self.oTokenMap) def get_line_succeeding_line(self, iLine, iNumLines=1): return extract.get_line_succeeding_line(iLine, self.lAllObjects, iNumLines, self.oTokenMap)