robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local api =vim . api 2local nvim_on =require ( 'vim._core.util' ). nvim_on 3 4local Range =require ( 'vim.treesitter._range' ) 5 6local M ={} 7 8---@class (private) vim.treesitter.dev.TSTreeView 9---@field ns integer API namespace 10---@field opts vim.treesitter.dev.TSTreeViewOpts 11---@field nodes vim.treesitter.dev.Node[] 12---@field named vim.treesitter.dev.Node[] 13local TSTreeView ={} 14 15---@class (private) vim.treesitter.dev.TSTreeViewOpts 16---@field anon boolean If true, display anonymous nodes. 17---@field lang boolean If true, display the language alongside each node. 18---@field indent integer Number of spaces to indent nested lines. 19 20---@class (private) vim.treesitter.dev.Node 21---@field node TSNode Treesitter node 22---@field field string? Node field 23---@field depth integer Depth of this node in the tree 24---@field text string? Text displayed in the inspector for this node. Not computed until the 25--- inspector is drawn. 26---@field lang string Source language of this node 27 28---@class (private) vim.treesitter.dev.Injection 29---@field lang string Source language of this injection 30---@field root TSNode Root node of the injection 31 32--- Traverse all child nodes starting at {node}. 33--- 34--- This is a recursive function. The {depth} parameter indicates the current recursion level. 35--- {lang} is a string indicating the language of the tree currently being traversed. Each traversed 36--- node is added to {tree}. When recursion completes, {tree} is an array of all nodes in the order 37--- they were visited. 38--- 39--- {injections} is a table mapping node ids from the primary tree to language tree injections. Each 40--- injected language has a series of trees nested within the primary language's tree, and the root 41--- node of each of these trees is contained within a node in the primary tree. The {injections} 42--- table maps nodes in the primary tree to root nodes of injected trees. 43--- 44---@param node TSNode Starting node to begin traversal |tsnode| 45---@param depth integer Current recursion depth 46---@param field string|nil The field of the current node 47---@param lang string Language of the tree currently being traversed 48---@param injections table<string, vim.treesitter.dev.Injection[]> Mapping of node ids to root nodes 49--- of injected language trees (see explanation above) 50---@param tree vim.treesitter.dev.Node[] Output table containing a list of tables each representing a node in the tree 51local function traverse ( node , depth , field , lang , injections , tree ) 52table . insert ( tree , { 53node =node , 54depth =depth , 55lang =lang , 56field =field , 57} ) 58 59for _ , injection in ipairs ( injections [ node : id ()] or {} ) do 60traverse ( injection . root , depth + 1 , nil , injection . lang , injections , tree ) 61end 62 63for child , child_field in node : iter_children () do 64traverse ( child , depth + 1 , child_field , lang , injections , tree ) 65end 66 67return tree 68end 69 70--- Create a new treesitter view. 71--- 72---@param buf integer Source buffer number 73---@param lang string|nil Language of source buffer 74--- 75---@return vim.treesitter.dev.TSTreeView|nil 76---@return string|nil Error message, if any 77--- 78---@package 79function TSTreeView : new ( buf , lang ) 80buf =buf or 0 81lang =lang or vim . treesitter . language . get_lang ( vim . bo [ buf ]. filetype ) 82local parser =vim . treesitter . get_parser ( buf , lang ) 83if not parser then 84return nil , 85string . format ( 'Failed to create TSTreeView for buffer %s: no parser for lang "%s"' , buf , lang ) 86end 87 88-- For each child tree (injected language), find the root of the tree and locate the node within 89-- the primary tree that contains that root. Add a mapping from the node in the primary tree to 90-- the root in the child tree to the {injections} table. 91local root =parser : parse ( true )[ 1 ]: root () 92local injections ={} ---@type table<string, table<string, TSNode>> 93 94parser : for_each_tree ( function ( parent_tree , parent_ltree ) 95local parent =parent_tree : root () 96local parent_range ={ parent : range () } 97for _ , child in pairs ( parent_ltree : children ()) do 98for _ , tree in pairs ( child : trees ()) do 99local r =tree : root () 100local r_range ={ r : range () } 101if Range . contains ( parent_range , r_range ) then 102local node =assert ( parent : named_descendant_for_range ( r : range ())) 103local id =node : id () 104local ilang =child : lang () 105injections [ id ] =injections [ id ] or {} 106local injection =injections [ id ][ ilang ] 107if not injection or r : byte_length () > injection : byte_length () then 108injections [ id ][ ilang ] =r 109end 110end 111end 112end 113end ) 114 115local sorted_injections ={} ---@type table<string, vim.treesitter.dev.Injection[]> 116for id , lang_injections in pairs ( injections ) do 117local langs =vim . tbl_keys ( lang_injections ) 118---@param a string 119---@param b string 120table . sort ( langs , function ( a , b ) 121return lang_injections [ a ]: byte_length () > lang_injections [ b ]: byte_length () 122end ) 123---@param ilang string 124sorted_injections [ id ] =vim . tbl_map ( function ( ilang ) 125return { lang =ilang , root =lang_injections [ ilang ] } 126end , langs ) 127end 128 129local nodes =traverse ( root , 0 , nil , parser : lang (), sorted_injections , {} ) 130 131local named ={} ---@type vim.treesitter.dev.Node[] 132for _ , v in ipairs ( nodes ) do 133if v . node : named () then 134named [ # named + 1 ] =v 135end 136end 137 138local t ={ 139ns =api . nvim_create_namespace ( 'nvim.treesitter.dev_inspect' ), 140nodes =nodes , 141named =named , 142---@type vim.treesitter.dev.TSTreeViewOpts 143opts ={ 144anon =false , 145lang =false , 146indent =2 , 147} , 148} 149 150self . __index =self 151return setmetatable ( t , self ) 152end 153 154local decor_ns =api . nvim_create_namespace ( 'nvim.treesitter.dev' ) 155 156---@param w integer 157---@return boolean closed Whether the window was closed. 158local function close_win ( w ) 159if api . nvim_win_is_valid ( w ) then 160api . nvim_win_close ( w , true ) 161return true 162end 163 164return false 165end 166 167---@param w integer 168---@param b integer 169---@param opts nil|{ indent?: integer } 170local function set_dev_options ( w , b , opts ) 171vim . wo [ w ][ 0 ]. scrolloff =5 172vim . wo [ w ][ 0 ]. wrap =false 173vim . wo [ w ][ 0 ]. foldmethod ='expr' 174vim . wo [ w ][ 0 ]. foldexpr ='v:lua.vim.treesitter.foldexpr()' -- explicitly set foldexpr 175vim . wo [ w ][ 0 ]. foldenable =false -- Don't fold on first open InspectTree 176vim . wo [ w ][ 0 ]. foldlevel =99 177vim . bo [ b ]. buflisted =false 178vim . bo [ b ]. buftype ='nofile' 179vim . bo [ b ]. bufhidden ='wipe' 180vim . bo [ b ]. filetype ='query' 181vim . bo [ b ]. swapfile =false 182 183opts =opts or {} 184if opts . indent then 185vim . bo [ b ]. shiftwidth =opts . indent 186end 187end 188 189--- Updates the cursor position in the inspector to match the node under the cursor. 190--- 191--- @param treeview vim.treesitter.dev.TSTreeView 192--- @param lang string? 193--- @param source_buf integer 194--- @param inspect_buf integer 195--- @param inspect_win integer 196--- @param pos? [integer, integer] 197local function set_inspector_cursor ( treeview , lang , source_buf , inspect_buf , inspect_win , pos ) 198api . nvim_buf_clear_namespace ( inspect_buf , treeview . ns , 0 , - 1 ) 199 200local cursor_node =vim . treesitter . get_node ( { 201bufnr =source_buf , 202lang =lang , 203pos =pos , 204ignore_injections =false , 205include_anonymous =treeview . opts . anon , 206} ) 207if not cursor_node then 208return 209end 210 211local cursor_node_id =cursor_node : id () 212for i , v in treeview : iter () do 213if v . node : id () == cursor_node_id then 214local start =v . depth * treeview . opts . indent ---@type integer 215local end_col =start + # v . text 216api . nvim_buf_set_extmark ( inspect_buf , treeview . ns , i - 1 , start , { 217end_col =end_col , 218hl_group ='Visual' , 219} ) 220api . nvim_win_set_cursor ( inspect_win , { i , 0 } ) 221break 222end 223end 224end 225 226--- Write the contents of this View into {bufnr}. 227--- 228--- Calling this function computes the text that is displayed for each node. 229--- 230---@param buf integer Buffer number to write into. 231---@package 232function TSTreeView : draw ( buf ) 233vim . bo [ buf ]. modifiable =true 234local lines ={} ---@type string[] 235local lang_hl_marks ={} ---@type table[] 236 237for i , item in self : iter () do 238local range_str =( '[%d, %d] - [%d, %d]' ): format ( item . node : range ()) 239local lang_str =self . opts . lang and string . format ( ' %s' , item . lang ) or '' 240 241local text ---@type string 242if item . node : named () then 243text =string . format ( '(%s%s' , item . node : missing () and 'MISSING ' or '' , item . node : type ()) 244else 245text =string . format ( '%q' , item . node : type ()): gsub ( '\n' , 'n' ) 246if item . node : missing () then 247text =string . format ( '(MISSING %s)' , text ) 248end 249end 250if item . field then 251text =string . format ( '%s: %s' , item . field , text ) 252end 253 254local next =self : get ( i + 1 ) 255if not next or next . depth <= item . depth then 256local parens =item . depth - ( next and next . depth or 0 ) + ( item . node : named () and 1 or 0 ) 257if parens > 0 then 258text =string . format ( '%s%s' , text , string . rep ( ')' , parens )) 259end 260end 261 262item . text =text 263 264local line =string . format ( 265'%s%s ; %s%s' , 266string . rep ( ' ' , item . depth * self . opts . indent ), 267text , 268range_str , 269lang_str 270) 271 272if self . opts . lang then 273lang_hl_marks [ # lang_hl_marks + 1 ] ={ 274col =# line - # lang_str , 275end_col =# line , 276} 277end 278 279lines [ i ] =line 280end 281 282api . nvim_buf_set_lines ( buf , 0 , - 1 , false , lines ) 283 284api . nvim_buf_clear_namespace ( buf , decor_ns , 0 , - 1 ) 285 286for i , m in ipairs ( lang_hl_marks ) do 287api . nvim_buf_set_extmark ( buf , decor_ns , i - 1 , m . col , { 288hl_group ='Title' , 289end_col =m . end_col , 290} ) 291end 292 293vim . bo [ buf ]. modifiable =false 294end 295 296--- Get node {i} from this View. 297--- 298--- The node number is dependent on whether or not anonymous nodes are displayed. 299--- 300---@param i integer Node number to get 301---@return vim.treesitter.dev.Node? 302---@package 303function TSTreeView : get ( i ) 304local t =self . opts . anon and self . nodes or self . named 305return t [ i ] 306end 307 308--- Iterate over all of the nodes in this View. 309--- 310---@return (fun(): integer, vim.treesitter.dev.Node) Iterator over all nodes in this View 311---@return table 312---@return integer 313---@package 314function TSTreeView : iter () 315-- TODO(lewis6991): EmmyLua 0.25.1's ipairs annotation omits the table and initial index. 316--- @diagnostic disable-next-line: missing-return-value 317return ipairs ( self . opts . anon and self . nodes or self . named ) 318end 319 320--- @class vim.treesitter.dev.inspect_tree.Opts 321--- @inlinedoc 322--- 323--- The language of the source buffer. If omitted, the filetype of the source 324--- buffer is used. 325--- @field lang string? 326--- 327--- Buffer to draw the tree into. If omitted, a new buffer is created. 328--- @field bufnr integer? 329--- 330--- Window id to display the tree buffer in. If omitted, a new window is 331--- created with {command}. 332--- @field winid integer? 333--- 334--- Vimscript command to create the window. Default value is "60vnew". 335--- Only used when {winid} is nil. 336--- @field command string? 337--- 338--- Title of the window. If a function, it accepts the buffer number of the 339--- source buffer as its only argument and should return a string. 340--- @field title? string|fun(bufnr:integer):string 341 342--- @nodoc 343--- @param opts vim.treesitter.dev.inspect_tree.Opts? 344function M . inspect_tree ( opts ) 345vim . validate ( 'opts' , opts , 'table' , true ) 346 347opts =opts or {} 348 349-- source buffer 350local buf =api . nvim_get_current_buf () 351 352-- window id for source buffer 353local win =api . nvim_get_current_win () 354local treeview , err =TSTreeView : new ( buf , opts . lang ) 355if err and err : match ( 'no parser for lang' ) then 356api . nvim_echo ( { { err , 'WarningMsg' } } , true ) 357return 358elseif not treeview then 359error ( err ) 360end 361 362-- Close any existing inspector window 363if vim . b [ buf ]. dev_inspect then 364close_win ( vim . b [ buf ]. dev_inspect ) 365end 366 367-- window id for tree buffer 368local w =opts . winid 369if not w then 370vim . cmd ( opts . command or '60vnew' ) 371w =api . nvim_get_current_win () 372end 373 374-- tree buffer 375local b =opts . bufnr 376if b then 377api . nvim_win_set_buf ( w , b ) 378else 379b =api . nvim_win_get_buf ( w ) 380end 381 382vim . b [ buf ]. dev_inspect =w 383vim . b [ b ]. dev_base =win -- base window handle 384vim . b [ b ]. disable_query_linter =true 385set_dev_options ( w , b , { indent =treeview . opts . indent } ) 386 387local title --- @type string? 388local opts_title =opts . title 389if not opts_title then 390local bufname =api . nvim_buf_get_name ( buf ) 391title =( 'Syntax tree for %s' ): format ( vim . fs . relpath ( '.' , bufname ) or bufname ) 392elseif type ( opts_title ) == 'function' then 393title =opts_title ( buf ) 394else 395title =opts_title 396end 397 398assert ( type ( title ) == 'string' , 'Window title must be a string' ) 399api . nvim_buf_set_name ( b , title ) 400 401treeview : draw ( b ) 402 403local cursor =api . nvim_win_get_cursor ( win ) 404set_inspector_cursor ( treeview , opts . lang , buf , b , w , { cursor [ 1 ] - 1 , cursor [ 2 ] } ) 405 406api . nvim_buf_clear_namespace ( buf , treeview . ns , 0 , - 1 ) 407api . nvim_buf_set_keymap ( b , 'n' , '<CR>' , '' , { 408desc ='Jump to the node under the cursor in the source buffer' , 409nowait =true , 410callback =function () 411local row =api . nvim_win_get_cursor ( w )[ 1 ] 412local lnum , col =assert ( treeview : get ( row )). node : start () 413 414-- update source window if original was closed 415if not api . nvim_win_is_valid ( win ) then 416win =assert ( vim . fn . win_findbuf ( buf )[ 1 ]) 417end 418 419api . nvim_set_current_win ( win ) 420api . nvim_win_set_cursor ( win , { lnum + 1 , col } ) 421end , 422} ) 423api . nvim_buf_set_keymap ( b , 'n' , 'a' , '' , { 424desc ='Toggle anonymous nodes' , 425nowait =true , 426callback =function () 427local row , col =unpack ( api . nvim_win_get_cursor ( w )) ---@type integer, integer 428local curnode =treeview : get ( row ) 429while curnode and not curnode . node : named () do 430row =row - 1 431curnode =treeview : get ( row ) 432end 433 434treeview . opts . anon =not treeview . opts . anon 435treeview : draw ( b ) 436 437if not curnode then 438return 439end 440 441local id =curnode . node : id () 442for i , node in treeview : iter () do 443if node . node : id () == id then 444api . nvim_win_set_cursor ( w , { i , col } ) 445break 446end 447end 448end , 449} ) 450api . nvim_buf_set_keymap ( b , 'n' , 'I' , '' , { 451desc ='Toggle language display' , 452nowait =true , 453callback =function () 454treeview . opts . lang =not treeview . opts . lang 455treeview : draw ( b ) 456end , 457} ) 458api . nvim_buf_set_keymap ( b , 'n' , 'o' , '' , { 459desc ='Toggle query editor' , 460nowait =true , 461callback =function () 462local edit_w =vim . b [ buf ]. dev_edit 463if not edit_w or not close_win ( edit_w ) then 464M . edit_query () 465end 466end , 467} ) 468api . nvim_buf_set_keymap ( b , 'n' , 'q' , '<Cmd>wincmd c<CR>' , { 469desc ='Close language tree window' , 470nowait =true , 471} ) 472 473local group =api . nvim_create_augroup ( 'nvim.treesitter.dev' ) 474 475nvim_on ( 'CursorMoved' , group , { buf =b } , function () 476if not api . nvim_buf_is_loaded ( buf ) then 477return true 478end 479 480w =api . nvim_get_current_win () 481api . nvim_buf_clear_namespace ( buf , treeview . ns , 0 , - 1 ) 482local row =api . nvim_win_get_cursor ( w )[ 1 ] 483local lnum , col , end_lnum , end_col =assert ( treeview : get ( row )). node : range () 484api . nvim_buf_set_extmark ( buf , treeview . ns , lnum , col , { 485end_row =end_lnum , 486end_col =math . max ( 0 , end_col ), 487hl_group ='Visual' , 488} ) 489 490-- update source window if original was closed 491if not api . nvim_win_is_valid ( win ) then 492win =assert ( vim . fn . win_findbuf ( buf )[ 1 ]) 493end 494 495local topline , botline =vim . fn . line ( 'w0' , win ), vim . fn . line ( 'w$' , win ) 496 497-- Move the cursor if highlighted range is completely out of view 498if lnum < topline and end_lnum < topline then 499api . nvim_win_set_cursor ( win , { end_lnum + 1 , 0 } ) 500elseif lnum > botline and end_lnum > botline then 501api . nvim_win_set_cursor ( win , { lnum + 1 , 0 } ) 502end 503end ) 504 505nvim_on ( 'CursorMoved' , group , { buf =buf } , function () 506if not api . nvim_buf_is_loaded ( b ) then 507return true 508end 509 510set_inspector_cursor ( treeview , opts . lang , buf , b , w ) 511end ) 512 513nvim_on ( { 'TextChanged' , 'InsertLeave' } , group , { buf =buf } , function () 514if not api . nvim_buf_is_loaded ( b ) then 515return true 516end 517 518local treeview_opts =treeview . opts 519treeview =assert ( TSTreeView : new ( buf , opts . lang )) 520treeview . opts =treeview_opts 521treeview : draw ( b ) 522end ) 523 524nvim_on ( 'BufLeave' , group , { buf =b } , function () 525if not api . nvim_buf_is_loaded ( buf ) then 526return true 527end 528api . nvim_buf_clear_namespace ( buf , treeview . ns , 0 , - 1 ) 529end ) 530 531nvim_on ( 'BufLeave' , group , { buf =buf } , function () 532if not api . nvim_buf_is_loaded ( b ) then 533return true 534end 535api . nvim_buf_clear_namespace ( b , treeview . ns , 0 , - 1 ) 536end ) 537 538nvim_on ( { 'BufHidden' , 'BufUnload' , 'QuitPre' } , group , { buf =buf } , function () 539-- don't close inpector window if source buffer 540-- has more than one open window 541if # vim . fn . win_findbuf ( buf ) > 1 then 542return 543end 544 545-- close all tree windows 546for _ , window in pairs ( vim . fn . win_findbuf ( b )) do 547close_win ( window ) 548end 549 550return true 551end ) 552end 553 554local edit_ns =api . nvim_create_namespace ( 'nvim.treesitter.dev_edit' ) 555 556---@param query_win integer 557---@param base_win integer 558---@param lang string 559local function update_editor_highlights ( query_win , base_win , lang ) 560local base_buf =api . nvim_win_get_buf ( base_win ) 561local query_buf =api . nvim_win_get_buf ( query_win ) 562local root_lang =vim . treesitter . language . get_lang ( vim . bo [ base_buf ]. filetype ) 563local parser =assert ( vim . treesitter . get_parser ( base_buf , root_lang )) 564api . nvim_buf_clear_namespace ( base_buf , edit_ns , 0 , - 1 ) 565local query_content =table . concat ( api . nvim_buf_get_lines ( query_buf , 0 , - 1 , false ), '\n' ) 566 567local ok_query , query =pcall ( vim . treesitter . query . parse , lang , query_content ) 568if not ok_query then 569return 570end 571 572local cursor_word =vim . fn . expand ( '<cword>' ) --[[@as string]] 573-- Only highlight captures if the cursor is on a capture name 574if cursor_word : find ( '^@' ) == nil then 575return 576end 577-- Remove the '@' from the cursor word 578cursor_word =cursor_word : sub ( 2 ) 579-- Parse buffer including injected languages. 580parser : parse ( true ) 581-- Query on the trees of the language requested to highlight captures. 582parser : for_each_tree ( function ( tree , ltree ) 583if ltree : lang () ~= lang then 584return 585end 586local root =tree : root () 587local topline , botline =vim . fn . line ( 'w0' , base_win ), vim . fn . line ( 'w$' , base_win ) 588for id , node , metadata in query : iter_captures ( root , base_buf , topline - 1 , botline ) do 589local capture_name =query . captures [ id ] 590if capture_name == cursor_word then 591local lnum , col , end_lnum , end_col = 592Range . unpack4 ( vim . treesitter . get_range ( node , base_buf , metadata [ id ])) 593 594api . nvim_buf_set_extmark ( base_buf , edit_ns , lnum , col , { 595end_row =end_lnum , 596end_col =end_col , 597hl_group ='Visual' , 598virt_text ={ 599{ capture_name , 'DiagnosticVirtualTextHint' } , 600} , 601} ) 602end 603end 604end ) 605end 606 607--- @nodoc 608--- @param lang? string language to open the query editor for. 609--- @return boolean? `true` on success, `nil` on failure 610--- @return string? error message, if applicable 611function M . edit_query ( lang ) 612local buf =api . nvim_get_current_buf () 613local win =api . nvim_get_current_win () 614 615-- Close any existing editor window 616if vim . b [ buf ]. dev_edit then 617close_win ( vim . b [ buf ]. dev_edit ) 618end 619 620local cmd ='60vnew' 621-- If the inspector is open, place the editor above it. 622local base_win =vim . b [ buf ]. dev_base ---@type integer? 623local base_buf =base_win and api . nvim_win_get_buf ( base_win ) 624local inspect_win =base_buf and vim . b [ base_buf ]. dev_inspect 625if base_win and base_buf and api . nvim_win_is_valid ( inspect_win ) then 626api . nvim_set_current_win ( inspect_win ) 627buf =base_buf 628win =base_win 629cmd ='new' 630end 631vim . cmd ( cmd ) 632 633local parser =vim . treesitter . get_parser ( buf , lang ) 634if not parser then 635return nil , 636string . format ( 'Failed to show query editor for buffer %s: no parser for lang "%s"' , buf , lang ) 637end 638lang =parser : lang () 639 640local query_win =api . nvim_get_current_win () 641local query_buf =api . nvim_win_get_buf ( query_win ) 642 643vim . b [ buf ]. dev_edit =query_win 644vim . bo [ query_buf ]. omnifunc =vim . treesitter . query . omnifunc 645set_dev_options ( query_win , query_buf ) 646 647-- Note that omnifunc guesses the language based on the containing folder, 648-- so we add the parser's language to the buffer's name so that omnifunc 649-- can infer the language later. 650api . nvim_buf_set_name ( query_buf , string . format ( '%s/query_editor.scm' , lang )) 651 652local group =api . nvim_create_augroup ( 'nvim.treesitter.dev_edit' ) 653nvim_on ( { 'TextChanged' , 'InsertLeave' } , group , { 654buf =query_buf , 655desc ='Update query editor diagnostics when the query changes' , 656} , function () 657vim . treesitter . query . lint ( query_buf , { langs =lang , clear =false } ) 658end ) 659nvim_on ( { 'TextChanged' , 'InsertLeave' , 'CursorMoved' , 'BufEnter' } , group , { 660buf =query_buf , 661desc ='Update query editor highlights when the cursor moves' , 662} , function () 663if api . nvim_win_is_valid ( win ) then 664update_editor_highlights ( query_win , win , lang ) 665end 666end ) 667nvim_on ( 'BufLeave' , group , { 668buf =query_buf , 669desc ='Clear highlights when leaving the query editor' , 670} , function () 671api . nvim_buf_clear_namespace ( buf , edit_ns , 0 , - 1 ) 672end ) 673nvim_on ( 'BufLeave' , group , { 674buf =buf , 675desc ='Clear the query editor highlights when leaving the source buffer' , 676} , function () 677if not api . nvim_buf_is_loaded ( query_buf ) then 678return true 679end 680 681api . nvim_buf_clear_namespace ( query_buf , edit_ns , 0 , - 1 ) 682end ) 683nvim_on ( { 'BufHidden' , 'BufUnload' } , group , { 684buf =buf , 685desc ='Close the editor window when the source buffer is hidden or unloaded' , 686once =true , 687} , function () 688close_win ( query_win ) 689end ) 690 691api . nvim_buf_set_lines ( query_buf , 0 , - 1 , false , { 692';; Write queries here (see $VIMRUNTIME/queries/ for examples).' , 693';; Move cursor to a capture ("@foo") to highlight matches in the source buffer.' , 694';; Completion for grammar nodes is available (:help compl-omni)' , 695'' , 696'' , 697} ) 698vim . cmd ( 'normal! G' ) 699vim . cmd . startinsert () 700 701return true 702end 703 704return M