37
37
38
38
--- @param apply fun ( module_name : string ):( string | nil ) Callback to apply the module name to. If the callback returns , this function returns.
39
39
--- @param content string The content to parse from
40
- --- @param query_string ? string The tree-sitter query string with a ' @mod' capture
40
+ --- @param query_string string The tree-sitter query string with a ' @mod' capture (v0.21.0 rewrite )
41
+ --- @param legacy_query_string string The legacy tree-sitter query string with a ' @mod' capture
41
42
--- @return string | nil
42
- local function treesitter_module_name (apply , content , query_string )
43
+ local function treesitter_module_name (apply , content , query_string , legacy_query_string )
43
44
assert (has_haskell_parser , ' No tree-sitter parser for Haskell found.' )
44
- query_string = query_string or ' (module) @mod'
45
- local module_query = vim .treesitter .query .parse (hs_lang , query_string )
45
+ local ok , module_query = pcall (vim .treesitter .query .parse , hs_lang , query_string )
46
+ if not ok then
47
+ module_query = vim .treesitter .query .parse (hs_lang , legacy_query_string )
48
+ end
46
49
local lang_tree = vim .treesitter .get_string_parser (content , hs_lang , { injections = { [hs_lang ] = ' ' } })
47
50
local root = fast_parse (lang_tree ):root ()
48
51
--- @diagnostic disable-next-line
@@ -62,7 +65,7 @@ local function get_buf_module_name(_)
62
65
local buf_content = table.concat (vim .api .nvim_buf_get_lines (0 , 0 , - 1 , false ), ' \n ' )
63
66
return treesitter_module_name (function (mod )
64
67
return mod
65
- end , buf_content , ' [( module)(qualified_module)] @mod' )
68
+ end , buf_content , ' (haskell (header module: (module) @mod)) ' , ' (haskell module: (module) @mod) ' )
66
69
end
67
70
68
71
local function get_module_name_node ()
@@ -111,7 +114,7 @@ local function get_qualified_name_node(args)
111
114
treesitter_module_name (function (mod )
112
115
table.insert (choices , 1 , text (mod :sub (1 , 1 )))
113
116
table.insert (choices , 1 , text (mod ))
114
- end , import_stmt )
117
+ end , import_stmt , ' (module_id) @mod ' , ' (module) @mod ' )
115
118
end
116
119
return sn (nil , {
117
120
choice (1 , choices ),
0 commit comments