Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add format instead of only prefix for labels #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/flutter-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ local defaults = {
},
closing_tags = {
highlight = "Comment",
prefix = "// ",
format = "// %s",
enabled = true,
},
lsp = {
Expand Down
26 changes: 23 additions & 3 deletions lua/flutter-tools/labels.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,33 @@ local namespace = api.nvim_create_namespace("flutter_tools_closing_labels")
local function render_labels(labels, opts)
api.nvim_buf_clear_namespace(0, namespace, 0, -1)
opts = opts or {}
local highlight = opts and opts.highlight or "Comment"
local prefix = opts and opts.prefix or "// "
local highlight = opts.highlight or "Comment"

-- Have ot use `rawget` to override what the default format is that we provide in config.
local user_format = rawget(opts, "format")

local prefix = opts.prefix
if prefix and user_format then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config module actually already has a mechanism for deprecations, you just need to specify what the fallback key is and the message to show the user
https://github.com/akinsho/flutter-tools.nvim/blob/a2a67dae9ff43d95caddf4d68cf5afcd4cb9d65d/lua/flutter-tools/config.lua#L61

error(
"[flutter-tools.labels] Cannot have both prefix and format specified" .. vim.inspect(opts)
)
end

-- A bit complicated, but basically just keeps backwards compat of passing "prefix"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd actually rather break this (mean as that sounds) whilst this plugin is still fairly small/doesn't have that many users so I can keep things as simple as possible. I think if the user has overridden prefix with the deprecation table it should warn them of the change and they can change their config

-- while still allowing users to pass "format" instead.
local format
if user_format then
format = user_format
elseif prefix then
format = prefix .. "%s"
else
format = opts.format
end

for _, item in ipairs(labels) do
local line = item.range["end"].line
api.nvim_buf_set_virtual_text(0, namespace, tonumber(line), {
{ prefix .. item.label, highlight },
{ string.format(format, item.label), highlight },
}, {})
end
end
Expand Down