diff --git a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/tojson.lua b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/tojson.lua new file mode 100644 index 000000000000..78730fe8711a --- /dev/null +++ b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/tojson.lua @@ -0,0 +1,3 @@ +local data = loadfile()() +local json = require"json".encode(data) +io.write(json) diff --git a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update-shell.nix b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update-shell.nix index d811a1b35c35..c9349396ca32 100644 --- a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update-shell.nix +++ b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update-shell.nix @@ -10,11 +10,17 @@ let requests ] ); + luaWithPackages = luajit.withPackages ( + ps: with ps; [ + json + ] + ); in mkShell { packages = [ nurl pythonWithPackages + luaWithPackages ]; } 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 ce1ff9bdef87..7596d6ce8a26 100755 --- a/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py +++ b/pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py @@ -12,8 +12,8 @@ import requests log = logging.getLogger("vim-updater") -NURR_JSON_URL = ( - "https://raw.githubusercontent.com/lumen-oss/nurr/main/tree-sitter-parsers.json" +NVIM_TREESITTER_PARSERS_URL = ( + "https://raw.githubusercontent.com/nvim-treesitter/nvim-treesitter/refs/heads/main/lua/nvim-treesitter/parsers.lua" ) NVIM_TREESITTER_QUERIES_URL = "https://api.github.com/repos/nvim-treesitter/nvim-treesitter/contents/runtime/queries" @@ -95,9 +95,9 @@ def generate_query(lang: str, parser_info: dict | None, queries_set: set[str]): return generated -def fetch_nurr_parsers(): - """Fetch the parser information from nurr repository""" - log.info("Fetching parser data from %s", NURR_JSON_URL) +def fetch_pinned_parsers(): + """Fetch the parser information from the nvim-treesitter repository""" + log.info("Fetching parser data from %s", NVIM_TREESITTER_PARSERS_URL) headers = {} github_token = os.environ.get("GITHUB_TOKEN") @@ -107,18 +107,18 @@ def fetch_nurr_parsers(): else: log.warning("No GITHUB_TOKEN found. GitHub API requests may be rate-limited.") - response = requests.get(NURR_JSON_URL, headers=headers, timeout=30) + response = requests.get(NVIM_TREESITTER_PARSERS_URL, headers=headers, timeout=30) response.raise_for_status() - data = response.json() + lua_path = Path(__file__).parent / "tojson.lua" + lua_output = subprocess.check_output( + ["luajit", lua_path], + text=True, + input=response.text, + ) + data = json.loads(lua_output) - try: - parsers = data["parsers"] - except KeyError: - raise ValueError( - "Unexpected response from NURR:\n" + json.dumps(data, indent=2) - ) - log.info(f"Successfully fetched {len(parsers)} parsers") - return parsers + log.info(f"Successfully fetched {len(data)} parsers") + return data def fetch_available_queries(): @@ -140,21 +140,18 @@ def fetch_available_queries(): return languages -def process_parser_info(parser_info, parsers_map): +def process_parser_info(parser, parsers): """Process a single parser info entry and generate grammar for it""" - return generate_grammar(parser_info["lang"], parser_info, parsers_map) + return generate_grammar(parser, parsers[parser], parsers) def update_grammars(): - """Update grammar definitions using nurr's parser information""" - parsers_info = fetch_nurr_parsers() + """Update grammar definitions using nvim-treesitter's pinned parsers""" + parsers = fetch_pinned_parsers() queries_list = fetch_available_queries() - # Create a mapping of lang -> parser_info for quick lookup - parsers_map = {p["lang"]: p for p in parsers_info} - generated_file = """# generated by pkgs/applications/editors/vim/plugins/utils/nvim-treesitter/update.py -# Using parser data from https://github.com/nvim-neorocks/nurr/blob/main/tree-sitter-parsers.json +# Using parser data from https://github.com/nvim-treesitter/nvim-treesitter/blob/main/lua/nvim-treesitter/parsers.lua { buildGrammar, @@ -174,7 +171,7 @@ def update_grammars(): # Process parsers in parallel for better performance with ThreadPoolExecutor(max_workers=5) as executor: for generated in executor.map( - lambda p: process_parser_info(p, parsers_map), parsers_info + lambda p: process_parser_info(p, parsers), sorted(parsers.keys()) ): generated_file += generated @@ -188,7 +185,7 @@ def update_grammars(): # Process queries - include parser info if available for requires field for lang in queries_list: - parser_info = parsers_map.get(lang) + parser_info = parsers.get(lang) generated_file += generate_query(lang, parser_info, queries_set) generated_file += " };\n}\n"