Skip to content

Commit

Permalink
fix(tree-sitter): update queries for tree-sitter-haskell rewrite (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed May 13, 2024
1 parent 89a4f68 commit dfb3921
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
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

0 comments on commit dfb3921

Please sign in to comment.