コード例 #1
0
 def format(self, allowed_line_length):
     identifier_list_str = ', '.join(identifier.format()
                                     for identifier in self.identifiers)
     inner_code = self.code_block.format(
         allowed_line_length=allowed_line_length - INDENTATION)
     inner_code = indent(inner_code, INDENTATION)
     return f'with {identifier_list_str}:\n{inner_code}end'
コード例 #2
0
 def format(self, allowed_line_length):
     if self.hint_code == '':
         return '%{\n%}'
     if '\n' not in self.hint_code:
         # One liner.
         return f'%{{ {self.hint_code} %}}'
     code = indent(self.hint_code, INDENTATION)
     return f'%{{\n{code}\n%}}'
コード例 #3
0
 def to_str(self):
     if self.hint_code == '':
         return '%{\n%}'
     if '\n' not in self.hint_code:
         # One liner.
         return f'%{{ {self.hint_code} %}}'
     code = indent(self.hint_code, INDENTATION)
     return f'%{{\n{code}\n%}}'
コード例 #4
0
 def format(self, allowed_line_length):
     cond_particles = ['if ', *self.condition.get_particles()]
     cond_particles[-1] = cond_particles[-1] + ':'
     code = particles_in_lines(particles=cond_particles,
                               config=ParticleFormattingConfig(
                                   allowed_line_length=allowed_line_length,
                                   line_indent=INDENTATION))
     main_code = self.main_code_block.format(
         allowed_line_length=allowed_line_length - INDENTATION)
     main_code = indent(main_code, INDENTATION)
     code += f'\n{main_code}'
     if self.else_code_block is not None:
         code += f'else:'
         else_code = self.else_code_block.format(
             allowed_line_length=allowed_line_length - INDENTATION)
         else_code = indent(else_code, INDENTATION)
         code += f'\n{else_code}'
     code += 'end'
     return code
コード例 #5
0
    def format(self, allowed_line_length):
        code = self.code_block.format(allowed_line_length=allowed_line_length - INDENTATION)
        code = indent(code, INDENTATION)
        if self.element_type in ['struct', 'namespace']:
            particles = [f'{self.element_type} {self.name}:']
        elif self.returns is not None:
            particles = [
                f'{self.element_type} {self.name}(',
                create_particle_sublist(self.arguments.get_particles(), ') -> ('),
                create_particle_sublist(self.returns.get_particles(), '):')]
        else:
            particles = [
                f'{self.element_type} {self.name}(',
                create_particle_sublist(self.arguments.get_particles(), '):')]

        header = particles_in_lines(
            particles=particles,
            config=ParticleFormattingConfig(
                allowed_line_length=allowed_line_length,
                line_indent=INDENTATION * 2))
        return f'{header}\n{code}end'
コード例 #6
0
def test_indent():
    assert indent('aa\n  bb', 2) == '  aa\n    bb'
    assert indent('aa\n  bb\n', 2) == '  aa\n    bb\n'
    assert indent('  aa\n  bb\n\ncc\n', 2) == '    aa\n    bb\n\n  cc\n'