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

Add and require version_added lexer attribute #2589

Merged
merged 6 commits into from
Nov 26, 2023

Conversation

jeanas
Copy link
Contributor

@jeanas jeanas commented Nov 19, 2023

Next step for #2585 :)

  • MXML: Remove bogus mimetypes line

  • Savi: Fix indentation

  • Move versionadded data to a lexer attribute

    That way, we can set it to "" for old lexers, and check that it's
    present on new lexers. (In the future, we might also use it for better
    presentation in the documentation.)

  • Make documentation use the new version_added lexer attribute

  • Require the version_added lexer attribute

  • Harmonize version_added format

@jeanas jeanas changed the title Add and require version_added lexer attribute Add and require version_added lexer attribute Nov 19, 2023
@jeanas
Copy link
Contributor Author

jeanas commented Nov 19, 2023

Today I learnt that GitHub doesn't test the PR branch but the result of merging that branch with master... I'll fix the CI failures.

@jeanas
Copy link
Contributor Author

jeanas commented Nov 19, 2023

Done.

For the record, here's the script I've used for the third commit:
import importlib
import inspect
from pathlib import Path
import re

from pygments.lexers import LEXERS

for class_name, (module_name, _, _, _, _) in LEXERS.items():
    mod = importlib.import_module(module_name)
    mod = importlib.reload(mod)
    cls = getattr(mod, class_name)

    file = inspect.getsourcefile(cls)
    lines, first_line_index = inspect.getsourcelines(cls)
    first_line_index -= 1 # make it zero-based
    end_line_index = first_line_index + len(lines)

    docstring_finished = False
    for version_added_index, version_added_line in enumerate(lines):
        if m := re.match(r"    .. versionadded:: (.*)", version_added_line):
            version_added = m.group(1)
            cursor = version_added_index
            del lines[version_added_index]
            cursor -= 1
            if not lines[version_added_index-1].strip():
                del lines[version_added_index-1]
                cursor -= 1
            break
    else:
        version_added = ''
        assert lines[1].startswith('    """'), f"{file}:{first_line_index}"
        if lines[1].endswith('"""\n'):
            docstring_finished = True
        cursor = 2

    if not docstring_finished:
        for i, line in enumerate(lines[cursor:], cursor):
            if line == '    """\n':
                cursor = i+1
                break
        else:
            assert False, f"{file}:{first_line_index}"

    unlocked = False
    for insertion_index, line in enumerate(lines[cursor:], cursor):
        if re.match("    (name|url|aliases|filenames|mimetypes) ?=", line):
            unlocked = True
        elif unlocked and not line.startswith("     ") and not re.match(r"    (\[|\])", line):
            break
    else:
        insertion_index += 1

    new_version_added_line = f"    version_added = {version_added!r}\n"
    lines.insert(insertion_index, new_version_added_line)

    all_lines = Path(file).read_text(encoding="utf-8").splitlines(keepends=True)
    all_lines[first_line_index:end_line_index] = lines
    Path(file).write_text(''.join(all_lines), encoding="utf-8")

@birkenfeld
Copy link
Member

Very nice!

@jeanas
Copy link
Contributor Author

jeanas commented Nov 26, 2023

Should we merge this one? @Anteru What do you think about it?

This was inactive due to the misspelling as "mimetimes", and we don't
want it because it conflicts with XML.
That way, we can set it to "" for old lexers, and check that it's
present on new lexers. (In the future, we might also use it for better
presentation in the documentation.)
@jeanas
Copy link
Contributor Author

jeanas commented Nov 26, 2023

(Updated again for new lexers in master)

@Anteru
Copy link
Collaborator

Anteru commented Nov 26, 2023

Looks good to me, thanks! I assume finding the ones which are set to "" at the moment is a question of Git history analysis? After all, they must have been added at some point (not that I think it's worth fixing for now.)

@jeanas
Copy link
Contributor Author

jeanas commented Nov 26, 2023

Yes, that's it. It's not always easy to find the version for old lexers; some of them have been moved around, and there are also some quirks in the Git history like commit db1e5ef (I think Pygments was originally using Mercurial and migrated to Git at some point?).

@jeanas jeanas merged commit 1d73d08 into pygments:master Nov 26, 2023
15 checks passed
@jeanas jeanas deleted the version-added branch November 26, 2023 13:51
@Anteru Anteru added this to the 2.18.0 milestone Apr 28, 2024
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

Successfully merging this pull request may close these issues.

None yet

3 participants