From 137a367b17866f2984b266c9efa3e3c72878c69d Mon Sep 17 00:00:00 2001 From: PerchunPak Date: Sun, 18 May 2025 19:49:52 +0200 Subject: [PATCH 1/2] nvim-treesitter grammars updater: small improvements Mainly ran `ruff format` on the file and used newer `pathlib.Path` --- .../plugins/utils/nvim-treesitter/update.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py index 6b19eb24ebf6..44d421e37823 100755 --- a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py +++ b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py @@ -1,17 +1,21 @@ #!/usr/bin/env nix-shell -#!nix-shell update-shell.nix -i python +#!nix-shell ./update-shell.nix -i python import json import logging import os import subprocess from concurrent.futures import ThreadPoolExecutor +from pathlib import Path import requests log = logging.getLogger("vim-updater") -NURR_JSON_URL = "https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json" +NURR_JSON_URL = ( + "https://raw.githubusercontent.com/nvim-neorocks/nurr/main/tree-sitter-parsers.json" +) + def generate_grammar(lang, parser_info): """Generate grammar for a language based on the parser info""" @@ -30,7 +34,9 @@ def generate_grammar(lang, parser_info): version = "0.0.0+rev={rev[:7]}"; src = """ - generated += subprocess.check_output(["nurl", url, rev, "--indent=4"], text=True) + generated += subprocess.check_output( + ["nurl", url, rev, "--indent=4"], text=True + ) generated += ";" location = install_info.get("location", "") @@ -82,8 +88,7 @@ def fetch_nurr_parsers(): def process_parser_info(parser_info): """Process a single parser info entry and generate grammar for it""" try: - lang = parser_info["lang"] - return generate_grammar(lang, parser_info) + return generate_grammar(parser_info["lang"], parser_info) except Exception as e: log.error(f"Error processing parser: {e}") return "" @@ -119,13 +124,10 @@ def update_grammars(): if __name__ == "__main__": - logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') + logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") generated = update_grammars() - output_path = os.path.join( - os.path.dirname(__file__), - "../../nvim-treesitter/generated.nix" - ) + output_path = Path(__file__).parent.parent / "nvim-treesitter/generated.nix" log.info("Writing output to %s", output_path) with open(output_path, "w") as f: f.write(generated) From 1a47b9efd078cb8c458e99744258138214362aae Mon Sep 17 00:00:00 2001 From: PerchunPak Date: Sun, 18 May 2025 19:53:33 +0200 Subject: [PATCH 2/2] nvim-treesitter grammars updater: avoid silent fails What if some grammar fails to update? Well, it gets removed from the generated file. How would we know that the grammar failed? By reading logs. Who reads logs if the command didn't crash? Right, nobody. Or even if someone examines the logs, it is too easy to overlook the failure (are you sure you read every single line of code? It is not even colored) They are useful only in two cases: - During debugging, this happens rarely - Let's print something to keep the user informed that we are doing something It should not to be a mistake to overlook one line from the logs, because when you run the same script hundreds of times (we do updates weekly), you barely look at logs; you scan them, at most. By treating logs this way, we can inform ourselves about possible breakages before it gets even reviewed by another person, not by a user opening a bug report months later. ![image](https://github.com/user-attachments/assets/608dad73-626a-4e06-9122-38d2a085d473) --- .../plugins/utils/nvim-treesitter/update.py | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py index 44d421e37823..7ae13615d2ab 100755 --- a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py +++ b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py @@ -19,44 +19,40 @@ NURR_JSON_URL = ( def generate_grammar(lang, parser_info): """Generate grammar for a language based on the parser info""" - try: - if "install_info" not in parser_info: - log.warning(f"Parser {lang} does not have install_info, skipping") - return "" + if "install_info" not in parser_info: + log.warning(f"Parser {lang} does not have install_info, skipping") + return "" - install_info = parser_info["install_info"] + install_info = parser_info["install_info"] - url = install_info["url"] - rev = install_info["revision"] + url = install_info["url"] + rev = install_info["revision"] - generated = f""" {lang} = buildGrammar {{ + generated = f""" {lang} = buildGrammar {{ language = "{lang}"; version = "0.0.0+rev={rev[:7]}"; src = """ - generated += subprocess.check_output( - ["nurl", url, rev, "--indent=4"], text=True - ) - generated += ";" + generated += subprocess.check_output( + ["nurl", url, rev, "--indent=4"], text=True + ) + generated += ";" - location = install_info.get("location", "") - if location: - generated += f""" + location = install_info.get("location", "") + if location: + generated += f""" location = "{location}";""" - if install_info.get("generate", False): - generated += """ + if install_info.get("generate", False): + generated += """ generate = true;""" - generated += f""" + generated += f""" meta.homepage = "{url}"; }}; """ - return generated - except Exception as e: - log.error(f"Error generating grammar for {lang}: {e}") - return "" + return generated def fetch_nurr_parsers(): @@ -87,11 +83,7 @@ def fetch_nurr_parsers(): def process_parser_info(parser_info): """Process a single parser info entry and generate grammar for it""" - try: - return generate_grammar(parser_info["lang"], parser_info) - except Exception as e: - log.error(f"Error processing parser: {e}") - return "" + return generate_grammar(parser_info["lang"], parser_info) def update_grammars():