Example #1
0
 def format(self, allowed_line_length):
     self.assert_no_comments()
     return particles_in_lines(particles=self.get_particles(),
                               config=ParticleFormattingConfig(
                                   allowed_line_length=allowed_line_length,
                                   line_indent=INDENTATION,
                                   one_per_line=True))
Example #2
0
    def format(self, allowed_line_length):
        expr_codes = [x.format() for x in self.exprs]
        particles = ['return (', create_particle_sublist(expr_codes, ')')]

        return particles_in_lines(particles=particles,
                                  config=ParticleFormattingConfig(
                                      allowed_line_length=allowed_line_length,
                                      line_indent=INDENTATION,
                                      one_per_line=True))
Example #3
0
    def format(self, allowed_line_length):
        call_particles = self.func_call.get_particles()
        first_particle = f'let {self.typed_identifier.format()} = ' + call_particles[0]

        return particles_in_lines(
            particles=[first_particle] + call_particles[1:],
            config=ParticleFormattingConfig(
                allowed_line_length=allowed_line_length,
                line_indent=INDENTATION,
                one_per_line=True))
Example #4
0
    def format(self, allowed_line_length):
        particles = self.rvalue.get_particles()

        end_particle = ') = ' + particles[0]
        particles = ['let ('] + \
            create_particle_sublist(self.unpacking_list.get_particles(), end_particle) + \
            particles[1:]

        return particles_in_lines(particles=particles,
                                  config=ParticleFormattingConfig(
                                      allowed_line_length=allowed_line_length,
                                      line_indent=INDENTATION,
                                      one_per_line=True))
Example #5
0
    def format(self, allowed_line_length):
        for note in self.notes:
            note.assert_no_comments()

        items = [item.format() for item in self.import_items]
        prefix = f'from {self.path.format()} import '
        one_liner = prefix + ', '.join(items)

        if len(one_liner) <= allowed_line_length:
            return one_liner

        particles = [f'{prefix}(', create_particle_sublist(items, ')')]
        return particles_in_lines(particles=particles,
                                  config=ParticleFormattingConfig(
                                      allowed_line_length=allowed_line_length,
                                      line_indent=INDENTATION,
                                      one_per_line=False))
Example #6
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
Example #7
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'
def test_particles_in_lines():
    particles = [
        'start ',
        'foo ',
        'bar ',
        create_particle_sublist(['a', 'b', 'c', 'dddd', 'e', 'f'], '*'),
        ' asdf',
    ]
    expected = """\
start foo
  bar
  a, b, c,
  dddd, e,
  f* asdf\
"""
    assert particles_in_lines(
        particles=particles,
        config=ParticleFormattingConfig(allowed_line_length=12, line_indent=2),
    ) == expected

    particles = [
        'func f(',
        create_particle_sublist(['x', 'y', 'z'], ') -> ('),
        create_particle_sublist(['a', 'b', 'c'], '):'),
    ]
    expected = """\
func f(
    x, y,
    z) -> (
    a, b,
    c):\
"""
    assert particles_in_lines(
        particles=particles,
        config=ParticleFormattingConfig(allowed_line_length=12, line_indent=4),
    ) == expected

    # Same particles, using one_per_line=True.
    expected = """\
func f(
    x,
    y,
    z) -> (
    a,
    b,
    c):\
"""
    assert particles_in_lines(
        particles=particles,
        config=ParticleFormattingConfig(
            allowed_line_length=12, line_indent=4, one_per_line=True),
    ) == expected

    # Same particles, using one_per_line=True, longer lines.
    expected = """\
func f(
    x, y, z) -> (
    a, b, c):\
"""
    assert particles_in_lines(
        particles=particles,
        config=ParticleFormattingConfig(
            allowed_line_length=19, line_indent=4, one_per_line=True),
    ) == expected

    particles = [
        'func f(',
        create_particle_sublist(['x', 'y', 'z'], ') -> ('),
        create_particle_sublist([], '):'),
    ]
    expected = """\
func f(
    x, y, z) -> ():\
"""
    assert particles_in_lines(
        particles=particles,
        config=ParticleFormattingConfig(allowed_line_length=19, line_indent=4),
    ) == expected