diff --git a/README.md b/README.md index 2587124..25b6367 100644 --- a/README.md +++ b/README.md @@ -564,9 +564,15 @@ You can load a different font size for floating win ![multigrid2](https://user-images.githubusercontent.com/1681295/139196378-bf69ade9-c916-42a9-a91f-cccb39b9c4eb.jpg) -### Document Symbol +### Document Symbol and navigate through the list -![document symbol](https://github.com/ray-x/files/blob/master/img/navigator/doc_symbol.gif?raw=true) +![doc_symbol_and_navigate](https://user-images.githubusercontent.com/1681295/148642747-1870b1a4-67c2-4a0d-8a41-d462ecdc663e.gif) +The key binding to navigate in the list. + +- up and down key +- `` for page up and down +- number key 1~9 go to the ith item. +- If there are loads of results, would be good to use fzy search prompt to filter out the result you are interested. ### Workspace Symbol @@ -667,9 +673,11 @@ Codelens for C++/ccls. Symbol reference ### VS-code style folding with treesitter #### folding function + ![image](https://user-images.githubusercontent.com/1681295/148491596-6cd6c507-c157-4536-b8c4-dc969436763a.png) #### folding comments + ![image](https://user-images.githubusercontent.com/1681295/148491845-5ffb18ea-f05d-4229-aec3-aa635b3de814.png) # Debug the plugin diff --git a/lua/navigator.lua b/lua/navigator.lua index 45816fa..ca41788 100644 --- a/lua/navigator.lua +++ b/lua/navigator.lua @@ -20,6 +20,8 @@ _NgConfigValues = { external = nil, -- true: enable for goneovim multigrid otherwise false border = 'single', -- border style, can be one of 'none', 'single', 'double', "shadow" + lines_show_prompt = 10, -- when the result list items number more than lines_show_prompt, + -- fuzzy finder prompt will be shown combined_attach = 'both', -- both: use both customized attach and navigator default attach, mine: only use my attach defined in vimrc on_attach = function(client, bufnr) -- your on_attach will be called at end of navigator on_attach diff --git a/lua/navigator/foldts.lua b/lua/navigator/foldts.lua index f1d0e9d..d96fe0f 100644 --- a/lua/navigator/foldts.lua +++ b/lua/navigator/foldts.lua @@ -1,11 +1,10 @@ -local log = require"navigator.util".log - -- NOTE: this file is a modified version of fold.lua from nvim-treesitter +local log = require('navigator.util').log local api = vim.api -local tsutils = require "nvim-treesitter.ts_utils" -local query = require "nvim-treesitter.query" -local parsers = require "nvim-treesitter.parsers" +local tsutils = require('nvim-treesitter.ts_utils') +local query = require('nvim-treesitter.query') +local parsers = require('nvim-treesitter.parsers') local get_node_at_line = require('navigator.treesitter').get_node_at_line local M = {} @@ -21,27 +20,27 @@ function _G.custom_fold_text() local line = vim.fn.getline(vim.v.foldstart) local line_count = vim.v.foldend - vim.v.foldstart + 1 -- log("" .. line .. " // " .. line_count .. " lines") - return " ⚡" .. line .. ": " .. line_count .. " lines" + return ' ⚡' .. line .. ': ' .. line_count .. ' lines' end vim.opt.foldtext = custom_fold_text() -vim.opt.fillchars = {eob = "-", fold = " "} +vim.opt.fillchars = { eob = '-', fold = ' ' } -vim.opt.viewoptions:remove("options") +vim.opt.viewoptions:remove('options') function M.setup_fold() if not parsers.has_parser() then - vim.notify("treesitter folding not enabled for current file", vim.lsp.log_levels.WARN) + vim.notify('treesitter folding not enabled for current file', vim.lsp.log_levels.WARN) return end - log("setup treesitter folding") - api.nvim_command("augroup FoldingCommand") - api.nvim_command("autocmd! * ") - api.nvim_command("augroup end") + log('setup treesitter folding') + api.nvim_command('augroup FoldingCommand') + api.nvim_command('autocmd! * ') + api.nvim_command('augroup end') vim.opt.foldtext = 'v:lua.custom_fold_text()' - vim.opt.fillchars = {eob = "-", fold = " "} - vim.opt.viewoptions:remove("options") + vim.opt.fillchars = { eob = '-', fold = ' ' } + vim.opt.viewoptions:remove('options') local current_window = api.nvim_get_current_win() api.nvim_win_set_option(current_window, 'foldmethod', 'expr') @@ -51,7 +50,7 @@ end -- This is cached on buf tick to avoid computing that multiple times -- Especially not for every line in the file when `zx` is hit local folds_levels = tsutils.memoize_by_buf_tick(function(bufnr) - local max_fold_level = api.nvim_win_get_option(0, "foldnestmax") + local max_fold_level = api.nvim_win_get_option(0, 'foldnestmax') local trim_level = function(level) if level > max_fold_level then return max_fold_level @@ -62,15 +61,15 @@ local folds_levels = tsutils.memoize_by_buf_tick(function(bufnr) local parser = parsers.get_parser(bufnr) if not parser then - warn("treesitter parser not loaded") + warn('treesitter parser not loaded') return {} end local matches = query.get_capture_matches_recursively(bufnr, function(lang) if query.has_folds(lang) then - return "@fold", "folds" + return '@fold', 'folds' elseif query.has_locals(lang) then - return "@scope", "locals" + return '@scope', 'locals' end end) @@ -81,7 +80,7 @@ local folds_levels = tsutils.memoize_by_buf_tick(function(bufnr) local prev_start = -1 local prev_stop = -1 - local min_fold_lines = api.nvim_win_get_option(0, "foldminlines") + local min_fold_lines = api.nvim_win_get_option(0, 'foldminlines') for _, node in ipairs(matches) do local start, _, stop, stop_col = node.node:range() @@ -112,52 +111,57 @@ local folds_levels = tsutils.memoize_by_buf_tick(function(bufnr) local node, _ = get_node_at_line(lnum + 1) -- log(lnum, node:type()) local comment = node:type() == 'comment' + local last_trimmed_level = trim_level(current_level) current_level = current_level + (start_counts[lnum] or 0) local trimmed_level = trim_level(current_level) current_level = current_level - (stop_counts[lnum] or 0) local next_trimmed_level = trim_level(current_level) - -- Determine if it's the start/end of a fold - -- NB: vim's fold-expr interface does not have a mechanism to indicate that - -- two (or more) folds start at this line, so it cannot distinguish between - -- ( \n ( \n )) \n (( \n ) \n ) - -- versus - -- ( \n ( \n ) \n ( \n ) \n ) - -- If it did have such a mechansim, (trimmed_level - last_trimmed_level) - -- would be the correct number of starts to pass on. if comment then if lnum == 0 or levels[lnum] == tostring(trimmed_level) then - levels[lnum + 1] = ">" .. tostring(trimmed_level + 1) -- allow comment fold independtly + levels[lnum + 1] = '>' .. tostring(trimmed_level + 1) -- allow comment fold independtly else levels[lnum + 1] = tostring(trimmed_level + 1) -- allow comment fold independtly end else + -- Determine if it's the start/end of a fold + -- NB: vim's fold-expr interface does not have a mechanism to indicate that + -- two (or more) folds start at this line, so it cannot distinguish between + -- ( \n ( \n )) \n (( \n ) \n ) + -- versus + -- ( \n ( \n ) \n ( \n ) \n ) + -- If it did have such a mechansim, (trimmed_level - last_trimmed_level) + -- would be the correct number of starts to pass on. + levels[lnum + 1] = tostring(trimmed_level) + if trimmed_level - last_trimmed_level > 0 then - levels[lnum + 1] = tostring(trimmed_level - 1) -- hack - levels[lnum + 2] = ">" .. tostring(trimmed_level) -- dirty hack + levels[lnum + 1] = tostring(trimmed_level) -- hack + levels[lnum + 2] = '>' .. tostring(trimmed_level + 1) -- dirty hack elseif trimmed_level - next_trimmed_level > 0 then -- Ending marks tend to confuse vim more than it helps, particularly when -- the fold level changes by at least 2; we can uncomment this if -- vim's behavior gets fixed. if lnum ~= 0 then - levels[lnum] = "<" .. tostring(trimmed_level) + levels[lnum] = tostring(trimmed_level + 1) end - levels[lnum + 1] = tostring(trimmed_level - 1) + levels[lnum + 1] = tostring(trimmed_level) else - if levels[lnum + 1] == nil then - levels[lnum + 1] = tostring(trimmed_level) - end + -- if levels[lnum + 1] == nil then + levels[lnum + 1] = tostring(trimmed_level + 1) + -- end end end - end - -- log(levels) + log(levels) return levels end) function M.get_fold_indic(lnum) + if not parsers.has_parser() or not lnum then + return '0' + end local buf = api.nvim_get_current_buf() local shown = false for i = 1, vim.fn.tabpagenr('$') do @@ -168,12 +172,12 @@ function M.get_fold_indic(lnum) end end if not shown then - return "0" + return '0' end local levels = folds_levels(buf) or {} -- log(lnum, levels[lnum]) -- TODO: comment it out in master - return levels[lnum] or "0" + return levels[lnum] or '0' end return M diff --git a/lua/navigator/gui.lua b/lua/navigator/gui.lua index 10b1cbd..f59a9d4 100644 --- a/lua/navigator/gui.lua +++ b/lua/navigator/gui.lua @@ -42,6 +42,10 @@ function M.new_list_view(opts) end opts.transparency = _NgConfigValues.transparency + if #items >= _NgConfigValues.lines_show_prompt then + opts.prompt = true + end + opts.external = _NgConfigValues.external opts.preview_lines_before = 3 log(opts)