Skip to content

Commit

Permalink
feature: treesitter folding syntax highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-x committed Nov 9, 2023
1 parent 236fb06 commit 18304b8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
12 require('nvim-autopairs').setup{
# Navigator

- Source code analysis and navigate tool

Expand Down Expand Up @@ -102,7 +102,11 @@ variable is:

- ccls call hierarchy (Non-standard `ccls/call` API) supports

- Syntax folding based on treesitter or LSP_fold folding algorithm. (It behaves similar to vs-code); dedicated comment folding.
- Incorporates a modified folding algorithm based on treesitter or LSP_fold, providing a user experience comparable to Visual Studio Code.
- Ensures end or closing brackets remain visible.
- Features specific functionality for comment folding.
- Enables the display of folded lines.
- Includes syntax highlighting capabilities (supported in Neovim version 0.10.x and above).

- Treesitter symbols sidebar, LSP document symbole sidebar. Both with preview and folding

Expand Down Expand Up @@ -244,7 +248,7 @@ Nondefault configuration example:
```lua

require'navigator'.setup({
debug = false, -- log output, set to true and log path: ~/.cache/nvim/gh.log
debug = false, -- log output, set to true and log path: ~/.cache/nvim/gh.log
-- slowdownd startup and some actions
width = 0.75, -- max width ratio (number of cols for the floating window) / (window width)
height = 0.3, -- max list window height, 0.3 by default
Expand Down Expand Up @@ -557,7 +561,7 @@ LspDiagnosticsXXX are used for diagnostic. Please check highlight.lua and dochig

The plugin can be loaded lazily (packer `opt = true` ), And it will check if optional plugins existance and load those plugins only if they existed.

The terminal will need to be able to output nerdfont(v.3.0+) and emoji correctly. I am using Kitty with nerdfont (Victor Mono).
The terminal will need to be able to output nerdfont and emoji correctly. I am using Kitty with nerdfont (Victor Mono).

## Integrate with mason (williamboman/mason.nvim)

Expand Down
4 changes: 4 additions & 0 deletions lua/navigator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ _NgConfigValues = {
bracket_left = '',
bracket_right = '',
},
fold = {
prefix = '',
separator = '',
},

-- Treesitter
-- Note: many more node.type or kind may be available
Expand Down
41 changes: 37 additions & 4 deletions lua/navigator/foldts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,50 @@ function M.on_attach()
M.setup_fold()
-- M.update_folds()
end

function NG_custom_fold_text()
local prefix = _NgConfigValues.icons.fold.prefix
local sep = _NgConfigValues.icons.fold.separator
local function 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")
local ss, se = line:find('^%s*')
local spaces = line:sub(ss, se)
local tabspace = string.rep(' ', vim.o.tabstop)
spaces = spaces:gsub('\t', tabspace)
line = line:gsub('^%s*(.-)%s*$', '%1')
return spaces .. '' .. line .. ': ' .. line_count .. ' lines'
line = line:gsub('^%s*(.-)%s*$', '%1') -- trim leading and trailing whitespace
return spaces .. prefix .. line .. ': ' .. line_count .. ' lines'
end

function NG_custom_fold_text()
if vim.treesitter.foldtext then
local line_syntax = vim.treesitter.foldtext()
if type(line_syntax) ~= 'table' or #line_syntax < 1 then
return line_syntax
end

local line_count = vim.v.foldend - vim.v.foldstart + 1
if prefix ~= '' then
local spaces = line_syntax[1]
local s = spaces[1]
local first_char = s:sub(1, 1)
if first_char == '\t' then
local tabspace = string.rep(' ', vim.o.tabstop)
s = s:gsub('\t', tabspace)
end
s = s:gsub('^ ', prefix)
if s ~= spaces[1] then
spaces[1] = s
spaces[2] = { '@keyword' }
end
end
local sep2 = ' ' .. string.rep(sep, 3)
table.insert(line_syntax, { sep2, { '@comment' } })
table.insert(line_syntax, { ' ' .. tostring(line_count), { '@number' } })
table.insert(line_syntax, { ' lines', { '@comment' } })
table.insert(line_syntax, { sep, { '@comment' } })
return line_syntax
end
return custom_fold_text()
end

vim.opt.foldtext = NG_custom_fold_text()
Expand Down

0 comments on commit 18304b8

Please sign in to comment.