3737
3838--- @param apply fun ( module_name : string ):( string | nil ) Callback to apply the module name to. If the callback returns , this function returns.
3939--- @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
4142--- @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 )
4344 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
4649 local lang_tree = vim .treesitter .get_string_parser (content , hs_lang , { injections = { [hs_lang ] = ' ' } })
4750 local root = fast_parse (lang_tree ):root ()
4851 --- @diagnostic disable-next-line
@@ -62,7 +65,7 @@ local function get_buf_module_name(_)
6265 local buf_content = table.concat (vim .api .nvim_buf_get_lines (0 , 0 , - 1 , false ), ' \n ' )
6366 return treesitter_module_name (function (mod )
6467 return mod
65- end , buf_content , ' [( module)(qualified_module)] @mod' )
68+ end , buf_content , ' (haskell (header module: (module) @mod)) ' , ' (haskell module: (module) @mod) ' )
6669end
6770
6871local function get_module_name_node ()
@@ -111,7 +114,7 @@ local function get_qualified_name_node(args)
111114 treesitter_module_name (function (mod )
112115 table.insert (choices , 1 , text (mod :sub (1 , 1 )))
113116 table.insert (choices , 1 , text (mod ))
114- end , import_stmt )
117+ end , import_stmt , ' (module_id) @mod ' , ' (module) @mod ' )
115118 end
116119 return sn (nil , {
117120 choice (1 , choices ),
0 commit comments