def __prepare_wrapped_invocation(invocation: dict) -> dict: new_invoke = invocation.copy() if Tokens.is_spacing_token(new_invoke['arguments'][0]): new_invoke['arguments'] = new_invoke['arguments'][1:] if Tokens.is_spacing_token(new_invoke['arguments'][-1]): new_invoke['arguments'] = new_invoke['arguments'][:-1] return new_invoke
def __reindent_property_name(self, args: list, start_index: int) -> Tuple[list, int]: for i in range(start_index + 1, len(args)): if self.__is_argument(args[i]): if not args[i].startswith(Tokens.reindent(1)): args[i] = Tokens.reindent(1) + args[i] return args, i return args, 0
def __merge_line_comments_with_whitespaces_before(args: List[str]) -> list: merged = [] for i in range(len(args) - 1): if Tokens.is_spacing_token(args[i]) and Tokens.is_line_comment( args[i + 1]): args[i + 1] = args[i] + args[i + 1] else: merged.append(args[i]) return merged + [args[-1]] if args else []
def test_is_command_keyword(self): self.assertTrue(KeywordVerifier.is_command_keyword('COMMAND')) self.assertTrue( KeywordVerifier.is_command_keyword(Tokens.reindent(1) + 'COMMAND')) self.assertTrue( KeywordVerifier.is_command_keyword(Tokens.reindent(1) + 'ARGS')) self.assertFalse(KeywordVerifier.is_command_keyword('CMD')) self.assertFalse( KeywordVerifier.is_command_keyword(Tokens.reindent(2) + 'COMMAND'))
def test_double_keywords(self): self.assertTrue( self.verify.is_double_keyword( Tokens.reindent(1) + 'RUNTIME', 'DESTINATION')) self.assertTrue( self.verify.is_double_keyword('ARCHIVE', Tokens.reindent(1) + 'DESTINATION')) self.assertTrue(self.verify.is_double_keyword('LIBRARY', 'DESTINATION')) self.assertFalse(self.verify.is_double_keyword('OUTPUT', 'DESTINATION')) self.assertFalse(self.verify.is_double_keyword('LIBRARY', 'OUTPUT'))
def wrap(self, invocation: dict) -> dict: wrapped_invoke = self.__prepare_wrapped_invocation(invocation) wrapped_invoke['arguments'] = [ e if not Tokens.is_spacing_token(e) else ' ' for e in wrapped_invoke['arguments'] ] return wrapped_invoke
def __reindent_line_comments_after_keyword(self, args: List[str]) -> list: verifier = KeywordVerifier(self.__settings) for i in reversed(range(len(args))): if verifier.is_keyword(args[i]) and re.match( Tokens.get_reindent_regex(), args[i]): self.__try_reindent_all_previous_comments(args, i) return args
def test_whether_token_is_first_class_keyword(self): self.assertTrue(self.verify.is_first_class_keyword('PROPERTY')) self.assertTrue(self.verify.is_first_class_keyword('PROPERTIES')) self.assertTrue( self.verify.is_first_class_keyword( Tokens.reindent(1) + 'PROPERTY')) self.assertFalse(self.verify.is_first_class_keyword('PROPERTY2')) self.assertFalse(self.verify.is_first_class_keyword('proPERTY'))
def test_matching_spacing_tokens(self): self.assertFalse(Tokens.is_line_comment('')) self.assertTrue(Tokens.is_spacing_token(' ')) self.assertTrue(Tokens.is_spacing_token('\t ')) self.assertTrue(Tokens.is_spacing_token('\t \n')) self.assertTrue(Tokens.is_spacing_token(f'\t {Tokens.remove_spaces()}\n')) self.assertFalse(Tokens.is_spacing_token('\t d')) self.assertFalse(Tokens.is_spacing_token(f'\t {Tokens.reindent(1)}'))
def __realign_commands(self, args: List[str]) -> list: diff = 0 if self.__settings.get('keyword_and_single_value_in_one_line') else CommandRealignModifier.__DIFF_BETWEEN_KEYWORD_AND_VALUE for i in range(len(args)): if KeywordVerifier.is_command_keyword(args[i]): for j in range(i + diff, len(args) - 1): if self.__verifier.is_keyword(args[j + 1]): break if Tokens.is_spacing_token(args[j]): args[j] = ' ' return args
def test_unquoted_arguments_with_uppercase_letters_only_are_keywords(self): self.settings['unquoted_uppercase_as_keyword'] = True self.verify = KeywordVerifier(self.settings) self.assertTrue(self.verify.is_keyword('OTHER')) self.assertTrue(self.verify.is_keyword(Tokens.reindent(1) + 'OTHER')) self.assertTrue(self.verify.is_keyword('WITH_SEPARATION')) self.assertFalse(self.verify.is_keyword('"$OTHER"')) self.assertFalse(self.verify.is_keyword('SOMeARG')) self.assertFalse(self.verify.is_keyword('a_ARGUMENT')) self.assertFalse(self.verify.is_keyword('NOT_')) self.assertFalse(self.verify.is_keyword('_SOME'))
def __should_realign_value_after_keyword(self, args: List[str], current_index: int) -> bool: return self.__verifier.is_keyword_or_property(args[current_index]) and \ Tokens.is_spacing_token(args[current_index + 1]) and \ not self.__verifier.is_keyword_or_property(args[current_index + 2]) and \ self.__is_single_value(args, current_index)
def __is_property_followed_by_name(args: list, i: int) -> bool: return KeywordVerifier.is_first_class_keyword(args[i]) and not Tokens.is_line_comment(args[i + 2])
def __cleanup_whitespaces_at_line_ends(processed: str) -> str: return re.sub('[ \t]*' + Tokens.remove_spaces(), '', processed)
def __cleanup_reindent_all(data: str) -> str: return re.sub('[ \t]*' + Tokens.reindent(), '', data)
def __is_last_element_newline(invocation: dict) -> bool: return Tokens.is_spacing_token(invocation['arguments'][-1]) or \ Tokens.is_line_comment(invocation['arguments'][-1])
def __cleanup_end_invocations(self, formatted_file: str) -> str: for pattern in Tokens.get_reindent_patterns_list( 3, get_single_indent(self.__settings)): formatted_file = re.sub(pattern, '', formatted_file) return formatted_file
def test_ignore_reindent_tokens(self): self.assertEqual(0, self.calculator.calculate(Tokens.reindent(3))) self.assertEqual(14, self.calculator.calculate(f'\tsome{Tokens.reindent(99)}\tso'))
def test_ignore_remove_spaces_tokens(self): self.assertEqual(0, self.calculator.calculate(Tokens.remove_spaces())) self.assertEqual(4, self.calculator.calculate(f'piwo{Tokens.remove_spaces()}'))
def is_double_keyword(first: str, second: str) -> bool: first = first.replace(Tokens.reindent(1), '') second = second.replace(Tokens.reindent(1), '') return any(keyword.startswith(first) and keyword.endswith(second) \ for keyword in KeywordVerifier.__PROPERTIES['double-keywords'])
def is_conditional_invocation(data: str) -> bool: data = data.lower().replace(' ', '') return any( [token == data[:-1] for token in Tokens.conditional_tokens()]) and data[-1] == '('
def is_command_keyword(data: str) -> bool: data = data.replace(Tokens.reindent(1), '') return data == 'COMMAND' or data == 'ARGS'
def __reindent_all_previous_comments(args: list, start: int) -> None: for i in reversed(range(start)): if Tokens.is_line_comment(args[i]): args[i] = Tokens.reindent(1) + args[i] else: break
def is_property(self, data: str) -> bool: data = data.replace(Tokens.reindent(1), '') return data in KeywordVerifier.__PROPERTIES["properties_full_names"] or \ self.__is_property_regex_starting(data) or \ self.__is_property_ending_with(data)
def __is_end_of_special_command(self, original: str) -> bool: return any([ self.__matches(token, original.lower()) for token in Tokens.end_tokens() ])
def __is_spacing_token(args: List[str], index: int) -> bool: try: return Tokens.is_spacing_token(args[index]) except IndexError: return False
def __is_value_realignable(self, args, position): return not Tokens.is_line_comment(args[position + 2]) and \ self.__get_number_of_arguments(args, position) == 1
def __format_newlines(self, number_of_newlines: int) -> str: return Tokens.remove_spaces() + '\n' * min(self.__settings['succeeding_newlines'], number_of_newlines)
def __is_argument(data: str) -> bool: return not (Tokens.is_line_comment(data) or Tokens.is_spacing_token(data))
def __call__(self, data: list) -> str: formatted = ''.join(data) if '\n' in data[1]: formatted = Tokens.reindent(99) + formatted return formatted