mirror of
https://github.com/NixOS/nixpkgs.git
synced 2026-08-26 02:05:02 +00:00
nvim-treesitter/update.py: source from nvim-treesitter
Nurr hasn't always updated at the time nvim-treesitter updates, so it's better to get the data straight from the source to ensure it is correct. This uses lua itself to evaluate nvim-treesitter's lockfile and convert it to a json.
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
local data = loadfile()()
|
||||
local json = require"json".encode(data)
|
||||
io.write(json)
|
||||
@@ -10,11 +10,17 @@ let
|
||||
requests
|
||||
]
|
||||
);
|
||||
luaWithPackages = luajit.withPackages (
|
||||
ps: with ps; [
|
||||
json
|
||||
]
|
||||
);
|
||||
in
|
||||
|
||||
mkShell {
|
||||
packages = [
|
||||
nurl
|
||||
pythonWithPackages
|
||||
luaWithPackages
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user