Skip to content

Commit

Permalink
feat(sources/config)!: treesitter/markdown: perfer treesitter parser
Browse files Browse the repository at this point in the history
  • Loading branch information
bekaboo committed Jul 4, 2023
1 parent 5c8bd1a commit 15115eb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 87 deletions.
89 changes: 44 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,26 +333,27 @@ https://github.com/Bekaboo/dropbar.nvim/assets/76579810/e8c1ac26-0321-4762-9975-
},
bar = {
---@type dropbar_source_t[]|fun(buf: integer, win: integer): dropbar_source_t[]
sources = function(_, _)
local sources = require('dropbar.sources')
sources = function(buf, _)
local sources = require('plugin.winbar.sources')
local utils = require('dropbar.utils')
if vim.bo[buf].ft == 'markdown' then
return {
sources.path,
utils.source.fallback({
sources.treesitter,
sources.markdown,
sources.lsp,
}),
}
end
return {
sources.path,
{
get_symbols = function(buf, win, cursor)
if vim.bo[buf].ft == 'markdown' then
return sources.markdown.get_symbols(buf, win, cursor)
end
for _, source in ipairs({
sources.lsp,
sources.treesitter,
}) do
local symbols = source.get_symbols(buf, win, cursor)
if not vim.tbl_isempty(symbols) then
return symbols
end
end
return {}
end,
utils.source.fallback({
sources.lsp,
sources.treesitter,
}),
}
end,
},
}
end,
Expand Down Expand Up @@ -806,34 +807,29 @@ winbar:
winid and should return a list of sources
- Default:
```lua
function(_, _)
local sources = require('dropbar.sources')
function(buf, _)
local sources = require('plugin.winbar.sources')
local utils = require('dropbar.utils')
if vim.bo[buf].ft == 'markdown' then
return {
sources.path,
utils.source.fallback({
sources.treesitter,
sources.markdown,
sources.lsp,
}),
}
end
return {
sources.path,
{
get_symbols = function(buf, win, cursor)
if vim.bo[buf].ft == 'markdown' then
return sources.markdown.get_symbols(buf, win, cursor)
end
for _, source in ipairs({
sources.lsp,
sources.treesitter,
}) do
local symbols = source.get_symbols(buf, win, cursor)
if not vim.tbl_isempty(symbols) then
return symbols
end
end
return {}
end,
},
utils.source.fallback({
sources.lsp,
sources.treesitter,
}),
}
end
end,
```
- Notice that in the default config we register the second source as an
aggregation of LSP, treesitter, and markdown sources, so that we dynamically
choose the best source for the current buffer or window.
For more information about sources, see [`dropbar_source_t`](#dropbar_source_t).
- For more information about sources, see [`dropbar_source_t`](#dropbar_source_t).
- `opts.bar.padding`: `{ left: number, right: number }`
- Padding to use between the winbar and the window border
- Default: `{ left = 1, right = 1 }`
Expand Down Expand Up @@ -1571,9 +1567,12 @@ A [`dropbar_source_t`](#dropbar_source_t) instance is just a table with
window id, and the cursor position.
We have seen a simple example of a custom source in the [default config of
`opts.bar.sources`](#bar) where the second source is set to a table with its
field `get_symbols` set to a function that gets symbols from either the
markdown, LSP, or treesitter sources to achieve fall-back behavior.
`opts.bar.sources`](#bar) where the second source is set to a combination
of lsp/treesitter/markdown sources using the `utils.source.fallback()` factory
function, which simply returns a table containing a `get_symbols()` function
where each source passed to `utils.source.fallback()` is queried and the first
non-empty result get from the sources is returned as the result of the combined
source.
Here is another example of a custom source that will always return two symbols
saying 'Hello' and 'dropbar' with highlights `'hl-Keyword'` and `'hl-Title'`
Expand Down
49 changes: 26 additions & 23 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,34 +378,29 @@ the winbar:
winid and should return a list of sources
- Default: >lua

function(_, _)
local sources = require('dropbar.sources')
function(buf, _)
local sources = require('plugin.winbar.sources')
local utils = require('dropbar.utils')
if vim.bo[buf].ft == 'markdown' then
return {
sources.path,
utils.source.fallback({
sources.treesitter,
sources.markdown,
sources.lsp,
}),
}
end
return {
sources.path,
{
get_symbols = function(buf, win, cursor)
if vim.bo[buf].ft == 'markdown' then
return sources.markdown.get_symbols(buf, win, cursor)
end
for _, source in ipairs({
sources.lsp,
sources.treesitter,
}) do
local symbols = source.get_symbols(buf, win, cursor)
if not vim.tbl_isempty(symbols) then
return symbols
end
end
return {}
end,
},
utils.source.fallback({
sources.lsp,
sources.treesitter,
}),
}
end
<
- Notice that in the default config we register the second source as an
aggregation of LSP, treesitter, and markdown sources, so that we
dynamically choose the best source for the current buffer or window. For
more information about sources, see
- For more information about sources, see
|dropbar-developers-classes-dropbar_source_t|.

- `opts.bar.padding`: `{ left: number, right: number }`
Expand Down Expand Up @@ -1929,6 +1924,14 @@ We have seen a simple example of a custom source in
table with its field `get_symbols` set to a function tht gets symbols from
either the markdown, LSP, or treesitter sources to achieve fall-back behavior.

We have seen a simple example of a custom source in the
|dropbar-configuration-options-bar| where the second source is set to a
combination of lsp/treesitter/markdown sources using the
`utils.source.fallback()` factory function, which simply returns a table
containing a `get_symbols()` function where each source passed to
`utils.source.fallback()` is queried and the first non-empty result get from
the sources is returned as the result of the combined source.

Here is another example of a custom source that will always return two symbols
saying "Hello" and "dropbar" with highlights `'hl-Keyword'` and `'hl-Title'`
and a smiling face shown in `'hl-WarningMsg'` at the start of the first
Expand Down
48 changes: 29 additions & 19 deletions lua/dropbar/configs.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require('dropbar.utils')
local M = {}

---@class dropbar_configs_t
Expand Down Expand Up @@ -64,6 +65,12 @@ M.opts = {
Folder = '󰉋 ',
ForStatement = '󰑖 ',
Function = '󰊕 ',
H1Marker = '󰉫 ',
H2Marker = '󰉬 ',
H3Marker = '󰉭 ',
H4Marker = '󰉮 ',
H5Marker = '󰉯 ',
H6Marker = '󰉰 ',
Identifier = '󰀫 ',
IfStatement = '󰇉 ',
Interface = '',
Expand Down Expand Up @@ -152,27 +159,24 @@ M.opts = {
bar = {
hover = true,
---@type dropbar_source_t[]|fun(buf: integer, win: integer): dropbar_source_t[]
sources = function(_, _)
local sources = require('dropbar.sources')
sources = function(buf, _)
local sources = require('plugin.winbar.sources')
if vim.bo[buf].ft == 'markdown' then
return {
sources.path,
utils.source.fallback({
sources.treesitter,
sources.markdown,
sources.lsp,
}),
}
end
return {
sources.path,
{
get_symbols = function(buf, win, cursor)
if vim.bo[buf].ft == 'markdown' then
return sources.markdown.get_symbols(buf, win, cursor)
end
for _, source in ipairs({
sources.lsp,
sources.treesitter,
}) do
local symbols = source.get_symbols(buf, win, cursor)
if not vim.tbl_isempty(symbols) then
return symbols
end
end
return {}
end,
},
utils.source.fallback({
sources.lsp,
sources.treesitter,
}),
}
end,
padding = {
Expand Down Expand Up @@ -354,6 +358,12 @@ M.opts = {
'event',
'for_statement',
'function',
'h1_marker',
'h2_marker',
'h3_marker',
'h4_marker',
'h5_marker',
'h6_marker',
'if_statement',
'interface',
'keyword',
Expand Down
6 changes: 6 additions & 0 deletions lua/dropbar/hlgroups.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ local hlgroups = {
DropBarIconKindFolder = { link = 'Directory' },
DropBarIconKindForStatement = { link = 'Repeat' },
DropBarIconKindFunction = { link = 'Function' },
DropBarIconKindH1Marker = { link = 'markdownH1' },
DropBarIconKindH2Marker = { link = 'markdownH2' },
DropBarIconKindH3Marker = { link = 'markdownH3' },
DropBarIconKindH4Marker = { link = 'markdownH4' },
DropBarIconKindH5Marker = { link = 'markdownH5' },
DropBarIconKindH6Marker = { link = 'markdownH6' },
DropBarIconKindIdentifier = { link = 'CmpItemKindVariable' },
DropBarIconKindIfStatement = { link = 'Conditional' },
DropBarIconKindInterface = { link = 'CmpItemKindInterface' },
Expand Down

0 comments on commit 15115eb

Please sign in to comment.