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 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 __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 __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_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 __is_argument(data: str) -> bool: return not (Tokens.is_line_comment(data) or Tokens.is_spacing_token(data))
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_last_element_newline(invocation: dict) -> bool: return Tokens.is_spacing_token(invocation['arguments'][-1]) or \ Tokens.is_line_comment(invocation['arguments'][-1])
def __is_spacing_token(args: List[str], index: int) -> bool: try: return Tokens.is_spacing_token(args[index]) except IndexError: return False