def get_particles(self): self.assert_no_comments() particles = [self.func_ident.format()] if self.implicit_arguments is not None: particles[-1] += '{' particles.append(create_particle_sublist( [x.format() for x in self.implicit_arguments.args], '}(')) else: particles[-1] += '(' particles.append(create_particle_sublist([x.format() for x in self.arguments.args], ')')) return particles
def get_particles(self): expr_codes = [x.format() for x in self.exprs] particles = [ f'{self.func_ident.format()}(', create_particle_sublist(expr_codes, ')') ] return particles
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))
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 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))
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))
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