From 0a77fd79e1621b909ec869dbc307d0f36a8e288d Mon Sep 17 00:00:00 2001 From: Sergey Tarasov Date: Wed, 10 Jul 2024 12:41:59 +0300 Subject: [PATCH 1/6] Add telescope highlighting --- lua/telescope/_extensions/aerial.lua | 66 +++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/lua/telescope/_extensions/aerial.lua b/lua/telescope/_extensions/aerial.lua index c0634fa1..2ced95b2 100644 --- a/lua/telescope/_extensions/aerial.lua +++ b/lua/telescope/_extensions/aerial.lua @@ -5,6 +5,7 @@ local pickers = require("telescope.pickers") local telescope = require("telescope") local ext_config = { + only_lines = false, show_lines = true, show_nesting = { ["_"] = false, @@ -57,8 +58,51 @@ local function aerial_picker(opts) items = layout, }) + local function collect_buf_highlights() + local ts_parsers = require("nvim-treesitter.parsers") + local ts_query = require("nvim-treesitter.query") + + local lang = vim.bo[bufnr].filetype + local parser = ts_parsers.get_parser(bufnr, lang) + local tree = parser:trees()[1] -- get already parsed cached tree + local root = tree:root() + + local highlights = {} + local query = ts_query.get_query(lang, 'highlights') + if query then + for _, captures, _ in query:iter_matches(root, bufnr, 0, -1) do + for id, node in pairs(captures) do + table.insert(highlights, { capture = query.captures[id], range = { node:range() } }) + end + end + end + return highlights + end + local buf_highlights = collect_buf_highlights() + + local function highlights_for_row(row, offset) + offset = offset or 0 + local highlights = {} + for _, value in ipairs(buf_highlights) do + local start_row, start_col, end_row, end_col = unpack(value.range) + + if start_row == row then + local type = value.capture:gsub("%..*", "") -- strip subtypes after dot + table.insert(highlights, { { start_col + offset, end_col + offset }, type }) + end + end + return highlights + end + local function make_display(entry) local item = entry.value + local row = item.lnum - 1 + + if opts.only_lines or ext_config.only_lines then + local text = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or "" + return text, highlights_for_row(row) + end + local icon = config.get_icon(bufnr, item.kind) local icon_hl = highlight.get_highlight(item, true, false) or "NONE" local name_hl = highlight.get_highlight(item, false, false) or "NONE" @@ -66,12 +110,21 @@ local function aerial_picker(opts) { icon, icon_hl }, { entry.name, name_hl }, } - if ext_config.show_lines then - local text = vim.api.nvim_buf_get_lines(bufnr, item.lnum - 1, item.lnum, false)[1] or "" - text = vim.trim(text) - table.insert(columns, text) + + local highlights = {} + if opts.show_lines or ext_config.show_lines then + local text = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or "" + table.insert(columns, vim.trim(text)) + + local leading_spaces = text:match("^%s*") + local offset = layout[1].width + layout[2].width - #leading_spaces + 5 + if #entry.name > layout[2].width then + offset = offset + 2 + end + highlights = highlights_for_row(row, offset) end - return displayer(columns) + + return displayer(columns), highlights end local function make_entry(item) @@ -114,7 +167,8 @@ local function aerial_picker(opts) end -- Reverse the symbols so they have the same top-to-bottom order as in the file - if conf.sorting_strategy == "descending" then + local sorting_strategy = opts.sorting_strategy or conf.sorting_strategy + if sorting_strategy == "descending" then util.tbl_reverse(results) default_selection_index = #results - (default_selection_index - 1) end From f008624bd646766b15bd6a962b1f6b5b2d9da7d5 Mon Sep 17 00:00:00 2001 From: Sergey Tarasov Date: Thu, 11 Jul 2024 22:38:59 +0300 Subject: [PATCH 2/6] Fix no TS language parser edge case --- lua/telescope/_extensions/aerial.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/telescope/_extensions/aerial.lua b/lua/telescope/_extensions/aerial.lua index 2ced95b2..6aeac55c 100644 --- a/lua/telescope/_extensions/aerial.lua +++ b/lua/telescope/_extensions/aerial.lua @@ -64,6 +64,8 @@ local function aerial_picker(opts) local lang = vim.bo[bufnr].filetype local parser = ts_parsers.get_parser(bufnr, lang) + if not parser then return {} end + local tree = parser:trees()[1] -- get already parsed cached tree local root = tree:root() From 92695ca9d36359ae08e110bc06346ab49fb560fc Mon Sep 17 00:00:00 2001 From: Sergey Tarasov Date: Thu, 11 Jul 2024 22:59:21 +0300 Subject: [PATCH 3/6] Fix language detection for non-matching languages --- lua/telescope/_extensions/aerial.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lua/telescope/_extensions/aerial.lua b/lua/telescope/_extensions/aerial.lua index 6aeac55c..d5027a19 100644 --- a/lua/telescope/_extensions/aerial.lua +++ b/lua/telescope/_extensions/aerial.lua @@ -62,12 +62,11 @@ local function aerial_picker(opts) local ts_parsers = require("nvim-treesitter.parsers") local ts_query = require("nvim-treesitter.query") - local lang = vim.bo[bufnr].filetype - local parser = ts_parsers.get_parser(bufnr, lang) + local parser = ts_parsers.get_parser(bufnr) if not parser then return {} end - local tree = parser:trees()[1] -- get already parsed cached tree - local root = tree:root() + local lang = parser:lang() + local root = parser:trees()[1]:root() -- get root of already parsed cached tree local highlights = {} local query = ts_query.get_query(lang, 'highlights') From 2d9182723831bcff8e9c3150f7aefb3557b19328 Mon Sep 17 00:00:00 2001 From: Sergey Tarasov Date: Mon, 15 Jul 2024 23:02:36 +0300 Subject: [PATCH 4/6] Remove nvim-treesitter dependency, streamline logic --- lua/telescope/_extensions/aerial.lua | 29 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lua/telescope/_extensions/aerial.lua b/lua/telescope/_extensions/aerial.lua index d5027a19..9b816a4a 100644 --- a/lua/telescope/_extensions/aerial.lua +++ b/lua/telescope/_extensions/aerial.lua @@ -59,21 +59,22 @@ local function aerial_picker(opts) }) local function collect_buf_highlights() - local ts_parsers = require("nvim-treesitter.parsers") - local ts_query = require("nvim-treesitter.query") - - local parser = ts_parsers.get_parser(bufnr) - if not parser then return {} end + local parser = vim.treesitter.get_parser(bufnr) + if not parser then + return {} + end local lang = parser:lang() local root = parser:trees()[1]:root() -- get root of already parsed cached tree local highlights = {} - local query = ts_query.get_query(lang, 'highlights') + local query = vim.treesitter.query.get(lang, "highlights") if query then for _, captures, _ in query:iter_matches(root, bufnr, 0, -1) do for id, node in pairs(captures) do - table.insert(highlights, { capture = query.captures[id], range = { node:range() } }) + local start_row, start_col, _, end_col = node:range() + highlights[start_row] = highlights[start_row] or {} + table.insert(highlights[start_row], { start_col, end_col, query.captures[id] }) end end end @@ -83,14 +84,12 @@ local function aerial_picker(opts) local function highlights_for_row(row, offset) offset = offset or 0 + local row_highlights = buf_highlights[row] or {} local highlights = {} - for _, value in ipairs(buf_highlights) do - local start_row, start_col, end_row, end_col = unpack(value.range) - - if start_row == row then - local type = value.capture:gsub("%..*", "") -- strip subtypes after dot - table.insert(highlights, { { start_col + offset, end_col + offset }, type }) - end + for _, value in ipairs(row_highlights) do + local start_col, end_col, hl_type = unpack(value) + hl_type = hl_type:match("^[^.]+") -- strip subtypes after dot + table.insert(highlights, { { start_col + offset, end_col + offset }, hl_type }) end return highlights end @@ -118,7 +117,7 @@ local function aerial_picker(opts) table.insert(columns, vim.trim(text)) local leading_spaces = text:match("^%s*") - local offset = layout[1].width + layout[2].width - #leading_spaces + 5 + local offset = layout[1].width + layout[2].width - #leading_spaces + #icon if #entry.name > layout[2].width then offset = offset + 2 end From e55288cff1e57f3ba2ae14fa5a53167e6c783c95 Mon Sep 17 00:00:00 2001 From: Sergey Tarasov Date: Tue, 16 Jul 2024 11:47:19 +0300 Subject: [PATCH 5/6] Change to show_columns option --- lua/telescope/_extensions/aerial.lua | 31 +++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lua/telescope/_extensions/aerial.lua b/lua/telescope/_extensions/aerial.lua index 9b816a4a..da4655e6 100644 --- a/lua/telescope/_extensions/aerial.lua +++ b/lua/telescope/_extensions/aerial.lua @@ -5,8 +5,8 @@ local pickers = require("telescope.pickers") local telescope = require("telescope") local ext_config = { - only_lines = false, - show_lines = true, + -- show_lines = true, -- deprecated in favor of show_columns + show_columns = "both", -- { "symbols", "lines", "both" } show_nesting = { ["_"] = false, json = true, @@ -27,6 +27,19 @@ local function aerial_picker(opts) local filename = vim.api.nvim_buf_get_name(0) local filetype = vim.bo[bufnr].filetype local show_nesting = ext_config.show_nesting[filetype] + + local show_columns = opts.show_columns or conf.show_columns + local show_lines = opts.show_lines or conf.show_lines -- show_lines is deprecated + if show_columns == nil then + if show_lines == true then + show_columns = "both" + elseif show_lines == false then + show_columns = "symbols" + else + show_columns = ext_config.show_columns + end + end + if show_nesting == nil then show_nesting = ext_config.show_nesting["_"] end @@ -40,7 +53,7 @@ local function aerial_picker(opts) end local layout - if ext_config.show_lines then + if show_columns == "both" then layout = { { width = 4 }, { width = 30 }, @@ -80,7 +93,11 @@ local function aerial_picker(opts) end return highlights end - local buf_highlights = collect_buf_highlights() + + local buf_highlights = {} + if show_columns == "lines" or show_columns == "both" then + buf_highlights = collect_buf_highlights() -- collect buffer highlights only if needed + end local function highlights_for_row(row, offset) offset = offset or 0 @@ -98,7 +115,7 @@ local function aerial_picker(opts) local item = entry.value local row = item.lnum - 1 - if opts.only_lines or ext_config.only_lines then + if show_columns == "lines" then local text = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or "" return text, highlights_for_row(row) end @@ -112,14 +129,14 @@ local function aerial_picker(opts) } local highlights = {} - if opts.show_lines or ext_config.show_lines then + if show_columns == "both" then local text = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or "" table.insert(columns, vim.trim(text)) local leading_spaces = text:match("^%s*") local offset = layout[1].width + layout[2].width - #leading_spaces + #icon if #entry.name > layout[2].width then - offset = offset + 2 + offset = offset + 2 -- '...' symbol end highlights = highlights_for_row(row, offset) end From ee1fc3eb8c1342175dba8f1537ea69f751262a9a Mon Sep 17 00:00:00 2001 From: Sergey Tarasov Date: Tue, 16 Jul 2024 13:21:27 +0300 Subject: [PATCH 6/6] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d456ce70..9ee80cd2 100644 --- a/README.md +++ b/README.md @@ -613,6 +613,8 @@ require("telescope").setup({ json = true, -- You can set the option for specific filetypes yaml = true, }, + -- Available modes: symbols, lines, both + show_columns = "both", }, }, })