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

fix(tree-sitter): update queries for tree-sitter-haskell rewrite #27

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.4] - 2024-05-13

### Fixed

- Updated tree-sitter queries to be compatible with the
tree-sitter-haskell v0.21.0 rewrite.
Because there are not many queries, this plugin maintains backward
compatibility with the previous tree-sitter-haskell implementation for now.

## [1.4.3] - 2023-12-15

### Fixed
Expand Down
15 changes: 9 additions & 6 deletions lua/haskell-snippets/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ end

---@param apply fun(module_name: string):(string|nil) Callback to apply the module name to. If the callback returns, this function returns.
---@param content string The content to parse from
---@param query_string? string The tree-sitter query string with a '@mod' capture
---@param query_string string The tree-sitter query string with a '@mod' capture (v0.21.0 rewrite)
---@param legacy_query_string string The legacy tree-sitter query string with a '@mod' capture
---@return string|nil
local function treesitter_module_name(apply, content, query_string)
local function treesitter_module_name(apply, content, query_string, legacy_query_string)
assert(has_haskell_parser, 'No tree-sitter parser for Haskell found.')
query_string = query_string or '(module) @mod'
local module_query = vim.treesitter.query.parse(hs_lang, query_string)
local ok, module_query = pcall(vim.treesitter.query.parse, hs_lang, query_string)
if not ok then
module_query = vim.treesitter.query.parse(hs_lang, legacy_query_string)
end
local lang_tree = vim.treesitter.get_string_parser(content, hs_lang, { injections = { [hs_lang] = '' } })
local root = fast_parse(lang_tree):root()
---@diagnostic disable-next-line
Expand All @@ -62,7 +65,7 @@ local function get_buf_module_name(_)
local buf_content = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), '\n')
return treesitter_module_name(function(mod)
return mod
end, buf_content, '[(module)(qualified_module)] @mod')
end, buf_content, '(haskell (header module: (module) @mod))', '(haskell module: (module) @mod)')
end

local function get_module_name_node()
Expand Down Expand Up @@ -111,7 +114,7 @@ local function get_qualified_name_node(args)
treesitter_module_name(function(mod)
table.insert(choices, 1, text(mod:sub(1, 1)))
table.insert(choices, 1, text(mod))
end, import_stmt)
end, import_stmt, '(module_id) @mod', '(module) @mod')
end
return sn(nil, {
choice(1, choices),
Expand Down
Loading