@@ -13,6 +13,7 @@ local Path = require('plenary.path')
1313local os_sep = Path .path .sep
1414
1515local flatten = vim .tbl_flatten
16+ local filter = vim .tbl_filter
1617
1718local files = {}
1819
2930
3031-- Special keys:
3132-- opts.search_dirs -- list of directory to search in
33+ -- opts.grep_open_files -- boolean to restrict search to open files
3234files .live_grep = function (opts )
3335 local vimgrep_arguments = opts .vimgrep_arguments or conf .vimgrep_arguments
3436 local search_dirs = opts .search_dirs
35- opts .cwd = opts .cwd and vim .fn .expand (opts .cwd )
37+ local grep_open_files = opts .grep_open_files
38+ opts .cwd = opts .cwd and vim .fn .expand (opts .cwd ) or vim .loop .cwd ()
39+
40+ local filelist = {}
41+
42+ if grep_open_files then
43+ local bufnrs = filter (function (b )
44+ if 1 ~= vim .fn .buflisted (b ) then return false end
45+ return true
46+ end , vim .api .nvim_list_bufs ())
47+ if not next (bufnrs ) then return end
48+
49+ local tele_path = require ' telescope.path'
50+ for _ , bufnr in ipairs (bufnrs ) do
51+ local file = vim .api .nvim_buf_get_name (bufnr )
52+ table.insert (filelist , tele_path .make_relative (file , opts .cwd ))
53+ end
54+ end
3655
3756 if search_dirs then
3857 for i , path in ipairs (search_dirs ) do
@@ -49,15 +68,19 @@ files.live_grep = function(opts)
4968
5069 prompt = escape_chars (prompt )
5170
52- local args = flatten { vimgrep_arguments , prompt }
71+ local search_list = { }
5372
5473 if search_dirs then
55- table.insert (args , search_dirs )
74+ table.insert (search_list , search_dirs )
5675 elseif os_sep == ' \\ ' then
57- table.insert (args , ' .' )
76+ table.insert (search_list , ' .' )
5877 end
5978
60- return args
79+ if grep_open_files then
80+ search_list = filelist
81+ end
82+
83+ return flatten { vimgrep_arguments , prompt , search_list }
6184 end ,
6285 opts .entry_maker or make_entry .gen_from_vimgrep (opts ),
6386 opts .max_results ,
@@ -72,6 +95,7 @@ files.live_grep = function(opts)
7295 }):find ()
7396end
7497
98+
7599-- Special keys:
76100-- opts.search -- the string to search.
77101-- opts.search_dirs -- list of directory to search in
@@ -211,16 +235,16 @@ end
211235files .file_browser = function (opts )
212236 opts = opts or {}
213237
238+ opts .depth = opts .depth or 1
214239 opts .cwd = opts .cwd and vim .fn .expand (opts .cwd ) or vim .loop .cwd ()
215-
216- local gen_new_finder = function (path )
240+ opts .new_finder = opts .new_finder or function (path )
217241 opts .cwd = path
218242 local data = {}
219243
220244 scan .scan_dir (path , {
221245 hidden = opts .hidden or false ,
222246 add_dirs = true ,
223- depth = 1 ,
247+ depth = opts . depth ,
224248 on_insert = function (entry , typ )
225249 table.insert (data , typ == ' directory' and (entry .. os_sep ) or entry )
226250 end
@@ -242,8 +266,8 @@ files.file_browser = function(opts)
242266 end
243267
244268 pickers .new (opts , {
245- prompt_title = ' Find Files ' ,
246- finder = gen_new_finder (opts .cwd ),
269+ prompt_title = ' File Browser ' ,
270+ finder = opts . new_finder (opts .cwd ),
247271 previewer = conf .file_previewer (opts ),
248272 sorter = conf .file_sorter (opts ),
249273 attach_mappings = function (prompt_bufnr , map )
@@ -253,7 +277,7 @@ files.file_browser = function(opts)
253277 local new_cwd = vim .fn .expand (action_state .get_selected_entry ().path :sub (1 , - 2 ))
254278 local current_picker = action_state .get_current_picker (prompt_bufnr )
255279 current_picker .cwd = new_cwd
256- current_picker :refresh (gen_new_finder (new_cwd ), { reset_prompt = true })
280+ current_picker :refresh (opts . new_finder (new_cwd ), { reset_prompt = true })
257281 end )
258282
259283 local create_new_file = function ()
@@ -276,7 +300,7 @@ files.file_browser = function(opts)
276300 Path :new (fpath :sub (1 , - 2 )):mkdir ({ parents = true })
277301 local new_cwd = vim .fn .expand (fpath )
278302 current_picker .cwd = new_cwd
279- current_picker :refresh (gen_new_finder (new_cwd ), { reset_prompt = true })
303+ current_picker :refresh (opts . new_finder (new_cwd ), { reset_prompt = true })
280304 end
281305 end
282306
@@ -332,17 +356,65 @@ files.current_buffer_fuzzy_find = function(opts)
332356 -- All actions are on the current buffer
333357 local bufnr = vim .api .nvim_get_current_buf ()
334358 local filename = vim .fn .expand (vim .api .nvim_buf_get_name (bufnr ))
359+ local filetype = vim .api .nvim_buf_get_option (bufnr , " filetype" )
335360
336361 local lines = vim .api .nvim_buf_get_lines (0 , 0 , - 1 , false )
337362 local lines_with_numbers = {}
338363
339364 for lnum , line in ipairs (lines ) do
340365 table.insert (lines_with_numbers , {
341366 lnum = lnum ,
342- line = line ,
343367 bufnr = bufnr ,
344368 filename = filename ,
369+ text = line ,
370+ })
371+ end
372+
373+ local ok , parser = pcall (vim .treesitter .get_parser , bufnr , filetype )
374+ if ok then
375+ local query = vim .treesitter .get_query (filetype , " highlights" )
376+
377+ local root = parser :parse ()[1 ]:root ()
378+
379+ local highlighter = vim .treesitter .highlighter .new (parser )
380+ local highlighter_query = highlighter :get_query (filetype )
381+
382+ local line_highlights = setmetatable ({}, {
383+ __index = function (t , k )
384+ local obj = {}
385+ rawset (t , k , obj )
386+ return obj
387+ end ,
345388 })
389+ for id , node in query :iter_captures (root , bufnr , 0 , - 1 ) do
390+ local hl = highlighter_query .hl_cache [id ]
391+ if hl then
392+ local row1 , col1 , row2 , col2 = node :range ()
393+
394+ if row1 == row2 then
395+ local row = row1 + 1
396+
397+ for index = col1 , col2 do
398+ line_highlights [row ][index ] = hl
399+ end
400+ else
401+ local row = row1 + 1
402+ for index = col1 , # lines [row ] do
403+ line_highlights [row ][index ] = hl
404+ end
405+
406+ while row < row2 + 1 do
407+ row = row + 1
408+
409+ for index = 0 , # lines [row ] do
410+ line_highlights [row ][index ] = hl
411+ end
412+ end
413+ end
414+ end
415+ end
416+
417+ opts .line_highlights = line_highlights
346418 end
347419
348420 pickers .new (opts , {
0 commit comments