Skip to content

Commit

Permalink
feat: Pass in the buffer number for symbols.icon_fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
hedyhli committed Jul 5, 2024
1 parent 028e0bc commit bc58f19
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
- `OutlineOpen` without `!`, (and `outline.open`) will now focus in the outline if it is
already open when invoked.
([#71](https://github.com/hedyhli/outline.nvim/issues/71))
- The `symbols.icon_fetcher` function now supports a second parameter, the code
buffer number. Use it to fetch icons based on buffer options such as filetype.

## v1.0.0

Expand Down
54 changes: 51 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ Pass a table to the setup call with your configuration options.
-- You can use a custom function that returns the icon for each symbol kind.
-- This function takes a kind (string) as parameter and should return an
-- icon as string.
---@param kind string
---@param bufnr integer Code buffer
---@returns string|boolean The icon string (key of `icons` table), or `false`
--- to fallback to `icon_source`.
icon_fetcher = nil,
-- 3rd party source for fetching icons. Fallback if icon_fetcher returned
-- empty string. Currently supported values: 'lspkind'
Expand Down Expand Up @@ -890,6 +894,21 @@ symbols = {
}
```

The `icon_fetcher` function may also accept a second parameter, the buffer
number of the code buffer. For example, you can use it to determine the icon
to use based on the filetype.

```lua
symbols = {
icon_fetcher = function(kind, bufnr)
local ft = vim.api.nvim_buf_get_option(bufnr, 'ft')
-- ...
end,
}
```

See [this section](#custom-icons) for other examples of this function.

- You can customize the split command used for creating the outline window split
using `outline_window.split_command`, such as `"topleft vsp"`. See `:h windows`

Expand Down Expand Up @@ -1046,7 +1065,7 @@ that simply returns in plain text, the first letter of the given kind.

```lua
symbols = {
icon_fetcher = function(kind) return kind:sub(1,1) end
icon_fetcher = function(kind, bufnr) return kind:sub(1,1) end,
}
```

Expand All @@ -1055,21 +1074,32 @@ and `icons` as fallback.

<div align=center><img width="500" alt="outline with plain text icons" src="https://github.com/hedyhli/outline.nvim/assets/50042066/655b534b-da16-41a7-926e-f14475376a04"></div>

### Different icons based on filetype

```lua
symbols = {
icon_fetcher = function(kind, bufnr)
local ft = vim.api.nvim_buf_get_option(bufnr, 'ft')
-- ...
end,
}
```

### Disable icons

Disable all icons:

```lua
symbols = {
icon_fetcher = function(_) return "" end,
icon_fetcher = function() return "" end,
}
```

Disable icons for specific kinds, and for others use lspkind:

```lua
symbols = {
icon_fetcher = function(k)
icon_fetcher = function(k, buf)
if k == 'String' then
return ""
end
Expand All @@ -1081,6 +1111,24 @@ symbols = {

<div align=center><img width="500" alt="outline with disabled icon for String" src="https://github.com/hedyhli/outline.nvim/assets/50042066/26d258c6-9530-43d4-b88b-963304e3bf2d"></div>

### Disable icons for a specific filetype

In this example, icons are disabled for markdown, and `lspkind` is used for
other filetypes.

```lua
symbols = {
icon_fetcher = function(k, buf)
local ft = vim.api.nvim_buf_get_option(buf, "ft")
if ft == 'markdown' then
return ""
end
return false
end,
icon_source = "lspkind",
}
```

### Live, editable previews

Press `K` to open the preview, press `K` again to focus on the preview window
Expand Down
2 changes: 1 addition & 1 deletion lua/outline/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ local function parse_result(result, depth, hierarchy, parent, bufnr)
local node = {
deprecated = value.deprecated,
kind = value.kind,
icon = symbols.icon_from_kind(value.kind),
icon = symbols.icon_from_kind(value.kind, bufnr),
name = value.name or value.text,
detail = value.detail,
line = selectionRange.start.line,
Expand Down
5 changes: 3 additions & 2 deletions lua/outline/symbols.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ for k, v in pairs(M.kinds) do
end

---@param kind string|integer
function M.icon_from_kind(kind)
---@param bufnr integer
function M.icon_from_kind(kind, bufnr)
local kindstr = kind
if type(kind) ~= 'string' then
kindstr = M.kinds[kind]
Expand All @@ -56,7 +57,7 @@ function M.icon_from_kind(kind)
end

if type(cfg.o.symbols.icon_fetcher) == 'function' then
local icon = cfg.o.symbols.icon_fetcher(kindstr)
local icon = cfg.o.symbols.icon_fetcher(kindstr, bufnr)
-- Allow returning empty string
if icon then
return icon
Expand Down

0 comments on commit bc58f19

Please sign in to comment.