Skip to content

Commit

Permalink
feat(treesitter)!: enable treesitter for lines|blines|git_blame
Browse files Browse the repository at this point in the history
Semi-breaking (backward compat): builtin previewer `enable = ...`
options renamed to `enabled = ...` for consistency with other plugins
conventions:
- previewers.builtin.treesitter.enable -> enabled
- previewers.builtin.treesitter.disable -> disabled
- previewers.builtin.render_markdown.enable -> enabled

Warning messages will be sent to the message log about old deprecated
options (old ones too), can be disabled with `silent=true`:
```lua
require("fzf-lua").setup({ defaults = { silent = true } })
```

This commits also includes many other changes:
- `winopts.treesitter` can also be enabled (disabled by default)
  in grep/LSP pickers, but will be disabled in `live_grep` as ts
  highlights will override the regex highlighting
- When enabled `fzf_colors` will automatically set `hl,hl+` to
  `-1:reverse` which changes the background of the character,
  this is done to make the TS highlights more distingished
  NOTE1: `reverse` doesn't work as well with skim
  NOTE2: To disable set `winopts.treesitter.fzf_colors=false`
- Formatting changes for `lines,blines`, will match the formatting
  of `fzf.vim`, `treesitter` picker formatting slightly improved
- New highlights:
    + `FzfLuaBufId`: linked to `TabLine` by default, used to highlight
       the bufnr in `lines`
    + `FzfLuaBufLineNr`: linked to `LineNr` by default, used in
       `lines,blines,treesitter` for line numbers
- New option: `lines.show_bufname`, default value is `120`, will only
  display buffer name (filepath) if neovim's `columns > show_bufname`
- Separated `grep_curbuf|lgrep_curbuf` options to its own category
- Disabled with fzf-tmux profile as it does nothing (not a neovim
  buffer and thus can't enable the TS injector)
- Fixed Telescope profile gutter color (transparent)
- Highlights picker fuzzy matching colors set to "-1:reverse"
- Added `utf8.lua` library (unsued for now)
  • Loading branch information
ibhagwan committed Dec 17, 2024
1 parent 71de69c commit 7836b37
Show file tree
Hide file tree
Showing 16 changed files with 1,631 additions and 333 deletions.
20 changes: 16 additions & 4 deletions OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,25 +546,37 @@ Interactive headers description highlight group, e.g. `<ctrl-g> to Disable .giti

Type: `string`, Default: `FzfLuaPathLineNr`

Highlight group for the line part of paths, e.g. `file:<line>:<col>:`, used in pickers such as `buffers`, `lines`, `quickfix`, `lsp`, `diagnostics`, etc.
Highlight group for the line part of paths, e.g. `file:<line>:<col>:`, used in pickers such as `buffers`, `quickfix`, `lsp`, `diagnostics`, etc.

#### globals.hls.path_colnr

Type: `string`, Default: `FzfLuaPathColNr`

Highlight group for the column part of paths, e.g. `file:<line>:<col>:`, used in pickers such as `buffers`, `lines`, `quickfix`, `lsp`, `diagnostics`, etc.
Highlight group for the column part of paths, e.g. `file:<line>:<col>:`, used in pickers such as `buffers`, `quickfix`, `lsp`, `diagnostics`, etc.

#### globals.hls.buf_name

Type: `string`, Default: `FzfLuaBufName`

Highlight group for buffer name in `lines`.
Highlight group for buffer name (filepath) in `lines`.

#### globals.hls.buf_id

Type: `string`, Default: `FzfLuaBufId`

Highlight group for buffer id (number) in `lines`.

#### globals.hls.buf_nr

Type: `string`, Default: `FzfLuaBufNr`

Highlight group for buffer number in buffer type pickers, i.e. `buffers`, `tabs`, `lines`.
Highlight group for buffer number in `buffers`, `tabs`.

#### globals.hls.buf_linenr

Type: `string`, Default: `FzfLuaBufLineNr`

Highlight group for buffer line number in `lines`, `blines` and `treesitter`.

#### globals.hls.buf_flag_cur

Expand Down
70 changes: 30 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,14 @@ require'fzf-lua'.setup {
-- title = "Title",
-- title_pos = "center", -- 'left', 'center' or 'right'
fullscreen = false, -- start fullscreen?
-- enable treesitter highlighting for the main fzf window will only have
-- effect where grep like results are present, i.e. "file:line:col:text"
-- due to highlight color collisions will also override `fzf_colors`
-- set `fzf_colors=false` or `fzf_colors.hl=...` to override
treesitter = {
enabled = false,
fzf_colors = { ["hl"] = "-1:reverse", ["hl+"] = "-1:reverse" }
},
preview = {
-- default = 'bat', -- override the default previewer?
-- default uses the 'builtin' previewer
Expand Down Expand Up @@ -776,13 +784,13 @@ require'fzf-lua'.setup {
syntax_limit_b = 1024*1024, -- syntax limit (bytes), 0=nolimit
limit_b = 1024*1024*10, -- preview limit (bytes), 0=nolimit
-- previewer treesitter options:
-- enable specific filetypes with: `{ enable = { "lua" } }
-- exclude specific filetypes with: `{ disable = { "lua" } }
-- enable specific filetypes with: `{ enabled = { "lua" } }
-- exclude specific filetypes with: `{ disabled = { "lua" } }
-- disable `nvim-treesitter-context` with `context = false`
-- disable fully with: `{ enable = false }`
-- disable fully with: `treesitter = false` or `{ enabled = false }`
treesitter = {
enable = true,
disable = {},
enabled = true,
disabled = {},
-- nvim-treesitter-context config options
context = { max_lines = 1, trim_scope = "inner" }
},
Expand Down Expand Up @@ -814,7 +822,7 @@ require'fzf-lua'.setup {
-- the preview buffer, define them here instead
-- ext_ft_override = { ["ksql"] = "sql", ... },
-- render_markdown.nvim integration, enabled by default for markdown
render_markdown = { enable = true, filetypes = { ["markdown"] = true } },
render_markdown = { enabled = true, filetypes = { ["markdown"] = true } },
},
-- Code Action previewers, default is "codeaction" (set via `lsp.code_actions.previewer`)
-- "codeaction_native" uses fzf's native previewer, recommended when combined with git-delta
Expand Down Expand Up @@ -1107,45 +1115,25 @@ require'fzf-lua'.setup {
["--with-nth"] = '2..',
},
},
-- `blines` has the same defaults as `lines` aside from prompt and `show_bufname`
lines = {
previewer = "builtin", -- set to 'false' to disable
prompt = 'Lines❯ ',
file_icons = true,
show_bufname = true, -- display buffer name
show_unloaded = true, -- show unloaded buffers
show_unlisted = false, -- exclude 'help' buffers
no_term_buffers = true, -- exclude 'term' buffers
sort_lastused = true, -- sort by most recent
winopts = { treesitter = true }, -- enable TS highlights
fzf_opts = {
-- do not include bufnr in fuzzy matching
-- tiebreak by line no.
["--delimiter"] = "[\\]:]",
["--nth"] = '2..',
["--tiebreak"] = 'index',
["--tabstop"] = "1",
},
-- actions inherit from 'actions.files' and merge
actions = {
["enter"] = actions.buf_edit_or_qf,
["alt-q"] = actions.buf_sel_to_qf,
["alt-l"] = actions.buf_sel_to_ll
},
},
blines = {
previewer = "builtin", -- set to 'false' to disable
prompt = 'BLines❯ ',
show_unlisted = true, -- include 'help' buffers
no_term_buffers = false, -- include 'term' buffers
-- start = "cursor" -- start display from cursor?
fzf_opts = {
-- hide filename, tiebreak by line no.
["--delimiter"] = "[:]",
["--with-nth"] = '2..',
["--tiebreak"] = 'index',
["--multi"] = true,
["--delimiter"] = "[\t]",
["--tabstop"] = "1",
},
-- actions inherit from 'actions.files' and merge
actions = {
["enter"] = actions.buf_edit_or_qf,
["alt-q"] = actions.buf_sel_to_qf,
["alt-l"] = actions.buf_sel_to_ll
["--tiebreak"] = "index",
["--with-nth"] = "2..",
["--nth"] = "4..",
},
},
tags = {
Expand Down Expand Up @@ -1418,10 +1406,12 @@ temporarily overridden by its corresponding `winopts` option:
|FzfLuaHelpBorder |FzfLuaBorder |`hls.help_border` |Help win border|
|FzfLuaHeaderBind |*BlanchedAlmond |`hls.header_bind` |Header keybind|
|FzfLuaHeaderText |*Brown1 |`hls.header_text` |Header text|
|FzfLuaPathColNr |*CadetBlue1 |`hls.path_colnr` |Path col nr (`lines,qf,lsp,diag`)|
|FzfLuaPathLineNr |*LightGreen |`hls.path_linenr` |Path line nr (`lines,qf,lsp,diag`)|
|FzfLuaBufName |*LightMagenta |`hls.buf_name` |Buffer name (`lines`)|
|FzfLuaBufNr |*BlanchedAlmond |`hls.buf_nr` |Buffer number (all buffers)|
|FzfLuaPathColNr |*CadetBlue1 |`hls.path_colnr` |Path col nr (`qf,lsp,diag`)|
|FzfLuaPathLineNr |*LightGreen |`hls.path_linenr` |Path line nr (`qf,lsp,diag`)|
|FzfLuaBufName |Directory |`hls.buf_name` |Buffer name (`lines`)|
|FzfLuaBufId |TabLine |`hls.buf_id` |Buffer ID (`lines`)|
|FzfLuaBufNr |*BlanchedAlmond |`hls.buf_nr` |Buffer number (`buffers,tabs`)|
|FzfLuaBufLineNr |LineNr |`hls.buf_linenr` |Buffer line nr (`lines,blines`)|
|FzfLuaBufFlagCur |*Brown1 |`hls.buf_flag_cur` |Buffer line (`buffers`)|
|FzfLuaBufFlagAlt |*CadetBlue1 |`hls.buf_flag_alt` |Buffer line (`buffers`)|
|FzfLuaTabTitle |*LightSkyBlue1 |`hls.tab_title` |Tab title (`tabs`)|
Expand Down
27 changes: 21 additions & 6 deletions doc/fzf-lua-opts.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*fzf-lua-opts.txt* For Neovim >= 0.8.0 Last change: 2024 December 06
*fzf-lua-opts.txt* For Neovim >= 0.8.0 Last change: 2024 December 16

==============================================================================
Table of Contents *fzf-lua-opts-table-of-contents*
Expand Down Expand Up @@ -726,7 +726,7 @@ globals.hls.path_linenr *fzf-lua-opts-globals.hls.path_linenr*
Type: `string`, Default: `FzfLuaPathLineNr`

Highlight group for the line part of paths, e.g. `file:<line>:<col>:`, used in
pickers such as `buffers`, `lines`, `quickfix`, `lsp`, `diagnostics`, etc.
pickers such as `buffers`, `quickfix`, `lsp`, `diagnostics`, etc.



Expand All @@ -735,24 +735,39 @@ globals.hls.path_colnr *fzf-lua-opts-globals.hls.path_colnr*
Type: `string`, Default: `FzfLuaPathColNr`

Highlight group for the column part of paths, e.g. `file:<line>:<col>:`, used
in pickers such as `buffers`, `lines`, `quickfix`, `lsp`, `diagnostics`, etc.
in pickers such as `buffers`, `quickfix`, `lsp`, `diagnostics`, etc.



globals.hls.buf_name *fzf-lua-opts-globals.hls.buf_name*

Type: `string`, Default: `FzfLuaBufName`

Highlight group for buffer name in `lines`.
Highlight group for buffer name (filepath) in `lines`.



globals.hls.buf_id *fzf-lua-opts-globals.hls.buf_id*

Type: `string`, Default: `FzfLuaBufId`

Highlight group for buffer id (number) in `lines`.



globals.hls.buf_nr *fzf-lua-opts-globals.hls.buf_nr*

Type: `string`, Default: `FzfLuaBufNr`

Highlight group for buffer number in buffer type pickers, i.e. `buffers`,
`tabs`, `lines`.
Highlight group for buffer number in `buffers`, `tabs`.



globals.hls.buf_linenr *fzf-lua-opts-globals.hls.buf_linenr*

Type: `string`, Default: `FzfLuaBufLineNr`

Highlight group for buffer line number in `lines`, `blines` and `treesitter`.



Expand Down
132 changes: 98 additions & 34 deletions lua/fzf-lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,37 @@ function M.normalize_opts(opts, globals, __resume_key)
opts = M.resume_opts(opts)
end

local function convert_bool_opts()
-- Enforce conversion of boolean options that are tables with `enabled`
-- property, i.e. `winopts.treesitter = true` will be converted to
-- `winopts = { treesitter = { enabled = true, <defaults> } }`
-- Running a command and setting sub-values implies `enabled=true`, e.g:
-- `:FzfLua blines winopts.treesitter.fzf_colors=false`
-- `:FzfLua blines winopts.treesitter.fzf_colors={hl="-1:underline"}`
-- So the above will be converted to:
-- `winopts = { treesitter = { enabled = true, fzf_colors = false, <defaults> } }`
-- NOTE: this function runs once before merging with globals and once
-- later down the line (after merges with globals/defaults), this is done
-- so that commands as the example above won't inherit `{ enabled = false }`
-- from the defaults (e.g. the default is `winopts.treesitter.enabled = false`)
for k, vfrom in pairs({
["winopts.treesitter"] = "winopts.treesitter",
["previewer.treesitter"] = "previewers.builtin.treesitter",
["previewer.render_markdown"] = "previewers.builtin.render_markdown",
})
do
local v = utils.map_get(opts, k)
if v == false then
utils.map_set(opts, k, { enabled = false })
elseif v == true or type(v) == "table" then
local newv = vim.tbl_deep_extend("keep", type(v) == "table" and v or {},
{ enabled = true }, utils.map_get(M.defaults, vfrom) or {})
utils.map_set(opts, k, newv)
end
end
end
convert_bool_opts()

-- normalize all binds as lowercase or we can have duplicate keys (#654)
---@param m {fzf: table<string, unknown>, builtin: table<string, unknown>}
---@return {fzf: table<string, unknown>, builtin: table<string, unknown>}?
Expand All @@ -219,13 +250,13 @@ function M.normalize_opts(opts, globals, __resume_key)

-- Merge required tables from globals
for _, k in ipairs({
"winopts", "keymap", "fzf_opts", "fzf_tmux_opts", "hls"
"winopts", "keymap", "fzf_opts", "fzf_colors", "fzf_tmux_opts", "hls"
}) do
opts[k] = vim.tbl_deep_extend("keep",
-- must clone or map will be saved as reference
-- and then overwritten if found in 'backward_compat'
type(opts[k]) == "function" and opts[k]() or opts[k] or {},
type(M.globals[k]) == "function" and M.globals[k]() or
type(M.globals[k]) == "function" and M.globals[k](opts) or
type(M.globals[k]) == "table" and utils.tbl_deep_clone(M.globals[k]) or {})
end

Expand Down Expand Up @@ -316,40 +347,45 @@ function M.normalize_opts(opts, globals, __resume_key)
-- backward compatibility, rhs overrides lhs
-- (rhs being the "old" option)
local backward_compat = {
{ "winopts.row", "winopts.win_row" },
{ "winopts.col", "winopts.win_col" },
{ "winopts.width", "winopts.win_width" },
{ "winopts.height", "winopts.win_height" },
{ "winopts.border", "winopts.win_border" },
{ "winopts.on_create", "winopts.window_on_create" },
{ "winopts.preview.wrap", "preview_wrap" },
{ "winopts.preview.border", "preview_border" },
{ "winopts.preview.hidden", "preview_opts" },
{ "winopts.preview.vertical", "preview_vertical" },
{ "winopts.preview.horizontal", "preview_horizontal" },
{ "winopts.preview.layout", "preview_layout" },
{ "winopts.preview.flip_columns", "flip_columns" },
{ "winopts.preview.default", "default_previewer" },
{ "winopts.preview.delay", "previewers.builtin.delay" },
{ "winopts.preview.title", "previewers.builtin.title" },
{ "winopts.preview.title_pos", "winopts.preview.title_align" },
{ "winopts.preview.scrollbar", "previewers.builtin.scrollbar" },
{ "winopts.preview.scrollchar", "previewers.builtin.scrollchar" },
{ "cwd_header", "show_cwd_header" },
{ "cwd_prompt", "show_cwd_prompt" },
{ "resume", "continue_last_search" },
{ "resume", "repeat_last_search" },
{ "hls.normal", "winopts.hl_normal" },
{ "hls.border", "winopts.hl_border" },
{ "hls.cursor", "previewers.builtin.hl_cursor" },
{ "hls.cursorline", "previewers.builtin.hl_cursorline" },
{ "hls", "winopts.hl" },
{ "winopts.row", "winopts.win_row" },
{ "winopts.col", "winopts.win_col" },
{ "winopts.width", "winopts.win_width" },
{ "winopts.height", "winopts.win_height" },
{ "winopts.border", "winopts.win_border" },
{ "winopts.on_create", "winopts.window_on_create" },
{ "winopts.preview.wrap", "preview_wrap" },
{ "winopts.preview.border", "preview_border" },
{ "winopts.preview.hidden", "preview_opts" },
{ "winopts.preview.vertical", "preview_vertical" },
{ "winopts.preview.horizontal", "preview_horizontal" },
{ "winopts.preview.layout", "preview_layout" },
{ "winopts.preview.flip_columns", "flip_columns" },
{ "winopts.preview.default", "default_previewer" },
{ "winopts.preview.delay", "previewers.builtin.delay" },
{ "winopts.preview.title", "previewers.builtin.title" },
{ "winopts.preview.title_pos", "winopts.preview.title_align" },
{ "winopts.preview.scrollbar", "previewers.builtin.scrollbar" },
{ "winopts.preview.scrollchar", "previewers.builtin.scrollchar" },
{ "cwd_header", "show_cwd_header" },
{ "cwd_prompt", "show_cwd_prompt" },
{ "resume", "continue_last_search" },
{ "resume", "repeat_last_search" },
{ "hls.normal", "winopts.hl_normal" },
{ "hls.border", "winopts.hl_border" },
{ "hls.cursor", "previewers.builtin.hl_cursor" },
{ "hls.cursorline", "previewers.builtin.hl_cursorline" },
{ "hls", "winopts.hl" },
{ "previewer.treesitter.enabled", "previewer.treesitter.enable" },
{ "previewer.treesitter.disabled", "previewer.treesitter.disable" },
{ "previewers.builtin.treesitter.enabled", "previewers.builtin.treesitter.enable" },
{ "previewers.builtin.treesitter.disabled", "previewers.builtin.treesitter.disable" },
}

-- iterate backward compat map, retrieve values from opts or globals
for _, t in ipairs(backward_compat) do
local new_key, old_key = t[1], t[2]
local old_val = utils.map_get(opts, old_key) or utils.map_get(M.globals, old_key)
local v_opts = utils.map_get(opts, old_key)
local old_val = v_opts == nil and utils.map_get(M.globals, old_key) or v_opts
local new_val = utils.map_get(opts, new_key)
if old_val ~= nil then
if type(old_val) == "table" and type(new_val) == "table" then
Expand All @@ -358,7 +394,11 @@ function M.normalize_opts(opts, globals, __resume_key)
utils.map_set(opts, new_key, old_val)
end
utils.map_set(opts, old_key, nil)
-- utils.warn(string.format("option moved/renamed: '%s' -> '%s'", old_key, new_key))
if not opts.silent then
utils.warn(string.format(
"Deprecated option: '%s' -> '%s'. Add 'silent=true' to hide this message.",
old_key, new_key))
end
end
end

Expand Down Expand Up @@ -419,10 +459,34 @@ function M.normalize_opts(opts, globals, __resume_key)
-- globals.winopts.preview.default
opts.previewer = opts.previewer()
end
if type(opts.previewer) == "table" then
if type(opts.previewer) == "table" or opts.previewer == true then
-- merge with the default builtin previewer
opts.previewer = vim.tbl_deep_extend("keep",
opts.previewer, M.globals.previewers.builtin)
type(opts.previewer) == "table" and opts.previewer or {}, M.globals.previewers.builtin)
end

-- Convert again in case the bool option came from global opts
convert_bool_opts()

-- Adjust main fzf window treesitter settings
-- Disabled unless the picker is TS enabled with `_treesitter=true`
-- Unless `enabled=false` is specifically set `true` is asssumed
if not opts._treesitter then opts.winopts.treesitter = nil end
if not opts.winopts.treesitter or opts.winopts.treesitter.enabled == false then
opts.winopts.treesitter = nil
else
assert(type(opts.winopts.treesitter) == "table")
-- Unless the caller specifically disables `fzf_colors` fuzzy matching
-- colors "hl,hl+" will be set to "-1:reverse" which sets the background
-- color for matches to the corresponding original foreground color
-- NOTE: `fzf_colors` inherited from `defaults.winopts.treesitter`
if opts.winopts.treesitter.fzf_colors ~= false then
opts.fzf_colors = vim.tbl_deep_extend("force",
opts.fzf_colors or {},
M.defaults.winopts.treesitter.fzf_colors,
type(opts.winopts.treesitter.fzf_colors) == "table"
and opts.winopts.treesitter.fzf_colors or {})
end
end

-- we need the original `cwd` with `autochdir=true` (#882)
Expand Down
Loading

0 comments on commit 7836b37

Please sign in to comment.