Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve format tool #359

Open
Levitanus opened this issue Jan 26, 2022 · 0 comments
Open

improve format tool #359

Levitanus opened this issue Jan 26, 2022 · 0 comments

Comments

@Levitanus
Copy link

Levitanus commented Jan 26, 2022

As I understand, current format tool is the same as exists in https://github.com/frescobaldi/python-ly/blob/master/ly/reformat.py

I lack the possibility to keep reasonable line width, and making line-breacks in obvious places.

Currently, I've made quick format function for my Reaper lilypond exporter, so I pretty like how it looks like on simple generated code, but it is not as flexible as the original module.

import re
import textwrap

def format_lines(lilypond: str) -> List[str]:
    patterns = {
        re.compile(r'<<(?!\n)'): '<<\n',
        re.compile(r'{(?!\n)'): '{\n',
        re.compile(r'(?!\n)>>'): '\n>>',
        re.compile(r'(?!\n)}'): '\n}',
        re.compile(r'(?=\s)\|(?=\s)'): '|\n',
    }
    idents = re.compile(r'(<<)|{')
    dedents = re.compile(r'(>>)|}')
    for pattern, repl in patterns.items():
        lilypond = re.sub(pattern, repl, lilypond)
    lines = re.split(r'\n', lilypond)
    out = []
    ident = 0
    ident_am = 4
    for line in lines:
        line = line_strip(line)
        if m := re.search(dedents, line):
            ident -= 1
        # if line:
        ident_str = ' ' * ident
        ident_level = ident_str * ident_am
        wraped = textwrap.wrap(
            line,
            width=80 - len(ident_level + ident_str),
            subsequent_indent=ident_level + ident_str,
            # initial_indent=ident_level
        )
        out.append(ident_level + '\n'.join(wraped))
        if m := re.search(idents, line):
            ident += 1
    return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant